diff --git a/src/renderer/src/components/native-chat/NativeChatComposer.test.tsx b/src/renderer/src/components/native-chat/NativeChatComposer.test.tsx index fe6cbdd6f..bef615153 100644 --- a/src/renderer/src/components/native-chat/NativeChatComposer.test.tsx +++ b/src/renderer/src/components/native-chat/NativeChatComposer.test.tsx @@ -14,6 +14,8 @@ const mocks = vi.hoisted(() => ({ fieldProps: null as { onSend?: () => void onStop?: () => void + onCompositionStart?: () => void + onCompositionEnd?: (event: { currentTarget: HTMLTextAreaElement }) => void sessionOptionsSurface?: SessionOptionsSurface | null sessionOptionsSnapshot?: SessionOptionDescriptor[] } | null, @@ -251,6 +253,46 @@ describe('NativeChatComposer', () => { expect(new Set(mocks.draftScopeKeys)).toEqual(new Set(['tab-1:leaf-1'])) }) + it('adopts an IME deletion delivered only by compositionend', () => { + render( + + ) + const textarea = document.createElement('textarea') + textarea.value = '' + mocks.setDraft.mockClear() + + act(() => { + mocks.fieldProps?.onCompositionStart?.() + mocks.fieldProps?.onCompositionEnd?.({ currentTarget: textarea }) + }) + + expect(mocks.setDraft).toHaveBeenCalledOnce() + expect(mocks.setDraft).toHaveBeenCalledWith('') + }) + + it('does not duplicate a composition value already adopted by onChange', () => { + render( + + ) + const textarea = document.createElement('textarea') + textarea.value = 'hello' + mocks.setDraft.mockClear() + + act(() => mocks.fieldProps?.onCompositionEnd?.({ currentTarget: textarea })) + + expect(mocks.setDraft).not.toHaveBeenCalled() + }) + it('shows the model already selected in the Claude TUI when chat opens', async () => { mocks.getMainBufferSnapshot.mockResolvedValue({ data: 'Claude Code v2.1.211\r\nOpus 4.8 with medium effort · API Usage Billing', diff --git a/src/renderer/src/components/native-chat/NativeChatComposer.tsx b/src/renderer/src/components/native-chat/NativeChatComposer.tsx index 6e7488059..3cad0b3ac 100644 --- a/src/renderer/src/components/native-chat/NativeChatComposer.tsx +++ b/src/renderer/src/components/native-chat/NativeChatComposer.tsx @@ -375,6 +375,17 @@ export const NativeChatComposer = forwardRef { + setDraft(value) + setHistory((prev) => ({ entries: prev.entries, index: null })) + syncCaret(element) + handleDraftOrCaretChange(value, element.selectionStart ?? value.length) + setActiveSuggestion(0) + }, + [handleDraftOrCaretChange, setDraft, syncCaret] + ) + return ( { - setDraft(value) - setHistory((prev) => ({ entries: prev.entries, index: null })) - syncCaret(element) - handleDraftOrCaretChange(value, element.selectionStart ?? value.length) - setActiveSuggestion(0) - }} + onDraftChange={handleDraftChange} onTextareaSelect={(element) => { syncCaret(element) handleDraftOrCaretChange(element.value, element.selectionStart ?? element.value.length) @@ -408,8 +413,11 @@ export const NativeChatComposer = forwardRef { isComposingRef.current = true }} - onCompositionEnd={() => { + onCompositionEnd={(event) => { isComposingRef.current = false + if (event.currentTarget.value !== draft) { + handleDraftChange(event.currentTarget.value, event.currentTarget) + } }} onPaste={handlePaste} pickerListboxId={picker.listboxId} diff --git a/src/renderer/src/components/terminal-pane/terminal-ime-hangul-syllable-flush.test.ts b/src/renderer/src/components/terminal-pane/terminal-ime-hangul-syllable-flush.test.ts index 4a525ecca..7b285fa6a 100644 --- a/src/renderer/src/components/terminal-pane/terminal-ime-hangul-syllable-flush.test.ts +++ b/src/renderer/src/components/terminal-pane/terminal-ime-hangul-syllable-flush.test.ts @@ -14,6 +14,7 @@ function nextEventLoop(): Promise { } function openTerminal(TerminalType: typeof EsmTerminal): { + compositionView: HTMLElement emitted: string[] textarea: HTMLTextAreaElement } { @@ -25,9 +26,13 @@ function openTerminal(TerminalType: typeof EsmTerminal): { if (!terminal.textarea) { throw new Error('xterm textarea was not created') } + const compositionView = container.querySelector('.composition-view') + if (!compositionView) { + throw new Error('xterm composition view was not created') + } const emitted: string[] = [] terminal.onData((data) => emitted.push(data)) - return { emitted, textarea: terminal.textarea } + return { compositionView, emitted, textarea: terminal.textarea } } function composition( @@ -124,6 +129,21 @@ describe.each([ expect(emitted.join('')).toBe('한글') }) + it('shows 가나다 before the final macOS composition commits', async () => { + const { compositionView, emitted, textarea } = openTerminal(TerminalType) + composeSyllable(textarea, '', ['ㄱ', '가']) + composition(textarea, 'compositionend', '가') + composeSyllable(textarea, '가', ['ㄴ', '나']) + await nextEventLoop() + composition(textarea, 'compositionend', '나') + composeSyllable(textarea, '가나', ['ㄷ', '다']) + await nextEventLoop() + + expect(emitted.join('')).toBe('가나') + expect(compositionView.classList.contains('active')).toBe(true) + expect(compositionView.textContent).toContain('다') + }) + it('does not double-send after an IBus insertText commit', async () => { const { emitted, textarea } = openTerminal(TerminalType) textarea.setSelectionRange(0, 0) diff --git a/src/renderer/src/components/terminal-pane/terminal-ime-xterm-composition-cancel.test.ts b/src/renderer/src/components/terminal-pane/terminal-ime-xterm-composition-cancel.test.ts index 93fa08450..532918450 100644 --- a/src/renderer/src/components/terminal-pane/terminal-ime-xterm-composition-cancel.test.ts +++ b/src/renderer/src/components/terminal-pane/terminal-ime-xterm-composition-cancel.test.ts @@ -96,16 +96,15 @@ describe('xterm IME composition cancellation', () => { terminal.dispose() }) - it('emits nothing when the preedit is cancelled without observed input events', async () => { + it('drops a Sogou preedit cancelled without a trailing input event', async () => { const { emitted, terminal, textarea } = openTerminal() dispatchCompositionEvent(textarea, 'compositionstart') - dispatchCompositionEvent(textarea, 'compositionupdate', 'ce') - textarea.value = 'ce' - await nextEventLoop() - dispatchCompositionEvent(textarea, 'compositionupdate', 'c') - textarea.value = 'c' - await nextEventLoop() + for (const preedit of ['nihao', 'niha', 'nih', 'ni', 'n']) { + dispatchCompositionEvent(textarea, 'compositionupdate', preedit) + textarea.value = preedit + await nextEventLoop() + } textarea.value = '' dispatchCompositionEvent(textarea, 'compositionend') await nextEventLoop()