Merge pull request #16484 from Snuffleupagus/PDFSidebar-onUpdateThumbnails

Re-factor updating of thumbnails in the `PDFSidebar`-class
This commit is contained in:
Tim van der Meij 2023-05-29 12:56:24 +02:00 committed by GitHub
commit 036f855dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 47 deletions

View File

@ -447,7 +447,7 @@ const PDFViewerApplication = {
* @private * @private
*/ */
async _initializeViewerComponents() { async _initializeViewerComponents() {
const { appConfig, externalServices } = this; const { appConfig, externalServices, l10n } = this;
const eventBus = externalServices.isInAutomation const eventBus = externalServices.isInAutomation
? new AutomationEventBus() ? new AutomationEventBus()
@ -504,7 +504,7 @@ const PDFViewerApplication = {
} }
: null; : null;
this.pdfViewer = new PDFViewer({ const pdfViewer = new PDFViewer({
container, container,
viewer, viewer,
eventBus, eventBus,
@ -514,7 +514,7 @@ const PDFViewerApplication = {
findController, findController,
scriptingManager: scriptingManager:
AppOptions.get("enableScripting") && pdfScriptingManager, AppOptions.get("enableScripting") && pdfScriptingManager,
l10n: this.l10n, l10n,
textLayerMode: AppOptions.get("textLayerMode"), textLayerMode: AppOptions.get("textLayerMode"),
annotationMode: AppOptions.get("annotationMode"), annotationMode: AppOptions.get("annotationMode"),
annotationEditorMode, annotationEditorMode,
@ -526,9 +526,11 @@ const PDFViewerApplication = {
enablePermissions: AppOptions.get("enablePermissions"), enablePermissions: AppOptions.get("enablePermissions"),
pageColors, pageColors,
}); });
pdfRenderingQueue.setViewer(this.pdfViewer); this.pdfViewer = pdfViewer;
pdfLinkService.setViewer(this.pdfViewer);
pdfScriptingManager.setViewer(this.pdfViewer); pdfRenderingQueue.setViewer(pdfViewer);
pdfLinkService.setViewer(pdfViewer);
pdfScriptingManager.setViewer(pdfViewer);
if (appConfig.sidebar?.thumbnailView) { if (appConfig.sidebar?.thumbnailView) {
this.pdfThumbnailViewer = new PDFThumbnailViewer({ this.pdfThumbnailViewer = new PDFThumbnailViewer({
@ -536,7 +538,7 @@ const PDFViewerApplication = {
eventBus, eventBus,
renderingQueue: pdfRenderingQueue, renderingQueue: pdfRenderingQueue,
linkService: pdfLinkService, linkService: pdfLinkService,
l10n: this.l10n, l10n,
pageColors, pageColors,
}); });
pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer); pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer);
@ -553,7 +555,7 @@ const PDFViewerApplication = {
} }
if (!this.supportsIntegratedFind && appConfig.findBar) { 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) { if (appConfig.annotationEditorParams) {
@ -574,10 +576,8 @@ const PDFViewerApplication = {
appConfig.documentProperties, appConfig.documentProperties,
this.overlayManager, this.overlayManager,
eventBus, eventBus,
this.l10n, l10n,
/* fileNameLookup = */ () => { /* fileNameLookup = */ () => this._docFilename
return this._docFilename;
}
); );
} }
@ -601,13 +601,13 @@ const PDFViewerApplication = {
this.toolbar = new Toolbar( this.toolbar = new Toolbar(
appConfig.toolbar, appConfig.toolbar,
eventBus, eventBus,
this.l10n, l10n,
await this._nimbusDataPromise, await this._nimbusDataPromise,
this.externalServices externalServices
); );
} }
} else { } 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( this.secondaryToolbar = new SecondaryToolbar(
appConfig.secondaryToolbar, appConfig.secondaryToolbar,
eventBus, eventBus,
this.externalServices externalServices
); );
} }
@ -625,7 +625,7 @@ const PDFViewerApplication = {
) { ) {
this.pdfPresentationMode = new PDFPresentationMode({ this.pdfPresentationMode = new PDFPresentationMode({
container, container,
pdfViewer: this.pdfViewer, pdfViewer,
eventBus, eventBus,
}); });
} }
@ -634,7 +634,7 @@ const PDFViewerApplication = {
this.passwordPrompt = new PasswordPrompt( this.passwordPrompt = new PasswordPrompt(
appConfig.passwordOverlay, appConfig.passwordOverlay,
this.overlayManager, this.overlayManager,
this.l10n, l10n,
this.isViewerEmbedded this.isViewerEmbedded
); );
} }
@ -660,19 +660,30 @@ const PDFViewerApplication = {
this.pdfLayerViewer = new PDFLayerViewer({ this.pdfLayerViewer = new PDFLayerViewer({
container: appConfig.sidebar.layersView, container: appConfig.sidebar.layersView,
eventBus, eventBus,
l10n: this.l10n, l10n,
}); });
} }
if (appConfig.sidebar) { if (appConfig.sidebar) {
this.pdfSidebar = new PDFSidebar({ this.pdfSidebar = new PDFSidebar({
elements: appConfig.sidebar, elements: appConfig.sidebar,
pdfViewer: this.pdfViewer,
pdfThumbnailViewer: this.pdfThumbnailViewer,
eventBus, eventBus,
l10n: this.l10n, l10n,
}); });
this.pdfSidebar.onToggled = this.forceRendering.bind(this); 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
);
};
} }
}, },

