feat: add tinykeys for hotkey management and refactor command handling, fixed #3635
- Introduced `tinykeys` for improved hotkey management in the application. - Added global hotkey support for showing shortcuts. - Refactored command registration to streamline the integration of new commands. - Updated hotkey scope handling for better context awareness in the application. This enhances user experience by providing more responsive and context-sensitive keyboard shortcuts. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
}
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<T extends FollowCommandId> {
|
||||
commandId: T
|
||||
args?: Parameters<Extract<FollowCommand, { id: T }>["run"]>
|
||||
when?: boolean
|
||||
// hotkeyOptions?: Options
|
||||
}
|
||||
|
||||
export const useCommandHotkey = <T extends FollowCommandId>({
|
||||
shortcut,
|
||||
commandId,
|
||||
when,
|
||||
when = true,
|
||||
args,
|
||||
}: RegisterHotkeyOptions<T>) => {
|
||||
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<string, (event: KeyboardEvent) => 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])
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<T>
|
||||
}
|
||||
|
||||
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<T extends string> = 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 = {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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<PropsWithChildren> = ({ children }) => (
|
||||
<Provider store={jotaiStore}>
|
||||
<MotionProvider>
|
||||
<PersistQueryClientProvider persistOptions={persistConfig} client={queryClient}>
|
||||
<HotkeysProvider initiallyActiveScopes={HotKeyScopeMap.Home}>
|
||||
<HotkeysProvider initiallyActiveScopes={[HotKeyScopeMap.Home]}>
|
||||
<I18nProvider>
|
||||
<ModalStackProvider>
|
||||
<Toaster />
|
||||
@@ -54,6 +55,7 @@ export const RootProviders: FC<PropsWithChildren> = ({ children }) => (
|
||||
<StableRouterProvider />
|
||||
<SettingSync />
|
||||
<FollowCommandManager />
|
||||
<GlobalHotkeysProvider />
|
||||
{import.meta.env.DEV && <Devtools />}
|
||||
|
||||
{children}
|
||||
|
||||
Generated
+8
@@ -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: {}
|
||||
|
||||
Reference in New Issue
Block a user