@@ -51,6 +51,8 @@ class Selection {
51
51
node ,
52
52
{ global = false , longPressThreshold = 250 , validContainers = [ ] } = { }
53
53
) {
54
+ this . _initialEvent = null
55
+ this . selecting = false
54
56
this . isDetached = false
55
57
this . container = node
56
58
this . globalMouse = ! node || global
@@ -110,6 +112,11 @@ class Selection {
110
112
}
111
113
112
114
teardown ( ) {
115
+ this . _initialEvent = null
116
+ this . _initialEventData = null
117
+ this . _selectRect = null
118
+ this . selecting = false
119
+ this . _lastClickData = null
113
120
this . isDetached = true
114
121
this . _listeners = Object . create ( null )
115
122
this . _removeTouchMoveWindowListener && this . _removeTouchMoveWindowListener ( )
@@ -237,6 +244,7 @@ class Selection {
237
244
}
238
245
239
246
_handleInitialEvent ( e ) {
247
+ this . _initialEvent = e
240
248
if ( this . isDetached ) {
241
249
return
242
250
}
@@ -330,32 +338,36 @@ class Selection {
330
338
}
331
339
332
340
_handleTerminatingEvent ( e ) {
333
- const { pageX, pageY } = getEventCoordinates ( e )
341
+ const selecting = this . selecting
342
+ const bounds = this . _selectRect
343
+ // If it's not in selecting state, it's a click event
344
+ if ( ! selecting && e . type . includes ( 'key' ) ) {
345
+ e = this . _initialEvent
346
+ }
334
347
335
348
this . selecting = false
336
-
337
349
this . _removeEndListener && this . _removeEndListener ( )
338
350
this . _removeMoveListener && this . _removeMoveListener ( )
339
351
340
- if ( ! this . _initialEventData ) return
352
+ this . _selectRect = null
353
+ this . _initialEvent = null
354
+ this . _initialEventData = null
355
+ this . _lastClickData = null
356
+ if ( ! e ) return
341
357
342
358
let inRoot = ! this . container || contains ( this . container ( ) , e . target )
343
359
let isWithinValidContainer = this . _isWithinValidContainer ( e )
344
- let bounds = this . _selectRect
345
- let click = this . isClick ( pageX , pageY )
346
-
347
- this . _initialEventData = null
348
360
349
361
if ( e . key === 'Escape' || ! isWithinValidContainer ) {
350
362
return this . emit ( 'reset' )
351
363
}
352
364
353
- if ( click && inRoot ) {
365
+ if ( ! selecting && inRoot ) {
354
366
return this . _handleClickEvent ( e )
355
367
}
356
368
357
369
// User drag-clicked in the Selectable area
358
- if ( ! click ) return this . emit ( 'select' , bounds )
370
+ if ( selecting ) return this . emit ( 'select' , bounds )
359
371
360
372
return this . emit ( 'reset' )
361
373
}
@@ -403,28 +415,29 @@ class Selection {
403
415
let left = Math . min ( pageX , x ) ,
404
416
top = Math . min ( pageY , y ) ,
405
417
old = this . selecting
406
-
418
+ const click = this . isClick ( pageX , pageY )
407
419
// Prevent emitting selectStart event until mouse is moved.
408
420
// in Chrome on Windows, mouseMove event may be fired just after mouseDown event.
409
- if ( this . isClick ( pageX , pageY ) && ! old && ! ( w || h ) ) {
421
+ if ( click && ! old && ! ( w || h ) ) {
410
422
return
411
423
}
412
424
413
- this . selecting = true
414
- this . _selectRect = {
415
- top,
416
- left,
417
- x : pageX ,
418
- y : pageY ,
419
- right : left + w ,
420
- bottom : top + h ,
421
- }
422
-
423
- if ( ! old ) {
425
+ if ( ! old && ! click ) {
424
426
this . emit ( 'selectStart' , this . _initialEventData )
425
427
}
426
428
427
- if ( ! this . isClick ( pageX , pageY ) ) this . emit ( 'selecting' , this . _selectRect )
429
+ if ( ! click ) {
430
+ this . selecting = true
431
+ this . _selectRect = {
432
+ top,
433
+ left,
434
+ x : pageX ,
435
+ y : pageY ,
436
+ right : left + w ,
437
+ bottom : top + h ,
438
+ }
439
+ this . emit ( 'selecting' , this . _selectRect )
440
+ }
428
441
429
442
e . preventDefault ( )
430
443
}
0 commit comments