okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1import { tr } from '../lib/i18n' ; 2import { useState } from 'react' ; 3import { Dialog , Btn , IconBtn } from '../components/ui' ; 4import { setShortcuts , useStore } from '../lib/store' ; 5import { getCommands , MOD } from '../lib/commands' ; 6import { bindingFor , bindingEqual , bindingKeys , eventBinding , resetBindings , setBinding , useBindings } from '../lib/shortcut-bindings' ; 7export function ShortcutsDialog () { 8const open = useStore ( s => s . shortcutsOpen ); 9useBindings (); 10const [ recording , setRecording ] = useState < string | null >( null ), [ error , setError ] = useState ( '' ); 11const rows = [ ...getCommands (). filter ( c => c . key ). map ( c => ({ id :c . id , label :c . label , binding :c . binding ?? null , group :c . group })), { id :'chat:send' , label :'Send message' , binding :bindingFor ( 'chat:send' , { key :'enter' , mod :false , alt :false , shift :false }), group :'Chat' }]; 12function record ( id :string , e :React . KeyboardEvent ) { 13e . preventDefault (); 14e . stopPropagation (); 15if ([ 'Meta' , 'Control' , 'Shift' , 'Alt' ]. includes ( e . key )) 16return ; 17if ( e . key === 'Escape' ) { 18setRecording ( null ); 19setError ( '' ); 20return ; 21} 22const b = eventBinding ( e ); 23if ( id === 'chat:send' ?b . key !== 'enter' || b . alt || b . shift :! b . mod || [ 'tab' , 'escape' , 'enter' ]. includes ( b . key )) { 24setError ( id === 'chat:send' ?'Use Enter or Command/Ctrl + Enter. Shift + Enter stays available for a new line.' :'Use Command/Ctrl with a letter, number or punctuation key.' ); 25return ; 26} 27const duplicate = rows . find ( r => r . id !== id && bindingEqual ( r . binding , b )); 28if ( duplicate ) { 29setError ( `Already assigned to ${ duplicate . label } . Clear or change that binding first.` ); 30return ; 31} 32setBinding ( id , b ); 33setRecording ( null ); 34setError ( '' ); 35} 36return < Dialog open = { open } title = { tr ( "Keyboard shortcuts" )} onClose = {() => { setRecording ( null ); setShortcuts ( false ); }} actions = {<>< Btn onClick = {() => { resetBindings (); setRecording ( null ); setError ( '' ); }}>{ tr ( "Restore defaults" )}</ Btn >< Btn variant = "primary" onClick = {() => { setRecording ( null ); setError ( '' ); setShortcuts ( false ); }}>{ tr ( "Done" )}</ Btn ></>}> 37< p className = "small muted" >{ tr ( "Click a shortcut, then press your preferred keys. Changes are saved in this browser. Escape cancels recording." )}</ p > 38{ tr ( error ?< p className = "inline-error" role = "alert" >{ tr ( error )}</ p > :null )} 39< div className = "shortcut-editor" >{ tr ([ ...new Set ( rows . map ( r => r . group ))]. map ( group => < section key = { group }>< h3 className = "modal__group" >{ tr ( group )}</ h3 >{ tr ( rows . filter ( r => r . group === group ). map ( r => < div className = "shortcutrow" key = { r . id }>< span className = "shortcutrow__label" >{ tr ( r . label )}</ span >< button className = "shortcut-capture" aria-label = { tr ( "Change shortcut for {0}" , [ tr ( r . label )])} data-recording = { recording === r . id || undefined } onClick = {() => { setRecording ( r . id ); setError ( '' ); }} onKeyDown = { e => { 40if ( recording === r . id ) 41record ( r . id , e ); 42}}>{ tr ( recording === r . id ?'Press keys…' :bindingKeys ( r . binding , MOD ). map ( k => < kbd key = { k } className = "kbd" >{ tr ( k )}</ kbd >))}</ button >{ tr ( r . id !== 'chat:send' ?< IconBtn name = "x" title = { tr ( "Clear shortcut for {0}" , [ tr ( r . label )])} disabled = { ! r . binding } onClick = {() => setBinding ( r . id , null )}/> :null )}</ div >))}</ section >))}</ div > 43< p className = "xs muted" >{ tr ( "Shift + Enter adds a new line. Type / for chat commands. Escape closes panels." )}</ p > 44</ Dialog >; 45} 46export { MOD };