Skip to content

Commit

Permalink
Unify increaseScale/decreaseScale logic as updateScale
Browse files Browse the repository at this point in the history
`updateScale` receives a `drawingDelay`, a `scaleFactor` and/or a number of `steps`.
If `scaleFactor` is a positive number different from `1` the current scale is multiplied by
that number. Otherwise, if `steps` if a positive integer the current scale is multiplied by
`DEFAULT_SCALE_DELTA` `steps` times. Finally, if `steps` is a negative integer, the
current scale is divided by `DEFAULT_SCALE_DELTA` `abs(steps)` times.
  • Loading branch information
nicolo-ribaudo committed May 28, 2024
1 parent 95a7de9 commit 161c704
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 69 deletions.
51 changes: 12 additions & 39 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,26 +743,23 @@ const PDFViewerApplication = {
return this._initializedCapability.promise;
},

zoomIn(steps, scaleFactor) {
updateZoom(steps, scaleFactor) {
if (this.pdfViewer.isInPresentationMode) {
return;
}
this.pdfViewer.increaseScale({
this.pdfViewer.updateScale({
drawingDelay: AppOptions.get("defaultZoomDelay"),
steps,
scaleFactor,
});
},

zoomOut(steps, scaleFactor) {
if (this.pdfViewer.isInPresentationMode) {
return;
}
this.pdfViewer.decreaseScale({
drawingDelay: AppOptions.get("defaultZoomDelay"),
steps,
scaleFactor,
});
zoomIn() {
this.updateZoom(1);
},

zoomOut() {
this.updateZoom(-1);
},

zoomReset() {
Expand Down Expand Up @@ -2635,13 +2632,7 @@ function webViewerWheel(evt) {
scaleFactor,
"_wheelUnusedFactor"
);
if (scaleFactor < 1) {
PDFViewerApplication.zoomOut(null, scaleFactor);
} else if (scaleFactor > 1) {
PDFViewerApplication.zoomIn(null, scaleFactor);
} else {
return;
}
PDFViewerApplication.updateZoom(null, scaleFactor);
} else {
const delta = normalizeWheelEventDirection(evt);

Expand Down Expand Up @@ -2673,13 +2664,7 @@ function webViewerWheel(evt) {
);
}

if (ticks < 0) {
PDFViewerApplication.zoomOut(-ticks);
} else if (ticks > 0) {
PDFViewerApplication.zoomIn(ticks);
} else {
return;
}
PDFViewerApplication.updateZoom(ticks);
}

// After scaling the page via zoomIn/zoomOut, the position of the upper-
Expand Down Expand Up @@ -2794,26 +2779,14 @@ function webViewerTouchMove(evt) {
distance / pDistance,
"_touchUnusedFactor"
);
if (newScaleFactor < 1) {
PDFViewerApplication.zoomOut(null, newScaleFactor);
} else if (newScaleFactor > 1) {
PDFViewerApplication.zoomIn(null, newScaleFactor);
} else {
return;
}
PDFViewerApplication.updateZoom(null, newScaleFactor);
} else {
const PIXELS_PER_LINE_SCALE = 30;
const ticks = PDFViewerApplication._accumulateTicks(
(distance - pDistance) / PIXELS_PER_LINE_SCALE,
"_touchUnusedTicks"
);
if (ticks < 0) {
PDFViewerApplication.zoomOut(-ticks);
} else if (ticks > 0) {
PDFViewerApplication.zoomIn(ticks);
} else {
return;
}
PDFViewerApplication.updateZoom(ticks);
}

PDFViewerApplication._centerAtPos(
Expand Down
56 changes: 26 additions & 30 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2125,51 +2125,47 @@ class PDFViewer {
*/

/**
* Increase the current zoom level one, or more, times.
* Changes the current zoom level by the specified amount.
* @param {ChangeScaleOptions} [options]
*/
increaseScale({ drawingDelay, scaleFactor, steps } = {}) {
updateScale({ drawingDelay, scaleFactor = null, steps = null }) {
if (steps === null && scaleFactor === null) {
throw new Error(
"Invalid updateScale options: either `steps` or `scaleFactor` must be provided."
);
}
if (!this.pdfDocument) {
return;
}
let newScale = this._currentScale;
if (scaleFactor > 1) {
if (scaleFactor > 0 && scaleFactor !== 1) {
newScale = Math.round(newScale * scaleFactor * 100) / 100;
} else {
steps ??= 1;
} else if (steps) {
const delta = steps > 0 ? DEFAULT_SCALE_DELTA : 1 / DEFAULT_SCALE_DELTA;
const round = steps > 0 ? Math.ceil : Math.floor;
steps = Math.abs(steps);
do {
newScale =
Math.ceil((newScale * DEFAULT_SCALE_DELTA).toFixed(2) * 10) / 10;
} while (--steps > 0 && newScale < MAX_SCALE);
newScale = round((newScale * delta).toFixed(2) * 10) / 10;
} while (--steps > 0);
}
this.#setScale(Math.min(MAX_SCALE, newScale), {
noScroll: false,
drawingDelay,
});
newScale = Math.max(MIN_SCALE, Math.min(MAX_SCALE, newScale));
this.#setScale(newScale, { noScroll: false, drawingDelay });
}

/**
* Increase the current zoom level one, or more, times.
* @param {ChangeScaleOptions} [options]
*/
increaseScale(options = {}) {
this.updateScale({ ...options, steps: options.steps ?? 1 });
}

/**
* Decrease the current zoom level one, or more, times.
* @param {ChangeScaleOptions} [options]
*/
decreaseScale({ drawingDelay, scaleFactor, steps } = {}) {
if (!this.pdfDocument) {
return;
}
let newScale = this._currentScale;
if (scaleFactor > 0 && scaleFactor < 1) {
newScale = Math.round(newScale * scaleFactor * 100) / 100;
} else {
steps ??= 1;
do {
newScale =
Math.floor((newScale / DEFAULT_SCALE_DELTA).toFixed(2) * 10) / 10;
} while (--steps > 0 && newScale > MIN_SCALE);
}
this.#setScale(Math.max(MIN_SCALE, newScale), {
noScroll: false,
drawingDelay,
});
decreaseScale(options = {}) {
this.updateScale({ ...options, steps: -(options.steps ?? 1) });
}

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

0 comments on commit 161c704

Please sign in to comment.