@@ -5,6 +5,30 @@ import {Node} from "../../manifold/mod.tsx";
55import { Menu } from "../menu/mod.tsx" ;
66import { CommandPalette } from "../palette/mod.tsx" ;
77
8+ // Run this only once, it's unlikely the OS will change without a reload of the page
9+ const osType = ( ( ) => {
10+ if ( navigator . userAgent . toLowerCase ( ) . indexOf ( "win" ) != - 1 ) return "win" ;
11+ if ( navigator . userAgent . toLowerCase ( ) . indexOf ( "mac" ) != - 1 ) return "mac" ;
12+ if ( navigator . userAgent . toLowerCase ( ) . indexOf ( "linux" ) != - 1 ) return "linux" ;
13+ if ( navigator . userAgent . toLowerCase ( ) . indexOf ( "x11" ) != - 1 ) return "unix" ;
14+ return "unknown" ;
15+ } ) ( ) ;
16+
17+ // Returns an os-specific key from an object, the wildcard, or the first
18+ // Allows to define configurations that differ based on the OS the page is viewed from
19+ const osSpecific = function < T = unknown > ( options : Record < string , T > ) : T {
20+
21+ // Sanity checking
22+ const keys = Object . keys ( options ) ;
23+ if ( keys . length <= 0 ) {
24+ throw new Error ( "Invalid options record object given, must contain at least one option" ) ;
25+ }
26+
27+ // Return os-specific, fallback, or first, in that order of preference
28+ if ( keys . includes ( osType ) ) return options [ osType ] ;
29+ if ( keys . includes ( "*" ) ) return options [ "*" ] ;
30+ return options [ keys [ 0 ] ] ;
31+ } ;
832
933window . env = new Environment ( new LocalStorageStore ( ) ) ;
1034env . commands . registerCommand ( {
@@ -16,7 +40,7 @@ env.commands.registerCommand({
1640 m . redraw ( ) ;
1741 }
1842} ) ;
19- env . keybindings . registerBinding ( { command : "expand" , key : " meta+arrowdown"} ) ;
43+ env . keybindings . registerBinding ( { command : "expand" , key : osSpecific ( { "mac" : " meta+arrowdown", "*" : "ctrl+arrowdown" } ) } ) ;
2044env . commands . registerCommand ( {
2145 id : "collapse" ,
2246 title : "Collapse" ,
@@ -26,7 +50,7 @@ env.commands.registerCommand({
2650 m . redraw ( ) ;
2751 }
2852} ) ;
29- env . keybindings . registerBinding ( { command : "collapse" , key : " meta+arrowup"} ) ;
53+ env . keybindings . registerBinding ( { command : "collapse" , key : osSpecific ( { "mac" : " meta+arrowup", "*" : "ctrl+arrowup" } ) } ) ;
3054env . commands . registerCommand ( {
3155 id : "indent" ,
3256 title : "Indent" ,
@@ -106,7 +130,7 @@ env.commands.registerCommand({
106130 }
107131 }
108132} ) ;
109- env . keybindings . registerBinding ( { command : "delete" , key : " shift+meta+backspace"} ) ;
133+ env . keybindings . registerBinding ( { command : "delete" , key : osSpecific ( { "mac" : " shift+meta+backspace", "*" : "shift+ctrl+backspace" } ) } ) ;
110134env . commands . registerCommand ( {
111135 id : "prev" ,
112136 action : ( ctx : Context ) => {
@@ -180,4 +204,4 @@ export const App: m.Component = {
180204 { env . workspace . palette && < CommandPalette { ...env . workspace . palette } /> }
181205 </ main >
182206 }
183- } ;
207+ } ;
0 commit comments