@@ -40,8 +40,12 @@ import { InkEditor } from "./ink.js";
40
40
* Manage all the different editors on a page.
41
41
*/
42
42
class AnnotationEditorLayer {
43
+ #allowClick = false ;
44
+
43
45
#boundClick;
44
46
47
+ #boundMousedown;
48
+
45
49
#editors = new Map ( ) ;
46
50
47
51
#isCleaningUp = false ;
@@ -92,6 +96,7 @@ class AnnotationEditorLayer {
92
96
this . pageIndex = options . pageIndex ;
93
97
this . div = options . div ;
94
98
this . #boundClick = this . click . bind ( this ) ;
99
+ this . #boundMousedown = this . mousedown . bind ( this ) ;
95
100
96
101
for ( const editor of this . #uiManager. getEditors ( options . pageIndex ) ) {
97
102
this . add ( editor ) ;
@@ -103,7 +108,6 @@ class AnnotationEditorLayer {
103
108
/**
104
109
* Update the toolbar if it's required to reflect the tool currently used.
105
110
* @param {number } mode
106
- * @returns {undefined }
107
111
*/
108
112
updateToolbar ( mode ) {
109
113
this . #uiManager. updateToolbar ( mode ) ;
@@ -118,6 +122,9 @@ class AnnotationEditorLayer {
118
122
if ( mode === AnnotationEditorType . INK ) {
119
123
// We always want to an ink editor ready to draw in.
120
124
this . addInkEditorIfNeeded ( false ) ;
125
+ this . disableClick ( ) ;
126
+ } else {
127
+ this . enableClick ( ) ;
121
128
}
122
129
this . setActiveEditor ( null ) ;
123
130
}
@@ -177,7 +184,6 @@ class AnnotationEditorLayer {
177
184
178
185
/**
179
186
* Suppress the selected editor or all editors.
180
- * @returns {undefined }
181
187
*/
182
188
delete ( ) {
183
189
this . #uiManager. delete ( ) ;
@@ -199,7 +205,6 @@ class AnnotationEditorLayer {
199
205
200
206
/**
201
207
* Paste a previously copied editor.
202
- * @returns {undefined }
203
208
*/
204
209
paste ( ) {
205
210
this . #uiManager. paste ( ) ;
@@ -250,16 +255,21 @@ class AnnotationEditorLayer {
250
255
currentActive . commitOrRemove ( ) ;
251
256
}
252
257
253
- this . #uiManager. allowClick =
254
- this . #uiManager. getMode ( ) === AnnotationEditorType . INK ;
255
258
if ( editor ) {
256
259
this . unselectAll ( ) ;
257
- this . div . removeEventListener ( "click" , this . #boundClick) ;
258
- } else {
259
- this . div . addEventListener ( "click" , this . #boundClick) ;
260
260
}
261
261
}
262
262
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
+
263
273
attach ( editor ) {
264
274
this . #editors. set ( editor . id , editor ) ;
265
275
}
@@ -283,7 +293,6 @@ class AnnotationEditorLayer {
283
293
editor . isAttachedToDOM = false ;
284
294
if ( this . #uiManager. isActive ( editor ) || this . #editors. size === 0 ) {
285
295
this . setActiveEditor ( null ) ;
286
- this . #uiManager. allowClick = true ;
287
296
}
288
297
289
298
if ( ! this . #isCleaningUp) {
@@ -295,7 +304,6 @@ class AnnotationEditorLayer {
295
304
* An editor can have a different parent, for example after having
296
305
* being dragged and droped from a page to another.
297
306
* @param {AnnotationEditor } editor
298
- * @returns {undefined }
299
307
*/
300
308
#changeParent( editor ) {
301
309
if ( editor . parent === this ) {
@@ -423,21 +431,35 @@ class AnnotationEditorLayer {
423
431
/**
424
432
* Mouseclick callback.
425
433
* @param {MouseEvent } event
426
- * @returns {undefined }
427
434
*/
428
435
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 ;
431
442
return ;
432
443
}
433
444
434
445
this . #createAndAddNewEditor( event ) ;
435
446
}
436
447
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
+
437
460
/**
438
461
* Drag callback.
439
462
* @param {DragEvent } event
440
- * @returns {undefined }
441
463
*/
442
464
drop ( event ) {
443
465
const id = event . dataTransfer . getData ( "text/plain" ) ;
@@ -510,7 +532,6 @@ class AnnotationEditorLayer {
510
532
render ( parameters ) {
511
533
this . viewport = parameters . viewport ;
512
534
bindEvents ( this , this . div , [ "dragover" , "drop" , "keydown" ] ) ;
513
- this . div . addEventListener ( "click" , this . #boundClick) ;
514
535
this . setDimensions ( ) ;
515
536
this . updateMode ( ) ;
516
537
}
0 commit comments