feat(web): add text dictated with the iOS keyboard after 3 seconds

The installed iOS app has no speech recognition, so people dictate with the
keyboard's microphone, which only writes text into the input and never
ended it. Text that arrives without key presses (words or phrases at once,
or replacements) is now treated as a dictation: 3 seconds after the last
change it goes through the voice flow (AI split, "Deshacer"). Typing a key
cancels it; pastes and deletions never trigger it.

The form field is no longer called `name`, which made iOS offer to
autofill a contact into the add field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-28 19:41:39 -03:00
parent 48c0c83424
commit 9d4388ed8c
3 changed files with 157 additions and 18 deletions

View File

@@ -0,0 +1,29 @@
import { describe, expect, it } from 'vitest'
import { isDictatedInput } from './use-keyboard-dictation'
describe('isDictatedInput', () => {
it('takes words or phrases written without a key press as dictation', () => {
expect(isDictatedInput({ inputType: 'insertText', data: 'Yogur', msSinceKeydown: 5000 })).toBe(
true,
)
expect(
isDictatedInput({ inputType: 'insertReplacementText', data: null, msSinceKeydown: 5000 }),
).toBe(true)
})
it('does not take typing as dictation', () => {
expect(isDictatedInput({ inputType: 'insertText', data: 'Yogur', msSinceKeydown: 10 })).toBe(
false,
)
expect(isDictatedInput({ inputType: 'insertText', data: 'y', msSinceKeydown: 5000 })).toBe(
false,
)
})
it('never takes pastes, drops or deletions as dictation', () => {
for (const inputType of ['insertFromPaste', 'insertFromDrop', 'deleteContentBackward']) {
expect(isDictatedInput({ inputType, data: 'leche y pan', msSinceKeydown: 5000 })).toBe(false)
}
})
})

View File

@@ -0,0 +1,92 @@
import { type FormEvent, type KeyboardEvent, useCallback, useEffect, useRef } from 'react'
/** Quiet time after the keyboard's dictation last wrote into the input before it is added. */
export const KEYBOARD_DICTATION_IDLE_MS = 3000
/** How close to a key press an input event must be to count as typed. */
const KEYDOWN_WINDOW_MS = 100
/** Insertions that are never dictation (pasting or dropping text, deleting). */
const NOT_DICTATION = /^(insertFromPaste|insertFromDrop|delete)/
/**
* Whether an input event looks like the system keyboard's dictation writing into the
* field: text that arrives without a key press, several characters at once (dictation
* writes word by word or a whole phrase) or as a replacement of what it wrote before.
* Tapping a keyboard suggestion looks the same, which is the price of the guess.
*/
export function isDictatedInput(event: {
inputType: string
data: string | null
msSinceKeydown: number
}): boolean {
if (NOT_DICTATION.test(event.inputType)) return false
if (event.msSinceKeydown < KEYDOWN_WINDOW_MS) return false
if (event.inputType === 'insertReplacementText') return true
return event.inputType === 'insertText' && (event.data?.trim().length ?? 0) > 1
}
type UseKeyboardDictationOptions = {
/** Called with the input's text once the dictation has been quiet for a while. */
onIdle: () => void
}
/**
* The installed iOS app has no speech recognition, so people dictate with the
* keyboard's microphone, which only writes text into the input. This notices text that
* arrives that way and calls `onIdle` once nothing new has arrived for
* `KEYBOARD_DICTATION_IDLE_MS`, so it is added without pressing Enter. Typing a key
* cancels it: from then on the text is the user's to submit.
*/
export function useKeyboardDictation({ onIdle }: UseKeyboardDictationOptions) {
const lastKeydown = useRef(0)
const dictated = useRef(false)
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
const onIdleRef = useRef(onIdle)
useEffect(() => {
onIdleRef.current = onIdle
})
const cancel = useCallback(() => {
clearTimeout(timer.current)
dictated.current = false
}, [])
useEffect(() => cancel, [cancel])
const onKeyDown = useCallback(
(_event: KeyboardEvent<HTMLInputElement>) => {
lastKeydown.current = performance.now()
cancel()
},
[cancel],
)
const onInput = useCallback((event: FormEvent<HTMLInputElement>) => {
const native = event.nativeEvent as InputEvent
const inputType = native.inputType ?? ''
const looksDictated = isDictatedInput({
inputType,
data: native.data ?? null,
msSinceKeydown: performance.now() - lastKeydown.current,
})
// Once dictation started, any later update without a key press (corrections,
// composition) keeps it going; a paste or a deletion stops it.
if (looksDictated) dictated.current = true
else if (NOT_DICTATION.test(inputType)) dictated.current = false
clearTimeout(timer.current)
if (!dictated.current) return
timer.current = setTimeout(() => {
dictated.current = false
onIdleRef.current()
}, KEYBOARD_DICTATION_IDLE_MS)
}, [])
return { onKeyDown, onInput, cancel }
}