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
feat(options): make shortcut conflict detection scope-aware
Elian Dorana76d522ac232
diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts b/apps/client/src/widgets/type_widgets/options/shortcuts.spec.tsindex ceedc8494e..ed3c137274 100644--- a/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts@@ -7,8 +7,8 @@ function keyEvent(init: KeyboardEventInit) {return new KeyboardEvent("keydown", init);}-function action(actionName: string, friendlyName: string, effectiveShortcuts: string[], defaultShortcuts: string[] = effectiveShortcuts): ActionKeyboardShortcut {- return { actionName, friendlyName, effectiveShortcuts, defaultShortcuts } as ActionKeyboardShortcut;+function action(actionName: string, friendlyName: string, effectiveShortcuts: string[], defaultShortcuts: string[] = effectiveShortcuts, scope?: ActionKeyboardShortcut["scope"]): ActionKeyboardShortcut {+ return { actionName, friendlyName, effectiveShortcuts, defaultShortcuts, scope } as ActionKeyboardShortcut;}describe("keyboardEventToShortcut", () => {@@ -240,6 +240,39 @@ describe("computeConflicts", () => {expect(conflicts.get("a")?.get("Ctrl+J")).toEqual([ "Action B" ]);});++ describe("scope awareness", () => {+ const shortcuts = (sc: string) => [ sc ];++ it("flags same-combo actions that share a scope (e.g. Ctrl+0 lastTab vs zoomReset, both window)", () => {+ const conflicts = computeConflicts([+ action("lastTab", "Switch to Last Tab", shortcuts("Ctrl+0"), undefined, "window"),+ action("zoomReset", "Reset Zoom Level", shortcuts("Ctrl+0"), undefined, "window")+ ]);++ expect(conflicts.get("lastTab")?.get("Ctrl+0")).toEqual([ "Reset Zoom Level" ]);+ expect(conflicts.get("zoomReset")?.get("Ctrl+0")).toEqual([ "Switch to Last Tab" ]);+ });++ it("does not flag same-combo actions in mutually-exclusive scopes (e.g. Ctrl+Enter text vs code)", () => {+ const conflicts = computeConflicts([+ action("followLinkUnderCursor", "Follow Link Under Cursor", shortcuts("Ctrl+Enter"), undefined, "text-detail"),+ action("runActiveNote", "Run Active Note", shortcuts("Ctrl+Enter"), undefined, "code-detail")+ ]);++ expect(conflicts.size).toBe(0);+ });++ it("treats a window-scoped shortcut as conflicting with any other scope", () => {+ const conflicts = computeConflicts([+ action("a", "Window Action", shortcuts("Ctrl+K"), undefined, "window"),+ action("b", "Tree Action", shortcuts("Ctrl+K"), undefined, "note-tree")+ ]);++ expect(conflicts.get("a")?.get("Ctrl+K")).toEqual([ "Tree Action" ]);+ expect(conflicts.get("b")?.get("Ctrl+K")).toEqual([ "Window Action" ]);+ });+ });});describe("matchesFilter", () => {diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsxindex 6aa817ffd1..cc9fc9daa8 100644--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx@@ -211,14 +211,29 @@ export function groupShortcuts(shortcuts: KeyboardShortcut[]): ShortcutGroup[] {*/type ShortcutConflicts = Map<string, string[]>;+type ShortcutScope = ActionKeyboardShortcut["scope"];++/**+ * Whether two shortcuts attached at the given scopes can fire on the same keystroke. A `window`-scoped+ * shortcut is bound to the whole window, so it overlaps every other scope; two more specific scopes+ * (e.g. `text-detail` vs `code-detail`) target mutually-exclusive contexts and never collide unless+ * they are the same. An undefined scope is treated as `window` (the most permissive default).+ */+function scopesConflict(a: ShortcutScope, b: ShortcutScope) {+ const scopeA = a ?? "window";+ const scopeB = b ?? "window";+ return scopeA === scopeB || scopeA === "window" || scopeB === "window";+}+/*** Detects shortcut conflicts in a single O(total shortcuts) pass and returns a render-ready lookup:* `actionName → (shortcut string → conflicting action names)`. A row not present in the map has no* conflicts, and a shortcut not present on a row's entry is conflict-free — so the render path is* just two cheap map lookups, with all the canonicalization done up front.*- * Two shortcuts conflict when their {@link canonicalizeShortcut} forms match. The `global:` prefix is- * stripped first: an OS-level global shortcut still swallows the same combination an in-app one wants.+ * Two shortcuts conflict when their {@link canonicalizeShortcut} forms match *and* their scopes+ * overlap (see {@link scopesConflict}). The `global:` prefix is stripped first: an OS-level global+ * shortcut still swallows the same combination an in-app one wants.*/export function computeConflicts(shortcuts: KeyboardShortcut[]): Map<string, ShortcutConflicts> {// First pass: bucket every action by the canonical combination of each of its shortcuts.@@ -258,7 +273,7 @@ export function computeConflicts(shortcuts: KeyboardShortcut[]): Map<string, Sho}const others = actions- .filter((other) => other !== shortcut)+ .filter((other) => other !== shortcut && scopesConflict(shortcut.scope, other.scope)).map((other) => other.friendlyName ?? other.actionName);if (!others.length) {continue;