okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1import { tr } from '../lib/i18n' ; 2import { useEffect , useRef , useState } from 'react' ; 3import { createPortal } from 'react-dom' ; 4import { Btn , Icon , IconBtn } from './ui' ; 5import { getCommands , MOD } from '../lib/commands' ; 6import { bindingKeys , useBindings } from '../lib/shortcut-bindings' ; 7import { createDictation , type DictationState , type RecognizerConstructor } from '../lib/dictation' ; 8export function DictationControl ({ draft, onDraft, sessionId, disabled} :{ 9draft :string ; 10onDraft :( text :string ) => void ; 11sessionId ?:string ; 12disabled :boolean ; 13}) { 14useBindings (); 15const [ visible , setVisible ] = useState ( false ), [ state , setState ] = useState < DictationState | 'unsupported' >( 'finished' ), [ error , setError ] = useState ( '' ), [ spoken , setSpoken ] = useState ( '' ); 16const controller = useRef < ReturnType < typeof createDictation > | null >( null ); 17const latest = useRef ({ draft, onDraft}); 18latest . current = { draft, onDraft}; 19const active = useRef ( false ), base = useRef ( '' ), written = useRef ( '' ), manualEdit = useRef ( false ); 20const toggleRef = useRef <() => void >(() => { }); 21const constructor = ( window as unknown as { 22SpeechRecognition ?:RecognizerConstructor ; 23webkitSpeechRecognition ?:RecognizerConstructor ; 24}). SpeechRecognition ?? ( window as unknown as { 25webkitSpeechRecognition ?:RecognizerConstructor ; 26}). webkitSpeechRecognition ; 27const binding = getCommands (). find ( c => c . id === 'dictation' )?. binding ?? null ; 28const shortcut = bindingKeys ( binding , MOD ). map ( k => MOD === '⌘' && k === 'Shift' ?'⇧' :MOD === '⌘' && k === 'Alt' ?'⌥' :k ). join ( MOD === '⌘' ?'' :'+' ); 29const ariaShortcut = binding ?[ ...( binding . mod ?[ MOD === '⌘' ?'Meta' :'Control' ] :[]), ...( binding . alt ?[ 'Alt' ] :[]), ...( binding . shift ?[ 'Shift' ] :[]), binding . key . toUpperCase ()]. join ( '+' ) :undefined ; 30const listening = [ 'starting' , 'listening' , 'speaking' , 'finishing' ]. includes ( state ); 31function close () { controller . current ?. dispose (); controller . current = null ; active . current = false ; setState ( 'finished' ); setVisible ( false ); document . querySelector < HTMLTextAreaElement >( '.st-composer textarea' )?. focus (); } 32function toggle () { 33if ( active . current ) { 34controller . current ?. stop (); 35return ; 36} 37if ( disabled ) 38return ; 39controller . current ?. dispose (); 40controller . current = null ; 41setVisible ( true ); 42setError ( '' ); 43setSpoken ( '' ); 44if ( ! constructor ) { 45setState ( 'unsupported' ); 46return ; 47} 48base . current = latest . current . draft ; 49written . current = base . current ; 50manualEdit . current = false ; 51try { 52const recognizer = new constructor (); 53controller . current = createDictation ( recognizer , { lang :navigator . language || 'en-US' , onState :( next , message ) => { 54active . current = [ 'starting' , 'listening' , 'speaking' , 'finishing' ]. includes ( next ); 55setState ( next ); 56if ( message ) 57setError ( message ); 58}, onText :text => { 59setSpoken ( text ); 60if ( manualEdit . current ) 61return ; 62const value = ` ${ base . current } ${ base . current && text && ! /\s$ / . test ( base . current ) ? ' ' : '' } ${ text } ` ; 63written . current = value ; 64latest . current . onDraft ( value ); 65} }); 66controller . current . start (); 67} 68catch { 69active . current = false ; 70setState ( 'error' ); 71setError ( 'Speech recognition is unavailable in this browser. Use your computer’s built-in dictation.' ); 72} 73} 74toggleRef . current = toggle ; 75useEffect (() => { 76const event = () => { 77if ( ! document . querySelector ( '.dialog-layer' )) 78toggleRef . current (); 79}; 80window . addEventListener ( 'strata:dictation' , event ); 81return () => window . removeEventListener ( 'strata:dictation' , event ); 82}, []); 83useEffect (() => { controller . current ?. dispose (); controller . current = null ; active . current = false ; setState ( 'finished' ); setVisible ( false ); return () => controller . current ?. dispose (); }, [ sessionId ]); 84useEffect (() => { 85if ( ! visible ) 86return ; 87const escape = ( event :KeyboardEvent ) => { 88if ( event . key === 'Escape' && ! event . defaultPrevented && ! document . querySelector ( '.dialog-layer' )) { 89event . preventDefault (); 90close (); 91} 92}; 93document . addEventListener ( 'keydown' , escape ); 94return () => document . removeEventListener ( 'keydown' , escape ); 95}, [ visible ]); 96useEffect (() => { 97if ( disabled && active . current ) 98controller . current ?. stop (); 99}, [ disabled ]); 100useEffect (() => { 101if ( active . current && draft !== written . current ) { 102manualEdit . current = true ; 103controller . current ?. stop (); 104} 105}, [ draft ]); 106const title = state === 'unsupported' ?'Dictation unavailable' :state === 'error' ?'Dictation stopped' :state === 'finished' ?'Dictation finished' :state === 'starting' ?'Starting microphone…' :state === 'finishing' ?'Finishing dictation…' :state === 'speaking' ?'Listening to you…' :'Ready to listen' ; 107return <> 108< button className = "composer-mic" data-speech-supported = { !! constructor } aria-label = { tr ( "Speech to text" )} title = { tr ( "Speech to text \u00B7 {0}" , [ shortcut ])} aria-keyshortcuts = { ariaShortcut } disabled = { disabled && ! listening } aria-pressed = { listening } aria-controls = { visible ?'dictation-feedback' :undefined } onClick = { toggle }>< Icon name = "mic" size = { 19 }/></ button > 109{ tr ( visible ?createPortal (< aside id = "dictation-feedback" className = "dictation-feedback" aria-label = { tr ( "Dictation" )} data-active = { listening || undefined }> 110< div className = "dictation-feedback__head" >< span className = "dictation-feedback__icon" >< Icon name = { state === 'finished' ?'check' :'mic' } size = { 19 }/></ span >< strong role = "status" >{ tr ( title )}</ strong >< IconBtn name = "x" title = { tr ( "Close dictation" )} onClick = { close }/></ div > 111{ tr ( listening ?< div className = "voice-wave" data-speaking = { state === 'speaking' || undefined } aria-hidden = "true" >{ tr ([ 0 , 1 , 2 , 3 , 4 ]. map ( i => < span key = { i } style = {{ animationDelay :` ${ i * 100 } ms` }}/>))}</ div > :null )} 112< p className = "small muted" >{ tr ( state === 'unsupported' ?'This browser does not provide speech recognition. Use your computer’s built-in dictation in the message box.' :state === 'error' ?error :state === 'finished' ?( spoken ?'Text is in your message box. Review it before sending.' :'No text was added. Your draft is ready to edit.' ) :'Speak naturally. A 3.5-second pause finishes dictation. Text appears in your message box without sending.' )}</ p > 113{ tr ( spoken ?< p className = "dictation-feedback__transcript" >{ tr ( spoken )}</ p > :null )} 114< div className = "dictation-feedback__actions" >< kbd >{ tr ( shortcut )}</ kbd >{ tr ( listening ?< Btn size = "sm" onClick = {() => controller . current ?. stop ()} disabled = { state === 'finishing' }>{ tr ( "Stop" )}</ Btn > :<>{ tr ( constructor ?< Btn size = "sm" onClick = { toggle }>{ tr ( state === 'error' ?'Try again' :'Speak again' )}</ Btn > :null )}< Btn size = "sm" onClick = { close }>{ tr ( "Done" )}</ Btn ></>)}</ div > 115{ tr ( listening ?< p className = "xs muted dictation-feedback__provider" >{ tr ( "Uses your browser\u2019s speech service." )}</ p > :null )} 116</ aside >, document . body ) :null )} 117</>; 118}