View File

@ -16,7 +16,6 @@
import { import {
docStyle, docStyle,
PresentationModeState, PresentationModeState,
RenderingStates,
SidebarView, SidebarView,
toggleCheckedBtn, toggleCheckedBtn,
toggleExpandedBtn, toggleExpandedBtn,
@ -30,8 +29,6 @@ const UI_NOTIFICATION_CLASS = "pdfSidebarNotification";
/** /**
* @typedef {Object} PDFSidebarOptions * @typedef {Object} PDFSidebarOptions
* @property {PDFSidebarElements} elements - The DOM elements. * @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 {EventBus} eventBus - The application event bus.
* @property {IL10n} l10n - The localization service. * @property {IL10n} l10n - The localization service.
*/ */
@ -82,7 +79,7 @@ class PDFSidebar {
/** /**
* @param {PDFSidebarOptions} options * @param {PDFSidebarOptions} options
*/ */
constructor({ elements, pdfViewer, pdfThumbnailViewer, eventBus, l10n }) { constructor({ elements, eventBus, l10n }) {
this.isOpen = false; this.isOpen = false;
this.active = SidebarView.THUMBS; this.active = SidebarView.THUMBS;
this.isInitialViewSet = false; this.isInitialViewSet = false;
@ -93,9 +90,7 @@ class PDFSidebar {
* the viewers (PDFViewer/PDFThumbnailViewer) are updated correctly. * the viewers (PDFViewer/PDFThumbnailViewer) are updated correctly.
*/ */
this.onToggled = null; this.onToggled = null;
this.onUpdateThumbnails = null;
this.pdfViewer = pdfViewer;
this.pdfThumbnailViewer = pdfThumbnailViewer;
this.outerContainer = elements.outerContainer; this.outerContainer = elements.outerContainer;
this.sidebarContainer = elements.sidebarContainer; this.sidebarContainer = elements.sidebarContainer;
@ -246,7 +241,7 @@ class PDFSidebar {
return; // Opening will trigger rendering and dispatch the event. return; // Opening will trigger rendering and dispatch the event.
} }
if (forceRendering) { if (forceRendering) {
this.#updateThumbnailViewer(); this.onUpdateThumbnails();
this.onToggled(); this.onToggled();
} }
if (isViewChanged) { if (isViewChanged) {
@ -264,7 +259,7 @@ class PDFSidebar {
this.outerContainer.classList.add("sidebarMoving", "sidebarOpen"); this.outerContainer.classList.add("sidebarMoving", "sidebarOpen");
if (this.active === SidebarView.THUMBS) { if (this.active === SidebarView.THUMBS) {
this.#updateThumbnailViewer(); this.onUpdateThumbnails();
} }
this.onToggled(); this.onToggled();
this.#dispatchEvent(); 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() { #showUINotification() {
this.toggleButton.setAttribute( this.toggleButton.setAttribute(
"data-l10n-id", "data-l10n-id",
@ -428,7 +408,7 @@ class PDFSidebar {
evt.state === PresentationModeState.NORMAL && evt.state === PresentationModeState.NORMAL &&
this.visibleView === SidebarView.THUMBS this.visibleView === SidebarView.THUMBS
) { ) {
this.#updateThumbnailViewer(); this.onUpdateThumbnails();
} }
}); });

View File

@ -341,6 +341,10 @@ class PDFViewer {
return this._pages[index]; return this._pages[index];
} }
getCachedPageViews() {
return new Set(this.#buffer);
}
/** /**
* @type {boolean} - True if all {PDFPageView} objects are initialized. * @type {boolean} - True if all {PDFPageView} objects are initialized.
*/ */