Skip to content

Commit e9e4b91

Browse files
Merge pull request mozilla#14324 from Snuffleupagus/force-PAGE-scrolling
Enforce PAGE-scrolling for *very* large/long documents (bug 1588435, PR 11263 follow-up)
2 parents 4c145fc + 6dfe4a9 commit e9e4b91

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

web/base_viewer.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ import { XfaLayerBuilder } from "./xfa_layer_builder.js";
5454

5555
const DEFAULT_CACHE_SIZE = 10;
5656

57+
const PagesCountLimit = {
58+
FORCE_SCROLL_MODE_PAGE: 15000,
59+
FORCE_LAZY_PAGE_INIT: 7500,
60+
};
61+
5762
/**
5863
* @typedef {Object} PDFViewerOptions
5964
* @property {HTMLDivElement} container - The container for the viewer element.
@@ -515,6 +520,16 @@ class BaseViewer {
515520
// Rendering (potentially) depends on this, hence fetching it immediately.
516521
const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
517522

523+
// Given that browsers don't handle huge amounts of DOM-elements very well,
524+
// enforce usage of PAGE-scrolling when loading *very* long/large documents.
525+
if (pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
526+
console.warn(
527+
"Forcing PAGE-scrolling for performance reasons, given the length of the document."
528+
);
529+
const mode = (this._scrollMode = ScrollMode.PAGE);
530+
this.eventBus.dispatch("scrollmodechanged", { source: this, mode });
531+
}
532+
518533
this._pagesCapability.promise.then(() => {
519534
this.eventBus.dispatch("pagesloaded", {
520535
source: this,
@@ -618,7 +633,10 @@ class BaseViewer {
618633

619634
// In addition to 'disableAutoFetch' being set, also attempt to reduce
620635
// resource usage when loading *very* long/large documents.
621-
if (pdfDocument.loadingParams.disableAutoFetch || pagesCount > 7500) {
636+
if (
637+
pdfDocument.loadingParams.disableAutoFetch ||
638+
pagesCount > PagesCountLimit.FORCE_LAZY_PAGE_INIT
639+
) {
622640
// XXX: Printing is semi-broken with auto fetch disabled.
623641
this._pagesCapability.resolve();
624642
return;
@@ -1685,6 +1703,9 @@ class BaseViewer {
16851703
if (!isValidScrollMode(mode)) {
16861704
throw new Error(`Invalid scroll mode: ${mode}`);
16871705
}
1706+
if (this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
1707+
return; // Disabled for performance reasons.
1708+
}
16881709
this._previousScrollMode = this._scrollMode;
16891710

16901711
this._scrollMode = mode;
@@ -1957,4 +1978,4 @@ class BaseViewer {
19571978
}
19581979
}
19591980

1960-
export { BaseViewer, PDFPageViewBuffer };
1981+
export { BaseViewer, PagesCountLimit, PDFPageViewBuffer };

web/secondary_toolbar.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import { SCROLLBAR_PADDING, ScrollMode, SpreadMode } from "./ui_utils.js";
1717
import { CursorTool } from "./pdf_cursor_tools.js";
18-
import { PDFSinglePageViewer } from "./pdf_viewer.js";
18+
import { PagesCountLimit } from "./base_viewer.js";
1919

2020
/**
2121
* @typedef {Object} SecondaryToolbarOptions
@@ -166,22 +166,6 @@ class SecondaryToolbar {
166166

167167
// Bind the event listener for adjusting the 'max-height' of the toolbar.
168168
this.eventBus._on("resize", this._setMaxHeight.bind(this));
169-
170-
// Hide the Scroll/Spread mode buttons, when they're not applicable to the
171-
// current `BaseViewer` instance (in particular `PDFSinglePageViewer`).
172-
this.eventBus._on("baseviewerinit", evt => {
173-
if (evt.source instanceof PDFSinglePageViewer) {
174-
this.toolbarButtonContainer.classList.add(
175-
"hiddenScrollModeButtons",
176-
"hiddenSpreadModeButtons"
177-
);
178-
} else {
179-
this.toolbarButtonContainer.classList.remove(
180-
"hiddenScrollModeButtons",
181-
"hiddenSpreadModeButtons"
182-
);
183-
}
184-
});
185169
}
186170

187171
/**
@@ -252,7 +236,7 @@ class SecondaryToolbar {
252236
}
253237

254238
_bindScrollModeListener(buttons) {
255-
function scrollModeChanged({ mode }) {
239+
const scrollModeChanged = ({ mode }) => {
256240
buttons.scrollPageButton.classList.toggle(
257241
"toggled",
258242
mode === ScrollMode.PAGE
@@ -270,13 +254,22 @@ class SecondaryToolbar {
270254
mode === ScrollMode.WRAPPED
271255
);
272256

257+
// Permanently *disable* the Scroll buttons when PAGE-scrolling is being
258+
// enforced for *very* long/large documents; please see the `BaseViewer`.
259+
const forceScrollModePage =
260+
this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE;
261+
buttons.scrollPageButton.disabled = forceScrollModePage;
262+
buttons.scrollVerticalButton.disabled = forceScrollModePage;
263+
buttons.scrollHorizontalButton.disabled = forceScrollModePage;
264+
buttons.scrollWrappedButton.disabled = forceScrollModePage;
265+
273266
// Temporarily *disable* the Spread buttons when horizontal scrolling is
274267
// enabled, since the non-default Spread modes doesn't affect the layout.
275268
const isScrollModeHorizontal = mode === ScrollMode.HORIZONTAL;
276269
buttons.spreadNoneButton.disabled = isScrollModeHorizontal;
277270
buttons.spreadOddButton.disabled = isScrollModeHorizontal;
278271
buttons.spreadEvenButton.disabled = isScrollModeHorizontal;
279-
}
272+
};
280273
this.eventBus._on("scrollmodechanged", scrollModeChanged);
281274

282275
this.eventBus._on("secondarytoolbarreset", evt => {

web/viewer.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,6 @@ html[dir="rtl"] .secondaryToolbar {
625625
margin-bottom: -4px;
626626
}
627627

628-
#secondaryToolbarButtonContainer.hiddenScrollModeButtons > .scrollModeButtons,
629-
#secondaryToolbarButtonContainer.hiddenSpreadModeButtons > .spreadModeButtons {
630-
display: none !important;
631-
}
632-
633628
.doorHanger,
634629
.doorHangerRight {
635630
border-radius: 2px;

web/viewer.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,32 +196,32 @@
196196

197197
<div class="horizontalToolbarSeparator"></div>
198198

199-
<button id="scrollPage" class="secondaryToolbarButton scrollModeButtons scrollPage" title="Use Page Scrolling" tabindex="62" data-l10n-id="scroll_page">
199+
<button id="scrollPage" class="secondaryToolbarButton scrollPage" title="Use Page Scrolling" tabindex="62" data-l10n-id="scroll_page">
200200
<span data-l10n-id="scroll_page_label">Page Scrolling</span>
201201
</button>
202-
<button id="scrollVertical" class="secondaryToolbarButton scrollModeButtons scrollVertical toggled" title="Use Vertical Scrolling" tabindex="63" data-l10n-id="scroll_vertical">
202+
<button id="scrollVertical" class="secondaryToolbarButton scrollVertical toggled" title="Use Vertical Scrolling" tabindex="63" data-l10n-id="scroll_vertical">
203203
<span data-l10n-id="scroll_vertical_label">Vertical Scrolling</span>
204204
</button>
205-
<button id="scrollHorizontal" class="secondaryToolbarButton scrollModeButtons scrollHorizontal" title="Use Horizontal Scrolling" tabindex="64" data-l10n-id="scroll_horizontal">
205+
<button id="scrollHorizontal" class="secondaryToolbarButton scrollHorizontal" title="Use Horizontal Scrolling" tabindex="64" data-l10n-id="scroll_horizontal">
206206
<span data-l10n-id="scroll_horizontal_label">Horizontal Scrolling</span>
207207
</button>
208-
<button id="scrollWrapped" class="secondaryToolbarButton scrollModeButtons scrollWrapped" title="Use Wrapped Scrolling" tabindex="65" data-l10n-id="scroll_wrapped">
208+
<button id="scrollWrapped" class="secondaryToolbarButton scrollWrapped" title="Use Wrapped Scrolling" tabindex="65" data-l10n-id="scroll_wrapped">
209209
<span data-l10n-id="scroll_wrapped_label">Wrapped Scrolling</span>
210210
</button>
211211

212-
<div class="horizontalToolbarSeparator scrollModeButtons"></div>
212+
<div class="horizontalToolbarSeparator"></div>
213213

214-
<button id="spreadNone" class="secondaryToolbarButton spreadModeButtons spreadNone toggled" title="Do not join page spreads" tabindex="66" data-l10n-id="spread_none">
214+
<button id="spreadNone" class="secondaryToolbarButton spreadNone toggled" title="Do not join page spreads" tabindex="66" data-l10n-id="spread_none">
215215
<span data-l10n-id="spread_none_label">No Spreads</span>
216216
</button>
217-
<button id="spreadOdd" class="secondaryToolbarButton spreadModeButtons spreadOdd" title="Join page spreads starting with odd-numbered pages" tabindex="67" data-l10n-id="spread_odd">
217+
<button id="spreadOdd" class="secondaryToolbarButton spreadOdd" title="Join page spreads starting with odd-numbered pages" tabindex="67" data-l10n-id="spread_odd">
218218
<span data-l10n-id="spread_odd_label">Odd Spreads</span>
219219
</button>
220-
<button id="spreadEven" class="secondaryToolbarButton spreadModeButtons spreadEven" title="Join page spreads starting with even-numbered pages" tabindex="68" data-l10n-id="spread_even">
220+
<button id="spreadEven" class="secondaryToolbarButton spreadEven" title="Join page spreads starting with even-numbered pages" tabindex="68" data-l10n-id="spread_even">
221221
<span data-l10n-id="spread_even_label">Even Spreads</span>
222222
</button>
223223

224-
<div class="horizontalToolbarSeparator spreadModeButtons"></div>
224+
<div class="horizontalToolbarSeparator"></div>
225225

226226
<button id="documentProperties" class="secondaryToolbarButton documentProperties" title="Document Properties…" tabindex="69" data-l10n-id="document_properties">
227227
<span data-l10n-id="document_properties_label">Document Properties…</span>

0 commit comments

Comments
 (0)