diff --git a/apps/desktop/layer/renderer/package.json b/apps/desktop/layer/renderer/package.json index 0897a59ae..366ea43a8 100644 --- a/apps/desktop/layer/renderer/package.json +++ b/apps/desktop/layer/renderer/package.json @@ -88,6 +88,7 @@ "react-zoom-pan-pinch": "3.7.0", "shiki": "3.3.0", "sonner": "2.0.3", + "tinykeys": "3.0.0", "tldts": "7.0.4", "use-context-selector": "2.0.0", "use-sync-external-store": "1.5.0", diff --git a/apps/desktop/layer/renderer/src/constants/hotkeys.ts b/apps/desktop/layer/renderer/src/constants/hotkeys.ts index 90a797b8b..ba62a0b8d 100644 --- a/apps/desktop/layer/renderer/src/constants/hotkeys.ts +++ b/apps/desktop/layer/renderer/src/constants/hotkeys.ts @@ -1,6 +1,6 @@ export const HotKeyScopeMap = { - Home: ["home"], - Menu: ["menu"], - Modal: ["modal"], - DropdownMenu: ["dropdown-menu"], + Home: "home", + Menu: "menu", + Modal: "modal", + DropdownMenu: "dropdown-menu", } diff --git a/apps/desktop/layer/renderer/src/hooks/common/useSwitchHotkeyScope.ts b/apps/desktop/layer/renderer/src/hooks/common/useSwitchHotkeyScope.ts index b24c298a3..9f0c88be5 100644 --- a/apps/desktop/layer/renderer/src/hooks/common/useSwitchHotkeyScope.ts +++ b/apps/desktop/layer/renderer/src/hooks/common/useSwitchHotkeyScope.ts @@ -4,7 +4,7 @@ import { useHotkeysContext } from "react-hotkeys-hook" import { HotKeyScopeMap } from "~/constants" const allScopes = Object.keys(HotKeyScopeMap).reduce((acc, key) => { - acc.push(...HotKeyScopeMap[key]) + acc.push(HotKeyScopeMap[key]) return acc }, [] as string[]) export const useSwitchHotKeyScope = () => { @@ -18,9 +18,7 @@ export const useSwitchHotKeyScope = () => { for (const key of allScopes) { disableScope(key) } - for (const key of nextScope) { - enableScope(key) - } + enableScope(nextScope) }, [disableScope, enableScope], ) @@ -34,15 +32,11 @@ export const useHotkeyScopeFn = (scope: keyof typeof HotKeyScopeMap) => { for (const key of allScopes) { disableScope(key) } - for (const key of scope) { - enableScope(key) - } + enableScope(scope) return () => { - for (const key of scope) { - disableScope(key) - } - for (const scope of currentScope) { - enableScope(scope) + disableScope(scope) + for (const key of currentScope) { + enableScope(key) } } }, [enableScope, disableScope, scope]) diff --git a/apps/desktop/layer/renderer/src/modules/app-layout/feed-column/desktop.tsx b/apps/desktop/layer/renderer/src/modules/app-layout/feed-column/desktop.tsx index 1d94a8f57..fd4a30045 100644 --- a/apps/desktop/layer/renderer/src/modules/app-layout/feed-column/desktop.tsx +++ b/apps/desktop/layer/renderer/src/modules/app-layout/feed-column/desktop.tsx @@ -12,7 +12,7 @@ import { debounce } from "es-toolkit/compat" import type { PropsWithChildren } from "react" import * as React from "react" import { Suspense, useEffect, useRef, useState } from "react" -import { useHotkeys } from "react-hotkeys-hook" +import { useHotkeysContext } from "react-hotkeys-hook" import { Trans } from "react-i18next" import { useResizable } from "react-resizable-layout" import { Outlet } from "react-router" @@ -40,8 +40,9 @@ import { useI18n } from "~/hooks/common" import { EnvironmentIndicator } from "~/modules/app/EnvironmentIndicator" import { NetworkStatusIndicator } from "~/modules/app/NetworkStatusIndicator" import { LoginModalContent } from "~/modules/auth/LoginModalContent" +import { COMMAND_ID } from "~/modules/command/commands/id" +import { useCommandHotkey } from "~/modules/command/hooks/use-register-hotkey" import { DebugRegistry } from "~/modules/debug/registry" -import { useShortcutsModal } from "~/modules/modal/shortcuts" import { CmdF } from "~/modules/panel/cmdf" import { SearchCmdK } from "~/modules/panel/cmdk" import { CmdNTrigger } from "~/modules/panel/cmdn" @@ -241,17 +242,12 @@ const FeedResponsiveResizerContainer = ({ } }, [feedColumnShow]) - useHotkeys( - shortcuts.layout.toggleSidebar.key, - () => { - setTimelineColumnShow(!feedColumnShow) - }, - { - scopes: HotKeyScopeMap.Home, - }, - ) - const showShortcuts = useShortcutsModal() - useHotkeys(shortcuts.layout.showShortcuts.key, showShortcuts) + const { activeScopes } = useHotkeysContext() + useCommandHotkey({ + commandId: COMMAND_ID.layout.toggleTimelineColumn, + shortcut: shortcuts.layout.toggleSidebar.key, + when: activeScopes.includes(HotKeyScopeMap.Home), + }) const [delayShowSplitter, setDelayShowSplitter] = useState(feedColumnShow) diff --git a/apps/desktop/layer/renderer/src/modules/command/command-manager.ts b/apps/desktop/layer/renderer/src/modules/command/command-manager.ts index 63eb4b674..d78cede38 100644 --- a/apps/desktop/layer/renderer/src/modules/command/command-manager.ts +++ b/apps/desktop/layer/renderer/src/modules/command/command-manager.ts @@ -1,16 +1,16 @@ import { useRegisterEntryCommands } from "./commands/entry" +import { useRegisterGlobalCommands } from "./commands/global" import { useRegisterIntegrationCommands } from "./commands/integration" +import { useRegisterLayoutCommands } from "./commands/layout" import { useRegisterListCommands } from "./commands/list" import { useRegisterSettingsCommands } from "./commands/settings" -export function useRegisterFollowCommands() { +export const FollowCommandManager = () => { useRegisterSettingsCommands() useRegisterListCommands() useRegisterEntryCommands() useRegisterIntegrationCommands() -} - -export const FollowCommandManager = () => { - useRegisterFollowCommands() + useRegisterGlobalCommands() + useRegisterLayoutCommands() return null } diff --git a/apps/desktop/layer/renderer/src/modules/command/commands/entry.tsx b/apps/desktop/layer/renderer/src/modules/command/commands/entry.tsx index ad6654795..f0eb7d157 100644 --- a/apps/desktop/layer/renderer/src/modules/command/commands/entry.tsx +++ b/apps/desktop/layer/renderer/src/modules/command/commands/entry.tsx @@ -28,6 +28,7 @@ import { useTipModal } from "~/modules/wallet/hooks" import { entryActions, useEntryStore } from "~/store/entry" import { useRegisterFollowCommand } from "../hooks/use-register-command" +import type { Command } from "../types" import { COMMAND_ID } from "./id" const useCollect = () => { @@ -421,3 +422,107 @@ export const useRegisterEntryCommands = () => { }, ) } + +export type TipCommand = Command<{ + id: typeof COMMAND_ID.entry.tip + fn: (data: { userId?: string | null; feedId?: string; entryId?: string }) => void +}> + +export type StarCommand = Command<{ + id: typeof COMMAND_ID.entry.star + fn: (data: { entryId: string; view?: FeedViewType }) => void +}> + +export type DeleteCommand = Command<{ + id: typeof COMMAND_ID.entry.delete + fn: (data: { entryId: string }) => void +}> + +export type CopyLinkCommand = Command<{ + id: typeof COMMAND_ID.entry.copyLink + fn: (data: { entryId: string }) => void +}> + +export type ExportAsPDFCommand = Command<{ + id: typeof COMMAND_ID.entry.exportAsPDF + fn: (data: { entryId: string }) => void +}> + +export type CopyTitleCommand = Command<{ + id: typeof COMMAND_ID.entry.copyTitle + fn: (data: { entryId: string }) => void +}> + +export type OpenInBrowserCommand = Command<{ + id: typeof COMMAND_ID.entry.openInBrowser + fn: (data: { entryId: string }) => void +}> + +export type ViewSourceContentCommand = Command<{ + id: typeof COMMAND_ID.entry.viewSourceContent + fn: (data: { entryId: string; siteUrl?: string | null | undefined }) => void +}> + +export type ShareCommand = Command<{ + id: typeof COMMAND_ID.entry.share + fn: (data: { entryId: string }) => void +}> + +export type ReadCommand = Command<{ + id: typeof COMMAND_ID.entry.read + fn: (data: { entryId: string }) => void +}> + +export type ReadAboveCommand = Command<{ + id: typeof COMMAND_ID.entry.readAbove + fn: (data: { publishedAt: string }) => void +}> + +export type ReadBelowCommand = Command<{ + id: typeof COMMAND_ID.entry.readBelow + fn: (data: { publishedAt: string }) => void +}> + +export type ToggleAISummaryCommand = Command<{ + id: typeof COMMAND_ID.entry.toggleAISummary + fn: () => void +}> + +export type ToggleAITranslationCommand = Command<{ + id: typeof COMMAND_ID.entry.toggleAITranslation + fn: () => void +}> + +export type ImageGalleryCommand = Command<{ + id: typeof COMMAND_ID.entry.imageGallery + fn: (data: { entryId: string }) => void +}> + +export type TTSCommand = Command<{ + id: typeof COMMAND_ID.entry.tts + fn: (data: { entryId: string; entryContent: string }) => void +}> + +export type ReadabilityCommand = Command<{ + id: typeof COMMAND_ID.entry.readability + fn: (data: { entryId: string; entryUrl: string }) => void +}> + +export type EntryCommand = + | TipCommand + | StarCommand + | DeleteCommand + | CopyLinkCommand + | ExportAsPDFCommand + | CopyTitleCommand + | OpenInBrowserCommand + | ViewSourceContentCommand + | ShareCommand + | ReadCommand + | ReadAboveCommand + | ReadBelowCommand + | ToggleAISummaryCommand + | ToggleAITranslationCommand + | ImageGalleryCommand + | TTSCommand + | ReadabilityCommand diff --git a/apps/desktop/layer/renderer/src/modules/command/commands/global.tsx b/apps/desktop/layer/renderer/src/modules/command/commands/global.tsx new file mode 100644 index 000000000..60fd1a3d2 --- /dev/null +++ b/apps/desktop/layer/renderer/src/modules/command/commands/global.tsx @@ -0,0 +1,26 @@ +import { useShortcutsModal } from "~/modules/modal/shortcuts" + +import { useRegisterCommandEffect } from "../hooks/use-register-command" +import type { Command } from "../types" +import { COMMAND_ID } from "./id" + +export const useRegisterGlobalCommands = () => { + const showShortcuts = useShortcutsModal() + + useRegisterCommandEffect([ + { + id: COMMAND_ID.global.showShortcuts, + label: "Show shortcuts", + run: () => { + showShortcuts() + }, + }, + ]) +} + +export type ShowShortcutsCommand = Command<{ + id: typeof COMMAND_ID.global.showShortcuts + fn: () => void +}> + +export type GlobalCommand = ShowShortcutsCommand diff --git a/apps/desktop/layer/renderer/src/modules/command/commands/id.ts b/apps/desktop/layer/renderer/src/modules/command/commands/id.ts index 5790d9a24..6c6ac541a 100644 --- a/apps/desktop/layer/renderer/src/modules/command/commands/id.ts +++ b/apps/desktop/layer/renderer/src/modules/command/commands/id.ts @@ -41,4 +41,10 @@ export const COMMAND_ID = { changeThemeToLight: "follow:change-color-mode-to-light", customizeToolbar: "follow:customize-toolbar", }, + global: { + showShortcuts: "global:show-shortcuts", + }, + layout: { + toggleTimelineColumn: "layout:toggle-timeline-column", + }, } as const diff --git a/apps/desktop/layer/renderer/src/modules/command/commands/integration.tsx b/apps/desktop/layer/renderer/src/modules/command/commands/integration.tsx index 508b97e4b..7895ad87e 100644 --- a/apps/desktop/layer/renderer/src/modules/command/commands/integration.tsx +++ b/apps/desktop/layer/renderer/src/modules/command/commands/integration.tsx @@ -28,6 +28,7 @@ import { useEntryStore } from "~/store/entry" import { useRegisterCommandEffect } from "../hooks/use-register-command" import { defineFollowCommand } from "../registry/command" +import type { Command } from "../types" import { COMMAND_ID } from "./id" export const useRegisterIntegrationCommands = () => { @@ -558,3 +559,47 @@ const buildMemoRequestBody = (entry: FlatEntryModel, selectedText: string) => { source_url: entry.entries.url, } } + +export type SaveToEagleCommand = Command<{ + id: typeof COMMAND_ID.integration.saveToEagle + fn: (payload: { entryId: string }) => void +}> + +export type SaveToReadwiseCommand = Command<{ + id: typeof COMMAND_ID.integration.saveToReadwise + fn: (payload: { entryId: string }) => void +}> + +export type SaveToInstapaperCommand = Command<{ + id: typeof COMMAND_ID.integration.saveToInstapaper + fn: (payload: { entryId: string }) => void +}> + +export type SaveToObsidianCommand = Command<{ + id: typeof COMMAND_ID.integration.saveToObsidian + fn: (payload: { entryId: string }) => void +}> + +export type SaveToOutlineCommand = Command<{ + id: typeof COMMAND_ID.integration.saveToOutline + fn: (payload: { entryId: string }) => void +}> + +export type SaveToReadeckCommand = Command<{ + id: typeof COMMAND_ID.integration.saveToReadeck + fn: (payload: { entryId: string }) => void +}> + +export type SaveToCuboxCommand = Command<{ + id: typeof COMMAND_ID.integration.saveToCubox + fn: (payload: { entryId: string }) => void +}> + +export type IntegrationCommand = + | SaveToEagleCommand + | SaveToReadwiseCommand + | SaveToInstapaperCommand + | SaveToObsidianCommand + | SaveToOutlineCommand + | SaveToReadeckCommand + | SaveToCuboxCommand diff --git a/apps/desktop/layer/renderer/src/modules/command/commands/layout.tsx b/apps/desktop/layer/renderer/src/modules/command/commands/layout.tsx new file mode 100644 index 000000000..309d3811f --- /dev/null +++ b/apps/desktop/layer/renderer/src/modules/command/commands/layout.tsx @@ -0,0 +1,24 @@ +import { setTimelineColumnShow } from "~/atoms/sidebar" + +import { useRegisterCommandEffect } from "../hooks/use-register-command" +import type { Command } from "../types" +import { COMMAND_ID } from "./id" + +export const useRegisterLayoutCommands = () => { + useRegisterCommandEffect([ + { + id: COMMAND_ID.layout.toggleTimelineColumn, + label: "Toggle timeline column", + run: () => { + setTimelineColumnShow((show) => !show) + }, + }, + ]) +} + +export type ToggleTimelineColumnCommand = Command<{ + id: typeof COMMAND_ID.layout.toggleTimelineColumn + fn: () => void +}> + +export type LayoutCommand = ToggleTimelineColumnCommand diff --git a/apps/desktop/layer/renderer/src/modules/command/commands/settings.tsx b/apps/desktop/layer/renderer/src/modules/command/commands/settings.tsx index 2dc753a12..6231b0992 100644 --- a/apps/desktop/layer/renderer/src/modules/command/commands/settings.tsx +++ b/apps/desktop/layer/renderer/src/modules/command/commands/settings.tsx @@ -5,6 +5,7 @@ import { useSetTheme } from "~/hooks/common" import { useShowCustomizeToolbarModal } from "~/modules/customize-toolbar/modal" import { useRegisterCommandEffect } from "../hooks/use-register-command" +import type { Command } from "../types" import { COMMAND_ID } from "./id" export const useRegisterSettingsCommands = () => { @@ -66,3 +67,10 @@ const useRegisterThemeCommands = () => { }, ]) } + +export type CustomizeToolbarCommand = Command<{ + id: typeof COMMAND_ID.settings.customizeToolbar + fn: () => void +}> + +export type SettingsCommand = CustomizeToolbarCommand diff --git a/apps/desktop/layer/renderer/src/modules/command/commands/types.ts b/apps/desktop/layer/renderer/src/modules/command/commands/types.ts index 17bd050aa..5c3c57689 100644 --- a/apps/desktop/layer/renderer/src/modules/command/commands/types.ts +++ b/apps/desktop/layer/renderer/src/modules/command/commands/types.ts @@ -1,167 +1,14 @@ // Entry commands -import type { FeedViewType } from "@follow/constants" +import type { EntryCommand } from "./entry" +import type { GlobalCommand } from "./global" +import type { IntegrationCommand } from "./integration" +import type { LayoutCommand } from "./layout" +import type { SettingsCommand } from "./settings" -import type { Command } from "../types" -import type { COMMAND_ID } from "./id" - -export type TipCommand = Command<{ - id: typeof COMMAND_ID.entry.tip - fn: (data: { userId?: string | null; feedId?: string; entryId?: string }) => void -}> - -export type StarCommand = Command<{ - id: typeof COMMAND_ID.entry.star - fn: (data: { entryId: string; view?: FeedViewType }) => void -}> - -export type DeleteCommand = Command<{ - id: typeof COMMAND_ID.entry.delete - fn: (data: { entryId: string }) => void -}> - -export type CopyLinkCommand = Command<{ - id: typeof COMMAND_ID.entry.copyLink - fn: (data: { entryId: string }) => void -}> - -export type ExportAsPDFCommand = Command<{ - id: typeof COMMAND_ID.entry.exportAsPDF - fn: (data: { entryId: string }) => void -}> - -export type CopyTitleCommand = Command<{ - id: typeof COMMAND_ID.entry.copyTitle - fn: (data: { entryId: string }) => void -}> - -export type OpenInBrowserCommand = Command<{ - id: typeof COMMAND_ID.entry.openInBrowser - fn: (data: { entryId: string }) => void -}> - -export type ViewSourceContentCommand = Command<{ - id: typeof COMMAND_ID.entry.viewSourceContent - fn: (data: { entryId: string; siteUrl?: string | null | undefined }) => void -}> - -export type ShareCommand = Command<{ - id: typeof COMMAND_ID.entry.share - fn: (data: { entryId: string }) => void -}> - -export type ReadCommand = Command<{ - id: typeof COMMAND_ID.entry.read - fn: (data: { entryId: string }) => void -}> - -export type ReadAboveCommand = Command<{ - id: typeof COMMAND_ID.entry.readAbove - fn: (data: { publishedAt: string }) => void -}> - -export type ReadBelowCommand = Command<{ - id: typeof COMMAND_ID.entry.readBelow - fn: (data: { publishedAt: string }) => void -}> - -export type ToggleAISummaryCommand = Command<{ - id: typeof COMMAND_ID.entry.toggleAISummary - fn: () => void -}> - -export type ToggleAITranslationCommand = Command<{ - id: typeof COMMAND_ID.entry.toggleAITranslation - fn: () => void -}> - -export type ImageGalleryCommand = Command<{ - id: typeof COMMAND_ID.entry.imageGallery - fn: (data: { entryId: string }) => void -}> - -export type TTSCommand = Command<{ - id: typeof COMMAND_ID.entry.tts - fn: (data: { entryId: string; entryContent: string }) => void -}> - -export type ReadabilityCommand = Command<{ - id: typeof COMMAND_ID.entry.readability - fn: (data: { entryId: string; entryUrl: string }) => void -}> - -export type EntryCommand = - | TipCommand - | StarCommand - | DeleteCommand - | CopyLinkCommand - | ExportAsPDFCommand - | CopyTitleCommand - | OpenInBrowserCommand - | ViewSourceContentCommand - | ShareCommand - | ReadCommand - | ReadAboveCommand - | ReadBelowCommand - | ToggleAISummaryCommand - | ToggleAITranslationCommand - | ImageGalleryCommand - | TTSCommand - | ReadabilityCommand - -// Settings commands - -export type CustomizeToolbarCommand = Command<{ - id: typeof COMMAND_ID.settings.customizeToolbar - fn: () => void -}> - -export type SettingsCommand = CustomizeToolbarCommand - -// Integration commands - -export type SaveToEagleCommand = Command<{ - id: typeof COMMAND_ID.integration.saveToEagle - fn: (payload: { entryId: string }) => void -}> - -export type SaveToReadwiseCommand = Command<{ - id: typeof COMMAND_ID.integration.saveToReadwise - fn: (payload: { entryId: string }) => void -}> - -export type SaveToInstapaperCommand = Command<{ - id: typeof COMMAND_ID.integration.saveToInstapaper - fn: (payload: { entryId: string }) => void -}> - -export type SaveToObsidianCommand = Command<{ - id: typeof COMMAND_ID.integration.saveToObsidian - fn: (payload: { entryId: string }) => void -}> - -export type SaveToOutlineCommand = Command<{ - id: typeof COMMAND_ID.integration.saveToOutline - fn: (payload: { entryId: string }) => void -}> - -export type SaveToReadeckCommand = Command<{ - id: typeof COMMAND_ID.integration.saveToReadeck - fn: (payload: { entryId: string }) => void -}> - -export type SaveToCuboxCommand = Command<{ - id: typeof COMMAND_ID.integration.saveToCubox - fn: (payload: { entryId: string }) => void -}> - -export type IntegrationCommand = - | SaveToEagleCommand - | SaveToReadwiseCommand - | SaveToInstapaperCommand - | SaveToObsidianCommand - | SaveToOutlineCommand - | SaveToReadeckCommand - | SaveToCuboxCommand - -export type BasicCommand = EntryCommand | SettingsCommand | IntegrationCommand +export type BasicCommand = + | EntryCommand + | SettingsCommand + | IntegrationCommand + | GlobalCommand + | LayoutCommand diff --git a/apps/desktop/layer/renderer/src/modules/command/hooks/use-register-hotkey.ts b/apps/desktop/layer/renderer/src/modules/command/hooks/use-register-hotkey.ts index 5c4f1ff50..9da07a532 100644 --- a/apps/desktop/layer/renderer/src/modules/command/hooks/use-register-hotkey.ts +++ b/apps/desktop/layer/renderer/src/modules/command/hooks/use-register-hotkey.ts @@ -1,4 +1,5 @@ -import { useHotkeys } from "react-hotkeys-hook" +import { useEffect, useRef } from "react" +import { tinykeys } from "tinykeys" import type { FollowCommand, FollowCommandId } from "../types" import { getCommand } from "./use-command" @@ -8,36 +9,55 @@ interface RegisterHotkeyOptions { commandId: T args?: Parameters["run"]> when?: boolean - // hotkeyOptions?: Options } export const useCommandHotkey = ({ shortcut, commandId, - when, + when = true, args, }: RegisterHotkeyOptions) => { - useHotkeys( - shortcut, - () => { - const command = getCommand(commandId) + const unsubscribeRef = useRef<() => void>(void 0) - if (!command) return - if (Array.isArray(args)) { - // It should be safe to spread the args here because we are checking if it is an array - // @ts-expect-error - A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556) - command.run(...args) - return + useEffect(() => { + if (!when) { + unsubscribeRef.current?.() + return + } + + // Handle comma-separated shortcuts + const shortcuts = shortcut.split(",").map((s) => s.trim()) + const keyMap: Record void> = {} + + // Create a handler for each shortcut + shortcuts.forEach((key) => { + keyMap[key] = (event) => { + event.preventDefault() + + const command = getCommand(commandId) + if (!command) return + + if (Array.isArray(args)) { + // It should be safe to spread the args here because we are checking if it is an array + // @ts-expect-error - A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556) + command.run(...args) + return + } + + if (args === undefined) { + // @ts-expect-error + command.run() + return + } + + console.error("Invalid args", typeof args, args) } - if (args === undefined) { - // @ts-expect-error - command.run() - return - } - console.error("Invalid args", typeof args, args) - }, - { - enabled: when, - }, - ) + }) + + unsubscribeRef.current = tinykeys(document.documentElement, keyMap) + + return () => { + unsubscribeRef.current?.() + } + }, [shortcut, commandId, when, args]) } diff --git a/apps/desktop/layer/renderer/src/modules/command/registry/command.ts b/apps/desktop/layer/renderer/src/modules/command/registry/command.ts index de30ab791..ffadc8d08 100644 --- a/apps/desktop/layer/renderer/src/modules/command/registry/command.ts +++ b/apps/desktop/layer/renderer/src/modules/command/registry/command.ts @@ -23,9 +23,6 @@ export function createCommand< label = typeof label === "string" ? { title: label } : label return label }, - // when: !!(options.when ?? true), - // keyBinding: - // typeof options.keyBinding === "string" ? { binding: options.keyBinding } : options.keyBinding, } } diff --git a/apps/desktop/layer/renderer/src/modules/command/types.ts b/apps/desktop/layer/renderer/src/modules/command/types.ts index 8e84bec41..d62b74587 100644 --- a/apps/desktop/layer/renderer/src/modules/command/types.ts +++ b/apps/desktop/layer/renderer/src/modules/command/types.ts @@ -17,36 +17,6 @@ export interface KeybindingOptions { // skipRegister?: boolean } -export interface CommandKeybindingOptions< - ID extends string, - T extends (...args: any[]) => unknown = (...args: unknown[]) => unknown, -> { - /** - * the command id. - */ - commandId: ID - /** - * a set of predefined precondition strategies. - * - * note: this only used for keybinding and command menu. - * command will always available when called directly. - */ - when?: boolean - /** - * we use https://github.com/jamiebuilds/tinykeys so that we can use the same keybinding definition - * for both mac and windows. - * - * Use `$mod` for `Cmd` on Mac and `Ctrl` on Windows and Linux. - */ - keyBinding?: KeybindingOptions | string - /** - * additional arguments for the command. - * - * Only used when the command is called from a keybinding. - */ - args?: Parameters -} - export interface Command< T extends { id: string; fn: (...args: any[]) => unknown } = { id: string @@ -61,9 +31,6 @@ export interface Command< readonly icon?: ReactNode | ((props?: { isActive?: boolean }) => ReactNode) readonly category: CommandCategory readonly run: T["fn"] - - // readonly when: boolean - // readonly keyBinding?: KeybindingOptions } export type SimpleCommand = Command<{ id: T; fn: () => void }> @@ -87,7 +54,6 @@ export interface CommandOptions< run: T["fn"] when?: boolean - keyBinding?: T["fn"] extends () => void ? KeybindingOptions | string : never } export type FollowCommandMap = { diff --git a/apps/desktop/layer/renderer/src/providers/global-hotkeys-provider.tsx b/apps/desktop/layer/renderer/src/providers/global-hotkeys-provider.tsx new file mode 100644 index 000000000..cddf3909f --- /dev/null +++ b/apps/desktop/layer/renderer/src/providers/global-hotkeys-provider.tsx @@ -0,0 +1,18 @@ +import { useHotkeysContext } from "react-hotkeys-hook" + +import { HotKeyScopeMap } from "~/constants/hotkeys" +import { shortcuts } from "~/constants/shortcuts" +import { COMMAND_ID } from "~/modules/command/commands/id" +import { useCommandHotkey } from "~/modules/command/hooks/use-register-hotkey" + +export const GlobalHotkeysProvider = () => { + const { activeScopes } = useHotkeysContext() + + useCommandHotkey({ + commandId: COMMAND_ID.global.showShortcuts, + shortcut: shortcuts.layout.showShortcuts.key, + when: activeScopes.includes(HotKeyScopeMap.Home), + }) + + return null +} diff --git a/apps/desktop/layer/renderer/src/providers/root-providers.tsx b/apps/desktop/layer/renderer/src/providers/root-providers.tsx index 00a56ac2f..63c683920 100644 --- a/apps/desktop/layer/renderer/src/providers/root-providers.tsx +++ b/apps/desktop/layer/renderer/src/providers/root-providers.tsx @@ -18,6 +18,7 @@ import { jotaiStore } from "~/lib/jotai" import { persistConfig, queryClient } from "~/lib/query-client" import { FollowCommandManager } from "~/modules/command/command-manager" +import { GlobalHotkeysProvider } from "./global-hotkeys-provider" import { I18nProvider } from "./i18n-provider" import { InvalidateQueryProvider } from "./invalidate-query-provider" import { @@ -42,7 +43,7 @@ export const RootProviders: FC = ({ children }) => ( - + @@ -54,6 +55,7 @@ export const RootProviders: FC = ({ children }) => ( + {import.meta.env.DEV && } {children} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 418be9bae..77a8001dc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -677,6 +677,9 @@ importers: sonner: specifier: 2.0.3 version: 2.0.3(react-dom@19.0.0(react@19.0.0))(react@19.0.0) + tinykeys: + specifier: 3.0.0 + version: 3.0.0 tldts: specifier: 7.0.4 version: 7.0.4 @@ -14426,6 +14429,9 @@ packages: resolution: {integrity: sha512-mEwzpUgrLySlveBwEVDMKk5B57bhLPYovRfPAXD5gA/98Opn0rCDj3GtLwFvCvH5RK9uPCExUROW5NjDwvqkxw==} engines: {node: '>=12.0.0'} + tinykeys@3.0.0: + resolution: {integrity: sha512-nazawuGv5zx6MuDfDY0rmfXjuOGhD5XU2z0GLURQ1nzl0RUe9OuCJq+0u8xxJZINHe+mr7nw8PWYYZ9WhMFujw==} + tinypool@1.0.2: resolution: {integrity: sha512-al6n+QEANGFOMf/dmUMsuS5/r9B06uwlyNjZZql/zv8J7ybHCgoihBNORZCY2mzUuAnomQa2JdhyHKzZxPCrFA==} engines: {node: ^18.0.0 || >=20.0.0} @@ -31117,6 +31123,8 @@ snapshots: fdir: 6.4.4(picomatch@4.0.2) picomatch: 4.0.2 + tinykeys@3.0.0: {} + tinypool@1.0.2: {} tinyrainbow@2.0.0: {}