Skip to content

Build your personal knowledge base with Trilium Notes

feat(options): interactive keyboard shortcut editor

Elian Doran703a4eeebce1
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index d9b1b2b882..24d73054bc 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1721,7 +1721,7 @@
},
"shortcuts": {
"keyboard_shortcuts": "Keyboard Shortcuts",
- "multiple_shortcuts": "Multiple shortcuts for the same action can be separated by comma.",
+ "multiple_shortcuts": "Press the + button next to an action and type a key combination to assign a shortcut. Each action can have several shortcuts.",
"electron_documentation": "See <a href=\"https://www.electronjs.org/docs/latest/api/accelerator\">Electron documentation</a> for available modifiers and key codes.",
"type_text_to_filter": "Type text to filter shortcuts...",
"reload_app": "Reload app to apply changes",
@@ -1730,7 +1730,10 @@
"no_results": "No shortcuts found matching '{{filter}}'",
"modified_from_default": "This shortcut has been changed from its default.",
"revert_to_default": "Revert to default ({{shortcuts}})",
- "no_default_shortcut": "no shortcut"
+ "no_default_shortcut": "no shortcut",
+ "record_shortcut": "Add a shortcut",
+ "remove_shortcut": "Remove this shortcut",
+ "press_keys": "Press keys…"
},
"spellcheck": {
"title": "Spell Check",
diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.css b/apps/client/src/widgets/type_widgets/options/shortcuts.css
index 9bed831acf..fa080c8473 100644
--- a/apps/client/src/widgets/type_widgets/options/shortcuts.css
+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.css
@@ -38,7 +38,60 @@
gap: 0.5em;
}
- .shortcut-row-input > input {
- min-width: 220px;
+ .shortcut-editor {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ justify-content: flex-end;
+ gap: 0.4em;
+ }
+
+ .shortcut-chip {
+ display: inline-flex;
+ align-items: center;
+ gap: 0.25em;
+ padding: 0.1em 0.2em 0.1em 0.5em;
+ background: var(--input-background-color);
+ border-radius: 6px;
+ }
+
+ .shortcut-chip kbd {
+ background: none;
+ border: none;
+ box-shadow: none;
+ padding: 0;
+ font-size: 0.9em;
+ color: var(--main-text-color);
+ }
+
+ .shortcut-chip-remove {
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ padding: 0.1em;
+ border: none;
+ background: none;
+ color: var(--muted-text-color);
+ border-radius: 4px;
+ cursor: pointer;
+ }
+
+ .shortcut-chip-remove:hover {
+ color: var(--main-text-color);
+ background: var(--hover-item-background-color);
+ }
+
+ /* Idle state reuses the standard .icon-action button styling; only the recording pill is custom. */
+ .shortcut-recorder.recording {
+ display: inline-flex;
+ align-items: center;
+ height: 35px;
+ padding-inline: 0.75em;
+ border: 1px solid var(--link-color);
+ border-radius: var(--button-border-radius);
+ background: var(--button-background-color);
+ color: var(--link-color);
+ white-space: nowrap;
+ cursor: pointer;
}
}
diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
index 4241bdc1f2..227e8a0093 100644
--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
@@ -195,23 +195,136 @@ function ShortcutRow({ action }: { action: ActionKeyboardShortcut }) {
}
function ShortcutEditor({ keyboardShortcut: action }: { keyboardShortcut: ActionKeyboardShortcut }) {
- const originalShortcut = (action.effectiveShortcuts ?? []).join(", ");
+ const shortcuts = action.effectiveShortcuts ?? [];
+
+ const saveShortcuts = (newShortcuts: string[]) => {
+ void options.save(getOptionName(action.actionName), JSON.stringify(newShortcuts));
+ };
+
+ const addShortcut = (shortcut: string) => {
+ if (!shortcuts.includes(shortcut)) {
+ saveShortcuts([ ...shortcuts, shortcut ]);
+ }
+ };
return (
- <FormTextBox
- currentValue={originalShortcut}
- onBlur={(newShortcut) => {
- const { actionName } = action;
- const optionName = getOptionName(actionName);
- const newShortcuts = newShortcut
- .replace("+,", "+Comma")
- .split(",")
- .map((shortcut) => shortcut.replace("+Comma", "+,"))
- .filter((shortcut) => !!shortcut);
- options.save(optionName, JSON.stringify(newShortcuts));
- }}
- />
- )
+ <div class="shortcut-editor">
+ {shortcuts.map((shortcut) => (
+ <span class="shortcut-chip" key={shortcut}>
+ <kbd>{shortcut}</kbd>
+ <button
+ type="button"
+ class="shortcut-chip-remove"
+ title={t("shortcuts.remove_shortcut")}
+ onClick={() => saveShortcuts(shortcuts.filter((s) => s !== shortcut))}
+ >
+ <span class="bx bx-x" />
+ </button>
+ </span>
+ ))}
+
+ <ShortcutRecorder onCapture={addShortcut} />
+ </div>
+ );
+}
+
+function ShortcutRecorder({ onCapture }: { onCapture: (shortcut: string) => void }) {
+ const [ recording, setRecording ] = useState(false);
+
+ useEffect(() => {
+ if (!recording) {
+ return;
+ }
+
+ const onKeyDown = (e: KeyboardEvent) => {
+ // Swallow the combination so it never reaches the application's own shortcut handlers.
+ e.preventDefault();
+ e.stopImmediatePropagation();
+
+ if (e.key === "Escape") {
+ setRecording(false);
+ return;
+ }
+
+ // Wait for a non-modifier key before finalizing the combination.
+ if (MODIFIER_KEYS.has(e.key)) {
+ return;
+ }
+
+ const shortcut = keyboardEventToShortcut(e);
+ if (shortcut) {
+ onCapture(shortcut);
+ setRecording(false);
+ }
+ };
+
+ // Capture phase on window so we intercept the keystroke ahead of any other listener.
+ window.addEventListener("keydown", onKeyDown, true);
+ return () => window.removeEventListener("keydown", onKeyDown, true);
+ }, [ recording, onCapture ]);
+
+ return (
+ <button
+ type="button"
+ class={recording
+ ? "shortcut-recorder recording"
+ : "shortcut-recorder icon-action bx bx-plus"}
+ title={recording ? undefined : t("shortcuts.record_shortcut")}
+ onClick={() => setRecording((value) => !value)}
+ onBlur={() => setRecording(false)}
+ >
+ {recording ? t("shortcuts.press_keys") : null}
+ </button>
+ );
+}
+
+const 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 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);
+
+ return parts.join("+");
+}
+
+const NAMED_KEYS: Record<string, string> = {
+ ArrowUp: "Up", ArrowDown: "Down", ArrowLeft: "Left", ArrowRight: "Right",
+ " ": "Space", Spacebar: "Space"
+};
+
+function normalizeCapturedKey(e: KeyboardEvent): string | null {
+ const { code, key } = e;
+
+ if (MODIFIER_KEYS.has(key)) {
+ return null;
+ }
+
+ // Use the physical code for letters/digits so the result is keyboard-layout independent.
+ const letter = /^Key([A-Z])$/.exec(code);
+ if (letter) {
+ return letter[1];
+ }
+ const digit = /^(?:Digit|Numpad)(\d)$/.exec(code);
+ if (digit) {
+ return digit[1];
+ }
+
+ if (NAMED_KEYS[key]) {
+ return NAMED_KEYS[key];
+ }
+
+ // Function keys and named keys (Enter, Delete, F5, Home, …) already match the stored format.
+ return key.length === 1 ? key.toUpperCase() : key;
}
const PREFIX = "keyboardShortcuts";