From 205ab958194007ef834bf02a6c243f3f9ab6f998 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 28 Sep 2022 23:25:51 +0200 Subject: [PATCH] [Editing] Disable the HandTool during editing (bug 1792422) This extends the approach used in PresentationMode to also cover the AnnotationEditor, and tries to handle the combination of both cases correctly. In order to simplify the overall implementation we simply track the *first* seen "previous" cursorTool, and don't allow it to be reset as long as either PresentationMode or an AnnotationEditor is being used. --- web/pdf_cursor_tools.js | 71 +++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/web/pdf_cursor_tools.js b/web/pdf_cursor_tools.js index e70ba1367..beee11ead 100644 --- a/web/pdf_cursor_tools.js +++ b/web/pdf_cursor_tools.js @@ -13,6 +13,7 @@ * limitations under the License. */ +import { AnnotationEditorType } from "pdfjs-lib"; import { GrabToPan } from "./grab_to_pan.js"; import { PresentationModeState } from "./ui_utils.js"; @@ -40,7 +41,7 @@ class PDFCursorTools { this.eventBus = eventBus; this.active = CursorTool.SELECT; - this.activeBeforePresentationMode = null; + this.previouslyActive = null; this.handTool = new GrabToPan({ element: this.container, @@ -63,13 +64,13 @@ class PDFCursorTools { } /** - * NOTE: This method is ignored while Presentation Mode is active. * @param {number} tool - The cursor mode that should be switched to, * must be one of the values in {CursorTool}. */ switchTool(tool) { - if (this.activeBeforePresentationMode !== null) { - return; // Cursor tools cannot be used in Presentation Mode. + if (this.previouslyActive !== null) { + // Cursor tools cannot be used in PresentationMode/AnnotationEditor. + return; } if (tool === this.active) { return; // The requested tool is already active. @@ -121,24 +122,54 @@ class PDFCursorTools { this.switchTool(evt.tool); }); - this.eventBus._on("presentationmodechanged", evt => { - switch (evt.state) { - case PresentationModeState.FULLSCREEN: { - const previouslyActive = this.active; + let annotationEditorMode = AnnotationEditorType.NONE, + presentationModeState = PresentationModeState.NORMAL; - this.switchTool(CursorTool.SELECT); - this.activeBeforePresentationMode = previouslyActive; - break; - } - case PresentationModeState.NORMAL: { - const previouslyActive = this.activeBeforePresentationMode; + const disableActive = () => { + const previouslyActive = this.active; - if (previouslyActive !== null) { - this.activeBeforePresentationMode = null; - this.switchTool(previouslyActive); - } - break; - } + this.switchTool(CursorTool.SELECT); + this.previouslyActive ??= previouslyActive; // Keep track of the first one. + }; + const enableActive = () => { + const previouslyActive = this.previouslyActive; + + if ( + previouslyActive !== null && + annotationEditorMode === AnnotationEditorType.NONE && + presentationModeState === PresentationModeState.NORMAL + ) { + this.previouslyActive = null; + this.switchTool(previouslyActive); + } + }; + + this.eventBus._on("secondarytoolbarreset", evt => { + if (this.previouslyActive !== null) { + annotationEditorMode = AnnotationEditorType.NONE; + presentationModeState = PresentationModeState.NORMAL; + + enableActive(); + } + }); + + this.eventBus._on("annotationeditormodechanged", ({ mode }) => { + annotationEditorMode = mode; + + if (mode === AnnotationEditorType.NONE) { + enableActive(); + } else { + disableActive(); + } + }); + + this.eventBus._on("presentationmodechanged", ({ state }) => { + presentationModeState = state; + + if (state === PresentationModeState.NORMAL) { + enableActive(); + } else if (state === PresentationModeState.FULLSCREEN) { + disableActive(); } }); }