From 834a9fc89d498f9c45d2ca9c48240ffcda75af76 Mon Sep 17 00:00:00 2001 From: InphinitiZ Date: Fri, 5 Jun 2026 17:13:15 +0800 Subject: [PATCH] fix(tui): preserve IME text before return submit Preserve printable IME commit text when xterm delivers it in the same input burst as Return, so Dashboard/TUI submits the visible draft instead of dropping the final segment. Also fixes the TUI type-check stdio tuple typing and adds focused regression coverage. --- .../__tests__/textInputReturnBurst.test.ts | 25 ++++++++++++++ ui-tui/src/components/textInput.tsx | 33 +++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 ui-tui/src/__tests__/textInputReturnBurst.test.ts diff --git a/ui-tui/src/__tests__/textInputReturnBurst.test.ts b/ui-tui/src/__tests__/textInputReturnBurst.test.ts new file mode 100644 index 0000000000..8470623b82 --- /dev/null +++ b/ui-tui/src/__tests__/textInputReturnBurst.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from 'vitest' + +import { valueForReturnSubmit } from '../components/textInput.js' + +describe('valueForReturnSubmit', () => { + it('includes printable input that arrives in the same keypress as return', () => { + expect(valueForReturnSubmit('为什么打字上屏,', 8, '会丢失内容')).toEqual({ + cursor: 13, + value: '为什么打字上屏,会丢失内容' + }) + }) + + it('keeps IME commit text when it arrives in the same burst as return', () => { + expect(valueForReturnSubmit('为什么打字上屏,', 8, '会丢失内容\r')).toEqual({ + cursor: 13, + value: '为什么打字上屏,会丢失内容' + }) + }) + + it('leaves the draft unchanged when return carries no printable input', () => { + expect(valueForReturnSubmit('hello', 5, '')).toEqual({ cursor: 5, value: 'hello' }) + expect(valueForReturnSubmit('hello', 5, '\r')).toEqual({ cursor: 5, value: 'hello' }) + expect(valueForReturnSubmit('hello', 5, '\n')).toEqual({ cursor: 5, value: 'hello' }) + }) +}) diff --git a/ui-tui/src/components/textInput.tsx b/ui-tui/src/components/textInput.tsx index dc8cf3fac0..423547ca8f 100644 --- a/ui-tui/src/components/textInput.tsx +++ b/ui-tui/src/components/textInput.tsx @@ -157,6 +157,33 @@ export function applyPrintableInsert( export const shouldRouteMultiCharInputAsPaste = (text: string): boolean => text.includes('\n') +export function valueForReturnSubmit( + value: string, + cursor: number, + input: string, + range?: { end: number; start: number } | null +): TextInsertResult { + const pending = input.replace(BRACKET_PASTE, '').replace(/\r\n/g, '\n').replace(/\r/g, '\n') + + if (!pending) { + return { cursor, value } + } + + // Browser/xterm IME commits can arrive as one burst immediately followed by + // Return (for example "会丢失内容\r"). The Return keypath is already about to + // submit, but the committed text has not passed through the ordinary + // printable-input branch yet. Preserve the printable prefix before the first + // newline so the visible, just-committed IME text is part of the submitted + // prompt instead of being silently dropped. + const [beforeReturn] = pending.split('\n', 1) + + if (!beforeReturn) { + return { cursor, value } + } + + return applyPrintableInsert(value, cursor, beforeReturn, range) ?? { cursor, value } +} + export function shouldPreserveCtrlJNewline(env: MinimalEnv = process.env): boolean { if (env.WT_SESSION) { return true @@ -1163,13 +1190,15 @@ export function TextInput({ if (k.return) { flushKeyBurst() + const range = selRange() + const pending = valueForReturnSubmit(vRef.current, curRef.current, inp, range) const sequence = (event.keypress as { sequence?: string }).sequence const preserveBareLineFeed = shouldPreserveCtrlJNewline() && sequence === '\n' if (k.shift || k.ctrl || preserveBareLineFeed || (isMac ? isActionMod(k) : k.meta)) { - commit(ins(vRef.current, curRef.current, '\n'), curRef.current + 1) + commit(ins(pending.value, pending.cursor, '\n'), pending.cursor + 1) } else { - cbSubmit.current?.(vRef.current) + cbSubmit.current?.(pending.value) } return