Skip to content

Build your personal knowledge base with Trilium Notes

feat(options): group and count shortcut conflicts by combination

Elian Dorancf294839eadd
diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts b/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts
index ed3c137274..8806a567c4 100644
--- a/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts
+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts
@@ -1,7 +1,7 @@
import { ActionKeyboardShortcut, KeyboardShortcut } from "@triliumnext/commons";
import { describe, expect, it } from "vitest";
-import { computeConflicts, filterKeyboardAction, getActionNameFromOptionName, getOptionName, groupShortcuts, isRecorderCancelKey, keyboardEventToShortcut, matchesFilter, setGlobalShortcut } from "./shortcuts";
+import { computeConflictGroups, computeConflicts, filterKeyboardAction, getActionNameFromOptionName, getOptionName, groupShortcuts, isRecorderCancelKey, keyboardEventToShortcut, matchesFilter, setGlobalShortcut } from "./shortcuts";
function keyEvent(init: KeyboardEventInit) {
return new KeyboardEvent("keydown", init);
@@ -275,6 +275,41 @@ describe("computeConflicts", () => {
});
});
+describe("computeConflictGroups", () => {
+ it("groups conflicting actions by the combination they collide on, titled with the combo", () => {
+ const groups = computeConflictGroups([
+ action("a", "Action A", [ "Ctrl+J" ]),
+ action("b", "Action B", [ "Ctrl+J" ]),
+ action("c", "Action C", [ "Ctrl+K" ])
+ ]);
+
+ expect(groups).toHaveLength(1);
+ expect(groups[0].title).toBe("Ctrl+J");
+ expect(groups[0].actions.map((a) => a.actionName)).toEqual([ "a", "b" ]);
+ });
+
+ it("excludes combinations whose actions are in mutually-exclusive scopes", () => {
+ const groups = computeConflictGroups([
+ action("a", "Action A", [ "Ctrl+Enter" ], undefined, "text-detail"),
+ action("b", "Action B", [ "Ctrl+Enter" ], undefined, "code-detail")
+ ]);
+
+ expect(groups).toHaveLength(0);
+ });
+
+ it("lists an action under each combination it conflicts on", () => {
+ const groups = computeConflictGroups([
+ action("multi", "Multi", [ "Ctrl+J", "Ctrl+K" ]),
+ action("a", "Action A", [ "Ctrl+J" ]),
+ action("b", "Action B", [ "Ctrl+K" ])
+ ]);
+
+ expect(groups.map((g) => g.title)).toEqual([ "Ctrl+J", "Ctrl+K" ]);
+ expect(groups[0].actions.map((a) => a.actionName)).toEqual([ "multi", "a" ]);
+ expect(groups[1].actions.map((a) => a.actionName)).toEqual([ "multi", "b" ]);
+ });
+});
+
describe("matchesFilter", () => {
const globalAction = action("a", "Action A", [ "global:Ctrl+J" ]);
const localAction = action("b", "Action B", [ "Ctrl+K" ]);
diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
index 92ff073eef..7cbc43e564 100644
--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx
@@ -93,9 +93,16 @@ export default function ShortcutSettings() {
const filterLowerCase = filter?.toLowerCase() ?? "";
const groups = useMemo(() => groupShortcuts(keyboardShortcuts), [ keyboardShortcuts ]);
const conflicts = useMemo(() => computeConflicts(keyboardShortcuts), [ keyboardShortcuts ]);
+ const conflictGroups = useMemo(() => computeConflictGroups(keyboardShortcuts), [ keyboardShortcuts ]);
+ // Count distinct clashing key combinations, not the number of actions involved: two actions on one
+ // key is a single conflict, not two.
+ const conflictCount = conflictGroups.length;
const globalCount = useMemo(() => keyboardShortcuts.filter((s) => "actionName" in s && hasGlobalShortcut(s)).length, [ keyboardShortcuts ]);
const modifiedCount = useMemo(() => keyboardShortcuts.filter((s) => "actionName" in s && isShortcutModified(s)).length, [ keyboardShortcuts ]);
- const filteredGroups = groups
+
+ // When filtering by conflicts, group the actions by the combination they collide on instead of
+ // by their settings section, so the colliding actions sit together.
+ const filteredGroups = (activeFilter === "conflicts" ? conflictGroups : groups)
.map((group) => ({
...group,
actions: group.actions.filter((action) =>
@@ -108,11 +115,11 @@ export default function ShortcutSettings() {
<div className="shortcuts-options-section">
<header>
<div class="shortcut-header-row">
- {conflicts.size > 0 &&
+ {conflictCount > 0 &&
<Badge
className={`shortcut-conflicts-badge ${activeFilter === "conflicts" ? "active" : ""}`}
icon="bx bx-error-circle"
- text={t("shortcuts.conflicts_badge", { count: conflicts.size })}
+ text={t("shortcuts.conflicts_badge", { count: conflictCount })}
tooltip={t("shortcuts.conflicts_badge_tooltip")}
outline
onClick={() => setActiveFilter(activeFilter === "conflicts" ? null : "conflicts")}
@@ -146,7 +153,7 @@ export default function ShortcutSettings() {
<FilterContent
activeFilter={activeFilter}
onSelect={selectFilter}
- conflictCount={conflicts.size}
+ conflictCount={conflictCount}
globalCount={globalCount}
modifiedCount={modifiedCount}
/>
@@ -169,7 +176,7 @@ export default function ShortcutSettings() {
text={t("shortcuts.no_results", { filter })}
/>
)
- : activeFilter === "conflicts" && conflicts.size === 0
+ : activeFilter === "conflicts" && conflictCount === 0
? (
<NoItems
icon="bx bx-check-circle"
@@ -290,6 +297,48 @@ export function computeConflicts(shortcuts: KeyboardShortcut[]): Map<string, Sho
return result;
}
+/**
+ * Groups conflicting actions by the key combination they collide on, for the "Conflicts" filter view.
+ * Each returned group is titled with the combination (e.g. `Ctrl+0`) and lists every action bound to
+ * it that conflicts with another (scope-aware — see {@link scopesConflict}). An action with two
+ * conflicting shortcuts appears under both combinations. Shaped like {@link groupShortcuts} so it
+ * drops straight into the same render path.
+ */
+export function computeConflictGroups(shortcuts: KeyboardShortcut[]): ShortcutGroup[] {
+ const byCombo = new Map<string, { display: string; actions: ActionKeyboardShortcut[] }>();
+ for (const shortcut of shortcuts) {
+ if (!("actionName" in shortcut)) {
+ continue;
+ }
+
+ for (const combo of shortcut.effectiveShortcuts ?? []) {
+ const bare = stripGlobalPrefix(combo);
+ const key = canonicalizeShortcut(bare);
+ if (!key) {
+ continue;
+ }
+
+ let group = byCombo.get(key);
+ if (!group) {
+ // The first stored form of the combination is used as the readable group title.
+ byCombo.set(key, group = { display: bare, actions: [] });
+ }
+ if (!group.actions.includes(shortcut)) {
+ group.actions.push(shortcut);
+ }
+ }
+ }
+
+ const groups: ShortcutGroup[] = [];
+ for (const { display, actions } of byCombo.values()) {
+ const conflicting = actions.filter((a) => actions.some((b) => b !== a && scopesConflict(a.scope, b.scope)));
+ if (conflicting.length >= 2) {
+ groups.push({ title: display, actions: conflicting });
+ }
+ }
+ return groups;
+}
+
/**
* The active list filter: `"conflicts"` keeps only actions involved in a conflict, `"global"` keeps
* only actions that have a system-wide (global) shortcut, `"modified"` keeps only actions whose