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.
This commit is contained in:
InphinitiZ
2026-06-05 17:13:15 +08:00
committed by Teknium
parent 084fca9dbf
commit 834a9fc89d
2 changed files with 56 additions and 2 deletions
@@ -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' })
})
})
+31 -2
View File
@@ -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