diff --git a/apps/web/src/common/key-map.ts b/apps/web/src/common/key-map.ts index 40aa4ad15..3e7e45d01 100644 --- a/apps/web/src/common/key-map.ts +++ b/apps/web/src/common/key-map.ts @@ -23,8 +23,9 @@ import { useStore as useSearchStore } from "../stores/search-store"; import { useEditorManager } from "../components/editor/manager"; import { CommandPaletteDialog } from "../dialogs/command-palette"; import { hashNavigate } from "../navigation"; -import { keybindings } from "@notesnook/common"; +import { getKeybinding, keybindings } from "@notesnook/common"; import { KeyboardShortcutsDialog } from "../dialogs/keyboard-shortcuts-dialog"; +import { isMac } from "../utils/platform"; function isInEditor(e: KeyboardEvent) { return ( @@ -32,194 +33,68 @@ function isInEditor(e: KeyboardEvent) { ); } -const KEYMAP = [ - // { - // keys: ["command+n", "ctrl+n", "command+alt+n", "ctrl+alt+n"], - // description: "Create a new note", - // global: true, - // action: (e) => { - // e.preventDefault(); - // hashNavigate("/notes/create", { - // addNonce: true, - // replace: true, - // notify: true, - // }); - // }, - // }, - // { - // keys: [ - // "command+shift+n", - // "ctrl+shift+n", - // "command+shift+alt+n", - // "ctrl+shift+alt+n", - // ], - // description: "Create a new notebook", - // global: true, - // action: (e) => { - // e.preventDefault(); - // hashNavigate("/notebooks/create", { - // replace: true, - // notify: true, - // }); - // }, - // }, - { - keys: keybindings.nextTab.keys(IS_DESKTOP_APP), - description: keybindings.nextTab.description, - action: () => useEditorStore.getState().focusNextTab() - }, - { - keys: keybindings.previousTab.keys(IS_DESKTOP_APP), - description: keybindings.previousTab.description, - action: () => useEditorStore.getState().focusPreviousTab() - }, - { - keys: keybindings.newTab.keys(IS_DESKTOP_APP), - description: keybindings.newTab.description, - action: () => useEditorStore.getState().addTab() - }, - { - keys: keybindings.newNote.keys(IS_DESKTOP_APP), - description: keybindings.newNote.description, - action: () => useEditorStore.getState().newSession() - }, - { - keys: keybindings.closeActiveTab.keys(IS_DESKTOP_APP), - description: keybindings.closeActiveTab.description, - action: () => { - const activeTab = useEditorStore.getState().getActiveTab(); - if (activeTab?.pinned) { - useEditorStore.getState().focusLastActiveTab(); - return; - } - useEditorStore.getState().closeActiveTab(); +const actions: Partial< + Record void) | ((e: KeyboardEvent) => void)> +> = { + nextTab: () => useEditorStore.getState().focusNextTab(), + previousTab: () => useEditorStore.getState().focusPreviousTab(), + newTab: () => useEditorStore.getState().addTab(), + newNote: () => useEditorStore.getState().newSession(), + closeActiveTab: () => { + const activeTab = useEditorStore.getState().getActiveTab(); + if (activeTab?.pinned) { + useEditorStore.getState().focusLastActiveTab(); + return; } + useEditorStore.getState().closeActiveTab(); }, - { - keys: keybindings.closeAllTabs.keys(IS_DESKTOP_APP), - description: keybindings.closeAllTabs.description, - action: () => useEditorStore.getState().closeAllTabs() - }, - { - keys: keybindings.searchInNotes.keys(IS_DESKTOP_APP), - description: keybindings.searchInNotes.description, - global: false, - action: (e: KeyboardEvent) => { - if (isInEditor(e)) { - const activeSession = useEditorStore.getState().getActiveSession(); - if (activeSession?.type === "readonly") { - e.preventDefault(); - const editor = useEditorManager - .getState() - .getEditor(activeSession.id); - editor?.editor?.startSearch(); - } - return; + closeAllTabs: () => useEditorStore.getState().closeAllTabs(), + searchInNotes: (e: KeyboardEvent) => { + if (isInEditor(e)) { + const activeSession = useEditorStore.getState().getActiveSession(); + if (activeSession?.type === "readonly") { + e.preventDefault(); + const editor = useEditorManager.getState().getEditor(activeSession.id); + editor?.editor?.startSearch(); } - e.preventDefault(); + return; + } + e.preventDefault(); - useSearchStore.setState({ isSearching: true, searchType: "notes" }); - } + useSearchStore.setState({ isSearching: true, searchType: "notes" }); }, - // { - // keys: ["alt+n"], - // description: "Go to Notes", - // global: false, - // action: (e) => { - // e.preventDefault(); - // navigate("/notes"); - // }, - // }, - // { - // keys: ["alt+b"], - // description: "Go to Notebooks", - // global: false, - // action: (e) => { - // e.preventDefault(); - // navigate("/notebooks"); - // }, - // }, - // { - // keys: ["alt+f"], - // description: "Go to Favorites", - // global: false, - // action: (e) => { - // e.preventDefault(); - // navigate("/favorites"); - // }, - // }, - // { - // keys: ["alt+t"], - // description: "Go to Tags", - // global: false, - // action: (e) => { - // e.preventDefault(); - // navigate("/tags"); - // }, - // }, - // { - // keys: ["alt+d"], - // description: "Go to Trash", - // global: false, - // action: (e) => { - // e.preventDefault(); - // navigate("/trash"); - // }, - // }, - // { - // keys: ["alt+s"], - // description: "Go to Settings", - // global: false, - // action: (e) => { - // e.preventDefault(); - // navigate("/settings"); - // }, - // }, - // { - // keys: ["command+d", "ctrl+d"], - // description: "Toggle dark/light mode", - // global: true, - // action: (e) => { - // e.preventDefault(); - // themestore.get().toggleNightMode(); - // }, - // }, - { - keys: keybindings.openCommandPalette - .keys(IS_DESKTOP_APP) - .concat(keybindings.openQuickOpen.keys(IS_DESKTOP_APP)), - description: keybindings.openCommandPalette.description, - action: (e: KeyboardEvent) => { - e.preventDefault(); - CommandPaletteDialog.close(); - CommandPaletteDialog.show({ - isCommandMode: e.key === "k" - }).catch(() => {}); - } + openCommandPalette: () => { + CommandPaletteDialog.close(); + CommandPaletteDialog.show({ + isCommandMode: true + }).catch(() => {}); }, - { - keys: keybindings.openSettings.keys(IS_DESKTOP_APP), - description: keybindings.openSettings.description, - action: () => hashNavigate("/settings", { replace: true }) + openQuickOpen: () => { + CommandPaletteDialog.close(); + CommandPaletteDialog.show({ + isCommandMode: false + }).catch(() => {}); }, - { - keys: keybindings.openKeyboardShortcuts.keys(IS_DESKTOP_APP), - description: keybindings.openKeyboardShortcuts.description, - action: () => KeyboardShortcutsDialog.show({}) - } -]; + openSettings: () => hashNavigate("/settings", { replace: true }), + openKeyboardShortcuts: () => KeyboardShortcutsDialog.show({}) +}; export function registerKeyMap() { hotkeys.filter = function () { return true; }; - console.log("KEYMAP", KEYMAP); - KEYMAP.forEach((key) => { - if (key.keys.length === 0) return; - hotkeys(key.keys.join(","), (e) => { + Object.entries(actions).forEach(([id, action]) => { + const keys = getKeybinding( + id as keyof typeof keybindings, + IS_DESKTOP_APP, + isMac() + ); + if (!keys || keys.length === 0) return; + + hotkeys(keys.join(","), (e) => { e.preventDefault(); - key.action?.(e); + action(e); }); }); } diff --git a/packages/common/src/utils/keybindings.ts b/packages/common/src/utils/keybindings.ts index 5e5a99c4b..6eb5868e0 100644 --- a/packages/common/src/utils/keybindings.ts +++ b/packages/common/src/utils/keybindings.ts @@ -367,6 +367,22 @@ export const keybindings = { ...tiptapKeys }; +export function getKeybinding( + key: keyof typeof keybindings, + isDesktop = false, + isMac = false +) { + const keybinding = keybindings[key]; + if (keybinding.type === "hotkeys") { + const hotkeys = keybinding.keys(isDesktop); + return isMac ? hotkeys.map(macify) : hotkeys; + } + const tiptapKeys = Array.isArray(keybinding.keys) + ? keybinding.keys + : [keybinding.keys]; + return isMac ? tiptapKeys.map(macify) : tiptapKeys; +} + function normalizeKeys( keys: string[] | { web?: string[]; desktop?: string[] } ): (isDesktop?: boolean) => string[] {