From a7ac27e385baeeae70b7db62efc6cf5474622ac8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 29 Jun 2018 15:07:42 +0200 Subject: [PATCH] Replace `setScrollMode`/`setSpreadMode` methods with getters/setters Since all the other viewer methods use the getter/setter pattern, e.g. for setting page/scale/rotation, the way that the Scroll/Spread modes are set thus stands out. For consistency, this really ought to use the same pattern as the rest of the `BaseViewer`. (To avoid breaking third-party implementations, the old methods are kept around as aliases.) --- web/app.js | 8 +++--- web/base_viewer.js | 61 ++++++++++++++++++++++++++++++++++++++++++++-- web/pdf_viewer.js | 33 ------------------------- 3 files changed, 63 insertions(+), 39 deletions(-) diff --git a/web/app.js b/web/app.js index c924db3c5..fe6916cb1 100644 --- a/web/app.js +++ b/web/app.js @@ -1250,10 +1250,10 @@ let PDFViewerApplication = { }; let setViewerModes = (scroll, spread) => { if (Number.isInteger(scroll)) { - this.pdfViewer.setScrollMode(scroll); + this.pdfViewer.scrollMode = scroll; } if (Number.isInteger(spread)) { - this.pdfViewer.setSpreadMode(spread); + this.pdfViewer.spreadMode = spread; } }; @@ -2019,10 +2019,10 @@ function webViewerRotateCcw() { PDFViewerApplication.rotatePages(-90); } function webViewerSwitchScrollMode(evt) { - PDFViewerApplication.pdfViewer.setScrollMode(evt.mode); + PDFViewerApplication.pdfViewer.scrollMode = evt.mode; } function webViewerSwitchSpreadMode(evt) { - PDFViewerApplication.pdfViewer.setSpreadMode(evt.mode); + PDFViewerApplication.pdfViewer.spreadMode = evt.mode; } function webViewerDocumentProperties() { PDFViewerApplication.pdfDocumentProperties.open(); diff --git a/web/base_viewer.js b/web/base_viewer.js index 4bccae26e..d5d7b3d2b 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -1023,22 +1023,79 @@ class BaseViewer { }); } - setScrollMode(mode) { + /** + * @return {number} One of the values in {ScrollMode}. + */ + get scrollMode() { + return this._scrollMode; + } + + /** + * @param {number} mode - The direction in which the document pages should be + * laid out within the scrolling container. + * The constants from {ScrollMode} should be used. + */ + set scrollMode(mode) { + if (this._scrollMode === mode) { + return; // The Scroll mode didn't change. + } if (!Number.isInteger(mode) || !Object.values(ScrollMode).includes(mode)) { throw new Error(`Invalid scroll mode: ${mode}`); } this._scrollMode = mode; + this.eventBus.dispatch('scrollmodechanged', { source: this, mode, }); + + this._updateScrollModeClasses(); + + if (!this.pdfDocument) { + return; + } + const pageNumber = this._currentPageNumber; + // Non-numeric scale values can be sensitive to the scroll orientation. + // Call this before re-scrolling to the current page, to ensure that any + // changes in scale don't move the current page. + if (isNaN(this._currentScaleValue)) { + this._setScale(this._currentScaleValue, true); + } + this.scrollPageIntoView({ pageNumber, }); + this.update(); + } + + setScrollMode(mode) { + this.scrollMode = mode; } _updateScrollModeClasses() { // No-op in the base class. } - setSpreadMode(mode) { + /** + * @return {number} One of the values in {SpreadMode}. + */ + get spreadMode() { + return this._spreadMode; + } + + /** + * @param {number} mode - Group the pages in spreads, starting with odd- or + * even-number pages (unless `SpreadMode.NONE` is used). + * The constants from {SpreadMode} should be used. + */ + set spreadMode(mode) { + if (this._spreadMode === mode) { + return; // The Spread mode didn't change. + } if (!Number.isInteger(mode) || !Object.values(SpreadMode).includes(mode)) { throw new Error(`Invalid spread mode: ${mode}`); } this._spreadMode = mode; + this.eventBus.dispatch('spreadmodechanged', { source: this, mode, }); + + this._regroupSpreads(); + } + + setSpreadMode(mode) { + this.spreadMode = mode; } _regroupSpreads() { diff --git a/web/pdf_viewer.js b/web/pdf_viewer.js index 230895b20..b49fa16f5 100644 --- a/web/pdf_viewer.js +++ b/web/pdf_viewer.js @@ -94,29 +94,6 @@ class PDFViewer extends BaseViewer { false : this._scrollMode === ScrollMode.HORIZONTAL); } - setScrollMode(mode) { - if (mode === this._scrollMode) { - return; - } - super.setScrollMode(mode); - - this.eventBus.dispatch('scrollmodechanged', { mode, }); - this._updateScrollModeClasses(); - - if (!this.pdfDocument) { - return; - } - const pageNumber = this._currentPageNumber; - // Non-numeric scale modes can be sensitive to the scroll orientation. - // Call this before re-scrolling to the current page, to ensure that any - // changes in scale don't move the current page. - if (isNaN(this._currentScaleValue)) { - this._setScale(this._currentScaleValue, true); - } - this.scrollPageIntoView({ pageNumber, }); - this.update(); - } - _updateScrollModeClasses() { const scrollMode = this._scrollMode, viewer = this.viewer; @@ -132,16 +109,6 @@ class PDFViewer extends BaseViewer { } } - setSpreadMode(mode) { - if (mode === this._spreadMode) { - return; - } - super.setSpreadMode(mode); - - this.eventBus.dispatch('spreadmodechanged', { mode, }); - this._regroupSpreads(); - } - _regroupSpreads() { if (!this.pdfDocument) { return;