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.)
This commit is contained in:
Jonas Jenwald 2018-06-29 15:07:42 +02:00
parent 9515f579c6
commit a7ac27e385
3 changed files with 63 additions and 39 deletions

View file

@ -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() {