okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1/** User bindings are separate from commands, so the dispatcher and labels agree. */ 2import { useSyncExternalStore } from 'react' ; 3export interface Binding { key :string ; mod :boolean ; shift :boolean ; alt :boolean ; } 4const KEY = 'strata.shortcuts.v1' ; 5let overrides :Record < string , Binding | null > = {}; 6try { const raw = JSON . parse ( localStorage . getItem ( KEY ) ?? '{}' ); for ( const [ id , b ] of Object . entries ( raw )) if ( b === null || ( typeof b === 'object' && b && typeof ( b as Binding ). key === 'string' && [ 'mod' , 'shift' , 'alt' ]. every ( k => typeof ( b as Record < string , unknown >)[ k ] === 'boolean' ))) overrides [ id ] = b as Binding | null ; } catch { /* Defaults remain available. */ } 7const listeners = new Set <() => void >(); 8export const bindingFor = ( id :string , fallback :Binding | null ) :Binding | null => Object . hasOwn ( overrides , id ) ?overrides [ id ] :fallback ; 9export function setBinding ( id :string , value :Binding | null | undefined ) { overrides = { ...overrides }; if ( value === undefined ) delete overrides [ id ]; else overrides [ id ] = value ; localStorage . setItem ( KEY , JSON . stringify ( overrides )); listeners . forEach ( fn => fn ()); } 10export function resetBindings (){ overrides = {}; localStorage . removeItem ( KEY ); listeners . forEach ( fn => fn ());} 11export const useBindings = () => useSyncExternalStore ( fn => { listeners . add ( fn ); return () => listeners . delete ( fn );},() => overrides ); 12export const bindingEqual = ( a :Binding | null , b :Binding | null ) =>!! a &&!! b && a . key === b . key && a . mod === b . mod && a . shift === b . shift && a . alt === b . alt ; 13export function eventBinding ( e :Pick < KeyboardEvent , 'key' | 'metaKey' | 'ctrlKey' | 'shiftKey' | 'altKey' > & Partial < Pick < KeyboardEvent , 'code' >>) :Binding { 14// Option changes digit characters on macOS; navigation still uses the physical digit. 15const key = e . altKey && / ^Digit[0-9]$ / . test ( e . code ?? '' ) ?e . code ! . slice ( 5 ) :e . key ; 16return { key :key . toLowerCase (), mod :e . metaKey || e . ctrlKey , shift :e . shiftKey , alt :e . altKey }; 17} 18export function bindingKeys ( b :Binding | null , mod :string ) { return b ?[ ...( b . mod ?[ mod ] :[]), ...( b . alt ?[ 'Alt' ] :[]), ...( b . shift ?[ 'Shift' ] :[]), b . key === ' ' ?'Space' :b . key . length === 1 ?b . key . toUpperCase () :b . key ] :[ 'Unassigned' ]; } 19export function composerSends ( e :Pick < KeyboardEvent , 'key' | 'metaKey' | 'ctrlKey' | 'shiftKey' | 'altKey' >){ return bindingEqual ( eventBinding ( e ), bindingFor ( 'chat:send' ,{ key :'enter' , mod :false , shift :false , alt :false }));}