fix: guard missing inline input types
This commit is contained in:
@@ -31,6 +31,7 @@ import { handleInlineWikilinkKeyDown } from './inlineWikilinkKeydown'
|
||||
import { useInlineWikilinkSelection } from './useInlineWikilinkSelection'
|
||||
import { useInlineWikilinkSuggestionsState } from './useInlineWikilinkSuggestionsState'
|
||||
import { normalizeInlineWikilinkValue } from './inlineWikilinkTokens'
|
||||
import { isInsertBeforeInput } from './inlineWikilinkBeforeInput'
|
||||
|
||||
interface InlineWikilinkInputProps {
|
||||
entries: VaultEntry[]
|
||||
@@ -208,7 +209,7 @@ export function InlineWikilinkInput({
|
||||
if (disabled) return
|
||||
|
||||
const nativeEvent = event.nativeEvent as InputEvent
|
||||
if (!nativeEvent.inputType.startsWith('insert')) return
|
||||
if (!isInsertBeforeInput(nativeEvent)) return
|
||||
|
||||
const dataTransfer = nativeEvent.dataTransfer
|
||||
if (!dataTransfer || !hasUnsupportedClipboardPayload(dataTransfer)) return
|
||||
|
||||
@@ -8,7 +8,10 @@ import {
|
||||
waitFor,
|
||||
} from '@testing-library/react'
|
||||
import { WikilinkChatInput } from './WikilinkChatInput'
|
||||
import { UNSUPPORTED_INLINE_PASTE_MESSAGE } from './InlineWikilinkInput'
|
||||
import {
|
||||
UNSUPPORTED_INLINE_PASTE_MESSAGE,
|
||||
} from './InlineWikilinkInput'
|
||||
import { isInsertBeforeInput } from './inlineWikilinkBeforeInput'
|
||||
import type { VaultEntry } from '../types'
|
||||
|
||||
const makeEntry = (overrides: Partial<VaultEntry> = {}): VaultEntry => ({
|
||||
@@ -271,6 +274,27 @@ describe('WikilinkChatInput', () => {
|
||||
expect(editor.textContent).toContain('still works')
|
||||
})
|
||||
|
||||
it('treats missing inputType as a non-insert beforeinput event', () => {
|
||||
expect(() => isInsertBeforeInput({} as InputEvent)).not.toThrow()
|
||||
expect(isInsertBeforeInput({} as InputEvent)).toBe(false)
|
||||
expect(isInsertBeforeInput({ inputType: 'insertFromPaste' } as InputEvent)).toBe(true)
|
||||
})
|
||||
|
||||
it('ignores beforeinput events without inputType instead of crashing', () => {
|
||||
render(<Controlled />)
|
||||
|
||||
const editor = screen.getByTestId('agent-input')
|
||||
const beforeInputEvent = new Event('beforeinput', {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
})
|
||||
|
||||
expect(() => fireEvent(editor, beforeInputEvent)).not.toThrow()
|
||||
|
||||
updateEditorText('still works')
|
||||
expect(editor.textContent).toContain('still works')
|
||||
})
|
||||
|
||||
it('recovers if unsupported media lands in the editor DOM', async () => {
|
||||
const onUnsupportedPaste = vi.fn()
|
||||
render(<Controlled onUnsupportedPaste={onUnsupportedPaste} />)
|
||||
|
||||
4
src/components/inlineWikilinkBeforeInput.ts
Normal file
4
src/components/inlineWikilinkBeforeInput.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export function isInsertBeforeInput(nativeEvent: Pick<InputEvent, 'inputType'>) {
|
||||
return typeof nativeEvent.inputType === 'string'
|
||||
&& nativeEvent.inputType.startsWith('insert')
|
||||
}
|
||||
Reference in New Issue
Block a user