From 32f1d0de76d6566e62cc98f4351ed383337ed24b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 5 Apr 2020 10:24:02 +0200 Subject: [PATCH] Move the initialization of "page labels" out of `PDFViewerApplication.load` Over time, with more and more API-functionality added, the `PDFViewerApplication.load` method has become quite large and complex. In an attempt to improve the current situation somewhat, this patch moves the fetching and initialization of "page labels" out into its own (private) helper method instead. --- web/app.js | 76 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/web/app.js b/web/app.js index 155462fa3..aa8abfed3 100644 --- a/web/app.js +++ b/web/app.js @@ -1185,39 +1185,6 @@ 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(async () => { const [openAction, javaScript] = await Promise.all([ openActionPromise, @@ -1269,6 +1236,8 @@ const PDFViewerApplication = { }); }); + this._initializePageLabels(pdfDocument); + pdfDocument .getMetadata() .then(({ info, metadata, contentDispositionFilename }) => { @@ -1408,6 +1377,47 @@ const PDFViewerApplication = { }); }, + /** + * @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 */