Skip to content

Commit 8a3eb74

Browse files
hunterbmtdarthtrevino
authored andcommitted
Simplify the even flow, end drag whenever receiving mousemove after dragstart instead of depending on dragend to be trigger (#801)
1 parent aaf3cfb commit 8a3eb74

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

packages/react-dnd-html5-backend/src/HTML5Backend.js

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export default class HTML5Backend {
3232
this.getSourceClientOffset = this.getSourceClientOffset.bind(this);
3333
this.handleTopDragStart = this.handleTopDragStart.bind(this);
3434
this.handleTopDragStartCapture = this.handleTopDragStartCapture.bind(this);
35-
this.handleTopDragEndCapture = this.handleTopDragEndCapture.bind(this);
3635
this.handleTopDragEnter = this.handleTopDragEnter.bind(this);
3736
this.handleTopDragEnterCapture = this.handleTopDragEnterCapture.bind(this);
3837
this.handleTopDragLeaveCapture = this.handleTopDragLeaveCapture.bind(this);
@@ -41,7 +40,7 @@ export default class HTML5Backend {
4140
this.handleTopDrop = this.handleTopDrop.bind(this);
4241
this.handleTopDropCapture = this.handleTopDropCapture.bind(this);
4342
this.handleSelectStart = this.handleSelectStart.bind(this);
44-
this.endDragIfSourceWasRemovedFromDOM = this.endDragIfSourceWasRemovedFromDOM.bind(this);
43+
this.endDrag = this.endDrag.bind(this);
4544
this.endDragNativeItem = this.endDragNativeItem.bind(this);
4645
this.asyncEndDragNativeItem = this.asyncEndDragNativeItem.bind(this);
4746
}
@@ -83,7 +82,6 @@ export default class HTML5Backend {
8382
addEventListeners(target) {
8483
target.addEventListener('dragstart', this.handleTopDragStart);
8584
target.addEventListener('dragstart', this.handleTopDragStartCapture, true);
86-
target.addEventListener('dragend', this.handleTopDragEndCapture, true);
8785
target.addEventListener('dragenter', this.handleTopDragEnter);
8886
target.addEventListener('dragenter', this.handleTopDragEnterCapture, true);
8987
target.addEventListener('dragleave', this.handleTopDragLeaveCapture, true);
@@ -96,7 +94,6 @@ export default class HTML5Backend {
9694
removeEventListeners(target) {
9795
target.removeEventListener('dragstart', this.handleTopDragStart);
9896
target.removeEventListener('dragstart', this.handleTopDragStartCapture, true);
99-
target.removeEventListener('dragend', this.handleTopDragEndCapture, true);
10097
target.removeEventListener('dragenter', this.handleTopDragEnter);
10198
target.removeEventListener('dragenter', this.handleTopDragEnterCapture, true);
10299
target.removeEventListener('dragleave', this.handleTopDragLeaveCapture, true);
@@ -228,12 +225,7 @@ export default class HTML5Backend {
228225
this.currentNativeSource = null;
229226
}
230227

231-
endDragIfSourceWasRemovedFromDOM() {
232-
const node = this.currentDragSourceNode;
233-
if (document.body.contains(node)) {
234-
return;
235-
}
236-
228+
endDrag() {
237229
if (this.clearCurrentDragSourceNode()) {
238230
this.actions.endDrag();
239231
}
@@ -247,16 +239,17 @@ export default class HTML5Backend {
247239

248240
// Receiving a mouse event in the middle of a dragging operation
249241
// means it has ended and the drag source node disappeared from DOM,
250-
// so the browser didn't dispatch the dragend event.
251-
this.window.addEventListener('mousemove', this.endDragIfSourceWasRemovedFromDOM, true);
242+
// so the browser didn't dispatch the dragend event or browser fail
243+
// to trigger dragend (i.e: Chrome 59)
244+
this.window.addEventListener('mousemove', this.endDrag, true);
252245
}
253246

254247
clearCurrentDragSourceNode() {
255248
if (this.currentDragSourceNode) {
256249
this.currentDragSourceNode = null;
257250
this.currentDragSourceNodeOffset = null;
258251
this.currentDragSourceNodeOffsetChanged = false;
259-
this.window.removeEventListener('mousemove', this.endDragIfSourceWasRemovedFromDOM, true);
252+
this.window.removeEventListener('mousemove', this.endDrag, true);
260253
return true;
261254
}
262255

@@ -389,15 +382,6 @@ export default class HTML5Backend {
389382
}
390383
}
391384

392-
handleTopDragEndCapture() {
393-
if (this.clearCurrentDragSourceNode()) {
394-
// Firefox can dispatch this event in an infinite loop
395-
// if dragend handler does something like showing an alert.
396-
// Only proceed if we have not handled it already.
397-
this.actions.endDrag();
398-
}
399-
}
400-
401385
handleTopDragEnterCapture(e) {
402386
this.dragEnterTargetIds = [];
403387

@@ -539,8 +523,6 @@ export default class HTML5Backend {
539523

540524
if (this.isDraggingNativeItem()) {
541525
this.endDragNativeItem();
542-
} else {
543-
this.endDragIfSourceWasRemovedFromDOM();
544526
}
545527
}
546528

0 commit comments

Comments
 (0)