diff --git a/packages/app/src/terminal/runtime/terminal-emulator-runtime.browser.test.ts b/packages/app/src/terminal/runtime/terminal-emulator-runtime.browser.test.ts index bf044237b..a875ae5a3 100644 --- a/packages/app/src/terminal/runtime/terminal-emulator-runtime.browser.test.ts +++ b/packages/app/src/terminal/runtime/terminal-emulator-runtime.browser.test.ts @@ -115,6 +115,26 @@ interface CreateTerminalHostInput { isMac?: boolean; } +/** Text typed into xterm's textarea, as the browser reports a typed character. */ +function typeText(mounted: MountedTerminal, text: string): void { + mounted.host + .querySelector(".xterm-helper-textarea") + ?.dispatchEvent( + new InputEvent("input", { data: text, inputType: "insertText", bubbles: true }), + ); +} + +/** A native paste of plain text into xterm's textarea. */ +function pasteText(mounted: MountedTerminal, text: string): void { + const clipboardData = new DataTransfer(); + clipboardData.setData("text/plain", text); + mounted.host + .querySelector(".xterm-helper-textarea") + ?.dispatchEvent( + new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }), + ); +} + function createTerminalHost(input: CreateTerminalHostInput): MountedTerminal { const root = document.createElement("div"); root.style.width = `${input.width}px`; @@ -824,6 +844,23 @@ describe("incremental replay lifecycle", () => { expect(mounted.inputs[0]?.startsWith("\x1b]4;1;rgb:")).toBe(true); }); + it("keeps what the user types, but not pastes, while a replay is still parsing", async () => { + const mounted = createTerminalHost({ width: 720, height: 360 }); + await waitFor({ predicate: () => mounted.sizes.length > 0 }); + mounted.runtime.replay({ type: "begin" }); + mounted.runtime.replay({ type: "data", data: terminalOutput("\x1b]4;1;?\x07restored") }); + mounted.runtime.replay({ type: "end" }); + + typeText(mounted, "a"); + pasteText(mounted, "echo one\necho two\n"); + typeText(mounted, "f"); + await new Promise((resolve) => + mounted.runtime.write({ data: terminalOutput("live"), onCommitted: resolve }), + ); + + expect(mounted.inputs).toEqual(["a", "f"]); + }); + it("measures emoji clusters with the daemon's grapheme widths and copies them intact", async () => { const mounted = createTerminalHost({ width: 720, height: 360 }); const text = "A😀B❤️C👋🏽D👨‍👩‍👧E"; diff --git a/packages/app/src/terminal/runtime/terminal-emulator-runtime.ts b/packages/app/src/terminal/runtime/terminal-emulator-runtime.ts index ae74a4945..3aaf1e139 100644 --- a/packages/app/src/terminal/runtime/terminal-emulator-runtime.ts +++ b/packages/app/src/terminal/runtime/terminal-emulator-runtime.ts @@ -124,6 +124,7 @@ interface TerminalEmulatorRuntimeDisposables { clearFitTimeouts: () => void; removeFontListeners: () => void; removeFilePasteListener: () => void; + removeUserInputListeners: () => void; restoreDocumentStyles: () => void; restoreViewportStyles: () => void; disposeFitAddon: () => void; @@ -157,6 +158,10 @@ const isAppleHandheld = const FIT_TIMEOUT_DELAYS_MS = [0, 16, 48, 120, 250, 500, 1_000, 2_000]; const OUTPUT_OPERATION_TIMEOUT_MS = 5_000; const EMPTY_TERMINAL_OUTPUT = new Uint8Array(0); +// DOM events xterm turns into typed data on its textarea. Pastes stay suppressed during a +// replay: until the snapshot restores bracketed paste mode, a multi-line paste would run +// line by line in the shell. +const USER_INPUT_EVENT_TYPES = ["keydown", "keypress", "input"] as const; const RESET_TERMINAL_OUTPUT = new Uint8Array([0x1b, 0x63]); const terminalOutputEncoder = new TextEncoder(); @@ -313,6 +318,8 @@ export class TerminalEmulatorRuntime { private hasUngatedWrites = false; private readonly inputModeDecoder = new TextDecoder(); private suppressInput = false; + // The latest user input event the host saw; its eventPhase is NONE once dispatch ends. + private userInputEvent: Event | null = null; private readonly inputModeTracker = new TerminalInputModeTracker(); private lastInputModeState: TerminalInputModeState = this.inputModeTracker.getState(); private themeBackgroundElements: HTMLElement[] = []; @@ -739,8 +746,17 @@ export class TerminalEmulatorRuntime { fitAndEmitResize({ forceRefresh: true, shouldClaim: false }); + // xterm reports what the user types and the replies it generates while parsing + // through the same onData. Replies only come out of parsing a write, never while a + // user input event is dispatching, so typing survives a replay that suppresses them. + const recordUserInputEvent = (event: Event): void => { + this.userInputEvent = event; + }; + for (const type of USER_INPUT_EVENT_TYPES) { + input.host.addEventListener(type, recordUserInputEvent, true); + } const inputDisposable = terminal.onData((data) => { - if (this.suppressInput) { + if (this.suppressInput && !this.isDispatchingUserInput()) { return; } this.callbacks.onInput?.(data); @@ -844,6 +860,12 @@ export class TerminalEmulatorRuntime { removeFilePasteListener: () => { input.host.removeEventListener("paste", filePasteHandler, true); }, + removeUserInputListeners: () => { + for (const type of USER_INPUT_EVENT_TYPES) { + input.host.removeEventListener(type, recordUserInputEvent, true); + } + this.userInputEvent = null; + }, restoreDocumentStyles, restoreViewportStyles, disposeFitAddon: () => { @@ -873,6 +895,7 @@ export class TerminalEmulatorRuntime { disposables.clearFitTimeouts(); disposables.removeFontListeners(); disposables.removeFilePasteListener(); + disposables.removeUserInputListeners(); disposables.disposeFitAddon(); disposables.disposeWebglAddon(); disposables.disposeTerminal(); @@ -913,6 +936,10 @@ export class TerminalEmulatorRuntime { this.terminal?.paste(text); } + private isDispatchingUserInput(): boolean { + return (this.userInputEvent?.eventPhase ?? Event.NONE) !== Event.NONE; + } + private pasteClipboard(terminal: Pick): void { const text = this.clipboard.readText().catch(() => ""); const onFilePaste = this.callbacks.onFilePaste;