Skip to content

Commit 487496f

Browse files
committed
[IMP] drag&drop: stop dragging on every mouse up event
If the drag & drop is started, and that the mouse is released over an element that stops the event propagation, the drag & drop will not be stopped. This is less than ideal. Task: 4674384 Part-of: #6111 Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
1 parent 78e0ae7 commit 487496f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/components/helpers/drag_and_drop.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type EventFn = (ev: PointerEvent) => void;
77
*/
88
export function startDnd(onPointerMove: EventFn, onPointerUp: EventFn) {
99
const removeListeners = () => {
10-
window.removeEventListener("pointerup", _onPointerUp);
10+
window.removeEventListener("pointerup", _onPointerUp, { capture: true });
1111
window.removeEventListener("dragstart", _onDragStart);
1212
window.removeEventListener("pointermove", onPointerMove);
1313
window.removeEventListener("wheel", onPointerMove);
@@ -19,7 +19,7 @@ export function startDnd(onPointerMove: EventFn, onPointerUp: EventFn) {
1919
function _onDragStart(ev: DragEvent) {
2020
ev.preventDefault();
2121
}
22-
window.addEventListener("pointerup", _onPointerUp);
22+
window.addEventListener("pointerup", _onPointerUp, { capture: true });
2323
window.addEventListener("dragstart", _onDragStart);
2424
window.addEventListener("pointermove", onPointerMove);
2525
// mouse wheel on window is by default a passive event.

tests/grid/grid_drag_and_drop_component.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,17 @@ test("Drag&drop is stopped when the calling component is unmounted", async () =>
418418
expect(spyRemoveEventListener).toHaveBeenCalledTimes(4);
419419
});
420420

421+
test("Drag&drop is stopped when mouseup is called on an element that stops pointer events", async () => {
422+
const div = document.createElement("div");
423+
div.id = "test-div";
424+
div.addEventListener("pointerup", (ev) => ev.stopPropagation());
425+
document.body.appendChild(div);
426+
triggerMouseEvent(".o-fake-grid", "pointerdown", 0, 0);
427+
expect(mouseUpFn).toHaveBeenCalledTimes(0);
428+
triggerMouseEvent("#test-div", "pointerup", 0, 0);
429+
expect(mouseUpFn).toHaveBeenCalledTimes(1);
430+
});
431+
421432
test("drag And Drop is based on the current active sheet", async () => {
422433
setViewportOffset(model, 6 * DEFAULT_CELL_WIDTH, 6 * DEFAULT_CELL_HEIGHT);
423434
hideColumns(model, [numberToLetters(5)]);

0 commit comments

Comments
 (0)