mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-19 22:58:07 +02:00
Unify increaseScale
/decreaseScale
logic as updateScale
`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.
This commit is contained in:
parent
95a7de9f98
commit
161c7045f6
2 changed files with 38 additions and 69 deletions
51
web/app.js
51
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(
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue