Spaces:
Running
Running
| export interface Shortcut { | |
| key: string; | |
| ctrl?: boolean; | |
| meta?: boolean; | |
| shift?: boolean; | |
| alt?: boolean; | |
| action: () => void; | |
| description: string; | |
| } | |
| export const shortcuts: Shortcut[] = [ | |
| { | |
| key: "e", | |
| ctrl: true, | |
| meta: true, | |
| action: () => { | |
| import("../stores/ui").then(({ uiStore }) => { | |
| uiStore.toggleViewMode(); | |
| }); | |
| }, | |
| description: "Toggle between code and preview mode", | |
| }, | |
| ]; | |
| export function registerShortcuts(shortcuts: Shortcut[]): () => void { | |
| const handler = (e: KeyboardEvent) => { | |
| for (const shortcut of shortcuts) { | |
| const ctrlMatch = shortcut.ctrl | |
| ? e.ctrlKey | |
| : !shortcut.ctrl || !e.ctrlKey; | |
| const metaMatch = shortcut.meta | |
| ? e.metaKey | |
| : !shortcut.meta || !e.metaKey; | |
| const shiftMatch = shortcut.shift | |
| ? e.shiftKey | |
| : !shortcut.shift || !e.shiftKey; | |
| const altMatch = shortcut.alt ? e.altKey : !shortcut.alt || !e.altKey; | |
| if ( | |
| e.key === shortcut.key && | |
| (ctrlMatch || metaMatch) && | |
| shiftMatch && | |
| altMatch | |
| ) { | |
| e.preventDefault(); | |
| shortcut.action(); | |
| break; | |
| } | |
| } | |
| }; | |
| window.addEventListener("keydown", handler); | |
| return () => { | |
| window.removeEventListener("keydown", handler); | |
| }; | |
| } | |