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): filter shortcuts by global/local scope
Elian Doranb899d1d667c7
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 83acbacec5..945293f786 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1741,7 +1741,12 @@"conflict_chip": "This combination is also assigned to: {{actions}}","show_conflicts_only": "Show conflicts only","show_conflicts_only_count": "Show conflicts only ({{count}})",- "no_conflicts": "No conflicting shortcuts."+ "no_conflicts": "No conflicting shortcuts.",+ "no_matches": "No shortcuts match the selected filters.",+ "filter": "Filter shortcuts",+ "filter_all": "All shortcuts",+ "filter_global": "System-wide (global) shortcuts only",+ "filter_local": "In-app (local) shortcuts only"},"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.cssindex ee54b4d741..3a2df55fd0 100644--- a/apps/client/src/widgets/type_widgets/options/shortcuts.css+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.css@@ -20,6 +20,15 @@flex: 0 0 auto;white-space: nowrap;}++ .dropdown {+ flex: 0 0 auto;+ }++ /* Highlight the filter button while a scope filter is applied. */+ .icon-action.active {+ color: var(--link-color);+ }}> footer {diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts b/apps/client/src/widgets/type_widgets/options/shortcuts.spec.tsindex 3366049210..e3af6a1172 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, keyboardEventToShortcut } from "./shortcuts";+import { computeConflicts, keyboardEventToShortcut, matchesScopeFilter } from "./shortcuts";function keyEvent(init: KeyboardEventInit) {return new KeyboardEvent("keydown", init);@@ -146,3 +146,30 @@ describe("computeConflicts", () => {expect(conflicts.get("a")?.get("Ctrl+J")).toEqual([ "Action B" ]);});});++describe("matchesScopeFilter", () => {+ const globalAction = action("a", "Action A", [ "global:Ctrl+J" ]);+ const localAction = action("b", "Action B", [ "Ctrl+K" ]);+ const mixedAction = action("c", "Action C", [ "global:Ctrl+J", "Ctrl+K" ]);+ const noShortcuts = action("d", "Action D", []);++ it("matches everything when no scope is selected", () => {+ for (const a of [ globalAction, localAction, mixedAction, noShortcuts ]) {+ expect(matchesScopeFilter(a, null)).toBe(true);+ }+ });++ it("keeps only actions with a global shortcut when filtering by global", () => {+ expect(matchesScopeFilter(globalAction, "global")).toBe(true);+ expect(matchesScopeFilter(mixedAction, "global")).toBe(true);+ expect(matchesScopeFilter(localAction, "global")).toBe(false);+ expect(matchesScopeFilter(noShortcuts, "global")).toBe(false);+ });++ it("keeps only actions with an in-app shortcut when filtering by local", () => {+ expect(matchesScopeFilter(localAction, "local")).toBe(true);+ expect(matchesScopeFilter(mixedAction, "local")).toBe(true);+ expect(matchesScopeFilter(globalAction, "local")).toBe(false);+ expect(matchesScopeFilter(noShortcuts, "local")).toBe(false);+ });+});diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsxindex 4eb70b76bb..2b674b5350 100644--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx@@ -12,7 +12,9 @@ import toast from "../../../services/toast";import { arrayEqual, isElectron, reloadFrontendApp } from "../../../services/utils";import ActionButton from "../../react/ActionButton";import Button from "../../react/Button";+import Dropdown from "../../react/Dropdown";import FormCheckbox from "../../react/FormCheckbox";+import { FormDropdownDivider, FormListItem } from "../../react/FormList";import FormText from "../../react/FormText";import FormTextBox from "../../react/FormTextBox";import { useTriliumEvent } from "../../react/hooks";@@ -25,6 +27,7 @@ export default function ShortcutSettings() {const [ keyboardShortcuts, setKeyboardShortcuts ] = useState<KeyboardShortcut[]>([]);const [ filter, setFilter ] = useState<string>();const [ conflictsOnly, setConflictsOnly ] = useState(false);+ const [ scopeFilter, setScopeFilter ] = useState<ScopeFilter>(null);useEffect(() => {server.get<KeyboardShortcut[]>("keyboard-actions").then(setKeyboardShortcuts);@@ -87,6 +90,7 @@ export default function ShortcutSettings() {...group,actions: group.actions.filter((action) =>(!conflictsOnly || conflicts.has(action.actionName)) &&+ matchesScopeFilter(action, scopeFilter) &&(!filter || filterKeyboardAction(action, filterLowerCase)))})).filter((group) => group.actions.length > 0);@@ -110,6 +114,16 @@ export default function ShortcutSettings() {currentValue={conflictsOnly}onChange={setConflictsOnly}/>+ <Dropdown+ buttonClassName={`bx bx-filter-alt ${scopeFilter ? "active" : ""}`}+ hideToggleArrow+ noSelectButtonStyle+ noDropdownListStyle+ iconAction+ title={t("shortcuts.filter")}+ >+ <ScopeFilterContent scopeFilter={scopeFilter} setScopeFilter={setScopeFilter} />+ </Dropdown></header>{filteredGroups.length > 0@@ -120,19 +134,26 @@ export default function ShortcutSettings() {))}</OptionsSection>))- : conflictsOnly && conflicts.size === 0+ : filter? (- <NoItems- icon="bx bx-check-circle"- text={t("shortcuts.no_conflicts")}- />- )- : (<NoItemsicon="bx bx-filter-alt"text={t("shortcuts.no_results", { filter })}/>- )}+ )+ : conflictsOnly && conflicts.size === 0+ ? (+ <NoItems+ icon="bx bx-check-circle"+ text={t("shortcuts.no_conflicts")}+ />+ )+ : (+ <NoItems+ icon="bx bx-filter-alt"+ text={t("shortcuts.no_matches")}+ />+ )}<footer><Button@@ -238,6 +259,46 @@ export function computeConflicts(shortcuts: KeyboardShortcut[]): Map<string, Shoreturn result;}+/**+ * Restricts the list to actions that have at least one shortcut of a given scope: `"global"` for+ * system-wide shortcuts, `"local"` for in-app ones, or `null` for no restriction.+ */+type ScopeFilter = "global" | "local" | null;++export function matchesScopeFilter(action: ActionKeyboardShortcut, scopeFilter: ScopeFilter) {+ if (scopeFilter === null) {+ return true;+ }++ const shortcuts = action.effectiveShortcuts ?? [];+ return scopeFilter === "global"+ ? shortcuts.some(isGlobalShortcut)+ : shortcuts.some((shortcut) => !isGlobalShortcut(shortcut));+}++function ScopeFilterContent({ scopeFilter, setScopeFilter }: {+ scopeFilter: ScopeFilter;+ setScopeFilter: (value: ScopeFilter) => void;+}) {+ return (+ <>+ <FormListItem+ checked={scopeFilter === null}+ onClick={() => setScopeFilter(null)}+ >{t("shortcuts.filter_all")}</FormListItem>+ <FormDropdownDivider />+ <FormListItem+ checked={scopeFilter === "global"}+ onClick={() => setScopeFilter("global")}+ >{t("shortcuts.filter_global")}</FormListItem>+ <FormListItem+ checked={scopeFilter === "local"}+ onClick={() => setScopeFilter("local")}+ >{t("shortcuts.filter_local")}</FormListItem>+ </>+ );+}+function isShortcutModified(action: ActionKeyboardShortcut) {return !arrayEqual(action.effectiveShortcuts ?? [], action.defaultShortcuts ?? []);}