Move additional code/methods into BaseViewer and have the extending classes override/extend methods as necessary

This attempts to provide more "default" methods in the base class, in order to reduce unnecessary duplication and to improve self-documentation of the `BaseViewer` class slightly.
The following changes are made (in no particular order):
 - Have `BaseViewer` implement the `_scrollIntoView` method, and *extend* it as necessary in `PDFViewer`/`PDFSinglePageViewer`.
 - Simply inline the `BaseViewer._resizeBuffer` method, in `BaseViewer.update`, since there's only one call-site at this point.
 - Provide a default implementation of `_isScrollModeHorizontal` in `BaseViewer`, and have `PDFSinglePageViewer` override it.
 - Provide a default implementation of `_getVisiblePages`, and have `PDFViewer` extend it and `PDFSinglePageViewer` override it.
This commit is contained in:
Jonas Jenwald 2019-01-23 09:11:06 +01:00
parent 9128335c4d
commit 06cda4c2e7
3 changed files with 24 additions and 38 deletions

View file

@ -13,8 +13,7 @@
* limitations under the License.
*/
import { BaseViewer, ScrollMode } from './base_viewer';
import { getVisibleElements, scrollIntoView } from './ui_utils';
import { BaseViewer } from './base_viewer';
import { shadow } from 'pdfjs-lib';
class PDFViewer extends BaseViewer {
@ -22,27 +21,26 @@ class PDFViewer extends BaseViewer {
return shadow(this, '_setDocumentViewerElement', this.viewer);
}
_scrollIntoView({ pageDiv, pageSpot = null, }) {
_scrollIntoView({ pageDiv, pageSpot = null, pageNumber = null, }) {
if (!pageSpot && !this.isInPresentationMode) {
const left = pageDiv.offsetLeft + pageDiv.clientLeft;
const right = left + pageDiv.clientWidth;
const { scrollLeft, clientWidth, } = this.container;
if (this._scrollMode === ScrollMode.HORIZONTAL ||
if (this._isScrollModeHorizontal ||
left < scrollLeft || right > scrollLeft + clientWidth) {
pageSpot = { left: 0, top: 0, };
}
}
scrollIntoView(pageDiv, pageSpot);
super._scrollIntoView({ pageDiv, pageSpot, pageNumber, });
}
_getVisiblePages() {
if (!this.isInPresentationMode) {
return getVisibleElements(this.container, this._pages, true,
this._scrollMode === ScrollMode.HORIZONTAL);
if (this.isInPresentationMode) {
// The algorithm in `getVisibleElements` doesn't work in all browsers and
// configurations (e.g. Chrome) when Presentation Mode is active.
return this._getCurrentVisiblePage();
}
// The algorithm in `getVisibleElements` doesn't work in all browsers and
// configurations when presentation mode is active.
return this._getCurrentVisiblePage();
return super._getVisiblePages();
}
_updateHelper(visiblePages) {
@ -66,13 +64,6 @@ class PDFViewer extends BaseViewer {
}
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 {