mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 19:36:12 +01:00
chore(react/settings): reimplement reset shortcuts
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { OptionNames } from "@triliumnext/commons";
|
||||||
import server from "./server.js";
|
import server from "./server.js";
|
||||||
import { isShare } from "./utils.js";
|
import { isShare } from "./utils.js";
|
||||||
|
|
||||||
@@ -76,6 +77,10 @@ class Options {
|
|||||||
await server.put(`options`, payload);
|
await server.put(`options`, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async saveMany<T extends OptionNames>(newValues: Record<T, OptionValue>) {
|
||||||
|
await server.put<void>("options", newValues);
|
||||||
|
}
|
||||||
|
|
||||||
async toggle(key: string) {
|
async toggle(key: string) {
|
||||||
await this.save(key, (!this.is(key)).toString());
|
await this.save(key, (!this.is(key)).toString());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -750,6 +750,23 @@ export function toggleBodyClass(prefix: string, value: string) {
|
|||||||
$body.addClass(prefix + value);
|
$body.addClass(prefix + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function arrayEqual<T>(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 {
|
export default {
|
||||||
reloadFrontendApp,
|
reloadFrontendApp,
|
||||||
restartDesktopApp,
|
restartDesktopApp,
|
||||||
|
|||||||
@@ -167,10 +167,9 @@ export function useTriliumOptions<T extends OptionNames>(...names: T[]) {
|
|||||||
values[name] = options.get(name);
|
values[name] = options.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
const setValue = (newValues: Record<T, string>) => server.put<void>("options", newValues);
|
|
||||||
return [
|
return [
|
||||||
values as Record<T, string>,
|
values as Record<T, string>,
|
||||||
setValue
|
options.saveMany
|
||||||
] as const;
|
] as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { ActionKeyboardShortcut, KeyboardShortcut } from "@triliumnext/commons";
|
import { ActionKeyboardShortcut, KeyboardShortcut, OptionNames } from "@triliumnext/commons";
|
||||||
import { t } from "../../../services/i18n";
|
import { t } from "../../../services/i18n";
|
||||||
import { reloadFrontendApp } from "../../../services/utils";
|
import { arrayEqual, reloadFrontendApp } from "../../../services/utils";
|
||||||
import Button from "../../react/Button";
|
import Button from "../../react/Button";
|
||||||
import FormGroup from "../../react/FormGroup";
|
import FormGroup from "../../react/FormGroup";
|
||||||
import FormText from "../../react/FormText";
|
import FormText from "../../react/FormText";
|
||||||
@@ -11,6 +11,7 @@ import { useCallback, useEffect, useState } from "preact/hooks";
|
|||||||
import server from "../../../services/server";
|
import server from "../../../services/server";
|
||||||
import options from "../../../services/options";
|
import options from "../../../services/options";
|
||||||
import dialog from "../../../services/dialog";
|
import dialog from "../../../services/dialog";
|
||||||
|
import { useTriliumOptions } from "../../react/hooks";
|
||||||
|
|
||||||
export default function ShortcutSettings() {
|
export default function ShortcutSettings() {
|
||||||
const [ keyboardShortcuts, setKeyboardShortcuts ] = useState<KeyboardShortcut[]>([]);
|
const [ keyboardShortcuts, setKeyboardShortcuts ] = useState<KeyboardShortcut[]>([]);
|
||||||
@@ -25,13 +26,17 @@ export default function ShortcutSettings() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newKeyboardShortcuts = [];
|
const optionsToSet: Record<string, string> = {};
|
||||||
for (const keyboardShortcut of keyboardShortcuts) {
|
for (const keyboardShortcut of keyboardShortcuts) {
|
||||||
if (!("effectiveShortcuts" in keyboardShortcut)) {
|
if (!("effectiveShortcuts" in keyboardShortcut)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!arrayEqual(keyboardShortcut.effectiveShortcuts, keyboardShortcut.defaultShortcuts)) {
|
||||||
|
optionsToSet[getOptionName(keyboardShortcut.actionName)] = JSON.stringify(keyboardShortcut.defaultShortcuts);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
options.saveMany(optionsToSet);
|
||||||
}, [ keyboardShortcuts ]);
|
}, [ keyboardShortcuts ]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -125,7 +130,7 @@ function ShortcutEditor({ keyboardShortcut: action }: { keyboardShortcut: Action
|
|||||||
<FormTextBox
|
<FormTextBox
|
||||||
currentValue={originalShortcut} onChange={(newShortcut) => {
|
currentValue={originalShortcut} onChange={(newShortcut) => {
|
||||||
const { actionName } = action;
|
const { actionName } = action;
|
||||||
const optionName = `keyboardShortcuts${actionName.substr(0, 1).toUpperCase()}${actionName.substr(1)}`;
|
const optionName = getOptionName(actionName);
|
||||||
const newShortcuts = newShortcut
|
const newShortcuts = newShortcut
|
||||||
.replace("+,", "+Comma")
|
.replace("+,", "+Comma")
|
||||||
.split(",")
|
.split(",")
|
||||||
@@ -136,3 +141,7 @@ function ShortcutEditor({ keyboardShortcut: action }: { keyboardShortcut: Action
|
|||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getOptionName(actionName: string) {
|
||||||
|
return `keyboardShortcuts${actionName.substr(0, 1).toUpperCase()}${actionName.substr(1)}` as OptionNames;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user