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:
commit
09cccd8ecc
141
web/app.js
141
web/app.js
@ -1185,44 +1185,35 @@ const PDFViewerApplication = {
|
||||
});
|
||||
});
|
||||
|
||||
pdfDocument.getPageLabels().then(labels => {
|
||||
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;
|
||||
}
|
||||
|
||||
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(() => {
|
||||
this._initializeAutoPrint(pdfDocument, openActionPromise);
|
||||
});
|
||||
|
||||
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([
|
||||
openActionPromise,
|
||||
pdfDocument.getJavaScript(),
|
||||
]);
|
||||
|
||||
if (pdfDocument !== this.pdfDocument) {
|
||||
return; // The document was closed while the auto print data resolved.
|
||||
}
|
||||
let triggerAutoPrint = false;
|
||||
|
||||
if (openAction && openAction.action === "Print") {
|
||||
@ -1258,39 +1249,31 @@ const PDFViewerApplication = {
|
||||
window.print();
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
onePageRendered.then(() => {
|
||||
pdfDocument.getOutline().then(outline => {
|
||||
this.pdfOutlineViewer.render({ outline });
|
||||
});
|
||||
pdfDocument.getAttachments().then(attachments => {
|
||||
this.pdfAttachmentViewer.render({ attachments });
|
||||
});
|
||||
});
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
async _initializeMetadata(pdfDocument) {
|
||||
const {
|
||||
info,
|
||||
metadata,
|
||||
contentDispositionFilename,
|
||||
} = await pdfDocument.getMetadata();
|
||||
|
||||
pdfDocument
|
||||
.getMetadata()
|
||||
.then(({ info, metadata, contentDispositionFilename }) => {
|
||||
if (pdfDocument !== this.pdfDocument) {
|
||||
return; // The document was closed while the metadata resolved.
|
||||
}
|
||||
this.documentInfo = info;
|
||||
this.metadata = metadata;
|
||||
this.contentDispositionFilename = contentDispositionFilename;
|
||||
|
||||
// Provides some basic debug information
|
||||
console.log(
|
||||
"PDF " +
|
||||
pdfDocument.fingerprint +
|
||||
" [" +
|
||||
info.PDFFormatVersion +
|
||||
" " +
|
||||
(info.Producer || "-").trim() +
|
||||
" / " +
|
||||
(info.Creator || "-").trim() +
|
||||
"]" +
|
||||
" (PDF.js: " +
|
||||
(version || "-") +
|
||||
(AppOptions.get("enableWebGL") ? " [WebGL]" : "") +
|
||||
")"
|
||||
`PDF ${pdfDocument.fingerprint} [${info.PDFFormatVersion} ` +
|
||||
`${(info.Producer || "-").trim()} / ${(info.Creator || "-").trim()}] ` +
|
||||
`(PDF.js: ${version || "-"}` +
|
||||
`${this.pdfViewer.enableWebGL ? " [WebGL]" : ""})`
|
||||
);
|
||||
|
||||
let pdfTitle;
|
||||
@ -1405,14 +1388,54 @@ const PDFViewerApplication = {
|
||||
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
|
||||
*/
|
||||
_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,
|
||||
// i.e. not when it is embedded in a web page.
|
||||
return;
|
||||
|
Loading…
x
Reference in New Issue
Block a user