Skip to content

The agent that grows with you

fix(desktop): block Enter keyCode 229 after IME compositionend

liyunlong202e5b813af4
diff --git a/apps/desktop/src/app/chat/composer/ime-composition-dom-repro.test.tsx b/apps/desktop/src/app/chat/composer/ime-composition-dom-repro.test.tsx
index 962183ec7d8..0653dfb4633 100644
--- a/apps/desktop/src/app/chat/composer/ime-composition-dom-repro.test.tsx
+++ b/apps/desktop/src/app/chat/composer/ime-composition-dom-repro.test.tsx
@@ -105,4 +105,87 @@ describe('composer IME composition — send button visibility (#39614)', () => {
expect(hasPayload).toBe(false)
}
})
+
+ it('blocks Enter with keyCode 229 even after compositionend (macOS Chinese IME)', async () => {
+ let submitCount = 0
+ let hasPayload = false
+
+ function KeyDownHarness({ onPayload }: { onPayload: (hasPayload: boolean) => void }) {
+ const editorRef = useRef<HTMLDivElement>(null)
+ const composingRef = useRef(false)
+ const draftRef = useRef('')
+ const [draft, setDraft] = useState('')
+
+ const flushEditorToDraft = (editor: HTMLDivElement) => {
+ const next = editor.textContent ?? ''
+ if (next !== draftRef.current) {
+ draftRef.current = next
+ setDraft(next)
+ }
+ }
+
+ onPayload(draft.trim().length > 0)
+
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
+ if (composingRef.current || event.nativeEvent.isComposing) {
+ return
+ }
+ if (event.key === 'Enter' && event.keyCode === 229) {
+ return
+ }
+ if (event.key === 'Enter' && !event.shiftKey) {
+ event.preventDefault()
+ submitCount++
+ }
+ }
+
+ return (
+ <div
+ contentEditable
+ data-testid="editor"
+ onCompositionEnd={event => {
+ composingRef.current = false
+ flushEditorToDraft(event.currentTarget)
+ }}
+ onCompositionStart={() => {
+ composingRef.current = true
+ }}
+ onInput={event => {
+ if (composingRef.current) return
+ flushEditorToDraft(event.currentTarget)
+ }}
+ onKeyDown={handleKeyDown}
+ ref={editorRef}
+ suppressContentEditableWarning
+ />
+ )
+ }
+
+ const { getByTestId } = render(<KeyDownHarness onPayload={p => (hasPayload = p)} />)
+ const editor = getByTestId('editor')
+
+ // Simulate macOS Chinese IME: compositionend fires, then Enter with keyCode 229.
+ await act(async () => {
+ fireEvent.compositionStart(editor)
+ editor.textContent = '测试'
+ fireEvent.input(editor)
+ fireEvent.compositionEnd(editor)
+ })
+
+ expect(hasPayload).toBe(true)
+
+ // This Enter must NOT trigger submit.
+ await act(async () => {
+ fireEvent.keyDown(editor, { key: 'Enter', keyCode: 229 })
+ })
+
+ expect(submitCount).toBe(0)
+
+ // A normal Enter afterwards should still submit.
+ await act(async () => {
+ fireEvent.keyDown(editor, { key: 'Enter', keyCode: 13 })
+ })
+
+ expect(submitCount).toBe(1)
+ })
})
diff --git a/apps/desktop/src/app/chat/composer/index.tsx b/apps/desktop/src/app/chat/composer/index.tsx
index d583affcce1..ec041ddfd78 100644
--- a/apps/desktop/src/app/chat/composer/index.tsx
+++ b/apps/desktop/src/app/chat/composer/index.tsx
@@ -560,6 +560,15 @@ export function ChatBar({
return
}
+ // macOS Chinese IME (and some 3rd-party IMEs on Windows) emit Enter with
+ // keyCode 229 (legacy VK_PROCESSKEY) while isComposing is already false.
+ // The compositionend has fired but the keydown still carries 229, signalling
+ // "this Enter is an IME commit, not a user send". If we let it through,
+ // the message fires before the committed text is fully in the DOM.
+ if (event.key === 'Enter' && event.keyCode === 229) {
+ return
+ }
+
// Undo/redo before anything else — we own the stack (see useComposerUndo),
// so these never reach Chromium's native history, which has no record of
// the Range-based edits the rich editor makes.