Skip to content

Commit

Permalink
Merge pull request #16484 from Snuffleupagus/PDFSidebar-onUpdateThumb…
Browse files Browse the repository at this point in the history
…nails

Re-factor updating of thumbnails in the `PDFSidebar`-class
  • Loading branch information
timvandermeij committed May 29, 2023
2 parents f06d0b2 + c4c8227 commit 036f855
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
55 changes: 33 additions & 22 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ const PDFViewerApplication = {
* @private
*/
async _initializeViewerComponents() {
const { appConfig, externalServices } = this;
const { appConfig, externalServices, l10n } = this;

const eventBus = externalServices.isInAutomation
? new AutomationEventBus()
Expand Down Expand Up @@ -504,7 +504,7 @@ const PDFViewerApplication = {
}
: null;

this.pdfViewer = new PDFViewer({
const pdfViewer = new PDFViewer({
container,
viewer,
eventBus,
Expand All @@ -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,
Expand All @@ -526,17 +526,19 @@ 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({
container: appConfig.sidebar.thumbnailView,
eventBus,
renderingQueue: pdfRenderingQueue,
linkService: pdfLinkService,
l10n: this.l10n,
l10n,
pageColors,
});
pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer);
Expand All @@ -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) {
Expand All @@ -574,10 +576,8 @@ const PDFViewerApplication = {
appConfig.documentProperties,
this.overlayManager,
eventBus,
this.l10n,
/* fileNameLookup = */ () => {
return this._docFilename;
}
l10n,
/* fileNameLookup = */ () => this._docFilename
);
}

Expand All @@ -601,21 +601,21 @@ 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);
}
}

if (appConfig.secondaryToolbar) {
this.secondaryToolbar = new SecondaryToolbar(
appConfig.secondaryToolbar,
eventBus,
this.externalServices
externalServices
);
}

Expand All @@ -625,7 +625,7 @@ const PDFViewerApplication = {
) {
this.pdfPresentationMode = new PDFPresentationMode({
container,
pdfViewer: this.pdfViewer,
pdfViewer,
eventBus,
});
}
Expand All @@ -634,7 +634,7 @@ const PDFViewerApplication = {
this.passwordPrompt = new PasswordPrompt(
appConfig.passwordOverlay,
this.overlayManager,
this.l10n,
l10n,
this.isViewerEmbedded
);
}
Expand All @@ -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
);
};
}
},

Expand Down
30 changes: 5 additions & 25 deletions web/pdf_sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import {
docStyle,
PresentationModeState,
RenderingStates,
SidebarView,
toggleCheckedBtn,
toggleExpandedBtn,
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -428,7 +408,7 @@ class PDFSidebar {
evt.state === PresentationModeState.NORMAL &&
this.visibleView === SidebarView.THUMBS
) {
this.#updateThumbnailViewer();
this.onUpdateThumbnails();
}
});

Expand Down
4 changes: 4 additions & 0 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 036f855

Please sign in to comment.