Merge pull request #16616 from Snuffleupagus/PDFCursorTools-tweaks

A couple of small tweaks of the `PDFCursorTools` class
This commit is contained in:
Tim van der Meij 2023-07-01 13:04:44 +02:00 committed by GitHub
commit 8a954823b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 54 deletions

View File

@ -21,18 +21,10 @@ class GrabToPan {
/** /**
* Construct a GrabToPan instance for a given HTML element. * Construct a GrabToPan instance for a given HTML element.
* @param {Element} options.element * @param {Element} options.element
* @param {function} [options.ignoreTarget] - See `ignoreTarget(node)`.
* @param {function(boolean)} [options.onActiveChanged] - Called when
* grab-to-pan is (de)activated. The first argument is a boolean that
* shows whether grab-to-pan is activated.
*/ */
constructor(options) { constructor({ element }) {
this.element = options.element; this.element = element;
this.document = options.element.ownerDocument; this.document = element.ownerDocument;
if (typeof options.ignoreTarget === "function") {
this.ignoreTarget = options.ignoreTarget;
}
this.onActiveChanged = options.onActiveChanged;
// Bind the contexts to ensure that `this` always points to // Bind the contexts to ensure that `this` always points to
// the GrabToPan instance. // the GrabToPan instance.
@ -57,8 +49,6 @@ class GrabToPan {
this.active = true; this.active = true;
this.element.addEventListener("mousedown", this._onMouseDown, true); this.element.addEventListener("mousedown", this._onMouseDown, true);
this.element.classList.add(CSS_CLASS_GRAB); this.element.classList.add(CSS_CLASS_GRAB);
this.onActiveChanged?.(true);
} }
} }
@ -71,8 +61,6 @@ class GrabToPan {
this.element.removeEventListener("mousedown", this._onMouseDown, true); this.element.removeEventListener("mousedown", this._onMouseDown, true);
this._endPan(); this._endPan();
this.element.classList.remove(CSS_CLASS_GRAB); this.element.classList.remove(CSS_CLASS_GRAB);
this.onActiveChanged?.(false);
} }
} }
@ -140,18 +128,12 @@ class GrabToPan {
} }
const xDiff = event.clientX - this.clientXStart; const xDiff = event.clientX - this.clientXStart;
const yDiff = event.clientY - this.clientYStart; const yDiff = event.clientY - this.clientYStart;
const scrollTop = this.scrollTopStart - yDiff; this.element.scrollTo({
const scrollLeft = this.scrollLeftStart - xDiff; top: this.scrollTopStart - yDiff,
if (this.element.scrollTo) { left: this.scrollLeftStart - xDiff,
this.element.scrollTo({ behavior: "instant",
top: scrollTop, });
left: scrollLeft,
behavior: "instant",
});
} else {
this.element.scrollTop = scrollTop;
this.element.scrollLeft = scrollLeft;
}
if (!this.overlay.parentNode) { if (!this.overlay.parentNode) {
document.body.append(this.overlay); document.body.append(this.overlay);
} }

View File

