[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.
This commit is contained in:
Calixte Denizet 2022-07-19 16:10:31 +02:00
parent 5bfba89b0a
commit 7024a53e79
2 changed files with 36 additions and 34 deletions

View File

@ -40,8 +40,12 @@ import { InkEditor } from "./ink.js";
* Manage all the different editors on a page. * Manage all the different editors on a page.
*/ */
class AnnotationEditorLayer { class AnnotationEditorLayer {
#allowClick = false;
#boundClick; #boundClick;
#boundMousedown;
#editors = new Map(); #editors = new Map();
#isCleaningUp = false; #isCleaningUp = false;
@ -92,6 +96,7 @@ class AnnotationEditorLayer {
this.pageIndex = options.pageIndex; this.pageIndex = options.pageIndex;
this.div = options.div; this.div = options.div;
this.#boundClick = this.click.bind(this); this.#boundClick = this.click.bind(this);
this.#boundMousedown = this.mousedown.bind(this);
for (const editor of this.#uiManager.getEditors(options.pageIndex)) { for (const editor of this.#uiManager.getEditors(options.pageIndex)) {
this.add(editor); this.add(editor);
@ -103,7 +108,6 @@ class AnnotationEditorLayer {
/** /**
* Update the toolbar if it's required to reflect the tool currently used. * Update the toolbar if it's required to reflect the tool currently used.
* @param {number} mode * @param {number} mode
* @returns {undefined}
*/ */
updateToolbar(mode) { updateToolbar(mode) {
this.#uiManager.updateToolbar(mode); this.#uiManager.updateToolbar(mode);
@ -118,6 +122,9 @@ class AnnotationEditorLayer {
if (mode === AnnotationEditorType.INK) { if (mode === AnnotationEditorType.INK) {
// We always want to an ink editor ready to draw in. // We always want to an ink editor ready to draw in.
this.addInkEditorIfNeeded(false); this.addInkEditorIfNeeded(false);
this.disableClick();
} else {
this.enableClick();
} }
this.setActiveEditor(null); this.setActiveEditor(null);
} }
@ -177,7 +184,6 @@ class AnnotationEditorLayer {
/** /**
* Suppress the selected editor or all editors. * Suppress the selected editor or all editors.
* @returns {undefined}
*/ */
delete() { delete() {
this.#uiManager.delete(); this.#uiManager.delete();
@ -199,7 +205,6 @@ class AnnotationEditorLayer {
/** /**
* Paste a previously copied editor. * Paste a previously copied editor.
* @returns {undefined}
*/ */
paste() { paste() {
this.#uiManager.paste(); this.#uiManager.paste();
@ -250,16 +255,21 @@ class AnnotationEditorLayer {
currentActive.commitOrRemove(); currentActive.commitOrRemove();
} }
this.#uiManager.allowClick =
this.#uiManager.getMode() === AnnotationEditorType.INK;
if (editor) { if (editor) {
this.unselectAll(); 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) { attach(editor) {
this.#editors.set(editor.id, editor); this.#editors.set(editor.id, editor);
} }
@ -283,7 +293,6 @@ class AnnotationEditorLayer {
editor.isAttachedToDOM = false; editor.isAttachedToDOM = false;
if (this.#uiManager.isActive(editor) || this.#editors.size === 0) { if (this.#uiManager.isActive(editor) || this.#editors.size === 0) {
this.setActiveEditor(null); this.setActiveEditor(null);
this.#uiManager.allowClick = true;
} }
if (!this.#isCleaningUp) { if (!this.#isCleaningUp) {
@ -295,7 +304,6 @@ class AnnotationEditorLayer {
* An editor can have a different parent, for example after having * An editor can have a different parent, for example after having
* being dragged and droped from a page to another. * being dragged and droped from a page to another.
* @param {AnnotationEditor} editor * @param {AnnotationEditor} editor
* @returns {undefined}
*/ */
#changeParent(editor) { #changeParent(editor) {
if (editor.parent === this) { if (editor.parent === this) {
@ -423,21 +431,35 @@ class AnnotationEditorLayer {
/** /**
* Mouseclick callback. * Mouseclick callback.
* @param {MouseEvent} event * @param {MouseEvent} event
* @returns {undefined}
*/ */
click(event) { click(event) {
if (!this.#uiManager.allowClick) { if (event.target !== this.div) {
this.#uiManager.allowClick = true; return;
}
if (!this.#allowClick) {
this.#allowClick = true;
return; return;
} }
this.#createAndAddNewEditor(event); this.#createAndAddNewEditor(event);
} }
/**
* Mousedown callback.
* @param {MouseEvent} event
*/
mousedown(event) {
if (event.target !== this.div) {
return;
}
this.#allowClick = !this.#uiManager.hasActive();
}
/** /**
* Drag callback. * Drag callback.
* @param {DragEvent} event * @param {DragEvent} event
* @returns {undefined}
*/ */
drop(event) { drop(event) {
const id = event.dataTransfer.getData("text/plain"); const id = event.dataTransfer.getData("text/plain");
@ -510,7 +532,6 @@ class AnnotationEditorLayer {
render(parameters) { render(parameters) {
this.viewport = parameters.viewport; this.viewport = parameters.viewport;
bindEvents(this, this.div, ["dragover", "drop", "keydown"]); bindEvents(this, this.div, ["dragover", "drop", "keydown"]);
this.div.addEventListener("click", this.#boundClick);
this.setDimensions(); this.setDimensions();
this.updateMode(); this.updateMode();
} }

View File

@ -393,8 +393,6 @@ class AnnotationEditorUIManager {
#allLayers = new Map(); #allLayers = new Map();
#allowClick = true;
#clipboardManager = new ClipboardManager(); #clipboardManager = new ClipboardManager();
#commandManager = new CommandManager(); #commandManager = new CommandManager();
@ -744,22 +742,6 @@ class AnnotationEditorUIManager {
return false; 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. * Unselect the current editor.
*/ */
@ -767,7 +749,6 @@ class AnnotationEditorUIManager {
if (this.#activeEditor) { if (this.#activeEditor) {
this.#activeEditor.parent.setActiveEditor(null); this.#activeEditor.parent.setActiveEditor(null);
} }
this.#allowClick = true;
} }
/** /**