From b76a2ddae6c1f06e6044be2381bb5a89b37d893d Mon Sep 17 00:00:00 2001 From: Mantaroh Yoshinaga Date: Wed, 24 Jun 2026 16:12:22 +0900 Subject: [PATCH] fix(settings): defer Display Name persistence until IME composition ends (#6238) * fix(settings): defer Display Name persistence until IME composition ends The repository Display Name input persisted every keystroke via async updateRepo, including unconfirmed IME text. For Japanese (and other CJK) input this wrote pre-confirmation text to the store, and its async echo could cancel the active composition. Track composition state and hold persistence until compositionend so only confirmed text reaches updateRepo; non-IME typing still persists per keystroke. * fix(settings): de-duplicate Display Name persist on trailing IME change Some IMEs (e.g. Firefox) fire the final change event after compositionend, which repeated the already-persisted confirmed value through updateRepo. Track the composition-end text and skip the next matching change so the confirmed value is persisted exactly once. Model the trailing input event in the unit test so the single-fire guard is exercised. * fix(settings): reset IME draft guards after repo changes Co-authored-by: Orca --------- Co-authored-by: Jinwoo-H Co-authored-by: Orca --- .../RepositoryPaneDraftInput.test.tsx | 108 +++++++++++++++++- .../settings/RepositorySettingsDraftInput.tsx | 51 ++++++++- 2 files changed, 152 insertions(+), 7 deletions(-) diff --git a/src/renderer/src/components/settings/RepositoryPaneDraftInput.test.tsx b/src/renderer/src/components/settings/RepositoryPaneDraftInput.test.tsx index e7d49a6e9..274c6e5de 100644 --- a/src/renderer/src/components/settings/RepositoryPaneDraftInput.test.tsx +++ b/src/renderer/src/components/settings/RepositoryPaneDraftInput.test.tsx @@ -39,17 +39,46 @@ function getInput(): HTMLInputElement { return input } +function setNativeValue(input: HTMLInputElement, text: string): void { + // Why: React reads controlled-input changes via the native value setter; + // assigning input.value directly is swallowed by React's value tracking. + const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set + setValue?.call(input, text) +} + function typeText(text: string): void { act(() => { const input = getInput() - // Why: React reads controlled-input changes via the native value setter; - // assigning input.value directly is swallowed by React's value tracking. - const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set - setValue?.call(input, text) + setNativeValue(input, text) input.dispatchEvent(new Event('input', { bubbles: true })) }) } +function compositionStart(): void { + act(() => { + getInput().dispatchEvent(new CompositionEvent('compositionstart', { bubbles: true })) + }) +} + +// Why: an input event fired between compositionstart and compositionend models a +// keystroke of unconfirmed IME text (e.g. Japanese kana before conversion). +function composingInput(text: string): void { + typeText(text) +} + +function compositionEnd(text: string, options?: { trailingInput?: boolean }): void { + act(() => { + const input = getInput() + setNativeValue(input, text) + input.dispatchEvent(new CompositionEvent('compositionend', { bubbles: true, data: text })) + // Why: some IMEs (e.g. Firefox) emit the final input event after + // compositionend; model it so the single-persist guard is exercised. + if (options?.trailingInput !== false) { + input.dispatchEvent(new Event('input', { bubbles: true })) + } + }) +} + describe('RepoSettingsDraftInput', () => { it('keeps draft text while the store still holds the previous value (IME regression)', () => { const onTextChange = vi.fn() @@ -109,4 +138,75 @@ describe('RepoSettingsDraftInput', () => { expect(onTextChange).toHaveBeenNthCalledWith(1, 'a') expect(onTextChange).toHaveBeenNthCalledWith(2, 'ab') }) + + it('does not persist unconfirmed IME text while composing', () => { + const onTextChange = vi.fn() + render({ repoId: 'repo-1', storeValue: '', onTextChange }) + + // Japanese conversion: type kana, then convert, before confirming. + compositionStart() + composingInput('にほんご') + composingInput('日本語') + + // Why: persisting mid-composition writes the pre-confirmation value and its + // async store echo can cancel the IME session. + expect(onTextChange).not.toHaveBeenCalled() + // The input still shows the unconfirmed text via the local draft. + expect(getInput().value).toBe('日本語') + }) + + it('persists once with the confirmed value on compositionend', () => { + const onTextChange = vi.fn() + render({ repoId: 'repo-1', storeValue: '', onTextChange }) + + compositionStart() + composingInput('にほんご') + composingInput('日本語') + compositionEnd('日本語') + + expect(onTextChange).toHaveBeenCalledTimes(1) + expect(onTextChange).toHaveBeenCalledWith('日本語') + expect(getInput().value).toBe('日本語') + }) + + it('does not suppress later edits when no final input follows compositionend', () => { + const onTextChange = vi.fn() + render({ repoId: 'repo-1', storeValue: '', onTextChange }) + + compositionStart() + composingInput('にほんご') + compositionEnd('日本語', { trailingInput: false }) + render({ repoId: 'repo-1', storeValue: '日本語', onTextChange }) + + typeText('日本語!') + + expect(onTextChange).toHaveBeenNthCalledWith(1, '日本語') + expect(onTextChange).toHaveBeenNthCalledWith(2, '日本語!') + }) + + it('resumes per-keystroke persistence after composition ends', () => { + const onTextChange = vi.fn() + render({ repoId: 'repo-1', storeValue: '', onTextChange }) + + compositionStart() + composingInput('あ') + compositionEnd('亜') + typeText('亜b') + + expect(onTextChange).toHaveBeenNthCalledWith(1, '亜') + expect(onTextChange).toHaveBeenNthCalledWith(2, '亜b') + }) + + it('resets composition state when the pane switches repos', () => { + const onTextChange = vi.fn() + render({ repoId: 'repo-1', storeValue: '', onTextChange }) + + compositionStart() + composingInput('未確定') + render({ repoId: 'repo-2', storeValue: 'Repo Two', onTextChange }) + typeText('Renamed Repo Two') + + expect(onTextChange).toHaveBeenCalledTimes(1) + expect(onTextChange).toHaveBeenCalledWith('Renamed Repo Two') + }) }) diff --git a/src/renderer/src/components/settings/RepositorySettingsDraftInput.tsx b/src/renderer/src/components/settings/RepositorySettingsDraftInput.tsx index c58c2343b..07764e623 100644 --- a/src/renderer/src/components/settings/RepositorySettingsDraftInput.tsx +++ b/src/renderer/src/components/settings/RepositorySettingsDraftInput.tsx @@ -6,11 +6,14 @@ type RepoTextDraft = { repoId: string; text: string } // Why: updateRepo persists via async IPC before the store value updates, so a // store-controlled input resets mid-IME-composition (Hangul decomposes into -// jamo). Keep keystrokes in local draft state; persist stays per-keystroke. +// jamo). Keep keystrokes in local draft state; persist per-keystroke except +// while an IME composition is active (see composingRef below). export function RepoSettingsDraftInput({ repoId, storeValue, onTextChange, + onCompositionStart, + onCompositionEnd, ...inputProps }: { repoId: string @@ -19,15 +22,33 @@ export function RepoSettingsDraftInput({ } & Omit, 'value' | 'onChange'>): React.JSX.Element { const [draft, setDraft] = useState({ repoId, text: storeValue }) const pendingStoreEchoesRef = useRef([]) + // Why: IME composition (e.g. Japanese kana→kanji conversion) fires input + // events for unconfirmed text. Persisting those mid-composition writes the + // pre-confirmation value to the store and its async echo can cancel the + // composition. Hold persistence until compositionend so only confirmed text + // reaches updateRepo. + const composingRef = useRef(false) + // Why: some IMEs emit a trailing change event after compositionend that + // repeats the already-persisted confirmed value; consume that one change so + // the value is not persisted twice. + const skipNextChangeRef = useRef(null) + + const persist = (text: string): void => { + pendingStoreEchoesRef.current.push(text) + onTextChange(text) + } useEffect(() => { setDraft((current) => { if (current.repoId !== repoId) { pendingStoreEchoesRef.current = [] + composingRef.current = false + skipNextChangeRef.current = null return { repoId, text: storeValue } } if (storeValue === current.text) { pendingStoreEchoesRef.current = [] + skipNextChangeRef.current = null return current } const pendingEchoIndex = pendingStoreEchoesRef.current.indexOf(storeValue) @@ -38,6 +59,7 @@ export function RepoSettingsDraftInput({ return current } pendingStoreEchoesRef.current = [] + skipNextChangeRef.current = null return { repoId, text: storeValue } }) }, [repoId, storeValue]) @@ -49,9 +71,32 @@ export function RepoSettingsDraftInput({ value={text} onChange={(e) => { const nextText = e.target.value - pendingStoreEchoesRef.current.push(nextText) setDraft({ repoId, text: nextText }) - onTextChange(nextText) + // Why: during composition the input stays live via draft, but the + // unconfirmed text is not persisted until compositionend. + if (composingRef.current) { + return + } + if (skipNextChangeRef.current === nextText) { + skipNextChangeRef.current = null + return + } + skipNextChangeRef.current = null + persist(nextText) + }} + onCompositionStart={(e) => { + composingRef.current = true + skipNextChangeRef.current = null + onCompositionStart?.(e) + }} + onCompositionEnd={(e) => { + composingRef.current = false + const nextText = e.currentTarget.value + setDraft({ repoId, text: nextText }) + persist(nextText) + // Why: cover IMEs that fire the final change after compositionend. + skipNextChangeRef.current = nextText + onCompositionEnd?.(e) }} /> )