Merge pull request #10246 from Snuffleupagus/api-classes

Convert the remaining code in `src/display/api.js` to use ES6 classes
This commit is contained in:
Tim van der Meij 2018-11-18 19:41:41 +01:00 committed by GitHub
commit 6ebdbb244f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 666 additions and 669 deletions

View File

@ -24,7 +24,8 @@ var DEFAULT_SCALE = 1.0;
var container = document.getElementById('pageContainer');
// Fetch the PDF document from the URL using promises.
pdfjsLib.getDocument(DEFAULT_URL).then(function (doc) {
var loadingTask = pdfjsLib.getDocument(DEFAULT_URL);
loadingTask.promise.then(function(doc) {
// Use a promise to fetch and render the next page.
var promise = Promise.resolve();

View File

@ -37,11 +37,12 @@ var SCALE = 1.0;
var container = document.getElementById('pageContainer');
// Loading document.
pdfjsLib.getDocument({
var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) {
});
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, retrieving the page.
return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) {
// Creating the page view with default parameters.

View File

@ -60,11 +60,12 @@ document.addEventListener('pagesinit', function () {
});
// Loading document.
pdfjsLib.getDocument({
var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) {
});
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, specifying document for the viewer and
// the (optional) linkService.
pdfViewer.setDocument(pdfDocument);

View File

@ -60,11 +60,12 @@ document.addEventListener('pagesinit', function () {
});
// Loading document.
pdfjsLib.getDocument({
var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) {
});
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, specifying document for the viewer and
// the (optional) linkService.
pdfSinglePageViewer.setDocument(pdfDocument);

View File

@ -28,11 +28,12 @@
//
// Asynchronous download PDF
//
pdfjsLib.getDocument(url).then(function getPdfHelloWorld(pdf) {
var loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function(pdf) {
//
// Fetch the first page
//
pdf.getPage(1).then(function getPageHelloWorld(page) {
pdf.getPage(1).then(function(page) {
var scale = 1.5;
var viewport = page.getViewport(scale);

View File

@ -39,9 +39,10 @@
// Opening PDF by passing its binary data as a string. It is still preferable
// to use Uint8Array, but string or array-like structure will work too.
pdfjsLib.getDocument({data: pdfData}).then(function getPdfHelloWorld(pdf) {
var loadingTask = pdfjsLib.getDocument({data: pdfData});
loadingTask.promise.then(function(pdf) {
// Fetch the first page.
pdf.getPage(1).then(function getPageHelloWorld(page) {
pdf.getPage(1).then(function(page) {
var scale = 1.5;
var viewport = page.getViewport(scale);

View File

@ -117,7 +117,8 @@
/**
* Asynchronously downloads PDF.
*/
pdfjsLib.getDocument(url).then(function (pdfDoc_) {
var loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById('page_count').textContent = pdfDoc.numPages;

View File

@ -17,7 +17,8 @@ var pdfPath = process.argv[2] || '../../web/compressed.tracemonkey-pldi-09.pdf';
// Will be using promises to load document, pages and misc data instead of
// callback.
pdfjsLib.getDocument(pdfPath).then(function (doc) {
var loadingTask = pdfjsLib.getDocument(pdfPath);
loadingTask.promise.then(function(doc) {
var numPages = doc.numPages;
console.log('# Document Loaded');
console.log('Number of Pages: ' + numPages);

View File

@ -57,7 +57,8 @@ var pdfURL = '../../../web/compressed.tracemonkey-pldi-09.pdf';
var rawData = new Uint8Array(fs.readFileSync(pdfURL));
// Load the PDF file.
pdfjsLib.getDocument(rawData).then(function (pdfDocument) {
var loadingTask = pdfjsLib.getDocument(rawData);
loadingTask.promise.then(function(pdfDocument) {
console.log('# PDF document loaded.');
// Get the first page.
@ -72,7 +73,8 @@ pdfjsLib.getDocument(rawData).then(function (pdfDocument) {
canvasFactory: canvasFactory
};
page.render(renderContext).then(function () {
var renderTask = page.render(renderContext);
renderTask.promise.then(function() {
// Convert the canvas to an image buffer.
var image = canvasAndContext.canvas.toBuffer();
fs.writeFile('output.png', image, function (error) {

View File

@ -84,11 +84,12 @@ function writeSvgToFile(svgElement, filePath) {
// Will be using promises to load document, pages and misc data instead of
// callback.
pdfjsLib.getDocument({
var loadingTask = pdfjsLib.getDocument({
data: data,
// Try to export JPEG images directly if they don't need any further processing.
nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY
}).then(function (doc) {
});
loadingTask.promise.then(function(doc) {
var numPages = doc.numPages;
console.log('# Document Loaded');
console.log('Number of Pages: ' + numPages);

View File

@ -51,11 +51,12 @@ document.addEventListener('pagesinit', function () {
});
// Loading document.
pdfjsLib.getDocument({
var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) {
});
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, specifying document for the viewer and
// the (optional) linkService.
pdfViewer.setDocument(pdfDocument);

View File

@ -49,7 +49,8 @@ function buildSVG(viewport, textContent) {
function pageLoaded() {
// Loading document and page text content
pdfjsLib.getDocument({url: PDF_PATH}).then(function (pdfDocument) {
var loadingTask = pdfjsLib.getDocument({url: PDF_PATH});
loadingTask.promise.then(function(pdfDocument) {
pdfDocument.getPage(PAGE_NUMBER).then(function (page) {
var viewport = page.getViewport(PAGE_SCALE);
page.getTextContent().then(function (textContent) {

File diff suppressed because it is too large Load Diff

View File

@ -726,7 +726,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
CanvasGraphics.prototype = {
beginDrawing({ transform, viewport, transparency,
beginDrawing({ transform, viewport, transparency = false,
background = null, }) {
// For pdfs that use blend modes we have to clear the canvas else certain
// blend modes can look wrong since we'd be blending with a white

View File

@ -358,7 +358,7 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
let absoluteUrl = new URL(task.file, window.location).href;
try {
pdfjsLib.getDocument({
const loadingTask = pdfjsLib.getDocument({
url: absoluteUrl,
password: task.password,
nativeImageDecoderSupport: task.nativeImageDecoderSupport,
@ -367,7 +367,8 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
disableRange: task.disableRange,
disableAutoFetch: !task.enableAutoFetch,
pdfBug: true,
}).then((doc) => {
});
loadingTask.promise.then((doc) => {
task.pdfDoc = doc;
this._nextPage(task, failure);
}, (err) => {

View File

@ -1332,26 +1332,23 @@ describe('api', function() {
// Render the first page of the given PDF file.
// Fulfills the promise with the base64-encoded version of the PDF.
function renderPDF(filename) {
var loadingTask = getDocument(filename);
async function renderPDF(filename) {
const loadingTask = getDocument(filename);
loadingTasks.push(loadingTask);
return loadingTask.promise
.then(function(pdf) {
const pdf = await loadingTask.promise;
pdfDocuments.push(pdf);
return pdf.getPage(1);
}).then(function(page) {
var viewport = page.getViewport(1.2);
var canvasAndCtx = CanvasFactory.create(viewport.width,
const page = await pdf.getPage(1);
const viewport = page.getViewport(1.2);
const canvasAndCtx = CanvasFactory.create(viewport.width,
viewport.height);
return page.render({
const renderTask = page.render({
canvasContext: canvasAndCtx.context,
viewport,
}).then(function() {
var data = canvasAndCtx.canvas.toDataURL();
});
await renderTask.promise;
const data = canvasAndCtx.canvas.toDataURL();
CanvasFactory.destroy(canvasAndCtx);
return data;
});
});
}
afterEach(function(done) {

View File

@ -48,9 +48,7 @@ describe('custom canvas rendering', function() {
}).then(function(data) {
page = data;
done();
}).catch(function (reason) {
done.fail(reason);
});
}).catch(done.fail);
});
afterAll(function(done) {
@ -66,20 +64,16 @@ describe('custom canvas rendering', function() {
var viewport = page.getViewport(1);
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
page.render({
const renderTask = page.render({
canvasContext: canvasAndCtx.context,
viewport,
}).then(function() {
var { r, g, b, a, } = getTopLeftPixel(canvasAndCtx.context);
CanvasFactory.destroy(canvasAndCtx);
expect(r).toEqual(255);
expect(g).toEqual(255);
expect(b).toEqual(255);
expect(a).toEqual(255);
done();
}).catch(function (reason) {
done(reason);
});
renderTask.promise.then(function() {
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual(
{ r: 255, g: 255, b: 255, a: 255, });
CanvasFactory.destroy(canvasAndCtx);
done();
}).catch(done.fail);
});
it('renders to canvas with a custom background', function(done) {
@ -89,20 +83,16 @@ describe('custom canvas rendering', function() {
var viewport = page.getViewport(1);
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
page.render({
const renderTask = page.render({
canvasContext: canvasAndCtx.context,
viewport,
background: 'rgba(255,0,0,1.0)',
}).then(function() {
var { r, g, b, a, } = getTopLeftPixel(canvasAndCtx.context);
CanvasFactory.destroy(canvasAndCtx);
expect(r).toEqual(255);
expect(g).toEqual(0);
expect(b).toEqual(0);
expect(a).toEqual(255);
done();
}).catch(function (reason) {
done(reason);
});
renderTask.promise.then(function() {
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual(
{ r: 255, g: 0, b: 0, a: 255, });
CanvasFactory.destroy(canvasAndCtx);
done();
}).catch(done.fail);
});
});