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
refactor(options): consolidate shortcut filters into a single dropdown
Elian Doranaf94692cd64f
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 945293f786..18006e43b6 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1739,14 +1739,13 @@"make_local": "Make this an in-app shortcut","global_shortcut": "System-wide (global) shortcut, only active on the desktop app","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_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"+ "filter_conflicts": "Conflicts only",+ "filter_conflicts_count": "Conflicts only ({{count}})",+ "filter_global": "Global 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 3a2df55fd0..20a82cef69 100644--- a/apps/client/src/widgets/type_widgets/options/shortcuts.css+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.css@@ -16,16 +16,11 @@flex: 1;}- .form-checkbox {- flex: 0 0 auto;- white-space: nowrap;- }-.dropdown {flex: 0 0 auto;}- /* Highlight the filter button while a scope filter is applied. */+ /* Highlight the filter button while a filter is applied. */.icon-action.active {color: var(--link-color);}diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.spec.ts b/apps/client/src/widgets/type_widgets/options/shortcuts.spec.tsindex e3af6a1172..6dab7ce6d4 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, matchesScopeFilter } from "./shortcuts";+import { computeConflicts, keyboardEventToShortcut, matchesFilter } from "./shortcuts";function keyEvent(init: KeyboardEventInit) {return new KeyboardEvent("keydown", init);@@ -147,29 +147,30 @@ describe("computeConflicts", () => {});});-describe("matchesScopeFilter", () => {+describe("matchesFilter", () => {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", []);+ // `a` and `b`/`c` collide on Ctrl+J / global:Ctrl+J; `d` and the unique Ctrl+K do not.+ const conflicts = computeConflicts([ globalAction, localAction, mixedAction, noShortcuts ]);- it("matches everything when no scope is selected", () => {+ it("matches everything when no filter is selected", () => {for (const a of [ globalAction, localAction, mixedAction, noShortcuts ]) {- expect(matchesScopeFilter(a, null)).toBe(true);+ expect(matchesFilter(a, null, conflicts)).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);+ expect(matchesFilter(globalAction, "global", conflicts)).toBe(true);+ expect(matchesFilter(mixedAction, "global", conflicts)).toBe(true);+ expect(matchesFilter(localAction, "global", conflicts)).toBe(false);+ expect(matchesFilter(noShortcuts, "global", conflicts)).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);+ it("keeps only actions involved in a conflict when filtering by conflicts", () => {+ expect(matchesFilter(globalAction, "conflicts", conflicts)).toBe(true);+ expect(matchesFilter(mixedAction, "conflicts", conflicts)).toBe(true);+ expect(matchesFilter(noShortcuts, "conflicts", conflicts)).toBe(false);});});diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsxindex 2b674b5350..fc5f8f1d43 100644--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx@@ -13,7 +13,6 @@ import { arrayEqual, isElectron, reloadFrontendApp } from "../../../services/utiimport 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";@@ -26,8 +25,7 @@ import OptionsSection from "./components/OptionsSection";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);+ const [ activeFilter, setActiveFilter ] = useState<ShortcutFilter>(null);useEffect(() => {server.get<KeyboardShortcut[]>("keyboard-actions").then(setKeyboardShortcuts);@@ -89,8 +87,7 @@ export default function ShortcutSettings() {.map((group) => ({...group,actions: group.actions.filter((action) =>- (!conflictsOnly || conflicts.has(action.actionName)) &&- matchesScopeFilter(action, scopeFilter) &&+ matchesFilter(action, activeFilter, conflicts) &&(!filter || filterKeyboardAction(action, filterLowerCase)))})).filter((group) => group.actions.length > 0);@@ -107,22 +104,15 @@ export default function ShortcutSettings() {placeholder={t("shortcuts.type_text_to_filter")}currentValue={filter} onChange={(value) => setFilter(value)}/>- <FormCheckbox- label={conflicts.size > 0- ? t("shortcuts.show_conflicts_only_count", { count: conflicts.size })- : t("shortcuts.show_conflicts_only")}- currentValue={conflictsOnly}- onChange={setConflictsOnly}- /><Dropdown- buttonClassName={`bx bx-filter-alt ${scopeFilter ? "active" : ""}`}+ buttonClassName={`bx bx-filter-alt ${activeFilter ? "active" : ""}`}hideToggleArrownoSelectButtonStylenoDropdownListStyleiconActiontitle={t("shortcuts.filter")}>- <ScopeFilterContent scopeFilter={scopeFilter} setScopeFilter={setScopeFilter} />+ <FilterContent activeFilter={activeFilter} setActiveFilter={setActiveFilter} conflictCount={conflicts.size} /></Dropdown></header>@@ -141,7 +131,7 @@ export default function ShortcutSettings() {text={t("shortcuts.no_results", { filter })}/>)- : conflictsOnly && conflicts.size === 0+ : activeFilter === "conflicts" && conflicts.size === 0? (<NoItemsicon="bx bx-check-circle"@@ -260,41 +250,42 @@ export function computeConflicts(shortcuts: KeyboardShortcut[]): Map<string, Sho}/**- * 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.+ * The active list filter: `"conflicts"` keeps only actions involved in a conflict, `"global"` keeps+ * only actions that have a system-wide (global) shortcut, and `null` applies no restriction.*/-type ScopeFilter = "global" | "local" | null;--export function matchesScopeFilter(action: ActionKeyboardShortcut, scopeFilter: ScopeFilter) {- if (scopeFilter === null) {- return true;+type ShortcutFilter = "conflicts" | "global" | null;++export function matchesFilter(action: ActionKeyboardShortcut, activeFilter: ShortcutFilter, conflicts: Map<string, ShortcutConflicts>) {+ switch (activeFilter) {+ case "conflicts":+ return conflicts.has(action.actionName);+ case "global":+ return (action.effectiveShortcuts ?? []).some(isGlobalShortcut);+ default:+ 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;+function FilterContent({ activeFilter, setActiveFilter, conflictCount }: {+ activeFilter: ShortcutFilter;+ setActiveFilter: (value: ShortcutFilter) => void;+ conflictCount: number;}) {return (<><FormListItem- checked={scopeFilter === null}- onClick={() => setScopeFilter(null)}+ checked={activeFilter === null}+ onClick={() => setActiveFilter(null)}>{t("shortcuts.filter_all")}</FormListItem><FormDropdownDivider /><FormListItem- checked={scopeFilter === "global"}- onClick={() => setScopeFilter("global")}- >{t("shortcuts.filter_global")}</FormListItem>+ checked={activeFilter === "conflicts"}+ onClick={() => setActiveFilter("conflicts")}+ >{conflictCount > 0 ? t("shortcuts.filter_conflicts_count", { count: conflictCount }) : t("shortcuts.filter_conflicts")}</FormListItem><FormListItem- checked={scopeFilter === "local"}- onClick={() => setScopeFilter("local")}- >{t("shortcuts.filter_local")}</FormListItem>+ checked={activeFilter === "global"}+ onClick={() => setActiveFilter("global")}+ >{t("shortcuts.filter_global")}</FormListItem></>);}