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 {
#active = CursorTool.SELECT;
#prevActive = null;
/**
* @param {PDFCursorToolsOptions} options
*/
@ -34,9 +38,6 @@ class PDFCursorTools {
this.container = container;
this.eventBus = eventBus;
this.active = CursorTool.SELECT;
this.previouslyActive = null;
this.handTool = new GrabToPan({
element: this.container,
});
@ -54,7 +55,7 @@ class PDFCursorTools {
* @type {number} One of the values in {CursorTool}.
*/
get activeTool() {
return this.active;
return this.#active;
}
/**
@ -62,16 +63,16 @@ class PDFCursorTools {
* must be one of the values in {CursorTool}.
*/
switchTool(tool) {
if (this.previouslyActive !== null) {
if (this.#prevActive !== null) {
// Cursor tools cannot be used in PresentationMode/AnnotationEditor.
return;
}
if (tool === this.active) {
if (tool === this.#active) {
return; // The requested tool is already active.
}
const disableActiveTool = () => {
switch (this.active) {
switch (this.#active) {
case CursorTool.SELECT:
break;
case CursorTool.HAND:
@ -99,15 +100,11 @@ class PDFCursorTools {
}
// Update the active tool *after* it has been validated above,
// in order to prevent setting it to an invalid state.
this.active = tool;
this.#active = tool;
this.#dispatchEvent();
}
#dispatchEvent() {
this.eventBus.dispatch("cursortoolchanged", {
source: this,
tool: this.active,
tool,
});
}
@ -120,26 +117,26 @@ class PDFCursorTools {
presentationModeState = PresentationModeState.NORMAL;
const disableActive = () => {
const previouslyActive = this.active;
const prevActive = this.#active;
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 previouslyActive = this.previouslyActive;
const prevActive = this.#prevActive;
if (
previouslyActive !== null &&
prevActive !== null &&
annotationEditorMode === AnnotationEditorType.NONE &&
presentationModeState === PresentationModeState.NORMAL
) {
this.previouslyActive = null;
this.switchTool(previouslyActive);
this.#prevActive = null;
this.switchTool(prevActive);
}
};
this.eventBus._on("secondarytoolbarreset", evt => {
if (this.previouslyActive !== null) {
if (this.#prevActive !== null) {
annotationEditorMode = AnnotationEditorType.NONE;
presentationModeState = PresentationModeState.NORMAL;