Merge pull request #10489 from Snuffleupagus/single-page-scrollIntoView

Move more code/methods into `BaseViewer`, and simplify the `PDFSinglePageViewer._scrollIntoView` method slightly
This commit is contained in:
Tim van der Meij 2019-01-25 23:09:09 +01:00 committed by GitHub
commit 40863eb275
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 52 deletions

View File

@ -15,9 +15,10 @@
import { import {
CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, getGlobalEventBus, CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, getGlobalEventBus,
isPortraitOrientation, isValidRotation, MAX_AUTO_SCALE, moveToEndOfArray, getVisibleElements, isPortraitOrientation, isValidRotation, MAX_AUTO_SCALE,
NullL10n, PresentationModeState, RendererType, SCROLLBAR_PADDING, moveToEndOfArray, NullL10n, PresentationModeState, RendererType,
TextLayerMode, UNKNOWN_SCALE, VERTICAL_PADDING, watchScroll SCROLLBAR_PADDING, scrollIntoView, TextLayerMode, UNKNOWN_SCALE,
VERTICAL_PADDING, watchScroll
} from './ui_utils'; } from './ui_utils';
import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue'; import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue';
import { AnnotationLayerBuilder } from './annotation_layer_builder'; import { AnnotationLayerBuilder } from './annotation_layer_builder';
@ -360,6 +361,7 @@ class BaseViewer {
} }
get _setDocumentViewerElement() { get _setDocumentViewerElement() {
// In most viewers, e.g. `PDFViewer`, this should return `this.viewer`.
throw new Error('Not implemented: _setDocumentViewerElement'); throw new Error('Not implemented: _setDocumentViewerElement');
} }
@ -545,7 +547,7 @@ class BaseViewer {
} }
_scrollIntoView({ pageDiv, pageSpot = null, pageNumber = null, }) { _scrollIntoView({ pageDiv, pageSpot = null, pageNumber = null, }) {
throw new Error('Not implemented: _scrollIntoView'); scrollIntoView(pageDiv, pageSpot);
} }
_setScaleUpdatePages(newScale, newValue, noScroll = false, preset = false) { _setScaleUpdatePages(newScale, newValue, noScroll = false, preset = false) {
@ -784,17 +786,6 @@ class BaseViewer {
}); });
} }
/**
* visiblePages is optional; if present, it should be an array of pages and in
* practice its length is going to be numVisiblePages, but this is not
* required. The new size of the buffer depends only on numVisiblePages.
*/
_resizeBuffer(numVisiblePages, visiblePages) {
let suggestedCacheSize = Math.max(DEFAULT_CACHE_SIZE,
2 * numVisiblePages + 1);
this._buffer.resize(suggestedCacheSize, visiblePages);
}
_updateLocation(firstPage) { _updateLocation(firstPage) {
let currentScale = this._currentScale; let currentScale = this._currentScale;
let currentScaleValue = this._currentScaleValue; let currentScaleValue = this._currentScaleValue;
@ -835,7 +826,8 @@ class BaseViewer {
if (numVisiblePages === 0) { if (numVisiblePages === 0) {
return; return;
} }
this._resizeBuffer(numVisiblePages, visiblePages); const newCacheSize = Math.max(DEFAULT_CACHE_SIZE, 2 * numVisiblePages + 1);
this._buffer.resize(newCacheSize, visiblePages);
this.renderingQueue.renderHighestPriority(visible); this.renderingQueue.renderHighestPriority(visible);
@ -857,7 +849,10 @@ class BaseViewer {
} }
get _isScrollModeHorizontal() { get _isScrollModeHorizontal() {
throw new Error('Not implemented: _isScrollModeHorizontal'); // Used to ensure that pre-rendering of the next/previous page works
// correctly, since Scroll/Spread modes are ignored in Presentation Mode.
return (this.isInPresentationMode ?
false : this._scrollMode === ScrollMode.HORIZONTAL);
} }
get isInPresentationMode() { get isInPresentationMode() {
@ -903,7 +898,8 @@ class BaseViewer {
} }
_getVisiblePages() { _getVisiblePages() {
throw new Error('Not implemented: _getVisiblePages'); return getVisibleElements(this.container, this._pages, true,
this._isScrollModeHorizontal);
} }
/** /**

View File

@ -14,7 +14,6 @@
*/ */
import { BaseViewer } from './base_viewer'; import { BaseViewer } from './base_viewer';
import { scrollIntoView } from './ui_utils';
import { shadow } from 'pdfjs-lib'; import { shadow } from 'pdfjs-lib';
class PDFSinglePageViewer extends BaseViewer { class PDFSinglePageViewer extends BaseViewer {
@ -40,6 +39,7 @@ class PDFSinglePageViewer extends BaseViewer {
super._resetView(); super._resetView();
this._previousPageNumber = 1; this._previousPageNumber = 1;
this._shadowViewer = document.createDocumentFragment(); this._shadowViewer = document.createDocumentFragment();
this._updateScrollDown = null;
} }
_ensurePageViewVisible() { _ensurePageViewVisible() {
@ -83,28 +83,21 @@ class PDFSinglePageViewer extends BaseViewer {
if (pageNumber) { // Ensure that `this._currentPageNumber` is correct. if (pageNumber) { // Ensure that `this._currentPageNumber` is correct.
this._setCurrentPageNumber(pageNumber); this._setCurrentPageNumber(pageNumber);
} }
let scrolledDown = this._currentPageNumber >= this._previousPageNumber; const scrolledDown = this._currentPageNumber >= this._previousPageNumber;
let previousLocation = this._location;
this._ensurePageViewVisible();
scrollIntoView(pageDiv, pageSpot); 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 // Since scrolling is tracked using `requestAnimationFrame`, update the
// scroll direction during the next `this._scrollUpdate` invocation. // scroll direction during the next `this._scrollUpdate` invocation.
this._updateScrollDown = () => { this._updateScrollDown = () => {
this.scroll.down = scrolledDown; this.scroll.down = scrolledDown;
delete this._updateScrollDown; this._updateScrollDown = null;
}; };
// If the scroll position doesn't change as a result of the `scrollIntoView`
// call, ensure that rendering always occurs to avoid showing a blank page.
setTimeout(() => {
if (this._location === previousLocation) {
if (this._updateScrollDown) {
this._updateScrollDown();
}
this.update();
}
}, 0);
} }
_getVisiblePages() { _getVisiblePages() {

View File

@ -13,8 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { BaseViewer, ScrollMode } from './base_viewer'; import { BaseViewer } from './base_viewer';
import { getVisibleElements, scrollIntoView } from './ui_utils';
import { shadow } from 'pdfjs-lib'; import { shadow } from 'pdfjs-lib';
class PDFViewer extends BaseViewer { class PDFViewer extends BaseViewer {
@ -22,27 +21,26 @@ class PDFViewer extends BaseViewer {
return shadow(this, '_setDocumentViewerElement', this.viewer); return shadow(this, '_setDocumentViewerElement', this.viewer);
} }
_scrollIntoView({ pageDiv, pageSpot = null, }) { _scrollIntoView({ pageDiv, pageSpot = null, pageNumber = null, }) {
if (!pageSpot && !this.isInPresentationMode) { if (!pageSpot && !this.isInPresentationMode) {
const left = pageDiv.offsetLeft + pageDiv.clientLeft; const left = pageDiv.offsetLeft + pageDiv.clientLeft;
const right = left + pageDiv.clientWidth; const right = left + pageDiv.clientWidth;
const { scrollLeft, clientWidth, } = this.container; const { scrollLeft, clientWidth, } = this.container;
if (this._scrollMode === ScrollMode.HORIZONTAL || if (this._isScrollModeHorizontal ||
left < scrollLeft || right > scrollLeft + clientWidth) { left < scrollLeft || right > scrollLeft + clientWidth) {
pageSpot = { left: 0, top: 0, }; pageSpot = { left: 0, top: 0, };
} }
} }
scrollIntoView(pageDiv, pageSpot); super._scrollIntoView({ pageDiv, pageSpot, pageNumber, });
} }
_getVisiblePages() { _getVisiblePages() {
if (!this.isInPresentationMode) { if (this.isInPresentationMode) {
return getVisibleElements(this.container, this._pages, true, // The algorithm in `getVisibleElements` doesn't work in all browsers and
this._scrollMode === ScrollMode.HORIZONTAL); // configurations (e.g. Chrome) when Presentation Mode is active.
return this._getCurrentVisiblePage();
} }
// The algorithm in `getVisibleElements` doesn't work in all browsers and return super._getVisiblePages();
// configurations when presentation mode is active.
return this._getCurrentVisiblePage();
} }
_updateHelper(visiblePages) { _updateHelper(visiblePages) {
@ -66,13 +64,6 @@ class PDFViewer extends BaseViewer {
} }
this._setCurrentPageNumber(currentId); this._setCurrentPageNumber(currentId);
} }
get _isScrollModeHorizontal() {
// Used to ensure that pre-rendering of the next/previous page works
// correctly, since Scroll/Spread modes are ignored in Presentation Mode.
return (this.isInPresentationMode ?
false : this._scrollMode === ScrollMode.HORIZONTAL);
}
} }
export { export {