Skip to content

Commit 642676a

Browse files
authored
Merge pull request #15184 from calixteman/rm_allowclick
[Editor] Simplify the way to create an editor on click
2 parents 5bfba89 + 7024a53 commit 642676a

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

src/display/editor/annotation_editor_layer.js

+36-15
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ import { InkEditor } from "./ink.js";
4040
* Manage all the different editors on a page.
4141
*/
4242
class AnnotationEditorLayer {
43+
#allowClick = false;
44+
4345
#boundClick;
4446

47+
#boundMousedown;
48+
4549
#editors = new Map();
4650

4751
#isCleaningUp = false;
@@ -92,6 +96,7 @@ class AnnotationEditorLayer {
9296
this.pageIndex = options.pageIndex;
9397
this.div = options.div;
9498
this.#boundClick = this.click.bind(this);
99+
this.#boundMousedown = this.mousedown.bind(this);
95100

96101
for (const editor of this.#uiManager.getEditors(options.pageIndex)) {
97102
this.add(editor);
@@ -103,7 +108,6 @@ class AnnotationEditorLayer {
103108
/**
104109
* Update the toolbar if it's required to reflect the tool currently used.
105110
* @param {number} mode
106-
* @returns {undefined}
107111
*/
108112
updateToolbar(mode) {
109113
this.#uiManager.updateToolbar(mode);
@@ -118,6 +122,9 @@ class AnnotationEditorLayer {
118122
if (mode === AnnotationEditorType.INK) {
119123
// We always want to an ink editor ready to draw in.
120124
this.addInkEditorIfNeeded(false);
125+
this.disableClick();
126+
} else {
127+
this.enableClick();
121128
}
122129
this.setActiveEditor(null);
123130
}
@@ -177,7 +184,6 @@ class AnnotationEditorLayer {
177184

178185
/**
179186
* Suppress the selected editor or all editors.
180-
* @returns {undefined}
181187
*/
182188
delete() {
183189
this.#uiManager.delete();
@@ -199,7 +205,6 @@ class AnnotationEditorLayer {
199205

200206
/**
201207
* Paste a previously copied editor.
202-
* @returns {undefined}
203208
*/
204209
paste() {
205210
this.#uiManager.paste();
@@ -250,16 +255,21 @@ class AnnotationEditorLayer {
250255
currentActive.commitOrRemove();
251256
}
252257

253-
this.#uiManager.allowClick =
254-
this.#uiManager.getMode() === AnnotationEditorType.INK;
255258
if (editor) {
256259
this.unselectAll();
257-
this.div.removeEventListener("click", this.#boundClick);
258-
} else {
259-
this.div.addEventListener("click", this.#boundClick);
260260
}
261261
}
262262

263+
enableClick() {
264+
this.div.addEventListener("mousedown", this.#boundMousedown);
265+
this.div.addEventListener("click", this.#boundClick);
266+
}
267+
268+
disableClick() {
269+
this.div.removeEventListener("mousedown", this.#boundMousedown);
270+
this.div.removeEventListener("click", this.#boundClick);
271+
}
272+
263273
attach(editor) {
264274
this.#editors.set(editor.id, editor);
265275
}
@@ -283,7 +293,6 @@ class AnnotationEditorLayer {
283293
editor.isAttachedToDOM = false;
284294
if (this.#uiManager.isActive(editor) || this.#editors.size === 0) {
285295
this.setActiveEditor(null);
286-
this.#uiManager.allowClick = true;
287296
}
288297

289298
if (!this.#isCleaningUp) {
@@ -295,7 +304,6 @@ class AnnotationEditorLayer {
295304
* An editor can have a different parent, for example after having
296305
* being dragged and droped from a page to another.
297306
* @param {AnnotationEditor} editor
298-
* @returns {undefined}
299307
*/
300308
#changeParent(editor) {
301309
if (editor.parent === this) {
@@ -423,21 +431,35 @@ class AnnotationEditorLayer {
423431
/**
424432
* Mouseclick callback.
425433
* @param {MouseEvent} event
426-
* @returns {undefined}
427434
*/
428435
click(event) {
429-
if (!this.#uiManager.allowClick) {
430-
this.#uiManager.allowClick = true;
436+
if (event.target !== this.div) {
437+
return;
438+
}
439+
440+
if (!this.#allowClick) {
441+
this.#allowClick = true;
431442
return;
432443
}
433444

434445
this.#createAndAddNewEditor(event);
435446
}
436447

448+
/**
449+
* Mousedown callback.
450+
* @param {MouseEvent} event
451+
*/
452+
mousedown(event) {
453+
if (event.target !== this.div) {
454+
return;
455+
}
456+
457+
this.#allowClick = !this.#uiManager.hasActive();
458+
}
459+
437460
/**
438461
* Drag callback.
439462
* @param {DragEvent} event
440-
* @returns {undefined}
441463
*/
442464
drop(event) {
443465
const id = event.dataTransfer.getData("text/plain");
@@ -510,7 +532,6 @@ class AnnotationEditorLayer {
510532
render(parameters) {
511533
this.viewport = parameters.viewport;
512534
bindEvents(this, this.div, ["dragover", "drop", "keydown"]);
513-
this.div.addEventListener("click", this.#boundClick);
514535
this.setDimensions();
515536
this.updateMode();
516537
}

src/display/editor/tools.js

-19
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,6 @@ class AnnotationEditorUIManager {
393393

394394
#allLayers = new Map();
395395

396-
#allowClick = true;
397-
398396
#clipboardManager = new ClipboardManager();
399397

400398
#commandManager = new CommandManager();
@@ -744,30 +742,13 @@ class AnnotationEditorUIManager {
744742
return false;
745743
}
746744

747-
/**
748-
* When set to true a click on the current layer will trigger
749-
* an editor creation.
750-
* @return {boolean}
751-
*/
752-
get allowClick() {
753-
return this.#allowClick;
754-
}
755-
756-
/**
757-
* @param {boolean} allow
758-
*/
759-
set allowClick(allow) {
760-
this.#allowClick = allow;
761-
}
762-
763745
/**
764746
* Unselect the current editor.
765747
*/
766748
unselect() {
767749
if (this.#activeEditor) {
768750
this.#activeEditor.parent.setActiveEditor(null);
769751
}
770-
this.#allowClick = true;
771752
}
772753

773754
/**

0 commit comments

Comments
 (0)