Skip to content

Commit 937b4c5

Browse files
forcedddjixiang13
andauthored
fix(Selection): handling of terminating event
* fix: _handleTerminatingEvent * fix: reset _lastClickData --------- Co-authored-by: forceddd <[email protected]>
1 parent 5f2bf50 commit 937b4c5

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

src/Selection.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class Selection {
5151
node,
5252
{ global = false, longPressThreshold = 250, validContainers = [] } = {}
5353
) {
54+
this._initialEvent = null
55+
this.selecting = false
5456
this.isDetached = false
5557
this.container = node
5658
this.globalMouse = !node || global
@@ -110,6 +112,11 @@ class Selection {
110112
}
111113

112114
teardown() {
115+
this._initialEvent = null
116+
this._initialEventData = null
117+
this._selectRect = null
118+
this.selecting = false
119+
this._lastClickData = null
113120
this.isDetached = true
114121
this._listeners = Object.create(null)
115122
this._removeTouchMoveWindowListener && this._removeTouchMoveWindowListener()
@@ -237,6 +244,7 @@ class Selection {
237244
}
238245

239246
_handleInitialEvent(e) {
247+
this._initialEvent = e
240248
if (this.isDetached) {
241249
return
242250
}
@@ -330,32 +338,36 @@ class Selection {
330338
}
331339

332340
_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+
}
334347

335348
this.selecting = false
336-
337349
this._removeEndListener && this._removeEndListener()
338350
this._removeMoveListener && this._removeMoveListener()
339351

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
341357

342358
let inRoot = !this.container || contains(this.container(), e.target)
343359
let isWithinValidContainer = this._isWithinValidContainer(e)
344-
let bounds = this._selectRect
345-
let click = this.isClick(pageX, pageY)
346-
347-
this._initialEventData = null
348360

349361
if (e.key === 'Escape' || !isWithinValidContainer) {
350362
return this.emit('reset')
351363
}
352364

353-
if (click && inRoot) {
365+
if (!selecting && inRoot) {
354366
return this._handleClickEvent(e)
355367
}
356368

357369
// User drag-clicked in the Selectable area
358-
if (!click) return this.emit('select', bounds)
370+
if (selecting) return this.emit('select', bounds)
359371

360372
return this.emit('reset')
361373
}
@@ -403,28 +415,29 @@ class Selection {
403415
let left = Math.min(pageX, x),
404416
top = Math.min(pageY, y),
405417
old = this.selecting
406-
418+
const click = this.isClick(pageX, pageY)
407419
// Prevent emitting selectStart event until mouse is moved.
408420
// 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)) {
410422
return
411423
}
412424

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) {
424426
this.emit('selectStart', this._initialEventData)
425427
}
426428

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+
}
428441

429442
e.preventDefault()
430443
}

0 commit comments

Comments
 (0)