From 7024a53e79e8717a688e047025720fe910154f4d Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Tue, 19 Jul 2022 16:10:31 +0200 Subject: [PATCH] [Editor] Simplify the way to create an editor on click Previously, we had to set the #allowClick property by hand which was a bit painful because it's easy to overlook one case or an other. So with this patch a new editor (for now FreeText one only because the Ink one is a bit different) is created on the first click if none is selected on mousedown, else the first click will just commit the data and then the second will creater a new editor. --- src/display/editor/annotation_editor_layer.js | 51 +++++++++++++------ src/display/editor/tools.js | 19 ------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/display/editor/annotation_editor_layer.js b/src/display/editor/annotation_editor_layer.js index ecc29b3f7..796dd9618 100644 --- a/src/display/editor/annotation_editor_layer.js +++ b/src/display/editor/annotation_editor_layer.js @@ -40,8 +40,12 @@ import { InkEditor } from "./ink.js"; * Manage all the different editors on a page. */ class AnnotationEditorLayer { + #allowClick = false; + #boundClick; + #boundMousedown; + #editors = new Map(); #isCleaningUp = false; @@ -92,6 +96,7 @@ class AnnotationEditorLayer { this.pageIndex = options.pageIndex; this.div = options.div; this.#boundClick = this.click.bind(this); + this.#boundMousedown = this.mousedown.bind(this); for (const editor of this.#uiManager.getEditors(options.pageIndex)) { this.add(editor); @@ -103,7 +108,6 @@ class AnnotationEditorLayer { /** * Update the toolbar if it's required to reflect the tool currently used. * @param {number} mode - * @returns {undefined} */ updateToolbar(mode) { this.#uiManager.updateToolbar(mode); @@ -118,6 +122,9 @@ class AnnotationEditorLayer { if (mode === AnnotationEditorType.INK) { // We always want to an ink editor ready to draw in. this.addInkEditorIfNeeded(false); + this.disableClick(); + } else { + this.enableClick(); } this.setActiveEditor(null); } @@ -177,7 +184,6 @@ class AnnotationEditorLayer { /** * Suppress the selected editor or all editors. - * @returns {undefined} */ delete() { this.#uiManager.delete(); @@ -199,7 +205,6 @@ class AnnotationEditorLayer { /** * Paste a previously copied editor. - * @returns {undefined} */ paste() { this.#uiManager.paste(); @@ -250,16 +255,21 @@ class AnnotationEditorLayer { currentActive.commitOrRemove(); } - this.#uiManager.allowClick = - this.#uiManager.getMode() === AnnotationEditorType.INK; if (editor) { this.unselectAll(); - this.div.removeEventListener("click", this.#boundClick); - } else { - this.div.addEventListener("click", this.#boundClick); } } + enableClick() { + this.div.addEventListener("mousedown", this.#boundMousedown); + this.div.addEventListener("click", this.#boundClick); + } + + disableClick() { + this.div.removeEventListener("mousedown", this.#boundMousedown); + this.div.removeEventListener("click", this.#boundClick); + } + attach(editor) { this.#editors.set(editor.id, editor); } @@ -283,7 +293,6 @@ class AnnotationEditorLayer { editor.isAttachedToDOM = false; if (this.#uiManager.isActive(editor) || this.#editors.size === 0) { this.setActiveEditor(null); - this.#uiManager.allowClick = true; } if (!this.#isCleaningUp) { @@ -295,7 +304,6 @@ class AnnotationEditorLayer { * An editor can have a different parent, for example after having * being dragged and droped from a page to another. * @param {AnnotationEditor} editor - * @returns {undefined} */ #changeParent(editor) { if (editor.parent === this) { @@ -423,21 +431,35 @@ class AnnotationEditorLayer { /** * Mouseclick callback. * @param {MouseEvent} event - * @returns {undefined} */ click(event) { - if (!this.#uiManager.allowClick) { - this.#uiManager.allowClick = true; + if (event.target !== this.div) { + return; + } + + if (!this.#allowClick) { + this.#allowClick = true; return; } this.#createAndAddNewEditor(event); } + /** + * Mousedown callback. + * @param {MouseEvent} event + */ + mousedown(event) { + if (event.target !== this.div) { + return; + } + + this.#allowClick = !this.#uiManager.hasActive(); + } + /** * Drag callback. * @param {DragEvent} event - * @returns {undefined} */ drop(event) { const id = event.dataTransfer.getData("text/plain"); @@ -510,7 +532,6 @@ class AnnotationEditorLayer { render(parameters) { this.viewport = parameters.viewport; bindEvents(this, this.div, ["dragover", "drop", "keydown"]); - this.div.addEventListener("click", this.#boundClick); this.setDimensions(); this.updateMode(); } diff --git a/src/display/editor/tools.js b/src/display/editor/tools.js index 83d8696c6..39f2f26bc 100644 --- a/src/display/editor/tools.js +++ b/src/display/editor/tools.js @@ -393,8 +393,6 @@ class AnnotationEditorUIManager { #allLayers = new Map(); - #allowClick = true; - #clipboardManager = new ClipboardManager(); #commandManager = new CommandManager(); @@ -744,22 +742,6 @@ class AnnotationEditorUIManager { return false; } - /** - * When set to true a click on the current layer will trigger - * an editor creation. - * @return {boolean} - */ - get allowClick() { - return this.#allowClick; - } - - /** - * @param {boolean} allow - */ - set allowClick(allow) { - this.#allowClick = allow; - } - /** * Unselect the current editor. */ @@ -767,7 +749,6 @@ class AnnotationEditorUIManager { if (this.#activeEditor) { this.#activeEditor.parent.setActiveEditor(null); } - this.#allowClick = true; } /**