The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.
TypeScript code-editorcross-platformdapdatabase-clientdebuggerdesktop-appdeveloper-toolsgithttp-clientideintellijlsprusttauriterminal
fix(agents): keep text selection inside the conversation (#437)
l0ng-ai195356c4b13e
diff --git a/src/styles/global.css b/src/styles/global.cssindex f311b3846..a764d88c6 100644--- a/src/styles/global.css+++ b/src/styles/global.css@@ -33,6 +33,11 @@ body {cursor: default;}+html[data-selection-lock] *:not([data-selection-scope], [data-selection-scope] *) {+ -webkit-user-select: none !important;+ user-select: none !important;+}+input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"]),textarea,[contenteditable="true"] {diff --git a/src/ui/useSelectionBoundary.test.tsx b/src/ui/useSelectionBoundary.test.tsxindex b11b17266..2980949d4 100644--- a/src/ui/useSelectionBoundary.test.tsx+++ b/src/ui/useSelectionBoundary.test.tsx@@ -62,7 +62,14 @@ it("preserves cross-message selection and lets the endpoint return inside", () =expect(selection.focusNode).toBe(text("Last message"));});-it("leaves selections originating outside the conversation alone", () => {+it("clamps a selection dragged up from another pane into the conversation", () => {+ const { text, select } = fixture();+ const selection = select(text("Branch or tag"), 6, text("Last message"), 4);+ expect(selection.toString()).toBe(" message");+ expect(selection.focusNode).toBe(text("Last message"));+});++it("leaves selections that do not touch the conversation alone", () => {const { text, select } = fixture();const selection = select(text("Reply to the agent"), 0, text("Branch or tag"), 13);expect(selection.toString()).toBe("Reply to the agentBranch or tag");diff --git a/src/ui/useSelectionBoundary.ts b/src/ui/useSelectionBoundary.tsindex ff464b362..8fcee3d71 100644--- a/src/ui/useSelectionBoundary.ts+++ b/src/ui/useSelectionBoundary.ts@@ -8,26 +8,39 @@ export function useSelectionBoundary(ref: RefObject<HTMLElement | null>): void {const doc = root.ownerDocument;const clamp = () => {const selection = doc.getSelection();- if (- !selection?.anchorNode ||- !selection.focusNode ||- selection.isCollapsed ||- !root.contains(selection.anchorNode) ||- root.contains(selection.focusNode)- )- return;+ if (!selection?.anchorNode || !selection.focusNode || selection.isCollapsed) return;+ const anchorIn = root.contains(selection.anchorNode);+ const focusIn = root.contains(selection.focusNode);+ if (anchorIn === focusIn) return;const bounds = doc.createRange();bounds.selectNodeContents(root);- const before = bounds.comparePoint(selection.focusNode, selection.focusOffset) < 0;- selection.setBaseAndExtent(- selection.anchorNode,- selection.anchorOffset,- root,- before ? 0 : root.childNodes.length,- );+ const outside = anchorIn+ ? { node: selection.focusNode, offset: selection.focusOffset }+ : { node: selection.anchorNode, offset: selection.anchorOffset };+ const edge = bounds.comparePoint(outside.node, outside.offset) < 0 ? 0 : root.childNodes.length;+ if (anchorIn) selection.setBaseAndExtent(selection.anchorNode, selection.anchorOffset, root, edge);+ else selection.setBaseAndExtent(root, edge, selection.focusNode, selection.focusOffset);};+ // While a drag starts in the pane, make everything outside it unselectable so the+ // browser never extends the selection there (see global.css).+ const html = doc.documentElement;+ const lock = (e: MouseEvent) => {+ if (e.button === 0) html.setAttribute("data-selection-lock", "");+ };+ const unlock = () => html.removeAttribute("data-selection-lock");+ root.setAttribute("data-selection-scope", "");+ root.addEventListener("mousedown", lock);+ doc.addEventListener("mouseup", unlock, true);+ doc.addEventListener("dragend", unlock, true);doc.addEventListener("selectionchange", clamp);- return () => doc.removeEventListener("selectionchange", clamp);+ return () => {+ root.removeAttribute("data-selection-scope");+ root.removeEventListener("mousedown", lock);+ doc.removeEventListener("mouseup", unlock, true);+ doc.removeEventListener("dragend", unlock, true);+ doc.removeEventListener("selectionchange", clamp);+ unlock();+ };}, [ref]);}