diff --git a/src/features/project-tree/ProjectView.tsx b/src/features/project-tree/ProjectView.tsx index 0751ee43a..758283da4 100644 --- a/src/features/project-tree/ProjectView.tsx +++ b/src/features/project-tree/ProjectView.tsx @@ -39,6 +39,7 @@ import { HighlightedText, Icon, Popup, + beginDrag, dragSession, registerDropTarget, Tree, @@ -641,6 +642,7 @@ export function ProjectView() { // editor opens only files, so folders carry the trailing slash `findDirs` // uses and the editor leaves them out. let files: DragSession | null = null; + let endDrag: (() => void) | null = null; const move = (ev: PointerEvent) => { // A press with no button held is a reordered macOS tap, not a drag (see pointerDrag.ts). if (!(ev.buttons & 1)) { @@ -651,6 +653,7 @@ export function ProjectView() { if (Math.hypot(ev.clientX - startX, ev.clientY - startY) < DRAG_THRESHOLD) return; active = true; dragged.current = true; + endDrag = beginDrag(); const m = selectModel(useProjectTree.getState()); const keys = before.has(node.key) ? [...before] : [node.key]; if (before.has(node.key)) setSelection(before, lead); @@ -697,6 +700,7 @@ export function ProjectView() { window.removeEventListener("pointercancel", cleanup); window.removeEventListener("keydown", key, true); files?.cancel(); + endDrag?.(); setDropTarget(null); setDrag(null); }; diff --git a/src/ui/index.ts b/src/ui/index.ts index 4842eeb97..f13766ea7 100644 --- a/src/ui/index.ts +++ b/src/ui/index.ts @@ -13,6 +13,7 @@ export { FILES_DRAG, REMOTE_FILES_DRAG, dragRequiresAlt, + beginDrag, dragSession, dropZoneAt, registerDropTarget, diff --git a/src/ui/pointerDrag.module.css b/src/ui/pointerDrag.module.css index afe8ecde2..aa73dc526 100644 --- a/src/ui/pointerDrag.module.css +++ b/src/ui/pointerDrag.module.css @@ -17,3 +17,10 @@ cursor: grabbing !important; user-select: none !important; } + +/* WebKit keeps editable text selectable whatever its user-select, so the pointer passing over + the editor or a text field during a drag selected text there (#3334). Take them out of hit + testing; drop targets are their containers. */ +.dragging :is(input, textarea, [contenteditable]) { + pointer-events: none !important; +} diff --git a/src/ui/pointerDrag.test.ts b/src/ui/pointerDrag.test.ts index 843bb39a9..4f5b5de4a 100644 --- a/src/ui/pointerDrag.test.ts +++ b/src/ui/pointerDrag.test.ts @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { dropZoneAt, isOutsideWindow, startPointerDrag } from "./pointerDrag"; +import { beginDrag, dropZoneAt, isOutsideWindow, startPointerDrag } from "./pointerDrag"; const fire = (type: string, buttons: number, x = 0, y = 0) => window.dispatchEvent(new MouseEvent(type, { bubbles: true, buttons, clientX: x, clientY: y })); @@ -47,6 +47,23 @@ describe("startPointerDrag with a reordered macOS tap", () => { fire("pointerup", 0, 60, 0); expect(onEnd).toHaveBeenCalledWith(true, expect.objectContaining({ x: 60 })); }); + + it("clears a text selection that began before the drag threshold (#3334)", () => { + const text = document.body.appendChild(document.createElement("p")); + text.textContent = "dragging a file must not select this"; + const range = document.createRange(); + range.selectNodeContents(text); + const selection = window.getSelection()!; + selection.removeAllRanges(); + selection.addRange(range); + + const { onStart } = start(); + fire("pointermove", 1, 40, 0); + fire("pointerup", 0, 40, 0); + + expect(onStart).toHaveBeenCalledOnce(); + expect(window.getSelection()?.rangeCount).toBe(0); + }); }); const viewport = { left: 0, top: 0, right: 1280, bottom: 800 }; @@ -76,3 +93,17 @@ describe("isOutsideWindow", () => { expect(isOutsideWindow(viewport, 640, 800)).toBe(true); }); }); + +describe("beginDrag", () => { + it("clears the selection and marks the drag until it ends (#3334)", () => { + const text = document.body.appendChild(document.createElement("p")); + text.textContent = "selected before the drag"; + window.getSelection()!.selectAllChildren(text); + const end = beginDrag(); + expect(window.getSelection()?.rangeCount).toBe(0); + expect(document.documentElement.classList.length).toBe(1); + end(); + expect(document.documentElement.classList.length).toBe(0); + text.remove(); + }); +}); diff --git a/src/ui/pointerDrag.ts b/src/ui/pointerDrag.ts index 33858a0a3..69132955b 100644 --- a/src/ui/pointerDrag.ts +++ b/src/ui/pointerDrag.ts @@ -115,6 +115,19 @@ export function dragSession(data: DragData): DragSession { /** Movement before a press becomes a drag, px. */ const DRAG_THRESHOLD = 4; +/** + * Marks a pointer drag as running until the returned function is called: no text + * selection, and editable text out of hit testing. WebKit starts a selection in + * editable text the pointer passes over even when the press selected nothing + * (#3334), so every pointer-driven drag, built on `startPointerDrag` or not, uses this. + */ +export function beginDrag(): () => void { + window.getSelection()?.removeAllRanges(); + const html = document.documentElement; + html.classList.add(styles.dragging!); + return () => html.classList.remove(styles.dragging!); +} + /** Settings › Appearance › "Drag-and-drop with Alt pressed only" for tabs and stripe buttons. */ let requiresAlt = () => false; export function setDragRequiresAlt(fn: () => boolean) { @@ -150,6 +163,7 @@ export function startPointerDrag( if (e.button !== 0 || (!opts.always && requiresAlt() && !e.altKey)) return; const start = { x: e.clientX, y: e.clientY }; let ghost: HTMLElement | null = null; + let endDrag: (() => void) | null = null; const session = dragSession(data); const onMove = (ev: PointerEvent) => { if (!ghost) { @@ -172,7 +186,7 @@ export function startPointerDrag( height: `${r.height}px`, }); document.body.appendChild(ghost); - document.documentElement.classList.add(styles.dragging!); + endDrag = beginDrag(); opts.onStart?.(); } // Late moves after release may still precede the pointerup; ignore, don't cancel the drop. @@ -204,7 +218,7 @@ export function startPointerDrag( window.removeEventListener("pointercancel", onCancel); window.removeEventListener("keydown", onKey, true); ghost?.remove(); - document.documentElement.classList.remove(styles.dragging!); + endDrag?.(); }; const onCancel = () => { session.cancel(); diff --git a/src/workbench/toolwindows/ToolWindows.tsx b/src/workbench/toolwindows/ToolWindows.tsx index d848d0d40..73e63b79f 100644 --- a/src/workbench/toolwindows/ToolWindows.tsx +++ b/src/workbench/toolwindows/ToolWindows.tsx @@ -18,6 +18,7 @@ import { call, commands } from "@/platform/ipc"; import { shortcutLabel } from "@/platform/keymap"; import { useProject } from "@/platform/project"; import { + beginDrag, dragRequiresAlt, Icon, IconButton, @@ -366,6 +367,7 @@ function startStripeDrag(e: PointerEvent, id: string) { const button = e.currentTarget; const start = { x: e.clientX, y: e.clientY }; let ghost: HTMLElement | null = null; + let endDrag: (() => void) | null = null; const place = (x: number, y: number) => { if (ghost) ghost.style.transform = `translate(${x - start.x}px, ${y - start.y}px)`; }; @@ -385,6 +387,7 @@ function startStripeDrag(e: PointerEvent, id: string) { height: `${r.height}px`, }); document.body.appendChild(ghost); + endDrag = beginDrag(); useStripeDrag.setState({ id, drop: null }); } if (!(ev.buttons & 1)) return; @@ -400,6 +403,7 @@ function startStripeDrag(e: PointerEvent, id: string) { window.removeEventListener("keydown", onKey, true); if (!ghost) return; ghost.remove(); + endDrag?.(); const { drop } = useStripeDrag.getState(); useStripeDrag.setState({ id: null, drop: null }); dragEnded = true;