Move the zoomIn/zoomOut functionality into BaseViewer (PR 14038 follow-up)

Given the simplicity of this functionality, we can move it from the default viewer and into the `BaseViewer` class instead. This way, it's possible to support more scripting functionality in the standalone viewer components; please see PR 14038.

Please note that I purposely went with `increaseScale`/`decreaseScale`-method names, rather than using "zoom", to better match the existing `currentScale`/`currentScaleValue` getters/setters that's being used in the `BaseViewer` class.
This commit is contained in:
Jonas Jenwald 2021-09-19 11:54:57 +02:00
parent 83d3bb43f4
commit d9f9fa4f1c
4 changed files with 39 additions and 21 deletions

View File

@ -26,8 +26,6 @@ import {
isValidRotation, isValidRotation,
isValidScrollMode, isValidScrollMode,
isValidSpreadMode, isValidSpreadMode,
MAX_SCALE,
MIN_SCALE,
noContextMenuHandler, noContextMenuHandler,
normalizeWheelEventDirection, normalizeWheelEventDirection,
parseQueryString, parseQueryString,
@ -81,7 +79,6 @@ import { SecondaryToolbar } from "./secondary_toolbar.js";
import { Toolbar } from "./toolbar.js"; import { Toolbar } from "./toolbar.js";
import { ViewHistory } from "./view_history.js"; import { ViewHistory } from "./view_history.js";
const DEFAULT_SCALE_DELTA = 1.1;
const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000; // ms const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000; // ms
const FORCE_PAGES_LOADED_TIMEOUT = 10000; // ms const FORCE_PAGES_LOADED_TIMEOUT = 10000; // ms
const WHEEL_ZOOM_DISABLED_TIMEOUT = 1000; // ms const WHEEL_ZOOM_DISABLED_TIMEOUT = 1000; // ms
@ -637,30 +634,18 @@ const PDFViewerApplication = {
return this._initializedCapability.promise; return this._initializedCapability.promise;
}, },
zoomIn(ticks) { zoomIn(steps) {
if (this.pdfViewer.isInPresentationMode) { if (this.pdfViewer.isInPresentationMode) {
return; return;
} }
let newScale = this.pdfViewer.currentScale; this.pdfViewer.increaseScale(steps);
do {
newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
newScale = Math.ceil(newScale * 10) / 10;
newScale = Math.min(MAX_SCALE, newScale);
} while (--ticks > 0 && newScale < MAX_SCALE);
this.pdfViewer.currentScaleValue = newScale;
}, },
zoomOut(ticks) { zoomOut(steps) {
if (this.pdfViewer.isInPresentationMode) { if (this.pdfViewer.isInPresentationMode) {
return; return;
} }
let newScale = this.pdfViewer.currentScale; this.pdfViewer.decreaseScale(steps);
do {
newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
newScale = Math.floor(newScale * 10) / 10;
newScale = Math.max(MIN_SCALE, newScale);
} while (--ticks > 0 && newScale > MIN_SCALE);
this.pdfViewer.currentScaleValue = newScale;
}, },
zoomReset() { zoomReset() {

View File

@ -17,6 +17,7 @@ import { AnnotationMode, createPromiseCapability, version } from "pdfjs-lib";
import { import {
CSS_UNITS, CSS_UNITS,
DEFAULT_SCALE, DEFAULT_SCALE,
DEFAULT_SCALE_DELTA,
DEFAULT_SCALE_VALUE, DEFAULT_SCALE_VALUE,
getVisibleElements, getVisibleElements,
isPortraitOrientation, isPortraitOrientation,
@ -24,6 +25,8 @@ import {
isValidScrollMode, isValidScrollMode,
isValidSpreadMode, isValidSpreadMode,
MAX_AUTO_SCALE, MAX_AUTO_SCALE,
MAX_SCALE,
MIN_SCALE,
moveToEndOfArray, moveToEndOfArray,
PresentationModeState, PresentationModeState,
RendererType, RendererType,
@ -1693,6 +1696,34 @@ class BaseViewer {
this.currentPageNumber = Math.max(currentPageNumber - advance, 1); this.currentPageNumber = Math.max(currentPageNumber - advance, 1);
return true; return true;
} }
/**
* Increase the current zoom level one, or more, times.
* @param {number} [steps] - Defaults to zooming once.
*/
increaseScale(steps = 1) {
let newScale = this._currentScale;
do {
newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
newScale = Math.ceil(newScale * 10) / 10;
newScale = Math.min(MAX_SCALE, newScale);
} while (--steps > 0 && newScale < MAX_SCALE);
this.currentScaleValue = newScale;
}
/**
* Decrease the current zoom level one, or more, times.
* @param {number} [steps] - Defaults to zooming once.
*/
decreaseScale(steps = 1) {
let newScale = this._currentScale;
do {
newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
newScale = Math.floor(newScale * 10) / 10;
newScale = Math.max(MIN_SCALE, newScale);
} while (--steps > 0 && newScale > MIN_SCALE);
this.currentScaleValue = newScale;
}
} }
export { BaseViewer }; export { BaseViewer };

View File

@ -324,13 +324,13 @@ class PDFScriptingManager {
if (isInPresentationMode) { if (isInPresentationMode) {
return; return;
} }
this._eventBus.dispatch("zoomin", { source: this }); this._pdfViewer.increaseScale();
break; break;
case "ZoomViewOut": case "ZoomViewOut":
if (isInPresentationMode) { if (isInPresentationMode) {
return; return;
} }
this._eventBus.dispatch("zoomout", { source: this }); this._pdfViewer.decreaseScale();
break; break;
} }
return; return;

View File

@ -18,6 +18,7 @@ import { PixelsPerInch } from "pdfjs-lib";
const CSS_UNITS = PixelsPerInch.CSS / PixelsPerInch.PDF; const CSS_UNITS = PixelsPerInch.CSS / PixelsPerInch.PDF;
const DEFAULT_SCALE_VALUE = "auto"; const DEFAULT_SCALE_VALUE = "auto";
const DEFAULT_SCALE = 1.0; const DEFAULT_SCALE = 1.0;
const DEFAULT_SCALE_DELTA = 1.1;
const MIN_SCALE = 0.1; const MIN_SCALE = 0.1;
const MAX_SCALE = 10.0; const MAX_SCALE = 10.0;
const UNKNOWN_SCALE = 0; const UNKNOWN_SCALE = 0;
@ -1005,6 +1006,7 @@ export {
binarySearchFirstItem, binarySearchFirstItem,
CSS_UNITS, CSS_UNITS,
DEFAULT_SCALE, DEFAULT_SCALE,
DEFAULT_SCALE_DELTA,
DEFAULT_SCALE_VALUE, DEFAULT_SCALE_VALUE,
EventBus, EventBus,
getActiveOrFocusedElement, getActiveOrFocusedElement,