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
fix(web): distinguish Command and Control in workspace shortcuts
tianma-if9ed9c6167ae5
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsxindex 571db7f5..921f8c93 100644--- a/apps/web/src/components/EditorPane.tsx+++ b/apps/web/src/components/EditorPane.tsx@@ -177,6 +177,7 @@ import {EDITOR_LOCAL_SAVE_DELAY_MS,formatShortcutBinding,getEditableMemoTitle,+ isPrimaryModifierPressed,getNotebookMoveOptions,readDesktopReadingProtectionPreference,readEditorOutlineCollapsedPreference,@@ -1317,7 +1318,8 @@ const RichEditorPane = ({}const shortcutKey = event.key.toLowerCase();- if ((event.ctrlKey || event.metaKey) && !event.altKey && !event.shiftKey && shortcutKey === "k") {+ // Command on Apple, Control elsewhere. Control stays free for macOS text movement.+ if (isPrimaryModifierPressed(event) && !event.altKey && !event.shiftKey && shortcutKey === "k") {event.preventDefault();openExternalLinkDialogRef.current();return true;@@ -1325,7 +1327,7 @@ const RichEditorPane = ({if ((shortcutKey !== "f" && shortcutKey !== "h") ||- (!event.ctrlKey && !event.metaKey) ||+ !isPrimaryModifierPressed(event) ||event.altKey ||(shortcutKey === "f" && event.shiftKey)) {diff --git a/apps/web/src/components/EditorSaveControls.test.mjs b/apps/web/src/components/EditorSaveControls.test.mjsindex 727c022d..974baefc 100644--- a/apps/web/src/components/EditorSaveControls.test.mjs+++ b/apps/web/src/components/EditorSaveControls.test.mjs@@ -14,7 +14,7 @@ describe("editor save controls", () => {expect(editorSource).not.toContain('aria-label={t("editor.save")}');});- test("keeps Ctrl or Command S as the explicit save and sync action", () => {+ test("keeps the platform primary modifier as the save and sync shortcut", () => {expect(helperSource).toContain('saveAndSync: { key: "s", ctrlOrMeta: true');expect(workspaceSource).toContain('action === "saveAndSync"');expect(workspaceSource).toContain("setNoteSaveAndSyncToken((value) => value + 1)");diff --git a/apps/web/src/components/editor/MarkdownSourceEditor.tsx b/apps/web/src/components/editor/MarkdownSourceEditor.tsxindex 3a898efd..85c09f76 100644--- a/apps/web/src/components/editor/MarkdownSourceEditor.tsx+++ b/apps/web/src/components/editor/MarkdownSourceEditor.tsx@@ -30,6 +30,7 @@ import {gruvboxDark,} from "@uiw/codemirror-themes-all";import type { MarkdownThemeName } from "../ThemeProvider";+import { isPrimaryModifierPressed } from "@/lib/app-helpers";import { getAiSlashCommandStart } from "@/lib/editor-shortcuts";import { lightMarkdownHighlightStyles } from "@/lib/markdown-source-highlight";import { cn } from "@/lib/utils";@@ -315,9 +316,9 @@ export const MarkdownSourceEditor = forwardRef<MarkdownSourceEditorRef, Markdownconst handleKeyDown = useCallback((event: React.KeyboardEvent | KeyboardEvent) => {- // Link shortcut: Cmd+K / Ctrl+K+ // Link shortcut uses the platform primary modifier, leaving macOS Control+K for deleting to the line end.if (- (event.ctrlKey || event.metaKey) &&+ isPrimaryModifierPressed(event) &&!event.altKey &&!event.shiftKey &&event.key.toLowerCase() === "k"diff --git a/apps/web/src/components/settings/ShortcutSettingsItem.tsx b/apps/web/src/components/settings/ShortcutSettingsItem.tsxindex 921b73c4..1d65e85c 100644--- a/apps/web/src/components/settings/ShortcutSettingsItem.tsx+++ b/apps/web/src/components/settings/ShortcutSettingsItem.tsx@@ -7,6 +7,7 @@ import {formatShortcutBinding,getShortcutActionOptions,getShortcutActionForEvent,+ resolvedShortcutModifiers,shortcutBindingFromKeyboardEvent,shortcutBindingsEqual,} from "@/lib/app-helpers";@@ -46,11 +47,14 @@ const formatGlobalBinding = (binding: DesktopGlobalShortcutBinding) => {return parts.join(isMac ? "" : " + ");};-const globalBindingMatchesLocal = (global: DesktopGlobalShortcutBinding, local: ShortcutBinding) =>- global.key === local.key- && (global.ctrl || global.meta) === local.ctrlOrMeta- && global.alt === local.alt- && global.shift === local.shift;+const globalBindingMatchesLocal = (global: DesktopGlobalShortcutBinding, local: ShortcutBinding) => {+ const modifiers = resolvedShortcutModifiers(local);+ return global.key === local.key+ && global.ctrl === modifiers.ctrl+ && global.meta === modifiers.meta+ && global.alt === local.alt+ && global.shift === local.shift;+};export const ShortcutSettingsItem = ({ shortcutSettings, onShortcutSettingsChange }: ShortcutSettingsItemProps) => {const { t } = useTranslation();diff --git a/apps/web/src/lib/app-helpers.test.mjs b/apps/web/src/lib/app-helpers.test.mjsindex a5c5e097..9f989f91 100644--- a/apps/web/src/lib/app-helpers.test.mjs+++ b/apps/web/src/lib/app-helpers.test.mjs@@ -26,7 +26,11 @@ import {getMaxMemoListWidthForViewport,getEditorPaneWidths,getCreateMemoNotebookId,+ formatShortcutBinding,getShortcutActionForEvent,+ isPrimaryModifierPressed,+ shortcutBindingFromKeyboardEvent,+ shortcutBindingsEqual,canToggleNotebookSidebar,clampMemoListWidth,clampNotebookSidebarWidth,@@ -605,7 +609,7 @@ describe("workspace shortcut preferences", () => {expect(settings.toggleOutline).toEqual(DEFAULT_SHORTCUT_SETTINGS.toggleOutline);});- test("recognizes Ctrl and Command variants for the new actions", () => {+ test("uses Control for default shortcuts on Windows and Linux", () => {const keyboardEvent = (key, modifiers = {}) => ({key,ctrlKey: false,@@ -616,45 +620,208 @@ describe("workspace shortcut preferences", () => {});expect(getShortcutActionForEvent(- keyboardEvent("f", { metaKey: true, shiftKey: true }),+ keyboardEvent("f", { ctrlKey: true, shiftKey: true }),DEFAULT_SHORTCUT_SETTINGS,+ "Win32",)).toBe("focusGlobalSearch");expect(getShortcutActionForEvent(keyboardEvent("o", { ctrlKey: true }),DEFAULT_SHORTCUT_SETTINGS,+ "Linux",)).toBe("openQuickSwitcher");- expect(getShortcutActionForEvent(- keyboardEvent("[", { metaKey: true }),- DEFAULT_SHORTCUT_SETTINGS,- )).toBe("openPreviousMemo");expect(getShortcutActionForEvent(keyboardEvent("]", { ctrlKey: true }),DEFAULT_SHORTCUT_SETTINGS,+ "Win32",)).toBe("openNextMemo");- expect(getShortcutActionForEvent(- keyboardEvent("j", { metaKey: true }),- DEFAULT_SHORTCUT_SETTINGS,- )).toBe("openAiAssistant");expect(getShortcutActionForEvent(keyboardEvent("s", { ctrlKey: true }),DEFAULT_SHORTCUT_SETTINGS,+ "Win32",)).toBe("saveAndSync");expect(getShortcutActionForEvent(keyboardEvent("e", { ctrlKey: true }),DEFAULT_SHORTCUT_SETTINGS,+ "Win32",)).toBe("toggleReadingProtection");- expect(getShortcutActionForEvent(- keyboardEvent("/", { metaKey: true }),- DEFAULT_SHORTCUT_SETTINGS,- )).toBe("toggleEditorMode");expect(getShortcutActionForEvent(keyboardEvent("!", { code: "Digit1", ctrlKey: true, shiftKey: true }),DEFAULT_SHORTCUT_SETTINGS,+ "Win32",)).toBe("toggleOutline");+ expect(getShortcutActionForEvent(+ keyboardEvent("f", { metaKey: true, shiftKey: true }),+ DEFAULT_SHORTCUT_SETTINGS,+ "Win32",+ )).toBeNull();+ expect(formatShortcutBinding(DEFAULT_SHORTCUT_SETTINGS.focusSearch, "Win32")).toBe("Ctrl + F");+ });++ test("keeps Command defaults on macOS and leaves Control for text movement", () => {+ const keyboardEvent = (key, modifiers = {}) => ({+ key,+ ctrlKey: false,+ metaKey: false,+ shiftKey: false,+ altKey: false,+ ...modifiers,+ });+ const mac = "MacIntel";++ expect(getShortcutActionForEvent(+ keyboardEvent("f", { metaKey: true }),+ DEFAULT_SHORTCUT_SETTINGS,+ mac,+ )).toBe("focusSearch");+ expect(getShortcutActionForEvent(+ keyboardEvent("n", { metaKey: true }),+ DEFAULT_SHORTCUT_SETTINGS,+ mac,+ )).toBe("createMemo");+ expect(getShortcutActionForEvent(+ keyboardEvent("f", { metaKey: true, shiftKey: true }),+ DEFAULT_SHORTCUT_SETTINGS,+ mac,+ )).toBe("focusGlobalSearch");+ expect(getShortcutActionForEvent(+ keyboardEvent("[", { metaKey: true }),+ DEFAULT_SHORTCUT_SETTINGS,+ mac,+ )).toBe("openPreviousMemo");+ expect(getShortcutActionForEvent(+ keyboardEvent("j", { metaKey: true }),+ DEFAULT_SHORTCUT_SETTINGS,+ mac,+ )).toBe("openAiAssistant");+ expect(getShortcutActionForEvent(+ keyboardEvent("/", { metaKey: true }),+ DEFAULT_SHORTCUT_SETTINGS,+ mac,+ )).toBe("toggleEditorMode");expect(getShortcutActionForEvent(keyboardEvent("\\", { metaKey: true }),DEFAULT_SHORTCUT_SETTINGS,+ mac,)).toBe("toggleSidebar");++ for (const key of ["f", "n", "e", "h", "o"]) {+ expect(getShortcutActionForEvent(+ keyboardEvent(key, { ctrlKey: true }),+ DEFAULT_SHORTCUT_SETTINGS,+ mac,+ )).toBeNull();+ }+ expect(getShortcutActionForEvent(+ keyboardEvent("f", { ctrlKey: true, metaKey: true }),+ DEFAULT_SHORTCUT_SETTINGS,+ mac,+ )).toBeNull();+ expect(formatShortcutBinding(DEFAULT_SHORTCUT_SETTINGS.focusSearch, mac)).toBe("⌘F");+ expect(formatShortcutBinding(DEFAULT_SHORTCUT_SETTINGS.createMemo, mac)).toBe("⌘N");+ });++ test("records Command and Control separately and keeps an older combined shortcut on the displayed key", () => {+ const keyboardEvent = (key, modifiers = {}) => ({+ key,+ code: "KeyF",+ ctrlKey: false,+ metaKey: false,+ shiftKey: false,+ altKey: false,+ ...modifiers,+ });++ expect(shortcutBindingFromKeyboardEvent(+ keyboardEvent("f", { metaKey: true, altKey: true }),+ "MacIntel",+ )).toEqual({+ key: "f",+ ctrlOrMeta: true,+ ctrl: false,+ meta: true,+ shift: false,+ alt: true,+ });+ expect(shortcutBindingFromKeyboardEvent(+ keyboardEvent("f", { ctrlKey: true, altKey: true }),+ "MacIntel",+ )).toEqual({+ key: "f",+ ctrlOrMeta: false,+ ctrl: true,+ meta: false,+ shift: false,+ alt: true,+ });+ expect(formatShortcutBinding({+ key: "f",+ ctrlOrMeta: false,+ ctrl: true,+ meta: false,+ shift: false,+ alt: true,+ }, "MacIntel")).toBe("⌃⌥F");++ const recordedSearch = shortcutBindingFromKeyboardEvent(+ keyboardEvent("f", { metaKey: true, altKey: true }),+ "MacIntel",+ );+ expect(getShortcutActionForEvent(+ keyboardEvent("f", { metaKey: true, altKey: true }),+ { ...DEFAULT_SHORTCUT_SETTINGS, focusSearch: recordedSearch },+ "MacIntel",+ )).toBe("focusSearch");+ expect(getShortcutActionForEvent(+ keyboardEvent("f", { ctrlKey: true, altKey: true }),+ { ...DEFAULT_SHORTCUT_SETTINGS, focusSearch: recordedSearch },+ "MacIntel",+ )).toBeNull();+ expect(shortcutBindingsEqual(DEFAULT_SHORTCUT_SETTINGS.focusSearch, {+ key: "f",+ ctrlOrMeta: true,+ ctrl: false,+ meta: true,+ shift: false,+ alt: false,+ }, "MacIntel")).toBe(true);+ expect(shortcutBindingsEqual(DEFAULT_SHORTCUT_SETTINGS.focusSearch, {+ key: "f",+ ctrlOrMeta: false,+ ctrl: true,+ meta: false,+ shift: false,+ alt: false,+ }, "MacIntel")).toBe(false);++ const values = installLocalStorage();+ values.set(SHORTCUT_SETTINGS_STORAGE_KEY, JSON.stringify({+ focusSearch: { key: "f", ctrlOrMeta: true, shift: false, alt: true },+ }));+ const settings = readShortcutSettingsPreference();+ expect(getShortcutActionForEvent(+ keyboardEvent("f", { metaKey: true, altKey: true }),+ settings,+ "MacIntel",+ )).toBe("focusSearch");+ expect(getShortcutActionForEvent(+ keyboardEvent("f", { ctrlKey: true, altKey: true }),+ settings,+ "MacIntel",+ )).toBeNull();+ expect(getShortcutActionForEvent(+ keyboardEvent("f", { ctrlKey: true, altKey: true }),+ settings,+ "Win32",+ )).toBe("focusSearch");+ expect(getShortcutActionForEvent(+ keyboardEvent("f", { metaKey: true, altKey: true }),+ settings,+ "Win32",+ )).toBeNull();+ expect(isPrimaryModifierPressed({ ctrlKey: true }, "MacIntel")).toBe(false);+ expect(isPrimaryModifierPressed({ metaKey: true }, "MacIntel")).toBe(true);+ expect(isPrimaryModifierPressed({ ctrlKey: true }, "Win32")).toBe(true);+ expect(isPrimaryModifierPressed({ ctrlKey: true, metaKey: true }, "Win32")).toBe(false);});test("toggles the sidebar only when it is visible", () => {diff --git a/apps/web/src/lib/app-helpers.ts b/apps/web/src/lib/app-helpers.tsindex 445c79cb..12fda533 100644--- a/apps/web/src/lib/app-helpers.ts+++ b/apps/web/src/lib/app-helpers.ts@@ -29,7 +29,12 @@ export type ShortcutAction =| "toggleSidebar";export type ShortcutBinding = {key: string;+ // Defaults and shortcuts saved before Control and Command were stored apart.+ // True means the platform primary modifier: Command on Apple, Control elsewhere.+ // A recorded shortcut also sets ctrl and meta, and those exact keys win.ctrlOrMeta: boolean;+ ctrl?: boolean;+ meta?: boolean;shift: boolean;alt: boolean;};@@ -556,13 +561,53 @@ export const normalizeShortcutKey = (key: string) => {return key.toLowerCase();};+const readShortcutPlatform = () => {+ if (typeof window !== "undefined" && window.navigator?.platform) return window.navigator.platform;+ if (typeof navigator !== "undefined" && navigator.platform) return navigator.platform;+ return "";+};++export const isAppleShortcutPlatform = (platform = readShortcutPlatform()) =>+ /mac|iphone|ipad|ipod/i.test(platform);++// Recorded ctrl/meta describe the keys the user pressed. Older ctrlOrMeta+// bindings follow the modifier the settings screen drew for this platform.+export const resolvedShortcutModifiers = (+ binding: Pick<ShortcutBinding, "ctrlOrMeta" | "ctrl" | "meta">,+ platform = readShortcutPlatform(),+) => {+ if (typeof binding.ctrl === "boolean" || typeof binding.meta === "boolean") {+ return { ctrl: binding.ctrl === true, meta: binding.meta === true };+ }++ if (binding.ctrlOrMeta && isAppleShortcutPlatform(platform)) {+ return { ctrl: false, meta: true };+ }++ if (binding.ctrlOrMeta) {+ return { ctrl: true, meta: false };+ }++ return { ctrl: false, meta: false };+};++export const isPrimaryModifierPressed = (+ event: { ctrlKey?: boolean; metaKey?: boolean },+ platform = readShortcutPlatform(),+) => {+ const ctrl = event.ctrlKey === true;+ const meta = event.metaKey === true;+ return isAppleShortcutPlatform(platform) ? meta && !ctrl : ctrl && !meta;+};+const isShortcutBinding = (value: unknown): value is ShortcutBinding => {+ if (!value || typeof value !== "object") return false;const binding = value as ShortcutBinding;+ const splitModifiers = typeof binding.ctrl === "boolean" && typeof binding.meta === "boolean";return (- Boolean(binding) &&typeof binding.key === "string" &&binding.key.trim().length > 0 &&- typeof binding.ctrlOrMeta === "boolean" &&+ (typeof binding.ctrlOrMeta === "boolean" || splitModifiers) &&typeof binding.shift === "boolean" &&typeof binding.alt === "boolean");@@ -579,7 +624,11 @@ export const readShortcutSettingsPreference = (): ShortcutSettings => {return SHORTCUT_ACTION_VALUES.reduce<ShortcutSettings>((settings, action) => {const storedBinding = parsedValue[action];const normalizedBinding = isShortcutBinding(storedBinding)- ? { ...storedBinding, key: normalizeShortcutKey(storedBinding.key) }+ ? {+ ...storedBinding,+ key: normalizeShortcutKey(storedBinding.key),+ ctrlOrMeta: typeof storedBinding.ctrlOrMeta === "boolean" ? storedBinding.ctrlOrMeta : false,+ }: DEFAULT_SHORTCUT_SETTINGS[action];const binding = action === "toggleReadingProtection"&& shortcutBindingsEqual(normalizedBinding, LEGACY_READING_PROTECTION_SHORTCUT)@@ -601,19 +650,33 @@ export const writeShortcutSettingsPreference = (settings: ShortcutSettings) => {}};-export const formatShortcutBinding = (binding: ShortcutBinding) => {- const isMac = /mac|iphone|ipad|ipod/i.test(window.navigator.platform);+export const formatShortcutBinding = (binding: ShortcutBinding, platform = readShortcutPlatform()) => {+ const apple = isAppleShortcutPlatform(platform);+ const modifiers = resolvedShortcutModifiers(binding, platform);const parts = [- binding.ctrlOrMeta ? (isMac ? "⌘" : "Ctrl") : null,- binding.alt ? (isMac ? "⌥" : "Alt") : null,- binding.shift ? (isMac ? "⇧" : "Shift") : null,+ modifiers.ctrl ? (apple ? "⌃" : "Ctrl") : null,+ modifiers.meta ? (apple ? "⌘" : "Meta") : null,+ binding.alt ? (apple ? "⌥" : "Alt") : null,+ binding.shift ? (apple ? "⇧" : "Shift") : null,binding.key === "space" ? "Space" : binding.key.length === 1 ? binding.key.toUpperCase() : binding.key,].filter(Boolean);- return parts.join(isMac ? "" : " + ");+ return parts.join(apple ? "" : " + ");};-export const shortcutBindingFromKeyboardEvent = (event: KeyboardEvent): ShortcutBinding | null => {+type ShortcutKeyEvent = {+ key: string;+ code?: string;+ ctrlKey: boolean;+ metaKey: boolean;+ altKey: boolean;+ shiftKey: boolean;+};++export const shortcutBindingFromKeyboardEvent = (+ event: ShortcutKeyEvent,+ platform = readShortcutPlatform(),+): ShortcutBinding | null => {const digitKey = typeof event.code === "string" ? /^Digit([0-9])$/.exec(event.code)?.[1] : undefined;const key = digitKey ?? normalizeShortcutKey(event.key);@@ -627,17 +690,27 @@ export const shortcutBindingFromKeyboardEvent = (event: KeyboardEvent): Shortcutreturn {key,- ctrlOrMeta: event.ctrlKey || event.metaKey,+ ctrlOrMeta: isPrimaryModifierPressed(event, platform),+ ctrl: event.ctrlKey,+ meta: 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 shortcutBindingsEqual = (+ first: ShortcutBinding,+ second: ShortcutBinding,+ platform = readShortcutPlatform(),+) => {+ const firstModifiers = resolvedShortcutModifiers(first, platform);+ const secondModifiers = resolvedShortcutModifiers(second, platform);+ return first.key === second.key &&+ firstModifiers.ctrl === secondModifiers.ctrl &&+ firstModifiers.meta === secondModifiers.meta &&+ first.shift === second.shift &&+ first.alt === second.alt;+};// The sidebar shortcut only acts when the sidebar can be seen: Focus Mode hides// it, so toggling there would change a persisted state without any feedback.@@ -646,16 +719,20 @@ export const canToggleNotebookSidebar = ({ isDesktop, focusModeActive }: {focusModeActive: boolean;}) => isDesktop && !focusModeActive;-export const getShortcutActionForEvent = (event: KeyboardEvent, settings: ShortcutSettings): ShortcutAction | null => {- const eventBinding = shortcutBindingFromKeyboardEvent(event);+export const getShortcutActionForEvent = (+ event: ShortcutKeyEvent,+ settings: ShortcutSettings,+ platform = readShortcutPlatform(),+): ShortcutAction | null => {+ const eventBinding = shortcutBindingFromKeyboardEvent(event, platform);if (!eventBinding) {return null;}return SHORTCUT_ACTION_VALUES.find((action) =>- shortcutBindingsEqual(settings[action], eventBinding) ||- (SHORTCUT_ALIASES[action] ?? []).some((binding) => shortcutBindingsEqual(binding, eventBinding))+ shortcutBindingsEqual(settings[action], eventBinding, platform) ||+ (SHORTCUT_ALIASES[action] ?? []).some((binding) => shortcutBindingsEqual(binding, eventBinding, platform))) ?? null;};