A couple of small tweaks of the PDFCursorTools class

- Introduce a few private fields for internal state.

 - Inline a small method at its only call-site.
This commit is contained in:
Jonas Jenwald 2023-06-28 12:16:46 +02:00
parent 50488d7a47
commit 789e318cf7

View File

@ -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,9 +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({ this.handTool = new GrabToPan({
element: this.container, element: this.container,
}); });
@ -54,7 +55,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,16 +63,16 @@ 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:
@ -99,15 +100,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 +117,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;