Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
test(options): cover keyboard shortcut recorder; reject single-key shortcuts
Elian Dorana4ecdf7f1315
diff --git a/apps/client/src/services/shortcuts.ts b/apps/client/src/services/shortcuts.tsindex 34d5f8eda7..800576653d 100644--- a/apps/client/src/services/shortcuts.ts+++ b/apps/client/src/services/shortcuts.ts@@ -43,7 +43,8 @@ for (let i = 1; i <= 19; i++) {keyMap[`f${i}`] = [ keyCode ];}-const KEYCODES_WITH_NO_MODIFIER = new Set([+// Key codes (e.g. function keys, Delete, Enter) that are allowed as shortcuts without a modifier.+export const KEYCODES_WITH_NO_MODIFIER = new Set(["Delete","Enter","NumpadEnter",diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts b/apps/client/src/widgets/type_widgets/options/shortcuts.spec.tsnew file mode 100644index 0000000000..98825134b0--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts@@ -0,0 +1,61 @@+import { describe, expect, it } from "vitest";++import { keyboardEventToShortcut } from "./shortcuts";++function keyEvent(init: KeyboardEventInit) {+ return new KeyboardEvent("keydown", init);+}++describe("keyboardEventToShortcut", () => {+ it("combines modifiers with the pressed key", () => {+ expect(keyboardEventToShortcut(keyEvent({ key: "j", code: "KeyJ", ctrlKey: true }))).toBe("Ctrl+J");+ expect(keyboardEventToShortcut(keyEvent({ key: "J", code: "KeyJ", ctrlKey: true, shiftKey: true }))).toBe("Ctrl+Shift+J");+ });++ it("always orders modifiers as Ctrl, Alt, Shift, Meta", () => {+ const shortcut = keyboardEventToShortcut(keyEvent({+ key: "k", code: "KeyK", ctrlKey: true, altKey: true, shiftKey: true, metaKey: true+ }));++ expect(shortcut).toBe("Ctrl+Alt+Shift+Meta+K");+ });++ it("uses the physical key code so the result is keyboard-layout independent", () => {+ // On macOS, Alt produces a special character in `key`, but `code` stays stable.+ expect(keyboardEventToShortcut(keyEvent({ key: "√", code: "KeyV", altKey: true }))).toBe("Alt+V");+ expect(keyboardEventToShortcut(keyEvent({ key: "1", code: "Digit1", ctrlKey: true }))).toBe("Ctrl+1");+ expect(keyboardEventToShortcut(keyEvent({ key: "1", code: "Numpad1", ctrlKey: true }))).toBe("Ctrl+1");+ });++ it("maps arrow and space keys to the stored names", () => {+ expect(keyboardEventToShortcut(keyEvent({ key: "ArrowLeft", code: "ArrowLeft", altKey: true }))).toBe("Alt+Left");+ expect(keyboardEventToShortcut(keyEvent({ key: "ArrowUp", code: "ArrowUp", ctrlKey: true }))).toBe("Ctrl+Up");+ expect(keyboardEventToShortcut(keyEvent({ key: " ", code: "Space", ctrlKey: true }))).toBe("Ctrl+Space");+ });++ it("returns null for a lone modifier key", () => {+ expect(keyboardEventToShortcut(keyEvent({ key: "Shift", code: "ShiftLeft", shiftKey: true }))).toBeNull();+ expect(keyboardEventToShortcut(keyEvent({ key: "Control", code: "ControlLeft", ctrlKey: true }))).toBeNull();+ expect(keyboardEventToShortcut(keyEvent({ key: "Meta", code: "MetaLeft", metaKey: true }))).toBeNull();+ });++ describe("single-key shortcuts", () => {+ it("rejects a modifier-less ordinary key", () => {+ expect(keyboardEventToShortcut(keyEvent({ key: "a", code: "KeyA" }))).toBeNull();+ expect(keyboardEventToShortcut(keyEvent({ key: "1", code: "Digit1" }))).toBeNull();+ expect(keyboardEventToShortcut(keyEvent({ key: "Escape", code: "Escape" }))).toBeNull();+ });++ it("allows modifier-less function keys, Delete and Enter", () => {+ expect(keyboardEventToShortcut(keyEvent({ key: "F5", code: "F5" }))).toBe("F5");+ expect(keyboardEventToShortcut(keyEvent({ key: "F11", code: "F11" }))).toBe("F11");+ expect(keyboardEventToShortcut(keyEvent({ key: "Delete", code: "Delete" }))).toBe("Delete");+ expect(keyboardEventToShortcut(keyEvent({ key: "Enter", code: "Enter" }))).toBe("Enter");+ expect(keyboardEventToShortcut(keyEvent({ key: "Enter", code: "NumpadEnter" }))).toBe("Enter");+ });++ it("allows a single key once a modifier is held", () => {+ expect(keyboardEventToShortcut(keyEvent({ key: "a", code: "KeyA", shiftKey: true }))).toBe("Shift+A");+ });+ });+});diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsxindex 227e8a0093..ffa7526c73 100644--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx@@ -8,6 +8,7 @@ import FormTextBox from "../../react/FormTextBox";import RawHtml from "../../react/RawHtml";import OptionsRow from "./components/OptionsRow";import OptionsSection from "./components/OptionsSection";+import { KEYCODES_WITH_NO_MODIFIER } from "../../../services/shortcuts";import { useCallback, useEffect, useMemo, useState } from "preact/hooks";import server from "../../../services/server";import options from "../../../services/options";@@ -246,11 +247,8 @@ function ShortcutRecorder({ onCapture }: { onCapture: (shortcut: string) => voidreturn;}- // Wait for a non-modifier key before finalizing the combination.- if (MODIFIER_KEYS.has(e.key)) {- return;- }-+ // Ignore lone modifiers and invalid single-key combinations (keyboardEventToShortcut+ // returns null) so recording continues until a bindable combination is pressed.const shortcut = keyboardEventToShortcut(e);if (shortcut) {onCapture(shortcut);@@ -280,27 +278,37 @@ function ShortcutRecorder({ onCapture }: { onCapture: (shortcut: string) => voidconst MODIFIER_KEYS = new Set([ "Control", "Alt", "Shift", "Meta" ]);-/** Converts a captured {@link KeyboardEvent} into a shortcut string matching the stored format (e.g. `Ctrl+Shift+J`). */-function keyboardEventToShortcut(e: KeyboardEvent): string | null {+const NAMED_KEYS: Record<string, string> = {+ ArrowUp: "Up", ArrowDown: "Down", ArrowLeft: "Left", ArrowRight: "Right",+ " ": "Space", Spacebar: "Space"+};++/**+ * Converts a captured {@link KeyboardEvent} into a shortcut string matching the stored format+ * (e.g. `Ctrl+Shift+J`). Returns `null` when the event is not a valid, bindable shortcut, i.e.:+ * - a lone modifier key (Ctrl/Alt/Shift/Meta), or+ * - a modifier-less key the matcher would never fire — everything except function keys, Delete+ * and Enter (see {@link KEYCODES_WITH_NO_MODIFIER}).+ */+export function keyboardEventToShortcut(e: KeyboardEvent): string | null {const key = normalizeCapturedKey(e);if (!key) {return null;}- const parts: string[] = [];- if (e.ctrlKey) parts.push("Ctrl");- if (e.altKey) parts.push("Alt");- if (e.shiftKey) parts.push("Shift");- if (e.metaKey) parts.push("Meta");- parts.push(key);+ const modifiers: string[] = [];+ if (e.ctrlKey) modifiers.push("Ctrl");+ if (e.altKey) modifiers.push("Alt");+ if (e.shiftKey) modifiers.push("Shift");+ if (e.metaKey) modifiers.push("Meta");- return parts.join("+");-}+ // Disallow single-key shortcuts that have no modifier, since they would never match at runtime.+ if (modifiers.length === 0 && !KEYCODES_WITH_NO_MODIFIER.has(e.code)) {+ return null;+ }-const NAMED_KEYS: Record<string, string> = {- ArrowUp: "Up", ArrowDown: "Down", ArrowLeft: "Left", ArrowRight: "Right",- " ": "Space", Spacebar: "Space"-};+ return [ ...modifiers, key ].join("+");+}function normalizeCapturedKey(e: KeyboardEvent): string | null {const { code, key } = e;