diff --git a/web/app.js b/web/app.js index 9d10550845e91..945b331e8e4af 100644 --- a/web/app.js +++ b/web/app.js @@ -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() { @@ -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); @@ -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- @@ -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( diff --git a/web/pdf_viewer.js b/web/pdf_viewer.js index 08752f35fb843..d6c3ae8a407ec 100644 --- a/web/pdf_viewer.js +++ b/web/pdf_viewer.js @@ -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) {