Skip to content

Commit 4f9cb7a

Browse files
t3dotggclaude
andcommitted
fix(web): terminal paste no longer lands twice in Chromium
The paste shortcut races navigator.clipboard.readText() against the browser's native paste event. The token only covered the native event landing first. When Chromium routes the paste through its menu (installed web apps, Electron), the read resolves first and the native paste that follows was also sent. The read now records the text it sent on the shortcut's gesture, and onPaste drops the one matching native paste. The gesture ends on the shortcut key's keyup, the next shortcut, blur, or that paste, so a later Edit > Paste of the same text still lands. Design from r4iju's #13357 and the #13346 triage. Fixes #13346 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ffceb61 commit 4f9cb7a

2 files changed

Lines changed: 67 additions & 6 deletions

File tree

‎apps/web/src/terminal/ghostty/surface.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,50 @@ describe("GhosttyTerminalSurface visibility", () => {
760760
expect(readText).not.toHaveBeenCalled();
761761
});
762762

763+
it("pastes once per shortcut whichever of the clipboard read and native paste lands first", async () => {
764+
const harness = createHarness();
765+
const readText = vi.fn(async () => "echo hi");
766+
vi.stubGlobal("navigator", { platform: "MacIntel", clipboard: { readText } });
767+
const surface = await harness.create({ beforeKey: () => true });
768+
const paste = (text: string) =>
769+
surface.input.dispatchEvent(
770+
Object.assign(new Event("paste", { cancelable: true }), {
771+
clipboardData: { getData: () => text },
772+
}),
773+
);
774+
const settleRead = async (reads: number) => {
775+
expect(readText).toHaveBeenCalledTimes(reads);
776+
await readText.mock.results[reads - 1]?.value;
777+
};
778+
779+
// Read first (Chromium menu paste): the native paste that follows is dropped.
780+
key(surface, "v", "KeyV", { metaKey: true });
781+
await settleRead(1);
782+
paste("echo hi");
783+
key(surface, "v", "KeyV", { metaKey: true }, "keyup");
784+
expect(harness.onData).toHaveBeenCalledTimes(1);
785+
786+
// Native first: the read that settles afterwards is dropped.
787+
key(surface, "v", "KeyV", { metaKey: true });
788+
paste("echo hi");
789+
await settleRead(2);
790+
key(surface, "v", "KeyV", { metaKey: true }, "keyup");
791+
expect(harness.onData).toHaveBeenCalledTimes(2);
792+
793+
// A shortcut served only by the read must not swallow a later Edit → Paste
794+
// of the same text, after the keyup or after a blur.
795+
key(surface, "v", "KeyV", { metaKey: true });
796+
await settleRead(3);
797+
key(surface, "v", "KeyV", { metaKey: true }, "keyup");
798+
paste("echo hi");
799+
key(surface, "v", "KeyV", { metaKey: true });
800+
await settleRead(4);
801+
surface.input.dispatchEvent(new Event("blur"));
802+
paste("echo hi");
803+
expect(harness.onData).toHaveBeenCalledTimes(6);
804+
expect(new Set(harness.onData.mock.calls.map(([data]) => data))).toEqual(new Set(["echo hi"]));
805+
});
806+
763807
it("starts a selection when dragging from a link", async () => {
764808
const harness = createHarness();
765809
const onLinkActivate = vi.fn();

‎apps/web/src/terminal/ghostty/surface.ts‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,11 @@ export class GhosttyTerminalSurface {
627627
private theme: GhosttyTheme;
628628
private readonly suppressedKeyCodes = new Set<string>();
629629
private pasteShortcutToken = 0;
630+
// The paste shortcut whose native paste event may still arrive, and the text
631+
// its clipboard read already sent ("" until then). Chromium can deliver that
632+
// event after the read resolves, but before the shortcut key's keyup, so the
633+
// gesture ends on that keyup, the next shortcut, blur, or the matching paste.
634+
private pasteShortcutGesture: { code: string; readText: string } | null = null;
630635
private copyShortcutToken = 0;
631636
private clearSelectionAfterCopy = false;
632637
private primedCopySelection = "";
@@ -1212,19 +1217,24 @@ export class GhosttyTerminalSurface {
12121217
}
12131218
if (isTerminalPasteShortcut(event)) {
12141219
this.suppressedKeyCodes.add(event.code);
1220+
const gesture = { code: event.code, readText: "" };
1221+
this.pasteShortcutGesture = gesture;
12151222
const clipboard = navigator.clipboard;
12161223
if (typeof clipboard?.readText === "function") {
1217-
// Race the async clipboard read against the browser's own paste event:
1218-
// the native event (dispatched synchronously with the default action)
1219-
// always claims the token first when it fires, and the read covers
1220-
// browsers whose paste shortcut produces no paste event. Not preventing
1221-
// the default keeps the native path alive when the read is denied.
1224+
// Race the async clipboard read against the browser's own paste event.
1225+
// The read covers browsers whose paste shortcut produces no paste
1226+
// event; not preventing the default keeps the native path alive when
1227+
// the read is denied. Whichever lands first wins: a native paste
1228+
// claims the token, and a read records its text on the gesture so
1229+
// onPaste drops the native paste that follows (Chromium menu paste).
12221230
const token = ++this.pasteShortcutToken;
12231231
void clipboard.readText().then(
12241232
(text) => {
12251233
if (this.disposed || this.pasteShortcutToken !== token) return;
12261234
this.pasteShortcutToken += 1;
1227-
if (text.length > 0) this.options.onData(this.core.encodePaste(text));
1235+
if (text.length === 0) return;
1236+
if (this.pasteShortcutGesture === gesture) gesture.readText = text;
1237+
this.options.onData(this.core.encodePaste(text));
12281238
},
12291239
() => {
12301240
// Clipboard read denied; the native paste event remains the path.
@@ -1243,6 +1253,7 @@ export class GhosttyTerminalSurface {
12431253
};
12441254

12451255
private readonly onKeyUp = (event: KeyboardEvent) => {
1256+
if (event.code === this.pasteShortcutGesture?.code) this.pasteShortcutGesture = null;
12461257
if (this.suppressedKeyCodes.delete(event.code)) return;
12471258
if (isTerminalCompositionKey(event, this.composing)) {
12481259
return;
@@ -1264,6 +1275,7 @@ export class GhosttyTerminalSurface {
12641275

12651276
private readonly onBlur = () => {
12661277
this.focused = false;
1278+
this.pasteShortcutGesture = null;
12671279
this.refreshHoveredLink();
12681280
// Suppressions survive blur deliberately: a shortcut that moves focus (for
12691281
// example terminal-toggle) must still swallow its own keyup if focus comes
@@ -1323,6 +1335,11 @@ export class GhosttyTerminalSurface {
13231335
event.preventDefault();
13241336
const data = event.clipboardData?.getData("text/plain") ?? "";
13251337
if (data.length === 0) return;
1338+
if (data === this.pasteShortcutGesture?.readText) {
1339+
// The shortcut's clipboard read already sent this paste.
1340+
this.pasteShortcutGesture = null;
1341+
return;
1342+
}
13261343
// The native paste won the race with actual text; a pending clipboard read
13271344
// must not double. An empty native paste leaves the read as the only path.
13281345
this.pasteShortcutToken += 1;

0 commit comments

Comments
 (0)