Skip to content

Commit 9bcab66

Browse files
committed
fix(sensor): return early when the target isn't in handle or draggable
elements
1 parent 1590e5d commit 9bcab66

File tree

17 files changed

+439
-164
lines changed

17 files changed

+439
-164
lines changed

jsconfig.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "./src/"
4+
}
5+
}

src/Draggable/Draggable.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export default class Draggable {
387387
*/
388388
[onDragStart](event) {
389389
const sensorEvent = getSensorEvent(event);
390-
const {target, container} = sensorEvent;
390+
const {target, container, originalSource} = sensorEvent;
391391

392392
if (!this.containers.includes(container)) {
393393
return;
@@ -398,15 +398,9 @@ export default class Draggable {
398398
return;
399399
}
400400

401-
// Find draggable source element
402-
this.originalSource = closest(target, this.options.draggable);
401+
this.originalSource = originalSource;
403402
this.sourceContainer = container;
404403

405-
if (!this.originalSource) {
406-
sensorEvent.cancel();
407-
return;
408-
}
409-
410404
if (this.lastPlacedSource && this.lastPlacedContainer) {
411405
clearTimeout(this.placedTimeoutID);
412406
this.lastPlacedSource.classList.remove(...this.getClassNamesFor('source:placed'));

src/Draggable/Sensors/DragSensor/DragSensor.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,17 @@ export default class DragSensor extends Sensor {
8080
event.dataTransfer.effectAllowed = this.options.type;
8181

8282
const target = document.elementFromPoint(event.clientX, event.clientY);
83-
this.currentContainer = closest(event.target, this.containers);
83+
const originalSource = this.draggableElement;
8484

85-
if (!this.currentContainer) {
85+
if (!originalSource) {
8686
return;
8787
}
8888

8989
const dragStartEvent = new DragStartSensorEvent({
9090
clientX: event.clientX,
9191
clientY: event.clientY,
9292
target,
93+
originalSource,
9394
container: this.currentContainer,
9495
originalEvent: event,
9596
});
@@ -187,6 +188,23 @@ export default class DragSensor extends Sensor {
187188
return;
188189
}
189190

191+
const target = event.target;
192+
this.currentContainer = closest(target, this.containers);
193+
194+
if (!this.currentContainer) {
195+
return;
196+
}
197+
198+
if (this.options.handle && target && !closest(target, this.options.handle)) {
199+
return;
200+
}
201+
202+
const originalSource = closest(target, this.options.draggable);
203+
204+
if (!originalSource) {
205+
return;
206+
}
207+
190208
const nativeDraggableElement = closest(event.target, (element) => element.draggable);
191209

192210
if (nativeDraggableElement) {
@@ -200,17 +218,11 @@ export default class DragSensor extends Sensor {
200218
document.addEventListener('dragend', this[onDragEnd], false);
201219
document.addEventListener('drop', this[onDrop], false);
202220

203-
const target = closest(event.target, this.options.draggable);
204-
205-
if (!target) {
206-
return;
207-
}
208-
209221
this.startEvent = event;
210222

211223
this.mouseDownTimeout = setTimeout(() => {
212-
target.draggable = true;
213-
this.draggableElement = target;
224+
originalSource.draggable = true;
225+
this.draggableElement = originalSource;
214226
}, this.delay.drag);
215227
}
216228

src/Draggable/Sensors/DragSensor/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Detaches sensors to the DOM
2222

2323
### Options
2424

25+
**`draggable {String}`**
26+
A css selector for draggable elements within the `containers` specified.
27+
2528
**`delay {Number}`**
2629
This value will delay touch start
2730

0 commit comments

Comments
 (0)