Repository navigation
feat: dictate a commit with a microphone button in the composer #1447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b3e7214
fdb2a4b
78a3a2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { Mic } from 'lucide-react'; | ||
| import type { RefObject } from 'react'; | ||
| import { useEffect } from 'react'; | ||
| import { ActionTooltip } from '@/components/action-tooltip'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { useDictation } from '@/hooks/use-dictation'; | ||
| import { cn } from '@/lib/utils'; | ||
|
|
||
| /** | ||
| * Writes the commit by voice where the caret is. Hidden in browsers without speech recognition, and drops whatever | ||
| * wasn't written yet once the commit is being sent, so nothing lands in the next one. | ||
| */ | ||
| export function DictationButton({ | ||
| body, | ||
| updateBody, | ||
| textareaRef, | ||
| maxLength, | ||
| disabled, | ||
| }: { | ||
| body: string; | ||
| updateBody: (body: string) => void; | ||
| textareaRef: RefObject<HTMLTextAreaElement | null>; | ||
| maxLength: number; | ||
| disabled: boolean; | ||
| }) { | ||
| const dictation = useDictation({ | ||
| body, | ||
| updateBody, | ||
| textareaRef, | ||
| maxLength, | ||
| }); | ||
| const { isListening, stop, abort } = dictation; | ||
|
|
||
| useEffect(() => { | ||
| if (disabled && isListening) { | ||
| abort(); | ||
| } | ||
| }, [disabled, isListening, abort]); | ||
|
|
||
| if (!dictation.isSupported) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <ActionTooltip label={isListening ? 'Stop dictating' : 'Dictate'}> | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="icon" | ||
| aria-label="Dictate" | ||
| aria-pressed={isListening} | ||
| disabled={disabled} | ||
| onClick={isListening ? stop : dictation.start} | ||
| className="text-muted-foreground aria-pressed:bg-primary-soft aria-pressed:text-primary aria-pressed:hover:bg-primary-soft aria-pressed:hover:text-primary" | ||
| data-test="dictation-button" | ||
| > | ||
| <Mic | ||
| className={cn('size-5', { 'animate-pulse': isListening })} | ||
| /> | ||
| </Button> | ||
| </ActionTooltip> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import type { RefObject } from 'react'; | ||
| import { | ||
| useEffect, | ||
| useLayoutEffect, | ||
| useRef, | ||
| useState, | ||
| useSyncExternalStore, | ||
| } from 'react'; | ||
| import { toast } from 'sonner'; | ||
| import { dictatedText } from '@/lib/dictation'; | ||
| import { insertAtSelection } from '@/lib/text-selection'; | ||
|
|
||
| type SpeechRecognitionResultList = ArrayLike< | ||
| ArrayLike<{ transcript: string }> & { isFinal: boolean } | ||
| >; | ||
|
|
||
| /** | ||
| * The part of the Web Speech API's SpeechRecognition used here, which TypeScript's DOM types don't include. | ||
| */ | ||
| type Recognition = { | ||
| lang: string; | ||
| continuous: boolean; | ||
| interimResults: boolean; | ||
| onresult: | ||
| | ((event: { | ||
| resultIndex: number; | ||
| results: SpeechRecognitionResultList; | ||
| }) => void) | ||
| | null; | ||
| onerror: ((event: { error: string }) => void) | null; | ||
| onend: (() => void) | null; | ||
| start: () => void; | ||
| stop: () => void; | ||
| abort: () => void; | ||
| }; | ||
|
|
||
| type RecognitionConstructor = new () => Recognition; | ||
|
|
||
| function recognitionConstructor(): RecognitionConstructor | null { | ||
| if (typeof window === 'undefined') { | ||
| return null; | ||
| } | ||
|
|
||
| const speech = window as unknown as { | ||
| SpeechRecognition?: RecognitionConstructor; | ||
| webkitSpeechRecognition?: RecognitionConstructor; | ||
| }; | ||
|
|
||
| return speech.SpeechRecognition ?? speech.webkitSpeechRecognition ?? null; | ||
| } | ||
|
|
||
| const subscribe = () => () => {}; | ||
|
|
||
| /** | ||
| * Dictation into the composer's textarea with the browser's own speech recognition (Chrome, Edge and Safari have it, | ||
| * Firefox doesn't). Each finished phrase is inserted where the caret is, as typing would, up to `maxLength`. | ||
| */ | ||
| export function useDictation({ | ||
| body, | ||
| updateBody, | ||
| textareaRef, | ||
| maxLength, | ||
| }: { | ||
| body: string; | ||
| updateBody: (body: string) => void; | ||
| textareaRef: RefObject<HTMLTextAreaElement | null>; | ||
| maxLength: number; | ||
| }): { | ||
| isSupported: boolean; | ||
| isListening: boolean; | ||
| start: () => void; | ||
| stop: () => void; | ||
| abort: () => void; | ||
| } { | ||
| // Unknown while rendering on the server, so the button only appears once the page runs in the browser. | ||
| const isSupported = useSyncExternalStore( | ||
| subscribe, | ||
| () => recognitionConstructor() !== null, | ||
| () => false, | ||
| ); | ||
| const [isListening, setIsListening] = useState(false); | ||
| const recognition = useRef<Recognition | null>(null); | ||
| const latest = useRef({ body, updateBody, maxLength }); | ||
| const caret = useRef<number | null>(null); | ||
|
|
||
| useLayoutEffect(() => { | ||
| latest.current = { body, updateBody, maxLength }; | ||
|
|
||
| const element = textareaRef.current; | ||
|
|
||
| if (caret.current === null || element === null) { | ||
| return; | ||
| } | ||
|
|
||
| element.focus(); | ||
| element.setSelectionRange(caret.current, caret.current); | ||
| caret.current = null; | ||
| }, [body, updateBody, maxLength, textareaRef]); | ||
|
|
||
| useEffect(() => () => recognition.current?.abort(), []); | ||
|
|
||
| // Phrases can arrive before React has written the previous one, so each insertion moves the body and caret on | ||
| // right away for the next, instead of waiting for the render. | ||
| const insert = (transcript: string) => { | ||
| const { body, updateBody, maxLength } = latest.current; | ||
| const selection = | ||
| caret.current === null | ||
| ? textareaRef.current | ||
| : { | ||
| selectionStart: caret.current, | ||
| selectionEnd: caret.current, | ||
| }; | ||
| const text = dictatedText( | ||
| body.slice(0, selection?.selectionStart ?? body.length), | ||
| transcript, | ||
| body.slice(selection?.selectionEnd ?? body.length), | ||
| ); | ||
| const inserted = | ||
| text === '' | ||
| ? null | ||
| : insertAtSelection(body, selection, text, maxLength); | ||
|
|
||
| if (inserted !== null) { | ||
| caret.current = inserted.caret; | ||
| latest.current = { ...latest.current, body: inserted.value }; | ||
| updateBody(inserted.value); | ||
| } | ||
| }; | ||
|
|
||
| const start = () => { | ||
| const Constructor = recognitionConstructor(); | ||
|
|
||
| if (Constructor === null || recognition.current !== null) { | ||
| return; | ||
| } | ||
|
|
||
| const listener = new Constructor(); | ||
| listener.lang = document.documentElement.lang || navigator.language; | ||
| listener.continuous = true; | ||
| listener.interimResults = false; | ||
| listener.onresult = (event) => { | ||
| for ( | ||
| let index = event.resultIndex; | ||
| index < event.results.length; | ||
| index++ | ||
| ) { | ||
| const result = event.results[index]; | ||
|
|
||
| if (result.isFinal) { | ||
| insert(result[0].transcript); | ||
| } | ||
| } | ||
| }; | ||
| listener.onerror = (event) => { | ||
| if ( | ||
| event.error === 'not-allowed' || | ||
| event.error === 'service-not-allowed' | ||
| ) { | ||
| toast.error('Allow the microphone to dictate your commit.'); | ||
| } else if ( | ||
| event.error !== 'no-speech' && | ||
| event.error !== 'aborted' | ||
| ) { | ||
| toast.error('Dictation stopped. Try again.'); | ||
| } | ||
| }; | ||
| listener.onend = () => { | ||
| recognition.current = null; | ||
| setIsListening(false); | ||
| }; | ||
|
|
||
| recognition.current = listener; | ||
| listener.start(); | ||
| setIsListening(true); | ||
| }; | ||
|
|
||
| // Keeps what was already heard, for a stop the user asked for. | ||
| const stop = () => recognition.current?.stop(); | ||
| // Drops what wasn't written yet, for when the commit is sent and the composer is about to be cleared. | ||
| const abort = () => recognition.current?.abort(); | ||
|
|
||
| return { isSupported, isListening, start, stop, abort }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * The text to insert for what was said, given the text before and after the caret: the transcript trimmed, with a space | ||
| * on either side where it would otherwise stick to a word. Empty when nothing was said. | ||
| */ | ||
| export function dictatedText( | ||
| before: string, | ||
| transcript: string, | ||
| after = '', | ||
| ): string { | ||
| const text = transcript.trim(); | ||
|
|
||
| if (text === '') { | ||
| return ''; | ||
| } | ||
|
|
||
| const space = (side: string) => | ||
| side === '' || /\s/u.test(side) ? '' : ' '; | ||
|
|
||
| return space(before.slice(-1)) + text + space(after.slice(0, 1)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not add a space before punctuation. When the caret is before a comma,
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { test } from 'node:test'; | ||
| import { dictatedText } from '../../resources/js/lib/dictation.ts'; | ||
|
|
||
| void test('dictated text is trimmed and starts a new word after the caret', () => { | ||
| assert.equal(dictatedText('', ' shipped it '), 'shipped it'); | ||
| assert.equal(dictatedText('Finally', 'shipped it'), ' shipped it'); | ||
| assert.equal(dictatedText('Finally ', 'shipped it'), 'shipped it'); | ||
| assert.equal(dictatedText('Finally\n', 'shipped it'), 'shipped it'); | ||
| assert.equal(dictatedText('Finally', ' '), ''); | ||
| }); | ||
|
|
||
| void test('dictated text before a word is kept apart from it', () => { | ||
| assert.equal(dictatedText('', 'Finally', 'shipped it'), 'Finally '); | ||
| assert.equal(dictatedText('We ', 'finally', ' shipped'), 'finally'); | ||
| assert.equal(dictatedText('We', 'finally', 'shipped'), ' finally '); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.