pdf.js/web/pdf_single_page_viewer.js
Jonas Jenwald a2b592f4a2 Add previous/next-page functionality that takes scroll/spread-modes into account (issue 11946)
- For wrapped scrolling, we unfortunately need to do a fair bit of parsing of the *current* page layout. Compared to e.g. the spread-modes, where we can easily tell how the pages are laid out, with wrapped scrolling we cannot tell without actually checking. In particular documents with varying page sizes require some care, since we need to check all pages on the "row" of the current page are visible and that there aren't any "holes" present. Otherwise, in the general case, there's a risk that we'd skip over pages if we'd simply always advance to the previous/next "row" in wrapped scrolling.

 - For horizontal scrolling, this patch simply maintains the current behaviour of advancing *one* page at a time. The reason for this is to prevent inconsistent behaviour for the next and previous cases, since those cannot be handled identically. For the next-case, it'd obviously be simple to advance to the first not completely visible page. However for the previous-case, we'd only be able to go back *one* page since it's not possible to (easily) determine the page layout of non-visible pages (documents with varying page sizes being a particular issue).

 - For vertical scrolling, this patch maintains the current behaviour by default. When spread-modes are being used, we'll now attempt to advance to the next *spread*, rather than just the next page, whenever possible. To prevent skipping over a page, this two-page advance will only apply when both pages of the current spread are visible (to avoid breaking documents with varying page sizes) and when the second page in the current spread is fully visible *horizontally* (to handle larger zoom values).

In order to reduce the performance impact of these changes, note that the previous/next-functionality will only call `getVisibleElements` for the scroll/spread-modes where that's necessary and that "normal" vertical scrolling is thus unaffected by these changes.

To support these changes, the `getVisibleElements` helper function will now also include the `widthPercent` in addition to the existing `percent` property.
The `PDFViewer._updateHelper` method is changed slightly w.r.t. updating the `currentPageNumber` for the non-vertical/spread modes, i.e. won't affect "normal" vertical scrolling, since that helped simplify the overall calculation of the page advance.

Finally, these new `BaseViewer` methods also allow (some) simplification of previous/next-page functionality in various viewer components.

*Please note:* There's one thing that this patch does not attempt to change, namely disabling of the previous/next toolbarButtons respectively the firstPage/lastPage secondaryToolbarButtons. The reason for this is that doing so would add quite a bit of complexity in general, and if for some reason `BaseViewer._getPageAdvance` would get things wrong we could end up incorrectly disabling the buttons. Hence it seemed overall safer to *not* touch this, and accept that the buttons won't be `disabled` despite in some edge-cases no further scrolling being possible.
2021-01-22 21:38:15 +01:00

131 lines
4.1 KiB
JavaScript

/* Copyright 2017 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BaseViewer } from "./base_viewer.js";
import { shadow } from "pdfjs-lib";
class PDFSinglePageViewer extends BaseViewer {
constructor(options) {
super(options);
this.eventBus._on("pagesinit", evt => {
// Since the pages are placed in a `DocumentFragment`, make sure that
// the current page becomes visible upon loading of the document.
this._ensurePageViewVisible();
});
}
get _viewerElement() {
// Since we only want to display *one* page at a time when using the
// `PDFSinglePageViewer`, we cannot append them to the `viewer` DOM element.
// Instead, they are placed in a `DocumentFragment`, and only the current
// page is displayed in the viewer (refer to `this._ensurePageViewVisible`).
return shadow(this, "_viewerElement", this._shadowViewer);
}
get _pageWidthScaleFactor() {
return 1;
}
_resetView() {
super._resetView();
this._previousPageNumber = 1;
this._shadowViewer = document.createDocumentFragment();
this._updateScrollDown = null;
}
_ensurePageViewVisible() {
const pageView = this._pages[this._currentPageNumber - 1];
const previousPageView = this._pages[this._previousPageNumber - 1];
const viewerNodes = this.viewer.childNodes;
switch (viewerNodes.length) {
case 0: // Should *only* occur on initial loading.
this.viewer.appendChild(pageView.div);
break;
case 1: // The normal page-switching case.
if (viewerNodes[0] !== previousPageView.div) {
throw new Error(
"_ensurePageViewVisible: Unexpected previously visible page."
);
}
if (pageView === previousPageView) {
break; // The correct page is already visible.
}
// Switch visible pages, and reset the viewerContainer scroll position.
this._shadowViewer.appendChild(previousPageView.div);
this.viewer.appendChild(pageView.div);
this.container.scrollTop = 0;
break;
default:
throw new Error(
"_ensurePageViewVisible: Only one page should be visible at a time."
);
}
this._previousPageNumber = this._currentPageNumber;
}
_scrollUpdate() {
if (this._updateScrollDown) {
this._updateScrollDown();
}
super._scrollUpdate();
}
_scrollIntoView({ pageDiv, pageSpot = null, pageNumber = null }) {
if (pageNumber) {
// Ensure that `this._currentPageNumber` is correct.
this._setCurrentPageNumber(pageNumber);
}
const scrolledDown = this._currentPageNumber >= this._previousPageNumber;
this._ensurePageViewVisible();
// Ensure that rendering always occurs, to avoid showing a blank page,
// even if the current position doesn't change when the page is scrolled.
this.update();
super._scrollIntoView({ pageDiv, pageSpot, pageNumber });
// Since scrolling is tracked using `requestAnimationFrame`, update the
// scroll direction during the next `this._scrollUpdate` invocation.
this._updateScrollDown = () => {
this.scroll.down = scrolledDown;
this._updateScrollDown = null;
};
}
_getVisiblePages() {
return this._getCurrentVisiblePage();
}
_updateHelper(visiblePages) {}
get _isScrollModeHorizontal() {
// The Scroll/Spread modes are never used in `PDFSinglePageViewer`.
return shadow(this, "_isScrollModeHorizontal", false);
}
_updateScrollMode() {}
_updateSpreadMode() {}
_getPageAdvance() {
return 1;
}
}
export { PDFSinglePageViewer };