From b97a5ef8884a1411de3d5b6e64857dccebc84f0b Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 18 Aug 2025 19:47:40 +0300 Subject: [PATCH] chore(react/settings): reimplement reset shortcuts --- apps/client/src/services/options.ts | 5 +++++ apps/client/src/services/utils.ts | 17 +++++++++++++++++ apps/client/src/widgets/react/hooks.tsx | 3 +-- .../widgets/type_widgets/options/shortcuts.tsx | 17 +++++++++++++---- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/apps/client/src/services/options.ts b/apps/client/src/services/options.ts index 9a97153d0..e58ff9ddb 100644 --- a/apps/client/src/services/options.ts +++ b/apps/client/src/services/options.ts @@ -1,3 +1,4 @@ +import { OptionNames } from "@triliumnext/commons"; import server from "./server.js"; import { isShare } from "./utils.js"; @@ -76,6 +77,10 @@ class Options { await server.put(`options`, payload); } + async saveMany(newValues: Record) { + await server.put("options", newValues); + } + async toggle(key: string) { await this.save(key, (!this.is(key)).toString()); } diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts index 306fdfbd1..d8f05c1a8 100644 --- a/apps/client/src/services/utils.ts +++ b/apps/client/src/services/utils.ts @@ -750,6 +750,23 @@ export function toggleBodyClass(prefix: string, value: string) { $body.addClass(prefix + value); } +export function arrayEqual(a: T[], b: T[]) { + if (a === b) { + return true; + } + if (a.length !== b.length) { + return false; + } + + for (let i=0; i < a.length; i++) { + if (a[i] !== b[i]) { + return false; + } + } + + return true; +} + export default { reloadFrontendApp, restartDesktopApp, diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx index 97614adc4..5c18ad4b2 100644 --- a/apps/client/src/widgets/react/hooks.tsx +++ b/apps/client/src/widgets/react/hooks.tsx @@ -167,10 +167,9 @@ export function useTriliumOptions(...names: T[]) { values[name] = options.get(name); } - const setValue = (newValues: Record) => server.put("options", newValues); return [ values as Record, - setValue + options.saveMany ] as const; } diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx index b948902c3..a4227056c 100644 --- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx +++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx @@ -1,6 +1,6 @@ -import { ActionKeyboardShortcut, KeyboardShortcut } from "@triliumnext/commons"; +import { ActionKeyboardShortcut, KeyboardShortcut, OptionNames } from "@triliumnext/commons"; import { t } from "../../../services/i18n"; -import { reloadFrontendApp } from "../../../services/utils"; +import { arrayEqual, reloadFrontendApp } from "../../../services/utils"; import Button from "../../react/Button"; import FormGroup from "../../react/FormGroup"; import FormText from "../../react/FormText"; @@ -11,6 +11,7 @@ import { useCallback, useEffect, useState } from "preact/hooks"; import server from "../../../services/server"; import options from "../../../services/options"; import dialog from "../../../services/dialog"; +import { useTriliumOptions } from "../../react/hooks"; export default function ShortcutSettings() { const [ keyboardShortcuts, setKeyboardShortcuts ] = useState([]); @@ -25,13 +26,17 @@ export default function ShortcutSettings() { return; } - const newKeyboardShortcuts = []; + const optionsToSet: Record = {}; for (const keyboardShortcut of keyboardShortcuts) { if (!("effectiveShortcuts" in keyboardShortcut)) { continue; } + if (!arrayEqual(keyboardShortcut.effectiveShortcuts, keyboardShortcut.defaultShortcuts)) { + optionsToSet[getOptionName(keyboardShortcut.actionName)] = JSON.stringify(keyboardShortcut.defaultShortcuts); + } } + options.saveMany(optionsToSet); }, [ keyboardShortcuts ]); return ( @@ -125,7 +130,7 @@ function ShortcutEditor({ keyboardShortcut: action }: { keyboardShortcut: Action { const { actionName } = action; - const optionName = `keyboardShortcuts${actionName.substr(0, 1).toUpperCase()}${actionName.substr(1)}`; + const optionName = getOptionName(actionName); const newShortcuts = newShortcut .replace("+,", "+Comma") .split(",") @@ -135,4 +140,8 @@ function ShortcutEditor({ keyboardShortcut: action }: { keyboardShortcut: Action }} /> ) +} + +function getOptionName(actionName: string) { + return `keyboardShortcuts${actionName.substr(0, 1).toUpperCase()}${actionName.substr(1)}` as OptionNames; } \ No newline at end of file