AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
feat: add configurable note shortcuts
tianma55d8e041a972
diff --git a/apps/web/src/components/SettingsPane.tsx b/apps/web/src/components/SettingsPane.tsxindex 6ab183f6..f9258703 100644--- a/apps/web/src/components/SettingsPane.tsx+++ b/apps/web/src/components/SettingsPane.tsx@@ -1,15 +1,19 @@import { ChevronLeft, User } from "lucide-react";import { Button } from "@/components/ui/button";+import type { ShortcutSettings } from "@/lib/app-helpers";import { AdvancedPlayCard } from "./settings/AdvancedPlayCard";import { EvernoteImportGuideCard } from "./settings/EvernoteImportGuideCard";import { McpConfigCard } from "./settings/McpConfigCard";import { PreferenceCard } from "./settings/PreferenceCard";import { SessionCard } from "./settings/SessionCard";+import { ShortcutSettingsCard } from "./settings/ShortcutSettingsCard";interface SettingsPaneProps {onClose: () => void;imageCompressionEnabled: boolean;onImageCompressionChange: (enabled: boolean) => void;+ shortcutSettings: ShortcutSettings;+ onShortcutSettingsChange: (settings: ShortcutSettings) => void;onLogout: () => void;isLoggingOut: boolean;authRequired: boolean;@@ -20,6 +24,8 @@ export const SettingsPane = ({onClose,imageCompressionEnabled,onImageCompressionChange,+ shortcutSettings,+ onShortcutSettingsChange,onLogout,isLoggingOut,authRequired,@@ -54,6 +60,10 @@ export const SettingsPane = ({imageCompressionEnabled={imageCompressionEnabled}onImageCompressionChange={onImageCompressionChange}/>+ <ShortcutSettingsCard+ shortcutSettings={shortcutSettings}+ onShortcutSettingsChange={onShortcutSettingsChange}+ /><AdvancedPlayCard /><EvernoteImportGuideCard onShowGuide={onShowGuide} /><McpConfigCard />diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsxindex 0276608f..965263ba 100644--- a/apps/web/src/components/WorkspaceApp.tsx+++ b/apps/web/src/components/WorkspaceApp.tsx@@ -39,6 +39,7 @@ import type {NotebookDropPosition,NotebookMoveOption,MemoTemplate,+ ShortcutSettings,} from "@/lib/app-helpers";import {DEFAULT_MEMO_TITLE,@@ -48,6 +49,9 @@ import {isTextEntryTarget,readImageCompressionPreference,writeImageCompressionPreference,+ readShortcutSettingsPreference,+ writeShortcutSettingsPreference,+ getShortcutActionForEvent,readMemoListWidthPreference,writeMemoListWidthPreference,clampMemoListWidth,@@ -504,6 +508,7 @@ export const WorkspaceApp = ({const [appNoticeDialog, setAppNoticeDialog] = useState<AppNoticeDialogState | null>(null);const [multiSelectKeyDown, setMultiSelectKeyDown] = useState(false);const [imageCompressionEnabled, setImageCompressionEnabled] = useState(readImageCompressionPreference);+ const [shortcutSettings, setShortcutSettings] = useState<ShortcutSettings>(readShortcutSettingsPreference);const [rightView, setRightView] = useState<"editor" | "settings" | "assets" | "evernote-migration">(() =>isInitialSettingsRoute ? "settings" : "editor");@@ -694,6 +699,10 @@ export const WorkspaceApp = ({writeImageCompressionPreference(imageCompressionEnabled);}, [imageCompressionEnabled]);+ useEffect(() => {+ writeShortcutSettingsPreference(shortcutSettings);+ }, [shortcutSettings]);+useEffect(() => {if (location.pathname === SETTINGS_PATH) {skipNextHomeRouteSyncRef.current = false;@@ -1700,19 +1709,19 @@ export const WorkspaceApp = ({useEffect(() => {const handleWorkspaceShortcut = (event: KeyboardEvent) => {- if (event.defaultPrevented || event.altKey || (!event.ctrlKey && !event.metaKey)) {+ if (event.defaultPrevented) {return;}- const key = event.key.toLowerCase();- if (key !== "f" && key !== "h" && key !== "n") {+ const action = getShortcutActionForEvent(event, shortcutSettings);+ if (!action) {return;}const targetElement = event.target instanceof Element ? event.target : null;const isEditorTextTarget = Boolean(targetElement?.closest(".ProseMirror"));- if ((key === "f" || key === "h") && isTextEntryTarget(event.target) && !isEditorTextTarget) {+ if ((action === "focusSearch" || action === "focusReplace") && isTextEntryTarget(event.target) && !isEditorTextTarget) {return;}@@ -1732,7 +1741,7 @@ export const WorkspaceApp = ({return;}- if (key === "f") {+ if (action === "focusSearch") {event.preventDefault();if (event.shiftKey || !selectedMemoId || !isDesktopViewport()) {if (event.shiftKey) {@@ -1747,7 +1756,7 @@ export const WorkspaceApp = ({return;}- if (key === "h") {+ if (action === "focusReplace") {if (!selectedMemoId || memoView === "trash" || !isDesktopViewport()) {return;}@@ -1759,14 +1768,14 @@ export const WorkspaceApp = ({event.preventDefault();- if (event.shiftKey) {+ if (action === "createNotebook") {if (!createNotebookMutation.isPending) {handleCreateNotebook(null);}return;}- if (canCreateMemo && !createMemoMutation.isPending) {+ if (action === "createMemo" && canCreateMemo && !createMemoMutation.isPending) {handleCreateMemo();}};@@ -1783,6 +1792,7 @@ export const WorkspaceApp = ({handleCreateNotebook,handleCreateMemo,handleMobileSearch,+ shortcutSettings,emptyTrashConfirmationOpen,memoDeleteConfirmation,memoView,@@ -2085,6 +2095,8 @@ export const WorkspaceApp = ({onClose={handleCloseSettings}imageCompressionEnabled={imageCompressionEnabled}onImageCompressionChange={setImageCompressionEnabled}+ shortcutSettings={shortcutSettings}+ onShortcutSettingsChange={setShortcutSettings}onLogout={onLogout}isLoggingOut={isLoggingOut}authRequired={authRequired}diff --git a/apps/web/src/components/settings/ShortcutSettingsCard.tsx b/apps/web/src/components/settings/ShortcutSettingsCard.tsxnew file mode 100644index 00000000..f2c23d14--- /dev/null+++ b/apps/web/src/components/settings/ShortcutSettingsCard.tsx@@ -0,0 +1,186 @@+import { useEffect, useMemo, useRef, useState } from "react";+import { Keyboard, RotateCcw } from "lucide-react";+import type { ShortcutAction, ShortcutBinding, ShortcutSettings } from "@/lib/app-helpers";+import {+ DEFAULT_SHORTCUT_SETTINGS,+ formatShortcutBinding,+ shortcutBindingFromKeyboardEvent,+ shortcutBindingsEqual,+ SHORTCUT_ACTION_OPTIONS,+} from "@/lib/app-helpers";+import { Button } from "@/components/ui/button";+import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";+import {+ Dialog,+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+} from "@/components/ui/dialog";+import { cn } from "@/lib/utils";++interface ShortcutSettingsCardProps {+ shortcutSettings: ShortcutSettings;+ onShortcutSettingsChange: (settings: ShortcutSettings) => void;+}++const getConflictAction = (+ action: ShortcutAction,+ binding: ShortcutBinding,+ settings: ShortcutSettings+) => SHORTCUT_ACTION_OPTIONS.find((item) => item.value !== action && shortcutBindingsEqual(settings[item.value], binding));++export const ShortcutSettingsCard = ({ shortcutSettings, onShortcutSettingsChange }: ShortcutSettingsCardProps) => {+ const [shortcutsOpen, setShortcutsOpen] = useState(false);+ const [recordingAction, setRecordingAction] = useState<ShortcutAction | null>(null);+ const [captureMessage, setCaptureMessage] = useState("");+ const captureButtonRef = useRef<HTMLButtonElement | null>(null);++ const shortcutSummary = useMemo(+ () =>+ SHORTCUT_ACTION_OPTIONS.map((item) => formatShortcutBinding(shortcutSettings[item.value]))+ .slice(0, 3)+ .join(" / "),+ [shortcutSettings]+ );++ useEffect(() => {+ if (!recordingAction) {+ return;+ }++ captureButtonRef.current?.focus();+ }, [recordingAction]);++ useEffect(() => {+ if (!recordingAction) {+ return;+ }++ const handleKeyDown = (event: KeyboardEvent) => {+ event.preventDefault();+ event.stopPropagation();++ if (event.key === "Escape") {+ setRecordingAction(null);+ setCaptureMessage("");+ return;+ }++ const nextBinding = shortcutBindingFromKeyboardEvent(event);+ if (!nextBinding) {+ setCaptureMessage("请按下包含 Ctrl、⌘ 或 Alt 的组合键。");+ return;+ }++ const conflictAction = getConflictAction(recordingAction, nextBinding, shortcutSettings);+ if (conflictAction) {+ setCaptureMessage(`这个组合键已用于「${conflictAction.label}」。`);+ return;+ }++ onShortcutSettingsChange({+ ...shortcutSettings,+ [recordingAction]: nextBinding,+ });+ setRecordingAction(null);+ setCaptureMessage("");+ };++ window.addEventListener("keydown", handleKeyDown, { capture: true });+ return () => window.removeEventListener("keydown", handleKeyDown, { capture: true });+ }, [onShortcutSettingsChange, recordingAction, shortcutSettings]);++ const handleResetShortcuts = () => {+ onShortcutSettingsChange(DEFAULT_SHORTCUT_SETTINGS);+ setRecordingAction(null);+ setCaptureMessage("");+ };++ return (+ <>+ <Card className="w-full min-w-0 overflow-hidden shadow-none">+ <CardHeader className="p-4">+ <CardTitle className="flex items-center gap-2 text-sm">+ <Keyboard className="h-4 w-4 text-emerald-700" />+ 快捷键+ </CardTitle>+ <CardDescription className="text-xs leading-4">为低频但关键的笔记动作绑定组合键。</CardDescription>+ </CardHeader>+ <CardContent className="p-4 pt-0">+ <div className="flex min-h-14 flex-col items-start gap-3 rounded-lg border border-slate-100 bg-slate-50/70 px-4 py-3 sm:flex-row sm:items-center sm:justify-between sm:gap-4">+ <div className="min-w-0">+ <div className="text-sm font-semibold text-slate-900">绑定快捷键</div>+ <div className="mt-0.5 truncate text-xs leading-4 text-slate-500">{shortcutSummary}</div>+ </div>+ <Button+ size="sm"+ variant="outline"+ className="h-8 w-full bg-white px-3 text-xs sm:w-auto"+ type="button"+ onClick={() => setShortcutsOpen(true)}+ >+ 管理+ </Button>+ </div>+ </CardContent>+ </Card>++ <Dialog open={shortcutsOpen} onOpenChange={setShortcutsOpen}>+ <DialogContent className="max-h-[min(640px,calc(100vh-2rem))] max-w-xl overflow-y-auto">+ <DialogHeader>+ <DialogTitle className="flex items-center gap-2">+ <Keyboard className="h-5 w-5 text-emerald-700" />+ 绑定快捷键+ </DialogTitle>+ <DialogDescription>为常用笔记动作设置组合键。按 Esc 可取消当前录制。</DialogDescription>+ </DialogHeader>++ <div className="grid gap-3">+ {SHORTCUT_ACTION_OPTIONS.map((item) => {+ const recording = recordingAction === item.value;++ return (+ <div+ key={item.value}+ className="flex min-w-0 flex-col gap-3 rounded-lg border border-slate-200 bg-slate-50/60 p-3 sm:flex-row sm:items-center sm:justify-between"+ >+ <div className="min-w-0">+ <div className="text-sm font-semibold text-slate-900">{item.label}</div>+ <div className="mt-0.5 text-xs leading-4 text-slate-500">{item.description}</div>+ </div>+ <Button+ ref={recording ? captureButtonRef : null}+ type="button"+ variant={recording ? "solid" : "outline"}+ className={cn("h-9 min-w-32 px-3 font-mono text-xs", !recording && "bg-white")}+ onClick={() => {+ setRecordingAction(item.value);+ setCaptureMessage("");+ }}+ >+ {recording ? "输入组合键" : formatShortcutBinding(shortcutSettings[item.value])}+ </Button>+ </div>+ );+ })}+ </div>++ {captureMessage ? (+ <div className="rounded-md border border-amber-200 bg-amber-50 px-3 py-2 text-xs font-medium text-amber-700">+ {captureMessage}+ </div>+ ) : null}++ <DialogFooter>+ <Button type="button" variant="outline" onClick={handleResetShortcuts}>+ <RotateCcw className="h-4 w-4" />+ 恢复默认+ </Button>+ </DialogFooter>+ </DialogContent>+ </Dialog>+ </>+ );+};diff --git a/apps/web/src/lib/app-helpers.ts b/apps/web/src/lib/app-helpers.tsindex a9e754e2..abb68432 100644--- a/apps/web/src/lib/app-helpers.ts+++ b/apps/web/src/lib/app-helpers.ts@@ -10,6 +10,14 @@ export type MemoFilterMode = "all" | "tagged" | "untagged" | "pinned";export type MemoSortMode = "updated-desc" | "created-desc" | "title-asc";export type NotebookSortMode = "name-asc" | "memo-count-desc" | "updated-desc";export type MemoListDensity = "preview" | "compact";+export type ShortcutAction = "createMemo" | "createNotebook" | "focusSearch" | "focusReplace";+export type ShortcutBinding = {+ key: string;+ ctrlOrMeta: boolean;+ shift: boolean;+ alt: boolean;+};+export type ShortcutSettings = Record<ShortcutAction, ShortcutBinding>;export type MobileBottomNavItem = "home" | "search" | "templates" | "settings";export type MemoContextMenuState = { memo: MemoSummary; x: number; y: number };export type MemoSelectionContextMenuState = { x: number; y: number };@@ -144,6 +152,7 @@ export const IMAGE_COMPRESSION_STORAGE_KEY = "edgeever.imageCompressionEnabled";export const MEMO_LIST_DENSITY_STORAGE_KEY = "edgeever.memoListDensity";export const MEMO_LIST_WIDTH_STORAGE_KEY = "edgeever.memoListWidth";export const NOTEBOOK_SORT_STORAGE_KEY = "edgeever.notebookSort";+export const SHORTCUT_SETTINGS_STORAGE_KEY = "edgeever.shortcutSettings";export const DEFAULT_MEMO_LIST_WIDTH_PX = 360;export const MIN_MEMO_LIST_WIDTH_PX = 300;export const MAX_MEMO_LIST_WIDTH_PX = 540;@@ -170,6 +179,22 @@ export const MEMO_FILTER_OPTIONS: Array<{ value: MemoFilterMode; label: string }{ value: "untagged", label: "无标签" },];+export const SHORTCUT_ACTION_OPTIONS: Array<{ value: ShortcutAction; label: string; description: string }> = [+ { value: "createMemo", label: "新建笔记", description: "在当前笔记本中创建一条新笔记。" },+ { value: "createNotebook", label: "新建笔记本", description: "在当前层级创建一个新笔记本。" },+ { value: "focusSearch", label: "搜索笔记", description: "打开搜索或聚焦笔记内搜索。" },+ { value: "focusReplace", label: "替换文本", description: "在当前笔记中打开替换。" },+];++export const DEFAULT_SHORTCUT_SETTINGS: ShortcutSettings = {+ createMemo: { key: "n", ctrlOrMeta: true, shift: false, alt: false },+ createNotebook: { key: "n", ctrlOrMeta: true, shift: true, alt: false },+ focusSearch: { key: "f", ctrlOrMeta: true, shift: false, alt: false },+ focusReplace: { key: "h", ctrlOrMeta: true, shift: false, alt: false },+};++const SHORTCUT_ACTION_VALUES = SHORTCUT_ACTION_OPTIONS.map((option) => option.value);+export const getMemoTitle = (title: string | null | undefined) => title?.trim() || DEFAULT_MEMO_TITLE;export const getActiveBlockValue = (editor: any): string => {@@ -263,6 +288,103 @@ export const writeNotebookSortPreference = (sortMode: NotebookSortMode) => {}};+export const normalizeShortcutKey = (key: string) => {+ if (key === " ") {+ return "space";+ }++ return key.toLowerCase();+};++const isShortcutBinding = (value: unknown): value is ShortcutBinding => {+ const binding = value as ShortcutBinding;+ return (+ Boolean(binding) &&+ typeof binding.key === "string" &&+ binding.key.trim().length > 0 &&+ typeof binding.ctrlOrMeta === "boolean" &&+ typeof binding.shift === "boolean" &&+ typeof binding.alt === "boolean"+ );+};++export const readShortcutSettingsPreference = (): ShortcutSettings => {+ try {+ const rawValue = window.localStorage.getItem(SHORTCUT_SETTINGS_STORAGE_KEY);+ if (!rawValue) {+ return DEFAULT_SHORTCUT_SETTINGS;+ }++ const parsedValue = JSON.parse(rawValue) as Partial<ShortcutSettings>;+ return SHORTCUT_ACTION_VALUES.reduce<ShortcutSettings>(+ (settings, action) => ({+ ...settings,+ [action]: isShortcutBinding(parsedValue[action])+ ? { ...parsedValue[action], key: normalizeShortcutKey(parsedValue[action].key) }+ : DEFAULT_SHORTCUT_SETTINGS[action],+ }),+ { ...DEFAULT_SHORTCUT_SETTINGS }+ );+ } catch {+ return DEFAULT_SHORTCUT_SETTINGS;+ }+};++export const writeShortcutSettingsPreference = (settings: ShortcutSettings) => {+ try {+ window.localStorage.setItem(SHORTCUT_SETTINGS_STORAGE_KEY, JSON.stringify(settings));+ } catch {+ // Local storage can be unavailable in private or restricted browser contexts.+ }+};++export const formatShortcutBinding = (binding: ShortcutBinding) => {+ const isMac = /mac|iphone|ipad|ipod/i.test(window.navigator.platform);+ const parts = [+ binding.ctrlOrMeta ? (isMac ? "⌘" : "Ctrl") : null,+ binding.alt ? (isMac ? "⌥" : "Alt") : null,+ binding.shift ? (isMac ? "⇧" : "Shift") : null,+ binding.key === "space" ? "Space" : binding.key.length === 1 ? binding.key.toUpperCase() : binding.key,+ ].filter(Boolean);++ return parts.join(isMac ? "" : " + ");+};++export const shortcutBindingFromKeyboardEvent = (event: KeyboardEvent): ShortcutBinding | null => {+ const key = normalizeShortcutKey(event.key);++ if (["control", "meta", "shift", "alt", "escape"].includes(key)) {+ return null;+ }++ if (!event.ctrlKey && !event.metaKey && !event.altKey) {+ return null;+ }++ return {+ key,+ ctrlOrMeta: event.ctrlKey || event.metaKey,+ shift: event.shiftKey,+ alt: event.altKey,+ };+};++export const shortcutBindingsEqual = (first: ShortcutBinding, second: ShortcutBinding) =>+ first.key === second.key &&+ first.ctrlOrMeta === second.ctrlOrMeta &&+ first.shift === second.shift &&+ first.alt === second.alt;++export const getShortcutActionForEvent = (event: KeyboardEvent, settings: ShortcutSettings): ShortcutAction | null => {+ const eventBinding = shortcutBindingFromKeyboardEvent(event);++ if (!eventBinding) {+ return null;+ }++ return SHORTCUT_ACTION_VALUES.find((action) => shortcutBindingsEqual(settings[action], eventBinding)) ?? null;+};+export const compareDateDesc = (first: string, second: string) => {const firstTime = Date.parse(first);const secondTime = Date.parse(second);