mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-09 01:35:43 +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;
|
return this._initializedCapability.promise;
|
||||||
},
|
},
|
||||||
|
|
||||||
zoomIn(steps, scaleFactor) {
|
updateZoom(steps, scaleFactor) {
|
||||||
if (this.pdfViewer.isInPresentationMode) {
|
if (this.pdfViewer.isInPresentationMode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.pdfViewer.increaseScale({
|
this.pdfViewer.updateScale({
|
||||||
drawingDelay: AppOptions.get("defaultZoomDelay"),
|
drawingDelay: AppOptions.get("defaultZoomDelay"),
|
||||||
steps,
|
steps,
|
||||||
scaleFactor,
|
scaleFactor,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
zoomOut(steps, scaleFactor) {
|
zoomIn() {
|
||||||
if (this.pdfViewer.isInPresentationMode) {
|
this.updateZoom(1);
|
||||||
return;
|
},
|
||||||
}
|
|
||||||
this.pdfViewer.decreaseScale({
|
zoomOut() {
|
||||||
drawingDelay: AppOptions.get("defaultZoomDelay"),
|
this.updateZoom(-1);
|
||||||
steps,
|
|
||||||
scaleFactor,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
zoomReset() {
|
zoomReset() {
|
||||||
|
@ -2635,13 +2632,7 @@ function webViewerWheel(evt) {
|
||||||
scaleFactor,
|
scaleFactor,
|
||||||
"_wheelUnusedFactor"
|
"_wheelUnusedFactor"
|
||||||
);
|
);
|
||||||
if (scaleFactor < 1) {
|
PDFViewerApplication.updateZoom(null, scaleFactor);
|
||||||
PDFViewerApplication.zoomOut(null, scaleFactor);
|
|
||||||
} else if (scaleFactor > 1) {
|
|
||||||
PDFViewerApplication.zoomIn(null, scaleFactor);
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const delta = normalizeWheelEventDirection(evt);
|
const delta = normalizeWheelEventDirection(evt);
|
||||||
|
|
||||||
|
@ -2673,13 +2664,7 @@ function webViewerWheel(evt) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ticks < 0) {
|
PDFViewerApplication.updateZoom(ticks);
|
||||||
PDFViewerApplication.zoomOut(-ticks);
|
|
||||||
} else if (ticks > 0) {
|
|
||||||
PDFViewerApplication.zoomIn(ticks);
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// After scaling the page via zoomIn/zoomOut, the position of the upper-
|
// After scaling the page via zoomIn/zoomOut, the position of the upper-
|
||||||
|
@ -2794,26 +2779,14 @@ function webViewerTouchMove(evt) {
|
||||||
distance / pDistance,
|
distance / pDistance,
|
||||||
"_touchUnusedFactor"
|
"_touchUnusedFactor"
|
||||||
);
|
);
|
||||||
if (newScaleFactor < 1) {
|
PDFViewerApplication.updateZoom(null, newScaleFactor);
|
||||||
PDFViewerApplication.zoomOut(null, newScaleFactor);
|
|
||||||
} else if (newScaleFactor > 1) {
|
|
||||||
PDFViewerApplication.zoomIn(null, newScaleFactor);
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const PIXELS_PER_LINE_SCALE = 30;
|
const PIXELS_PER_LINE_SCALE = 30;
|
||||||
const ticks = PDFViewerApplication._accumulateTicks(
|
const ticks = PDFViewerApplication._accumulateTicks(
|
||||||
(distance - pDistance) / PIXELS_PER_LINE_SCALE,
|
(distance - pDistance) / PIXELS_PER_LINE_SCALE,
|
||||||
"_touchUnusedTicks"
|
"_touchUnusedTicks"
|
||||||
);
|
);
|
||||||
if (ticks < 0) {
|
PDFViewerApplication.updateZoom(ticks);
|
||||||
PDFViewerApplication.zoomOut(-ticks);
|
|
||||||
} else if (ticks > 0) {
|
|
||||||
PDFViewerApplication.zoomIn(ticks);
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PDFViewerApplication._centerAtPos(
|
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]
|
* @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) {
|
if (!this.pdfDocument) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let newScale = this._currentScale;
|
let newScale = this._currentScale;
|
||||||
if (scaleFactor > 1) {
|
if (scaleFactor > 0 && scaleFactor !== 1) {
|
||||||
newScale = Math.round(newScale * scaleFactor * 100) / 100;
|
newScale = Math.round(newScale * scaleFactor * 100) / 100;
|
||||||
} else {
|
} else if (steps) {
|
||||||
steps ??= 1;
|
const delta = steps > 0 ? DEFAULT_SCALE_DELTA : 1 / DEFAULT_SCALE_DELTA;
|
||||||
|
const round = steps > 0 ? Math.ceil : Math.floor;
|
||||||
|
steps = Math.abs(steps);
|
||||||
do {
|
do {
|
||||||
newScale =
|
newScale = round((newScale * delta).toFixed(2) * 10) / 10;
|
||||||
Math.ceil((newScale * DEFAULT_SCALE_DELTA).toFixed(2) * 10) / 10;
|
} while (--steps > 0);
|
||||||
} while (--steps > 0 && newScale < MAX_SCALE);
|
|
||||||
}
|
}
|
||||||
this.#setScale(Math.min(MAX_SCALE, newScale), {
|
newScale = Math.max(MIN_SCALE, Math.min(MAX_SCALE, newScale));
|
||||||
noScroll: false,
|
this.#setScale(newScale, { noScroll: false, drawingDelay });
|
||||||
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.
|
* Decrease the current zoom level one, or more, times.
|
||||||
* @param {ChangeScaleOptions} [options]
|
* @param {ChangeScaleOptions} [options]
|
||||||
*/
|
*/
|
||||||
decreaseScale({ drawingDelay, scaleFactor, steps } = {}) {
|
decreaseScale(options = {}) {
|
||||||
if (!this.pdfDocument) {
|
this.updateScale({ ...options, steps: -(options.steps ?? 1) });
|
||||||
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,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#updateContainerHeightCss(height = this.container.clientHeight) {
|
#updateContainerHeightCss(height = this.container.clientHeight) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue