mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-18 16:08:55 +00:00
fix(ui): keep a file drag from selecting the text it passes over (#3358)
Dragging a file from the Project view selected the editor text the pointer passed over (#3334). WebKit keeps contenteditable text selectable whatever its user-select, and starts a selection there during a mouse drag even when the press selected nothing. beginDrag() now clears the selection and marks every pointer drag (startPointerDrag, the Project view's own file drag, tool window stripe buttons): no selection, and inputs, textareas and contenteditable out of hit testing (drop targets are their containers). Co-authored-by: akshathayadav378-boop <287247196+akshathayadav378-boop@users.noreply.github.com>
This commit is contained in:
co-authored by
akshathayadav378-boop
parent
f795aa3232
commit
b2c22ccec6
@@ -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);
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@ export {
|
||||
FILES_DRAG,
|
||||
REMOTE_FILES_DRAG,
|
||||
dragRequiresAlt,
|
||||
beginDrag,
|
||||
dragSession,
|
||||
dropZoneAt,
|
||||
registerDropTarget,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
+16
-2
@@ -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();
|
||||
|
||||
@@ -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<HTMLButtonElement>, 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<HTMLButtonElement>, 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<HTMLButtonElement>, 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;
|
||||
|
||||
Reference in New Issue
Block a user