fix: preserve composed ai input
This commit is contained in:
@@ -201,11 +201,6 @@ export function InlineWikilinkInput({
|
||||
onSelectionIndexChange: (nextSelectionIndex) => setSelectionRange(collapseSelectionRange(nextSelectionIndex)),
|
||||
focusSelectionAt: (nextSelectionIndex) => focusSelectionRange(collapseSelectionRange(nextSelectionIndex)),
|
||||
})
|
||||
const insertText = (text: string) => {
|
||||
const nextState = replaceInlineSelection(value, selectionRange, text)
|
||||
onChange(nextState.value)
|
||||
setSelectionRange(nextState.selection)
|
||||
}
|
||||
const insertTransferText = (text: string) => {
|
||||
const currentSelectionRange = editorRef.current
|
||||
? readSelectionRange(editorRef.current)
|
||||
@@ -363,7 +358,6 @@ export function InlineWikilinkInput({
|
||||
onCycleSuggestions: cycleSuggestions,
|
||||
onSelectSuggestion: () => selectSuggestion(selectedSuggestionIndex),
|
||||
onDeleteContent: deleteContent,
|
||||
onInsertText: insertText,
|
||||
canSubmit: onSubmit !== undefined,
|
||||
onSubmit: submitValue,
|
||||
})
|
||||
|
||||
@@ -299,6 +299,32 @@ describe('WikilinkChatInput', () => {
|
||||
removeAllRanges.mockRestore()
|
||||
})
|
||||
|
||||
it('lets committed composed characters reach the native input pipeline once', () => {
|
||||
const onDraftChange = vi.fn()
|
||||
render(<Controlled onDraftChange={onDraftChange} />)
|
||||
const editor = screen.getByTestId('agent-input')
|
||||
editor.focus()
|
||||
|
||||
const portugueseText = 'á é í ç ã õ'
|
||||
const accentedKey = createEvent.keyDown(editor, { key: 'á' })
|
||||
fireEvent(editor, accentedKey)
|
||||
expect(accentedKey.defaultPrevented).toBe(false)
|
||||
|
||||
editor.textContent = portugueseText
|
||||
setSelection(editor, portugueseText.length)
|
||||
fireEvent.input(editor)
|
||||
expect(onDraftChange).toHaveBeenLastCalledWith(portugueseText)
|
||||
|
||||
const cjkKey = createEvent.keyDown(editor, { key: '你' })
|
||||
fireEvent(editor, cjkKey)
|
||||
expect(cjkKey.defaultPrevented).toBe(false)
|
||||
|
||||
editor.textContent = `${portugueseText}你`
|
||||
setSelection(editor, portugueseText.length + 1)
|
||||
fireEvent.input(editor)
|
||||
expect(onDraftChange).toHaveBeenLastCalledWith(`${portugueseText}你`)
|
||||
})
|
||||
|
||||
it('deletes an inline chip with a single Backspace', () => {
|
||||
render(<Controlled />)
|
||||
updateEditorText('edit my [[alp')
|
||||
|
||||
@@ -77,26 +77,6 @@ function handleDeleteKeys({
|
||||
return false
|
||||
}
|
||||
|
||||
interface HandleInsertTextArgs {
|
||||
event: React.KeyboardEvent<HTMLDivElement>
|
||||
isComposing: boolean
|
||||
onInsertText: (text: string) => void
|
||||
}
|
||||
|
||||
function handleInsertText({
|
||||
event,
|
||||
isComposing,
|
||||
onInsertText,
|
||||
}: HandleInsertTextArgs): boolean {
|
||||
if (event.metaKey || event.ctrlKey || event.altKey) return false
|
||||
if (isInlineWikilinkCompositionEvent(event, isComposing)) return false
|
||||
if (event.key.length !== 1) return false
|
||||
|
||||
event.preventDefault()
|
||||
onInsertText(event.key)
|
||||
return true
|
||||
}
|
||||
|
||||
interface HandleSubmitKeyArgs {
|
||||
event: React.KeyboardEvent<HTMLDivElement>
|
||||
isComposing: boolean
|
||||
@@ -127,7 +107,6 @@ interface HandleInlineWikilinkKeyDownArgs {
|
||||
onCycleSuggestions: (direction: 1 | -1) => void
|
||||
onSelectSuggestion: () => void
|
||||
onDeleteContent: (direction: 'backward' | 'forward') => void
|
||||
onInsertText: (text: string) => void
|
||||
canSubmit: boolean
|
||||
onSubmit: () => void
|
||||
}
|
||||
@@ -140,7 +119,6 @@ export function handleInlineWikilinkKeyDown({
|
||||
onCycleSuggestions,
|
||||
onSelectSuggestion,
|
||||
onDeleteContent,
|
||||
onInsertText,
|
||||
canSubmit,
|
||||
onSubmit,
|
||||
}: HandleInlineWikilinkKeyDownArgs) {
|
||||
@@ -160,9 +138,5 @@ export function handleInlineWikilinkKeyDown({
|
||||
return
|
||||
}
|
||||
|
||||
if (handleInsertText({ event, isComposing, onInsertText })) {
|
||||
return
|
||||
}
|
||||
|
||||
handleSubmitKey({ event, isComposing, canSubmit, onSubmit })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user