fix(web): add copying terminal selection with ctrl+c in the web app #5638
MergedUtkarshUsername(GitHub profile, opens in a new tab)merged 9 commits into
main fromrefs/pull/5638/headmerged by shivamhwp(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
UtkarshUsername(GitHub profile, opens in a new tab) changed the title
UtkarshUsername(GitHub profile, opens in a new tab) changed the title
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
UtkarshUsername(GitHub profile, opens in a new tab) changed the title
UtkarshUsername(GitHub profile, opens in a new tab) changed the title
- apps/web/src/terminal/ghostty/surface.ts line 955Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -901,4 +900,11 @@901900 return;902901 }903902 if (isTerminalCopyShortcut(event) && this.hasSelection()) {904- event.preventDefault();903+ // A plain Ctrl+C/Cmd+C fires the browser's native copy event, caught in904+ // onCopyEvent; not preventing the default keeps that path alive. The905+ // Shift variant has no native event (Chrome binds Ctrl+Shift+C to906+ // inspect), so synthesize one with execCommand("copy").907+ if (event.shiftKey) {908+ event.preventDefault();909+ document.execCommand("copy");910+ }
- apps/web/src/terminal/ghostty/surface.ts line 954Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -903,2 +902,20 @@903902 if (isTerminalCopyShortcut(event) && this.hasSelection()) {904- event.preventDefault();903+ // A plain Ctrl+C/Cmd+C fires the browser's native copy event, caught in904+ // onCopyEvent; not preventing the default keeps that path alive. WebKit905+ // omits the keyboard copy event without a DOM selection, so race the906+ // clipboard write against it the same way paste races its read. The907+ // Shift variant has no native event (Chrome binds Ctrl+Shift+C to908+ // inspect), so synthesize one with execCommand("copy").909+ if (event.shiftKey) {910+ event.preventDefault();911+ document.execCommand("copy");912+ } else {913+ const clipboard = navigator.clipboard;914+ if (typeof clipboard?.writeText === "function") {915+ // The native copy event (dispatched with the default action) always916+ // wins when it fires; the write covers browsers whose shortcut917+ // produces no copy event.918+ void clipboard.writeText(this.getSelection()).catch(() => {919+ // Clipboard write denied; the native copy event remains the path.920+ });921+ }
- apps/web/src/terminal/ghostty/surface.ts line 955Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -904,0 +911,21 @@911+ event.preventDefault();912+ document.execCommand("copy");913+ } else {914+ const clipboard = navigator.clipboard;915+ if (typeof clipboard?.writeText === "function") {916+ // Defer the write past the default action: the native copy event917+ // (dispatched synchronously with the default action) claims the918+ // token first when it fires, and the write covers browsers whose919+ // shortcut produces no copy event. Skipping a write the native920+ // event already handled stops a stale resolution from clobbering a921+ // clipboard the user filled after this copy.922+ const token = ++this.copyShortcutToken;923+ const selection = this.getSelection();924+ void Promise.resolve().then(() => {925+ if (this.disposed || this.copyShortcutToken !== token) return;926+ void clipboard.writeText(selection).catch(() => {927+ // Clipboard write denied; the native copy event remains the path.928+ });929+ });930+ }931+ }
- apps/web/src/terminal/ghostty/surface.ts line 942Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -904,0 +917,21 @@917+ // Shift chord are copy-only, so they keep the selection.918+ this.clearSelectionAfterCopy = !isMacPlatform(navigator.platform);919+ const clipboard = navigator.clipboard;920+ if (typeof clipboard?.writeText === "function") {921+ // Defer the write past the default action: the native copy event922+ // (dispatched synchronously with the default action) claims the923+ // token first when it fires, and the write covers browsers whose924+ // shortcut produces no copy event. Skipping a write the native925+ // event already handled stops a stale resolution from clobbering a926+ // clipboard the user filled after this copy.927+ const token = ++this.copyShortcutToken;928+ const selection = this.getSelection();929+ void Promise.resolve().then(() => {930+ if (this.disposed || this.copyShortcutToken !== token) return;931+ void clipboard.writeText(selection).then(932+ () => {933+ if (this.clearSelectionAfterCopy) {934+ this.clearSelectionAfterCopy = false;935+ this.clearSelection();936+ }937+ },apps/web/src/terminal/ghostty/surface.ts line 951Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -904,0 +920,21 @@920+ if (typeof clipboard?.writeText === "function") {921+ // Defer the write past the default action: the native copy event922+ // (dispatched synchronously with the default action) claims the923+ // token first when it fires, and the write covers browsers whose924+ // shortcut produces no copy event. Skipping a write the native925+ // event already handled stops a stale resolution from clobbering a926+ // clipboard the user filled after this copy.927+ const token = ++this.copyShortcutToken;928+ const selection = this.getSelection();929+ void Promise.resolve().then(() => {930+ if (this.disposed || this.copyShortcutToken !== token) return;931+ void clipboard.writeText(selection).then(932+ () => {933+ if (this.clearSelectionAfterCopy) {934+ this.clearSelectionAfterCopy = false;935+ this.clearSelection();936+ }937+ },938+ () => {939+ // Clipboard write denied; the native copy event remains the path.940+ },
- apps/web/src/terminal/ghostty/surface.ts line 946OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -904,0 +926,21 @@926+ // shortcut produces no copy event. Skipping a write the native927+ // event already handled stops a stale resolution from clobbering a928+ // clipboard the user filled after this copy.929+ const token = ++this.copyShortcutToken;930+ const selection = this.getSelection();931+ void Promise.resolve().then(() => {932+ if (this.disposed || this.copyShortcutToken !== token) return;933+ void clipboard.writeText(selection).then(934+ () => {935+ // The write may have been superseded while in flight; only936+ // touch the selection if this gesture still owns the token.937+ if (this.disposed || this.copyShortcutToken !== token) return;938+ if (this.clearSelectionAfterCopy) {939+ this.clearSelectionAfterCopy = false;940+ this.clearSelection();941+ }942+ },943+ () => {944+ // The write failed and the native event has already had its945+ // chance, so nothing copied and no clear is owed.946+ this.clearSelectionAfterCopy = false;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/ThreadTerminalDrawer.tsx line 689OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -696,3 +684,6 @@696684 return;697685 }698686 clearSelectionAction();687+ // A copy shortcut that clears the selection (Ctrl+C) must also close688+ // the context menu that appears with the selection.689+ void localApi?.contextMenu.close();apps/web/src/contextMenuFallback.ts line 328Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -299,3 +319,4 @@299319 document.addEventListener("pointerdown", onPointerDown, true);300320 document.addEventListener("contextmenu", onContextMenu, true);301321 openMenu(items, position?.x ?? 0, position?.y ?? 0, 0);322+ activeContextMenuDismiss = dismiss;
shivamhwp(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
b264d219-b3c7-4556-b449-c663bd65b0c7You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share