Skip to content

Commit 7d4f6eb

Browse files
committed
fix(web): do not wipe IME composition when clearing copy primer
clearPrimedTerminalCopyInput ran on every non-copy keydown before the composition early-return, blanking the hidden textarea that holds the IME candidate. Skip the clear until after composing/Process/229 keys.
1 parent bd358ca commit 7d4f6eb

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ghosttyMouseButton,
1111
isTerminalAltGraphText,
1212
isTerminalCompositionCommitInput,
13+
isTerminalCompositionKey,
1314
isTerminalCopyShortcut,
1415
isTerminalLinkPointerGesture,
1516
isTerminalPasteShortcut,
@@ -351,6 +352,28 @@ describe("isTerminalCompositionCommitInput", () => {
351352
});
352353
});
353354

355+
describe("isTerminalCompositionKey", () => {
356+
const event = (
357+
overrides: Partial<Pick<KeyboardEvent, "isComposing" | "key" | "keyCode">> = {},
358+
) => ({
359+
isComposing: false,
360+
key: "a",
361+
keyCode: 65,
362+
...overrides,
363+
});
364+
365+
it("treats in-progress IME keydowns as composition so the copy primer cannot wipe them", () => {
366+
expect(isTerminalCompositionKey(event({ isComposing: true }), false)).toBe(true);
367+
expect(isTerminalCompositionKey(event(), true)).toBe(true);
368+
expect(isTerminalCompositionKey(event({ key: "Process" }), false)).toBe(true);
369+
expect(isTerminalCompositionKey(event({ keyCode: 229 }), false)).toBe(true);
370+
});
371+
372+
it("lets ordinary keydowns clear a primed copy", () => {
373+
expect(isTerminalCompositionKey(event(), false)).toBe(false);
374+
});
375+
});
376+
354377
describe("application mouse reporting", () => {
355378
const event = (overrides: Partial<Parameters<typeof shouldReportTerminalMouse>[1]> = {}) => ({
356379
ctrlKey: false,

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@ export function isTerminalCompositionCommitInput(event: Pick<InputEvent, "inputT
392392
);
393393
}
394394

395+
/** IME keydowns must not touch the hidden textarea; it holds the candidate. */
396+
export function isTerminalCompositionKey(
397+
event: Pick<KeyboardEvent, "isComposing" | "key" | "keyCode">,
398+
composing: boolean,
399+
): boolean {
400+
return event.isComposing || composing || event.key === "Process" || event.keyCode === 229;
401+
}
402+
395403
export function isTerminalAltGraphText(
396404
event: Pick<KeyboardEvent, "getModifierState" | "key">,
397405
): boolean {
@@ -1026,7 +1034,6 @@ export class GhosttyTerminalSurface {
10261034
this.suppressedKeyCodes.add(event.code);
10271035
return;
10281036
}
1029-
clearPrimedTerminalCopyInput(this.input);
10301037
if (isTerminalPasteShortcut(event)) {
10311038
this.suppressedKeyCodes.add(event.code);
10321039
const clipboard = navigator.clipboard;
@@ -1051,10 +1058,12 @@ export class GhosttyTerminalSurface {
10511058
return;
10521059
}
10531060
// keyCode 229 is Safari's only signal that this keydown opens an IME
1054-
// composition; encoding it would double the committed text.
1055-
if (event.isComposing || this.composing || event.key === "Process" || event.keyCode === 229) {
1061+
// composition; encoding it would double the committed text. Do not blank
1062+
// the textarea first: onInput leaves the in-progress candidate there.
1063+
if (isTerminalCompositionKey(event, this.composing)) {
10561064
return;
10571065
}
1066+
clearPrimedTerminalCopyInput(this.input);
10581067
const data = this.core.encodeKey(event);
10591068
if (data.length === 0) return;
10601069
this.suppressedKeyCodes.delete(event.code);
@@ -1066,7 +1075,7 @@ export class GhosttyTerminalSurface {
10661075
private readonly onKeyUp = (event: KeyboardEvent) => {
10671076
this.updateLinkModifier(event);
10681077
if (this.suppressedKeyCodes.delete(event.code)) return;
1069-
if (event.isComposing || this.composing || event.key === "Process" || event.keyCode === 229) {
1078+
if (isTerminalCompositionKey(event, this.composing)) {
10701079
return;
10711080
}
10721081
// Ghostty's encoder only emits release codes when the terminal enabled the

0 commit comments

Comments
 (0)