@ -13,8 +13,8 @@
* limitations under the License. * limitations under the License.
*/ */
import { AnnotationEditorType, shadow } from "pdfjs-lib";
import { CursorTool, PresentationModeState } from "./ui_utils.js"; import { CursorTool, PresentationModeState } from "./ui_utils.js";
import { AnnotationEditorType } from "pdfjs-lib";
import { GrabToPan } from "./grab_to_pan.js"; import { GrabToPan } from "./grab_to_pan.js";
/** /**
@ -27,6 +27,10 @@ import { GrabToPan } from "./grab_to_pan.js";
*/ */
class PDFCursorTools { class PDFCursorTools {
#active = CursorTool.SELECT;
#prevActive = null;
/** /**
* @param {PDFCursorToolsOptions} options * @param {PDFCursorToolsOptions} options
*/ */
@ -34,13 +38,6 @@ class PDFCursorTools {
this.container = container; this.container = container;
this.eventBus = eventBus; this.eventBus = eventBus;
this.active = CursorTool.SELECT;
this.previouslyActive = null;
this.handTool = new GrabToPan({
element: this.container,
});
this.#addEventListeners(); this.#addEventListeners();
// Defer the initial `switchTool` call, to give other viewer components // Defer the initial `switchTool` call, to give other viewer components
@ -54,7 +51,7 @@ class PDFCursorTools {
* @type {number} One of the values in {CursorTool}. * @type {number} One of the values in {CursorTool}.
*/ */
get activeTool() { get activeTool() {
return this.active; return this.#active;
} }
/** /**
@ -62,20 +59,20 @@ class PDFCursorTools {
* must be one of the values in {CursorTool}. * must be one of the values in {CursorTool}.
*/ */
switchTool(tool) { switchTool(tool) {
if (this.previouslyActive !== null) { if (this.#prevActive !== null) {
// Cursor tools cannot be used in PresentationMode/AnnotationEditor. // Cursor tools cannot be used in PresentationMode/AnnotationEditor.
return; return;
} }
if (tool === this.active) { if (tool === this.#active) {
return; // The requested tool is already active. return; // The requested tool is already active.
} }
const disableActiveTool = () => { const disableActiveTool = () => {
switch (this.active) { switch (this.#active) {
case CursorTool.SELECT: case CursorTool.SELECT:
break; break;
case CursorTool.HAND: case CursorTool.HAND:
this.handTool.deactivate(); this._handTool.deactivate();
break; break;
case CursorTool.ZOOM: case CursorTool.ZOOM:
/* falls through */ /* falls through */
@ -89,7 +86,7 @@ class PDFCursorTools {
break; break;
case CursorTool.HAND: case CursorTool.HAND:
disableActiveTool(); disableActiveTool();
this.handTool.activate(); this._handTool.activate();
break; break;
case CursorTool.ZOOM: case CursorTool.ZOOM:
/* falls through */ /* falls through */
@ -99,15 +96,11 @@ class PDFCursorTools {
} }
// Update the active tool *after* it has been validated above, // Update the active tool *after* it has been validated above,
// in order to prevent setting it to an invalid state. // in order to prevent setting it to an invalid state.
this.active = tool; this.#active = tool;
this.#dispatchEvent();
}
#dispatchEvent() {
this.eventBus.dispatch("cursortoolchanged", { this.eventBus.dispatch("cursortoolchanged", {
source: this, source: this,
tool: this.active, tool,
}); });
} }
@ -120,26 +113,26 @@ class PDFCursorTools {
presentationModeState = PresentationModeState.NORMAL; presentationModeState = PresentationModeState.NORMAL;
const disableActive = () => { const disableActive = () => {
const previouslyActive = this.active; const prevActive = this.#active;
this.switchTool(CursorTool.SELECT); this.switchTool(CursorTool.SELECT);
this.previouslyActive ??= previouslyActive; // Keep track of the first one. this.#prevActive ??= prevActive; // Keep track of the first one.
}; };
const enableActive = () => { const enableActive = () => {
const previouslyActive = this.previouslyActive; const prevActive = this.#prevActive;
if ( if (
previouslyActive !== null && prevActive !== null &&
annotationEditorMode === AnnotationEditorType.NONE && annotationEditorMode === AnnotationEditorType.NONE &&
presentationModeState === PresentationModeState.NORMAL presentationModeState === PresentationModeState.NORMAL
) { ) {
this.previouslyActive = null; this.#prevActive = null;
this.switchTool(previouslyActive); this.switchTool(prevActive);
} }
}; };
this.eventBus._on("secondarytoolbarreset", evt => { this.eventBus._on("secondarytoolbarreset", evt => {
if (this.previouslyActive !== null) { if (this.#prevActive !== null) {
annotationEditorMode = AnnotationEditorType.NONE; annotationEditorMode = AnnotationEditorType.NONE;
presentationModeState = PresentationModeState.NORMAL; presentationModeState = PresentationModeState.NORMAL;
@ -167,6 +160,19 @@ class PDFCursorTools {
} }
}); });
} }
/**
* @private
*/
get _handTool() {
return shadow(
this,
"_handTool",
new GrabToPan({
element: this.container,
})
);
}
} }
export { PDFCursorTools }; export { PDFCursorTools };