From 0e604f8f42c378f4385f7498f44078c51a03c348 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 28 May 2023 12:30:11 +0200 Subject: [PATCH 1/2] Use local variables more in `PDFViewerApplication._initializeViewerComponents` --- web/app.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/web/app.js b/web/app.js index 0acf47fd5..58d8a4ce6 100644 --- a/web/app.js +++ b/web/app.js @@ -447,7 +447,7 @@ const PDFViewerApplication = { * @private */ async _initializeViewerComponents() { - const { appConfig, externalServices } = this; + const { appConfig, externalServices, l10n } = this; const eventBus = externalServices.isInAutomation ? new AutomationEventBus() @@ -504,7 +504,7 @@ const PDFViewerApplication = { } : null; - this.pdfViewer = new PDFViewer({ + const pdfViewer = new PDFViewer({ container, viewer, eventBus, @@ -514,7 +514,7 @@ const PDFViewerApplication = { findController, scriptingManager: AppOptions.get("enableScripting") && pdfScriptingManager, - l10n: this.l10n, + l10n, textLayerMode: AppOptions.get("textLayerMode"), annotationMode: AppOptions.get("annotationMode"), annotationEditorMode, @@ -526,9 +526,11 @@ const PDFViewerApplication = { enablePermissions: AppOptions.get("enablePermissions"), pageColors, }); - pdfRenderingQueue.setViewer(this.pdfViewer); - pdfLinkService.setViewer(this.pdfViewer); - pdfScriptingManager.setViewer(this.pdfViewer); + this.pdfViewer = pdfViewer; + + pdfRenderingQueue.setViewer(pdfViewer); + pdfLinkService.setViewer(pdfViewer); + pdfScriptingManager.setViewer(pdfViewer); if (appConfig.sidebar?.thumbnailView) { this.pdfThumbnailViewer = new PDFThumbnailViewer({ @@ -536,7 +538,7 @@ const PDFViewerApplication = { eventBus, renderingQueue: pdfRenderingQueue, linkService: pdfLinkService, - l10n: this.l10n, + l10n, pageColors, }); pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer); @@ -553,7 +555,7 @@ const PDFViewerApplication = { } if (!this.supportsIntegratedFind && appConfig.findBar) { - this.findBar = new PDFFindBar(appConfig.findBar, eventBus, this.l10n); + this.findBar = new PDFFindBar(appConfig.findBar, eventBus, l10n); } if (appConfig.annotationEditorParams) { @@ -574,10 +576,8 @@ const PDFViewerApplication = { appConfig.documentProperties, this.overlayManager, eventBus, - this.l10n, - /* fileNameLookup = */ () => { - return this._docFilename; - } + l10n, + /* fileNameLookup = */ () => this._docFilename ); } @@ -601,13 +601,13 @@ const PDFViewerApplication = { this.toolbar = new Toolbar( appConfig.toolbar, eventBus, - this.l10n, + l10n, await this._nimbusDataPromise, - this.externalServices + externalServices ); } } else { - this.toolbar = new Toolbar(appConfig.toolbar, eventBus, this.l10n); + this.toolbar = new Toolbar(appConfig.toolbar, eventBus, l10n); } } @@ -615,7 +615,7 @@ const PDFViewerApplication = { this.secondaryToolbar = new SecondaryToolbar( appConfig.secondaryToolbar, eventBus, - this.externalServices + externalServices ); } @@ -625,7 +625,7 @@ const PDFViewerApplication = { ) { this.pdfPresentationMode = new PDFPresentationMode({ container, - pdfViewer: this.pdfViewer, + pdfViewer, eventBus, }); } @@ -634,7 +634,7 @@ const PDFViewerApplication = { this.passwordPrompt = new PasswordPrompt( appConfig.passwordOverlay, this.overlayManager, - this.l10n, + l10n, this.isViewerEmbedded ); } @@ -660,17 +660,17 @@ const PDFViewerApplication = { this.pdfLayerViewer = new PDFLayerViewer({ container: appConfig.sidebar.layersView, eventBus, - l10n: this.l10n, + l10n, }); } if (appConfig.sidebar) { this.pdfSidebar = new PDFSidebar({ elements: appConfig.sidebar, - pdfViewer: this.pdfViewer, + pdfViewer, pdfThumbnailViewer: this.pdfThumbnailViewer, eventBus, - l10n: this.l10n, + l10n, }); this.pdfSidebar.onToggled = this.forceRendering.bind(this); } From c4c8227d20b2a1662e8e119bfcabe2ca6e8738cc Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 28 May 2023 12:30:18 +0200 Subject: [PATCH 2/2] Re-factor updating of thumbnails in the `PDFSidebar`-class This patch does two things: - Moves the updating of thumbnails into `web/app.js`, via a new `PDFSidebar` callback-function, to avoid having to include otherwise unnecessary parameters when initializing a `PDFSidebar`-instance. - Only attempt to generate thumbnail-images from pages that are *cached* in the viewer. Note that only pages that exist in the `PDFPageViewBuffer`-instance can be rendered, hence it's not actually meaningful to check every single page when updating the thumbnails. For large documents, with thousands of pages, this should be a tiny bit more efficient when e.g. opening the sidebar since we no longer need to check pages that we know have not been rendered. --- web/app.js | 15 +++++++++++++-- web/pdf_sidebar.js | 30 +++++------------------------- web/pdf_viewer.js | 4 ++++ 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/web/app.js b/web/app.js index 58d8a4ce6..ee6001ef4 100644 --- a/web/app.js +++ b/web/app.js @@ -667,12 +667,23 @@ const PDFViewerApplication = { if (appConfig.sidebar) { this.pdfSidebar = new PDFSidebar({ elements: appConfig.sidebar, - pdfViewer, - pdfThumbnailViewer: this.pdfThumbnailViewer, eventBus, l10n, }); this.pdfSidebar.onToggled = this.forceRendering.bind(this); + this.pdfSidebar.onUpdateThumbnails = () => { + // Use the rendered pages to set the corresponding thumbnail images. + for (const pageView of pdfViewer.getCachedPageViews()) { + if (pageView.renderingState === RenderingStates.FINISHED) { + this.pdfThumbnailViewer + .getThumbnail(pageView.id - 1) + ?.setImage(pageView); + } + } + this.pdfThumbnailViewer.scrollThumbnailIntoView( + pdfViewer.currentPageNumber + ); + }; } }, diff --git a/web/pdf_sidebar.js b/web/pdf_sidebar.js index f1eabd6f0..69c47bc02 100644 --- a/web/pdf_sidebar.js +++ b/web/pdf_sidebar.js @@ -16,7 +16,6 @@ import { docStyle, PresentationModeState, - RenderingStates, SidebarView, toggleCheckedBtn, toggleExpandedBtn, @@ -30,8 +29,6 @@ const UI_NOTIFICATION_CLASS = "pdfSidebarNotification"; /** * @typedef {Object} PDFSidebarOptions * @property {PDFSidebarElements} elements - The DOM elements. - * @property {PDFViewer} pdfViewer - The document viewer. - * @property {PDFThumbnailViewer} pdfThumbnailViewer - The thumbnail viewer. * @property {EventBus} eventBus - The application event bus. * @property {IL10n} l10n - The localization service. */ @@ -82,7 +79,7 @@ class PDFSidebar { /** * @param {PDFSidebarOptions} options */ - constructor({ elements, pdfViewer, pdfThumbnailViewer, eventBus, l10n }) { + constructor({ elements, eventBus, l10n }) { this.isOpen = false; this.active = SidebarView.THUMBS; this.isInitialViewSet = false; @@ -93,9 +90,7 @@ class PDFSidebar { * the viewers (PDFViewer/PDFThumbnailViewer) are updated correctly. */ this.onToggled = null; - - this.pdfViewer = pdfViewer; - this.pdfThumbnailViewer = pdfThumbnailViewer; + this.onUpdateThumbnails = null; this.outerContainer = elements.outerContainer; this.sidebarContainer = elements.sidebarContainer; @@ -246,7 +241,7 @@ class PDFSidebar { return; // Opening will trigger rendering and dispatch the event. } if (forceRendering) { - this.#updateThumbnailViewer(); + this.onUpdateThumbnails(); this.onToggled(); } if (isViewChanged) { @@ -264,7 +259,7 @@ class PDFSidebar { this.outerContainer.classList.add("sidebarMoving", "sidebarOpen"); if (this.active === SidebarView.THUMBS) { - this.#updateThumbnailViewer(); + this.onUpdateThumbnails(); } this.onToggled(); this.#dispatchEvent(); @@ -305,21 +300,6 @@ class PDFSidebar { }); } - #updateThumbnailViewer() { - const { pdfViewer, pdfThumbnailViewer } = this; - - // Use the rendered pages to set the corresponding thumbnail images. - const pagesCount = pdfViewer.pagesCount; - for (let pageIndex = 0; pageIndex < pagesCount; pageIndex++) { - const pageView = pdfViewer.getPageView(pageIndex); - if (pageView?.renderingState === RenderingStates.FINISHED) { - const thumbnailView = pdfThumbnailViewer.getThumbnail(pageIndex); - thumbnailView.setImage(pageView); - } - } - pdfThumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber); - } - #showUINotification() { this.toggleButton.setAttribute( "data-l10n-id", @@ -428,7 +408,7 @@ class PDFSidebar { evt.state === PresentationModeState.NORMAL && this.visibleView === SidebarView.THUMBS ) { - this.#updateThumbnailViewer(); + this.onUpdateThumbnails(); } }); diff --git a/web/pdf_viewer.js b/web/pdf_viewer.js index 63cc21f72..14b18e3be 100644 --- a/web/pdf_viewer.js +++ b/web/pdf_viewer.js @@ -341,6 +341,10 @@ class PDFViewer { return this._pages[index]; } + getCachedPageViews() { + return new Set(this.#buffer); + } + /** * @type {boolean} - True if all {PDFPageView} objects are initialized. */