1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 14:48:08 +02:00

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
*/
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,19 +660,30 @@ 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,
pdfThumbnailViewer: this.pdfThumbnailViewer,
eventBus,
l10n: this.l10n,
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
);
};
}
},

View file

@ -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();
}
});

View file

@ -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.
*/