Skip to content

Commit

Permalink
Merge pull request #16171 from Snuffleupagus/PDFViewer-cleanup-scale-…
Browse files Browse the repository at this point in the history
…methods

Clean-up the `PDFViewer.{increaseScale, decreaseScale}` methods
  • Loading branch information
timvandermeij authored Mar 19, 2023
2 parents 9db4509 + 9060757 commit 27fe7e9
Showing 1 changed file with 21 additions and 46 deletions.
67 changes: 21 additions & 46 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2003,83 +2003,58 @@ class PDFViewer {
}

/**
* Increase the current zoom level one, or more, times.
* @param {Object|null} [options]
* @typedef {Object} ChangeScaleOptions
* @property {number} [drawingDelay]
* @property {number} [scaleFactor]
* @property {number} [steps]
*/
increaseScale(options = null) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
typeof options === "number"
) {
console.error(
"The `increaseScale` method-signature was updated, please use an object instead."
);
options = { steps: options };
}

/**
* Increase the current zoom level one, or more, times.
* @param {ChangeScaleOptions} [options]
*/
increaseScale({ drawingDelay, scaleFactor, steps } = {}) {
if (!this.pdfDocument) {
return;
}

options ||= Object.create(null);

let newScale = this._currentScale;
if (options.scaleFactor > 1) {
if (scaleFactor > 1) {
newScale = Math.min(
MAX_SCALE,
Math.round(newScale * options.scaleFactor * 100) / 100
Math.round(newScale * scaleFactor * 100) / 100
);
} else {
let steps = options.steps ?? 1;
steps ??= 1;
do {
newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
newScale = Math.ceil(newScale * 10) / 10;
newScale = Math.min(MAX_SCALE, newScale);
newScale = Math.min(MAX_SCALE, Math.ceil(newScale * 10) / 10);
} while (--steps > 0 && newScale < MAX_SCALE);
}

options.noScroll = false;
this._setScale(newScale, options);
this._setScale(newScale, { noScroll: false, drawingDelay });
}

/**
* Decrease the current zoom level one, or more, times.
* @param {Object|null} [options]
* @param {ChangeScaleOptions} [options]
*/
decreaseScale(options = null) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
typeof options === "number"
) {
console.error(
"The `decreaseScale` method-signature was updated, please use an object instead."
);
options = { steps: options };
}

decreaseScale({ drawingDelay, scaleFactor, steps } = {}) {
if (!this.pdfDocument) {
return;
}

options ||= Object.create(null);

let newScale = this._currentScale;
if (options.scaleFactor > 0 && options.scaleFactor < 1) {
if (scaleFactor > 0 && scaleFactor < 1) {
newScale = Math.max(
MIN_SCALE,
Math.round(newScale * options.scaleFactor * 100) / 100
Math.round(newScale * scaleFactor * 100) / 100
);
} else {
let steps = options.steps ?? 1;
steps ??= 1;
do {
newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
newScale = Math.floor(newScale * 10) / 10;
newScale = Math.max(MIN_SCALE, newScale);
newScale = Math.max(MIN_SCALE, Math.floor(newScale * 10) / 10);
} while (--steps > 0 && newScale > MIN_SCALE);
}

options.noScroll = false;
this._setScale(newScale, options);
this._setScale(newScale, { noScroll: false, drawingDelay });
}

#updateContainerHeightCss(height = this.container.clientHeight) {
Expand Down

0 comments on commit 27fe7e9

Please sign in to comment.