The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 03:00 UTC
fix(web): paste terminal text once and clear the shortcut on release (#19366)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Theo Browne committed
Oct 18, 2026 at 22:10 UTC
9f52dc28a9423e6b3201ed65b9820e5b8b52b887
2 files changed
+74
-6
apps/web/src/terminal/ghostty/surface.test.ts
+49
@@ -777,6 +777,55 @@ describe("GhosttyTerminalSurface visibility", () => {
777
expect(readText).not.toHaveBeenCalled();
778
});
779
780
+ function paste(surface: GhosttyTerminalSurface, text: string) {
781
+ surface.input.dispatchEvent(
782
+ Object.assign(new Event("paste", { cancelable: true }), {
783
+ clipboardData: { getData: () => text },
784
+ }),
785
+ );
786
+ }
787
+
788
+ it.each(["native", "clipboard"])("pastes once when %s arrives first", async (first) => {
789
+ const harness = createHarness();
790
+ const read = Promise.resolve("echo hi");
791
+ vi.stubGlobal("navigator", {
792
+ platform: "MacIntel",
793
+ clipboard: { readText: () => read },
794
+ });
795
+ const surface = await harness.create({ beforeKey: () => true });
796
+
797
+ key(surface, "v", "KeyV", { metaKey: true });
798
+ if (first === "native") paste(surface, "echo hi");
799
+ await read;
800
+ if (first === "clipboard") paste(surface, "echo hi");
801
+
802
+ expect(harness.onData.mock.calls).toEqual([["echo hi"]]);
803
+ });
804
+
805
+ it.each(["V keyup", "blur", "Meta keyup without V keyup"])(
806
+ "accepts a later identical menu paste after %s",
807
+ async (end) => {
808
+ const harness = createHarness();
809
+ const read = Promise.resolve("echo hi");
810
+ vi.stubGlobal("navigator", {
811
+ platform: "MacIntel",
812
+ clipboard: { readText: () => read },
813
+ });
814
+ const surface = await harness.create({ beforeKey: () => true });
815
+
816
+ key(surface, "v", "KeyV", { metaKey: true });
817
+ await read;
818
+ expect(harness.onData.mock.calls).toEqual([["echo hi"]]);
819
+ if (end === "blur") surface.input.dispatchEvent(new Event("blur"));
820
+ else if (end === "V keyup") key(surface, "v", "KeyV", { metaKey: true }, "keyup");
821
+ // macOS can omit V keyup while Command is held.
822
+ else key(surface, "Meta", "MetaLeft", {}, "keyup");
823
+ paste(surface, "echo hi");
824
+
825
+ expect(harness.onData.mock.calls).toEqual([["echo hi"], ["echo hi"]]);
826
+ },
827
+ );
828
+
829
it("starts a selection when dragging from a link", async () => {
830
const harness = createHarness();
831
const onLinkActivate = vi.fn();
apps/web/src/terminal/ghostty/surface.ts
+25
-6
@@ -580,6 +580,9 @@ export class GhosttyTerminalSurface {
580
private theme: GhosttyTheme;
581
private readonly suppressedKeyCodes = new Set<string>();
582
private pasteShortcutToken = 0;
583
+ // Chromium can deliver native paste after the clipboard read resolves.
584
+ // Suppress its duplicate only for the current shortcut gesture.
585
+ private pasteShortcutGesture: { code: string; readText: string } | null = null;
586
private copyShortcutToken = 0;
587
private clearSelectionAfterCopy = false;
588
private primedCopySelection = "";
@@ -1137,19 +1140,20 @@ export class GhosttyTerminalSurface {
1140
}
1141
if (isTerminalPasteShortcut(event)) {
1142
this.suppressedKeyCodes.add(event.code);
1143
+ const gesture = { code: event.code, readText: "" };
1144
+ this.pasteShortcutGesture = gesture;
1145
const clipboard = navigator.clipboard;
1146
if (typeof clipboard?.readText === "function") {
1142
- // Race the async clipboard read against the browser's own paste event:
1143
- // the native event (dispatched synchronously with the default action)
1144
- // always claims the token first when it fires, and the read covers
1145
- // browsers whose paste shortcut produces no paste event. Not preventing
1146
- // the default keeps the native path alive when the read is denied.
1147
+ // Keep native paste available if the read is denied. The token drops
1148
+ // a late read; the gesture drops a native duplicate when the read wins.
1149
const token = ++this.pasteShortcutToken;
1150
void clipboard.readText().then(
1151
(text) => {
1152
if (this.disposed || this.pasteShortcutToken !== token) return;
1153
this.pasteShortcutToken += 1;
1152
- if (text.length > 0) this.options.onData(this.core.encodePaste(text));
1154
+ if (text.length === 0) return;
1155
+ if (this.pasteShortcutGesture === gesture) gesture.readText = text;
1156
+ this.options.onData(this.core.encodePaste(text));
1157
},
1158
() => {},
1159
);
@@ -1166,6 +1170,15 @@ export class GhosttyTerminalSurface {
1170
};
1171
1172
private readonly onKeyUp = (event: KeyboardEvent) => {
1173
+ // macOS can omit V keyup while Command is held.
1174
+ if (
1175
+ event.code === this.pasteShortcutGesture?.code ||
1176
+ event.key === "Meta" ||
1177
+ event.key === "Control" ||
1178
+ event.key === "Shift"
1179
+ ) {
1180
+ this.pasteShortcutGesture = null;
1181
+ }
1182
if (this.suppressedKeyCodes.delete(event.code)) return;
1183
if (isTerminalCompositionKey(event, this.composing)) {
1184
return;
@@ -1187,6 +1200,7 @@ export class GhosttyTerminalSurface {
1200
1201
private readonly onBlur = () => {
1202
this.focused = false;
1203
+ this.pasteShortcutGesture = null;
1204
this.refreshHoveredLink();
1205
// Suppressions survive blur deliberately: a shortcut that moves focus (for
1206
// example terminal-toggle) must still swallow its own keyup if focus comes
@@ -1244,6 +1258,11 @@ export class GhosttyTerminalSurface {
1258
event.preventDefault();
1259
const data = event.clipboardData?.getData("text/plain") ?? "";
1260
if (data.length === 0) return;
1261
+ if (data === this.pasteShortcutGesture?.readText) {
1262
+ // The shortcut's clipboard read already sent this paste.
1263
+ this.pasteShortcutGesture = null;
1264
+ return;
1265
+ }
1266
// The native paste won the race with actual text; a pending clipboard read
1267
// must not double. An empty native paste leaves the read as the only path.
1268
this.pasteShortcutToken += 1;