feat(web): copy chat and terminal selections automatically, Herdr-style #9409
ClosedSotoAugusto(GitHub profile, opens in a new tab)wants to merge 6 commits into
main fromrefs/pull/9409/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/hooks/useCopyOnSelect.ts line 37OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +17,21 @@17+ * and shows the "copied to clipboard" toast.18+ *19+ * Keyboard selections never trigger it (no mouseup), editable and20+ * interactive targets are skipped, and consecutive copies of identical text21+ * are deduplicated so clicking around a stable selection stays quiet. The22+ * toast only fires after a successful clipboard write — never optimistically.23+ */24+export function useCopyOnSelect(container: HTMLElement | null): void {25+ const copyOnSelect = useClientSettings((settings) => settings.copyOnSelect);26+ const showToast = useClientSettings((settings) => settings.copyOnSelectToast);27+ const lastCopiedRef = React.useRef<string | null>(null);28+ const settingsRef = React.useRef({ copyOnSelect, showToast });29+ settingsRef.current = { copyOnSelect, showToast };30+31+ React.useEffect(() => {32+ if (!container) return;33+ const onMouseUp = (event: MouseEvent) => {34+ if (!settingsRef.current.copyOnSelect) return;35+ if (!shouldAutoCopyOnMouseUp(event)) return;36+ if (isCopyOnSelectInteractiveTarget(event.target)) return;37+ const text = getCopyableDomSelectionText(window.getSelection(), container);
- @@ -0,0 +33,21 @@33+/**34+ * Terminal selections use `\r\n` line endings and can carry blank leading or35+ * trailing lines from block drag-selects. Returns the copyable text, or null36+ * when there is nothing worth copying.37+ */38+export function normalizeTerminalSelectionText(text: string): string | null {39+ const normalized = text.replace(/\r\n/g, "\n").replace(/^\n+|\n+$/g, "");40+ return normalized.length > 0 ? text : null;41+}42+43+function nodeInEditable(node: Node | null): boolean {44+ let current: Node | null = node;45+ while (current !== null) {46+ if (current.nodeType === 1) {47+ const element = current as Element;48+ if (element.closest(COPY_ON_SELECT_INTERACTIVE_SELECTOR) !== null) return true;49+ }50+ current = current.parentNode;51+ }52+ return false;53+}apps/web/src/hooks/useCopyOnSelect.ts line 121Resolvedby SotoAugusto(GitHub profile, opens in a new tab)@@ -0,0 +36,21 @@36+ if (isCopyOnSelectInteractiveTarget(event.target)) return;37+ const text = getCopyableDomSelectionText(window.getSelection(), container);38+ if (text === null || text === lastCopiedRef.current) return;39+ void writeTextToClipboard(text, "selection").then(40+ (didCopy) => {41+ if (!didCopy) return;42+ lastCopiedRef.current = text;43+ if (settingsRef.current.showToast) {44+ toastManager.add({ type: "success", title: "Copied to clipboard" });45+ }46+ },47+ () => {48+ // Silent on failure: no false toast when the clipboard API is49+ // unavailable (e.g. plain-HTTP remote sessions).50+ },51+ );52+ };53+ container.addEventListener("mouseup", onMouseUp);54+ return () => {55+ container.removeEventListener("mouseup", onMouseUp);56+ };
- apps/web/src/hooks/useCopyOnSelect.ts lines 62 to 74OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +54,21 @@54+ gestureStartRef.current = { snapshot: null, armed: false };55+ if (!armed) return;56+ if (!settingsRef.current.copyOnSelect) return;57+ if (!shouldAutoCopyOnMouseUp(event)) return;58+ if (isCopyOnSelectInteractiveTarget(event.target)) return;59+ const selection = window.getSelection();60+ if (sameDomSelectionSnapshot(startSnapshot, snapshotDomSelection(selection))) return;61+ const text = getCopyableDomSelectionText(selection, container);62+ if (text === null || text === lastCopiedRef.current) return;63+ void writeTextToClipboard(text, "selection").then(64+ (didCopy) => {65+ if (!didCopy) return;66+ lastCopiedRef.current = text;67+ if (settingsRef.current.showToast) {68+ toastManager.add({ type: "success", title: "Copied to clipboard" });69+ }70+ },71+ () => {72+ // Silent on failure: no false toast when the clipboard API is73+ // unavailable (e.g. plain-HTTP remote sessions).74+ },
- apps/web/src/lib/copyOnSelect.ts lines 18 to 18OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,18 @@1+/**2+ * Copy-on-select helpers (Herdr-style: selecting text copies it and shows a3+ * toast). The DOM side owns reading `window.getSelection()`; the terminal4+ * side owns reading the canvas surface. Both sides share the emptiness and5+ * gesture guards here so neither path copies stale or empty selections.6+ *7+ * Used by `useCopyOnSelect` (chat timeline) and `ThreadTerminalDrawer`.8+ */9+10+/**11+ * Subtrees whose text must never auto-copy: form fields and explicitly12+ * opted-out regions. Links, buttons, and other clickable chips are13+ * intentionally NOT excluded — chat is full of selectable controls, and a14+ * drag across them is still a real selection. Clicks on those controls15+ * without a drag are filtered by gesture-identity comparison instead.16+ */17+export const COPY_ON_SELECT_EDITABLE_SELECTOR =18+ "input, textarea, select, [contenteditable], [data-no-copy-on-select]";
- apps/web/src/lib/copyOnSelect.ts line 18OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,18 @@1+/**2+ * Copy-on-select helpers (Herdr-style: selecting text copies it and shows a3+ * toast). The DOM side owns reading `window.getSelection()`; the terminal4+ * side owns reading the canvas surface. Both sides share the emptiness and5+ * gesture guards here so neither path copies stale or empty selections.6+ *7+ * Used by `useCopyOnSelect` (chat timeline) and `ThreadTerminalDrawer`.8+ */9+10+/**11+ * Subtrees whose text must never auto-copy: form fields and explicitly12+ * opted-out regions. Links, buttons, and other clickable chips are13+ * intentionally NOT excluded — chat is full of selectable controls, and a14+ * drag across them is still a real selection. Clicks on those controls15+ * without a drag are filtered by gesture-identity comparison instead.16+ */17+export const COPY_ON_SELECT_EDITABLE_SELECTOR =18+ "input, textarea, select, [contenteditable], [data-no-copy-on-select]";apps/web/src/hooks/useCopyOnSelect.ts line 89OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +69,21 @@69+ const text = getCopyableDomSelectionText(selection, container);70+ if (text === null || text === lastCopiedRef.current) return;71+ // Reserve before the await so a repeated gesture while this write is72+ // pending can't issue a second write and toast.73+ lastCopiedRef.current = text;74+ void writeTextToClipboard(text, "selection").then(75+ (didCopy) => {76+ if (!didCopy) {77+ if (lastCopiedRef.current === text) lastCopiedRef.current = null;78+ return;79+ }80+ if (settingsRef.current.showToast) {81+ toastManager.add({ type: "success", title: "Copied to clipboard" });82+ }83+ },84+ () => {85+ // Silent on failure: no false toast when the clipboard API is86+ // unavailable (e.g. plain-HTTP remote sessions).87+ if (lastCopiedRef.current === text) lastCopiedRef.current = null;88+ },89+ );
juliusmarminge added labelsmacroscope-review
maria-rcks closed this
Sign in to join this conversation.




macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed