Skip to content

Commit 036f855

Browse files
Merge pull request #16484 from Snuffleupagus/PDFSidebar-onUpdateThumbnails
Re-factor updating of thumbnails in the `PDFSidebar`-class
2 parents f06d0b2 + c4c8227 commit 036f855

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed

web/app.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ const PDFViewerApplication = {
447447
* @private
448448
*/
449449
async _initializeViewerComponents() {
450-
const { appConfig, externalServices } = this;
450+
const { appConfig, externalServices, l10n } = this;
451451

452452
const eventBus = externalServices.isInAutomation
453453
? new AutomationEventBus()
@@ -504,7 +504,7 @@ const PDFViewerApplication = {
504504
}
505505
: null;
506506

507-
this.pdfViewer = new PDFViewer({
507+
const pdfViewer = new PDFViewer({
508508
container,
509509
viewer,
510510
eventBus,
@@ -514,7 +514,7 @@ const PDFViewerApplication = {
514514
findController,
515515
scriptingManager:
516516
AppOptions.get("enableScripting") && pdfScriptingManager,
517-
l10n: this.l10n,
517+
l10n,
518518
textLayerMode: AppOptions.get("textLayerMode"),
519519
annotationMode: AppOptions.get("annotationMode"),
520520
annotationEditorMode,
@@ -526,17 +526,19 @@ const PDFViewerApplication = {
526526
enablePermissions: AppOptions.get("enablePermissions"),
527527
pageColors,
528528
});
529-
pdfRenderingQueue.setViewer(this.pdfViewer);
530-
pdfLinkService.setViewer(this.pdfViewer);
531-
pdfScriptingManager.setViewer(this.pdfViewer);
529+
this.pdfViewer = pdfViewer;
530+
531+
pdfRenderingQueue.setViewer(pdfViewer);
532+
pdfLinkService.setViewer(pdfViewer);
533+
pdfScriptingManager.setViewer(pdfViewer);
532534

533535
if (appConfig.sidebar?.thumbnailView) {
534536
this.pdfThumbnailViewer = new PDFThumbnailViewer({
535537
container: appConfig.sidebar.thumbnailView,
536538
eventBus,
537539
renderingQueue: pdfRenderingQueue,
538540
linkService: pdfLinkService,
539-
l10n: this.l10n,
541+
l10n,
540542
pageColors,
541543
});
542544
pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer);
@@ -553,7 +555,7 @@ const PDFViewerApplication = {
553555
}
554556

555557
if (!this.supportsIntegratedFind && appConfig.findBar) {
556-
this.findBar = new PDFFindBar(appConfig.findBar, eventBus, this.l10n);
558+
this.findBar = new PDFFindBar(appConfig.findBar, eventBus, l10n);
557559
}
558560

559561
if (appConfig.annotationEditorParams) {
@@ -574,10 +576,8 @@ const PDFViewerApplication = {
574576
appConfig.documentProperties,
575577
this.overlayManager,
576578
eventBus,
577-
this.l10n,
578-
/* fileNameLookup = */ () => {
579-
return this._docFilename;
580-
}
579+
l10n,
580+
/* fileNameLookup = */ () => this._docFilename
581581
);
582582
}
583583

@@ -601,21 +601,21 @@ const PDFViewerApplication = {
601601
this.toolbar = new Toolbar(
602602
appConfig.toolbar,
603603
eventBus,
604-
this.l10n,
604+
l10n,
605605
await this._nimbusDataPromise,
606-
this.externalServices
606+
externalServices
607607
);
608608
}
609609
} else {
610-
this.toolbar = new Toolbar(appConfig.toolbar, eventBus, this.l10n);
610+
this.toolbar = new Toolbar(appConfig.toolbar, eventBus, l10n);
611611
}
612612
}
613613

614614
if (appConfig.secondaryToolbar) {
615615
this.secondaryToolbar = new SecondaryToolbar(
616616
appConfig.secondaryToolbar,
617617
eventBus,
618-
this.externalServices
618+
externalServices
619619
);
620620
}
621621

@@ -625,7 +625,7 @@ const PDFViewerApplication = {
625625
) {
626626
this.pdfPresentationMode = new PDFPresentationMode({
627627
container,
628-
pdfViewer: this.pdfViewer,
628+
pdfViewer,
629629
eventBus,
630630
});
631631
}
@@ -634,7 +634,7 @@ const PDFViewerApplication = {
634634
this.passwordPrompt = new PasswordPrompt(
635635
appConfig.passwordOverlay,
636636
this.overlayManager,
637-
this.l10n,
637+
l10n,
638638
this.isViewerEmbedded
639639
);
640640
}
@@ -660,19 +660,30 @@ const PDFViewerApplication = {
660660
this.pdfLayerViewer = new PDFLayerViewer({
661661
container: appConfig.sidebar.layersView,
662662
eventBus,
663-
l10n: this.l10n,
663+
l10n,
664664
});
665665
}
666666

667667
if (appConfig.sidebar) {
668668
this.pdfSidebar = new PDFSidebar({
669669
elements: appConfig.sidebar,
670-
pdfViewer: this.pdfViewer,
671-
pdfThumbnailViewer: this.pdfThumbnailViewer,
672670
eventBus,
673-
l10n: this.l10n,
671+
l10n,
674672
});
675673
this.pdfSidebar.onToggled = this.forceRendering.bind(this);
674+
this.pdfSidebar.onUpdateThumbnails = () => {
675+
// Use the rendered pages to set the corresponding thumbnail images.
676+
for (const pageView of pdfViewer.getCachedPageViews()) {
677+
if (pageView.renderingState === RenderingStates.FINISHED) {
678+
this.pdfThumbnailViewer
679+
.getThumbnail(pageView.id - 1)
680+
?.setImage(pageView);
681+
}
682+
}
683+
this.pdfThumbnailViewer.scrollThumbnailIntoView(
684+
pdfViewer.currentPageNumber
685+
);
686+
};
676687
}
677688
},
678689

web/pdf_sidebar.js

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import {
1717
docStyle,
1818
PresentationModeState,
19-
RenderingStates,
2019
SidebarView,
2120
toggleCheckedBtn,
2221
toggleExpandedBtn,
@@ -30,8 +29,6 @@ const UI_NOTIFICATION_CLASS = "pdfSidebarNotification";
3029
/**
3130
* @typedef {Object} PDFSidebarOptions
3231
* @property {PDFSidebarElements} elements - The DOM elements.
33-
* @property {PDFViewer} pdfViewer - The document viewer.
34-
* @property {PDFThumbnailViewer} pdfThumbnailViewer - The thumbnail viewer.
3532
* @property {EventBus} eventBus - The application event bus.
3633
* @property {IL10n} l10n - The localization service.
3734
*/
@@ -82,7 +79,7 @@ class PDFSidebar {
8279
/**
8380
* @param {PDFSidebarOptions} options
8481
*/
85-
constructor({ elements, pdfViewer, pdfThumbnailViewer, eventBus, l10n }) {
82+
constructor({ elements, eventBus, l10n }) {
8683
this.isOpen = false;
8784
this.active = SidebarView.THUMBS;
8885
this.isInitialViewSet = false;
@@ -93,9 +90,7 @@ class PDFSidebar {
9390
* the viewers (PDFViewer/PDFThumbnailViewer) are updated correctly.
9491
*/
9592
this.onToggled = null;
96-
97-
this.pdfViewer = pdfViewer;
98-
this.pdfThumbnailViewer = pdfThumbnailViewer;
93+
this.onUpdateThumbnails = null;
9994

10095
this.outerContainer = elements.outerContainer;
10196
this.sidebarContainer = elements.sidebarContainer;
@@ -246,7 +241,7 @@ class PDFSidebar {
246241
return; // Opening will trigger rendering and dispatch the event.
247242
}
248243
if (forceRendering) {
249-
this.#updateThumbnailViewer();
244+
this.onUpdateThumbnails();
250245
this.onToggled();
251246
}
252247
if (isViewChanged) {
@@ -264,7 +259,7 @@ class PDFSidebar {
264259
this.outerContainer.classList.add("sidebarMoving", "sidebarOpen");
265260

266261
if (this.active === SidebarView.THUMBS) {
267-
this.#updateThumbnailViewer();
262+
this.onUpdateThumbnails();
268263
}
269264
this.onToggled();
270265
this.#dispatchEvent();
@@ -305,21 +300,6 @@ class PDFSidebar {
305300
});
306301
}
307302

308-
#updateThumbnailViewer() {
309-
const { pdfViewer, pdfThumbnailViewer } = this;
310-
311-
// Use the rendered pages to set the corresponding thumbnail images.
312-
const pagesCount = pdfViewer.pagesCount;
313-
for (let pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
314-
const pageView = pdfViewer.getPageView(pageIndex);
315-
if (pageView?.renderingState === RenderingStates.FINISHED) {
316-
const thumbnailView = pdfThumbnailViewer.getThumbnail(pageIndex);
317-
thumbnailView.setImage(pageView);
318-
}
319-
}
320-
pdfThumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
321-
}
322-
323303
#showUINotification() {
324304
this.toggleButton.setAttribute(
325305
"data-l10n-id",
@@ -428,7 +408,7 @@ class PDFSidebar {
428408
evt.state === PresentationModeState.NORMAL &&
429409
this.visibleView === SidebarView.THUMBS
430410
) {
431-
this.#updateThumbnailViewer();
411+
this.onUpdateThumbnails();
432412
}
433413
});
434414

web/pdf_viewer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ class PDFViewer {
341341
return this._pages[index];
342342
}
343343

344+
getCachedPageViews() {
345+
return new Set(this.#buffer);
346+
}
347+
344348
/**
345349
* @type {boolean} - True if all {PDFPageView} objects are initialized.
346350
*/

0 commit comments

Comments
 (0)