From 24b7fb20ef97cdd967b20b2dce3bf4015e60fff3 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 18 Oct 2021 12:13:54 +0200 Subject: [PATCH] Improve pre-rendering at the start/end of the document This is a very old "issue", which has existed since essentially forever, and it affects all of the available scrollModes. However, in the recently added Page-mode it's particularily noticeable since we use a *simulated* scroll direction there. When deciding what page(s) to pre-render, we only consider the current scroll direction. This works well in most cases, but can break down at the start/end of the document by trying to pre-render a page *outside* of the existing ones. To improve this, we'll thus *force* the scroll direction at the start/end of the document. *Steps to reproduce:* 0. Open the viewer, e.g. https://mozilla.github.io/pdf.js/web/viewer.html 1. Enable vertical scrolling. 2. Press the End key. 3. Open the devtools and, using the DOM Inspector, notice how page 13 is *not* being pre-rendered. --- web/base_viewer.js | 12 +++++++----- web/pdf_thumbnail_viewer.js | 12 +++++++++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/web/base_viewer.js b/web/base_viewer.js index fbacf361c..fdfb7857e 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -1361,10 +1361,12 @@ class BaseViewer { return promise; } - /** - * @private - */ - get _scrollAhead() { + #getScrollAhead(views) { + if (views.first.id === 1) { + return true; + } else if (views.last.id === this.pagesCount) { + return false; + } switch (this._scrollMode) { case ScrollMode.PAGE: return this._scrollModePageState.scrollDown; @@ -1376,7 +1378,7 @@ class BaseViewer { forceRendering(currentlyVisiblePages) { const visiblePages = currentlyVisiblePages || this._getVisiblePages(); - const scrollAhead = this._scrollAhead; + const scrollAhead = this.#getScrollAhead(visiblePages); const preRenderExtra = this._spreadMode !== SpreadMode.NONE && this._scrollMode !== ScrollMode.HORIZONTAL; diff --git a/web/pdf_thumbnail_viewer.js b/web/pdf_thumbnail_viewer.js index 36d2e6bd6..350b1a820 100644 --- a/web/pdf_thumbnail_viewer.js +++ b/web/pdf_thumbnail_viewer.js @@ -295,12 +295,22 @@ class PDFThumbnailViewer { return promise; } + #getScrollAhead(views) { + if (views.first.id === 1) { + return true; + } else if (views.last.id === this._thumbnails.length) { + return false; + } + return this.scroll.down; + } + forceRendering() { const visibleThumbs = this._getVisibleThumbs(); + const scrollAhead = this.#getScrollAhead(visibleThumbs); const thumbView = this.renderingQueue.getHighestPriority( visibleThumbs, this._thumbnails, - this.scroll.down + scrollAhead ); if (thumbView) { this._ensurePdfPageLoaded(thumbView).then(() => {