Merge pull request #11780 from Snuffleupagus/refactor-PDFViewerApplication-load

Move the initialization of "page labels"/"metadata"/"auto print" out of `PDFViewerApplication.load`
This commit is contained in:
Tim van der Meij 2020-04-05 15:46:36 +02:00 committed by GitHub
commit 09cccd8ecc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1185,44 +1185,35 @@ const PDFViewerApplication = {
}); });
}); });
pdfDocument.getPageLabels().then(labels => { pagesPromise.then(() => {
if (!labels || AppOptions.get("disablePageLabels")) { this._initializeAutoPrint(pdfDocument, openActionPromise);
return;
}
const numLabels = labels.length;
if (numLabels !== this.pagesCount) {
console.error(
"The number of Page Labels does not match " +
"the number of pages in the document."
);
return;
}
let i = 0;
// Ignore page labels that correspond to standard page numbering.
while (i < numLabels && labels[i] === (i + 1).toString()) {
i++;
}
if (i === numLabels) {
return;
}
pdfViewer.setPageLabels(labels);
pdfThumbnailViewer.setPageLabels(labels);
// Changing toolbar page display to use labels and we need to set
// the label of the current page.
this.toolbar.setPagesCount(pdfDocument.numPages, true);
this.toolbar.setPageNumber(
pdfViewer.currentPageNumber,
pdfViewer.currentPageLabel
);
}); });
pagesPromise.then(async () => { onePageRendered.then(() => {
pdfDocument.getOutline().then(outline => {
this.pdfOutlineViewer.render({ outline });
});
pdfDocument.getAttachments().then(attachments => {
this.pdfAttachmentViewer.render({ attachments });
});
});
this._initializePageLabels(pdfDocument);
this._initializeMetadata(pdfDocument);
},
/**
* @private
*/
async _initializeAutoPrint(pdfDocument, openActionPromise) {
const [openAction, javaScript] = await Promise.all([ const [openAction, javaScript] = await Promise.all([
openActionPromise, openActionPromise,
pdfDocument.getJavaScript(), pdfDocument.getJavaScript(),
]); ]);
if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while the auto print data resolved.
}
let triggerAutoPrint = false; let triggerAutoPrint = false;
if (openAction && openAction.action === "Print") { if (openAction && openAction.action === "Print") {
@ -1258,39 +1249,31 @@ const PDFViewerApplication = {
window.print(); window.print();
}); });
} }
}); },
onePageRendered.then(() => { /**
pdfDocument.getOutline().then(outline => { * @private
this.pdfOutlineViewer.render({ outline }); */
}); async _initializeMetadata(pdfDocument) {
pdfDocument.getAttachments().then(attachments => { const {
this.pdfAttachmentViewer.render({ attachments }); info,
}); metadata,
}); contentDispositionFilename,
} = await pdfDocument.getMetadata();
pdfDocument if (pdfDocument !== this.pdfDocument) {
.getMetadata() return; // The document was closed while the metadata resolved.
.then(({ info, metadata, contentDispositionFilename }) => { }
this.documentInfo = info; this.documentInfo = info;
this.metadata = metadata; this.metadata = metadata;
this.contentDispositionFilename = contentDispositionFilename; this.contentDispositionFilename = contentDispositionFilename;
// Provides some basic debug information // Provides some basic debug information
console.log( console.log(
"PDF " + `PDF ${pdfDocument.fingerprint} [${info.PDFFormatVersion} ` +
pdfDocument.fingerprint + `${(info.Producer || "-").trim()} / ${(info.Creator || "-").trim()}] ` +
" [" + `(PDF.js: ${version || "-"}` +
info.PDFFormatVersion + `${this.pdfViewer.enableWebGL ? " [WebGL]" : ""})`
" " +
(info.Producer || "-").trim() +
" / " +
(info.Creator || "-").trim() +
"]" +
" (PDF.js: " +
(version || "-") +
(AppOptions.get("enableWebGL") ? " [WebGL]" : "") +
")"
); );
let pdfTitle; let pdfTitle;
@ -1405,14 +1388,54 @@ const PDFViewerApplication = {
formType, formType,
}); });
} }
}); },
/**
* @private
*/
async _initializePageLabels(pdfDocument) {
const labels = await pdfDocument.getPageLabels();
if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while the page labels resolved.
}
if (!labels || AppOptions.get("disablePageLabels")) {
return;
}
const numLabels = labels.length;
if (numLabels !== this.pagesCount) {
console.error(
"The number of Page Labels does not match the number of pages in the document."
);
return;
}
let i = 0;
// Ignore page labels that correspond to standard page numbering.
while (i < numLabels && labels[i] === (i + 1).toString()) {
i++;
}
if (i === numLabels) {
return;
}
const { pdfViewer, pdfThumbnailViewer, toolbar } = this;
pdfViewer.setPageLabels(labels);
pdfThumbnailViewer.setPageLabels(labels);
// Changing toolbar page display to use labels and we need to set
// the label of the current page.
toolbar.setPagesCount(numLabels, true);
toolbar.setPageNumber(
pdfViewer.currentPageNumber,
pdfViewer.currentPageLabel
);
}, },
/** /**
* @private * @private
*/ */
_initializePdfHistory({ fingerprint, viewOnLoad, initialDest = null }) { _initializePdfHistory({ fingerprint, viewOnLoad, initialDest = null }) {
if (AppOptions.get("disableHistory") || this.isViewerEmbedded) { if (this.isViewerEmbedded || AppOptions.get("disableHistory")) {
// The browsing history is only enabled when the viewer is standalone, // The browsing history is only enabled when the viewer is standalone,
// i.e. not when it is embedded in a web page. // i.e. not when it is embedded in a web page.
return; return;