Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions apps/desktop/src/app/chat/composer/ime-keyboard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest'

import { shouldLetImeHandleKeyDown } from './ime-keyboard'

describe('shouldLetImeHandleKeyDown', () => {
it('lets the IME consume Enter while native composition is active', () => {
expect(shouldLetImeHandleKeyDown({ key: 'Enter', isComposing: true })).toBe(true)
})

it('lets the IME consume Enter for Process-key candidate confirmation', () => {
expect(shouldLetImeHandleKeyDown({ key: 'Process', isComposing: false })).toBe(true)
})

it('lets the IME consume Enter for keyCode 229 candidate confirmation', () => {
expect(shouldLetImeHandleKeyDown({ key: 'Enter', keyCode: 229, isComposing: false })).toBe(true)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This verifies only the pure helper. Please add a DOM-level regression that drives the composer keydown path and asserts a 229/Process candidate-confirmation event cannot submit or select a trigger item.

})

it('lets the IME consume Enter for which 229 candidate confirmation', () => {
expect(shouldLetImeHandleKeyDown({ key: 'Enter', which: 229, isComposing: false })).toBe(true)
})

it('does not treat normal Enter as IME input', () => {
expect(shouldLetImeHandleKeyDown({ key: 'Enter', keyCode: 13, isComposing: false })).toBe(false)
})
})
10 changes: 10 additions & 0 deletions apps/desktop/src/app/chat/composer/ime-keyboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface ImeKeyboardEventLike {
isComposing?: boolean
key?: string
keyCode?: number
which?: number
}

export function shouldLetImeHandleKeyDown(event: ImeKeyboardEventLike): boolean {
return Boolean(event.isComposing || event.key === 'Process' || event.keyCode === 229 || event.which === 229)
}
9 changes: 4 additions & 5 deletions apps/desktop/src/app/chat/composer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { useAtCompletions } from './hooks/use-at-completions'
import { useSlashCompletions } from './hooks/use-slash-completions'
import { useVoiceConversation } from './hooks/use-voice-conversation'
import { useVoiceRecorder } from './hooks/use-voice-recorder'
import { shouldLetImeHandleKeyDown } from './ime-keyboard'
import {
dragHasAttachments,
droppedFileInlineRef,
Expand Down Expand Up @@ -657,11 +658,9 @@ export function ChatBar({

const handleEditorKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
// IME composition: Enter confirms composed text, not a message submission.
// We check both composingRef (set by compositionstart/compositionend, robust
// across browsers) and nativeEvent.isComposing (Chromium fallback). Without
// this guard, pressing Enter to finalise a Korean/Japanese/Chinese IME
// preedit fires submitDraft() and splits the message mid-word.
if (composingRef.current || event.nativeEvent.isComposing) {
// Some Chinese IMEs report candidate confirmation as keyCode/which 229 or
// key "Process" after isComposing has already flipped false.
if (composingRef.current || shouldLetImeHandleKeyDown(event.nativeEvent)) {
return
}

Expand Down