From 517af6b6ab164d92f188524e80c56e3e129e3f8f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 19 Dec 2020 12:49:12 +0100 Subject: [PATCH] Delay initialization of the `AnnotationStorage` callbacks slightly in the default viewer These callbacks should not be necessary *before* the document has been initialized. Furthermore, move the functionality to a new helper-method since `PDFViewerApplication.load` is already quite large. --- web/app.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/web/app.js b/web/app.js index 7eb431c7d..80a67da55 100644 --- a/web/app.js +++ b/web/app.js @@ -1287,14 +1287,6 @@ const PDFViewerApplication = { this.pdfLinkService.setDocument(pdfDocument, baseDocumentUrl); this.pdfDocumentProperties.setDocument(pdfDocument, this.url); - const annotationStorage = pdfDocument.annotationStorage; - annotationStorage.onSetModified = function () { - window.addEventListener("beforeunload", beforeUnload); - }; - annotationStorage.onResetModified = function () { - window.removeEventListener("beforeunload", beforeUnload); - }; - const pdfViewer = this.pdfViewer; pdfViewer.setDocument(pdfDocument); const { firstPagePromise, onePageRendered, pagesPromise } = pdfViewer; @@ -1322,6 +1314,7 @@ const PDFViewerApplication = { firstPagePromise.then(pdfPage => { this.loadingBar.setWidth(this.appConfig.viewerContainer); + this._initializeAnnotationStorageCallbacks(pdfDocument); Promise.all([ animationStarted, @@ -1878,6 +1871,23 @@ const PDFViewerApplication = { } }, + /** + * @private + */ + _initializeAnnotationStorageCallbacks(pdfDocument) { + if (pdfDocument !== this.pdfDocument) { + return; + } + const { annotationStorage } = pdfDocument; + + annotationStorage.onSetModified = function () { + window.addEventListener("beforeunload", beforeUnload); + }; + annotationStorage.onResetModified = function () { + window.removeEventListener("beforeunload", beforeUnload); + }; + }, + setInitialView( storedHash, { rotation, sidebarView, scrollMode, spreadMode } = {}