chore(launch_bar): reintroduce keyboard shortcuts

This commit is contained in:
Elian Doran
2025-12-05 19:23:40 +02:00
parent 17241be4bc
commit fdb6677153
5 changed files with 47 additions and 18 deletions

View File

@@ -20,9 +20,9 @@ import options, { type OptionValue } from "../../services/options";
import protected_session_holder from "../../services/protected_session_holder";
import SpacedUpdate from "../../services/spaced_update";
import toast, { ToastOptions } from "../../services/toast";
import utils, { escapeRegExp, reloadFrontendApp } from "../../services/utils";
import utils, { escapeRegExp, randomString, reloadFrontendApp } from "../../services/utils";
import server from "../../services/server";
import { removeIndividualBinding } from "../../services/shortcuts";
import shortcuts, { Handler, removeIndividualBinding } from "../../services/shortcuts";
import froca from "../../services/froca";
export function useTriliumEvent<T extends EventNames>(eventName: T, handler: (data: EventData<T>) => void) {
@@ -812,6 +812,21 @@ export function useKeyboardShortcuts(scope: "code-detail" | "text-detail", conta
}, [ scope, containerRef, parentComponent, ntxId ]);
}
/**
* Register a global shortcut. Internally it uses the shortcut service and assignes a random namespace to make it unique.
*
* @param keyboardShortcut the keyboard shortcut combination to register.
* @param handler the corresponding handler to be called when the keyboard shortcut is invoked by the user.
*/
export function useGlobalShortcut(keyboardShortcut: string | null | undefined, handler: Handler) {
useEffect(() => {
if (!keyboardShortcut) return;
const namespace = randomString(10);
shortcuts.bindGlobalShortcut(keyboardShortcut, handler, namespace);
return () => shortcuts.removeGlobalShortcut(namespace);
}, [ keyboardShortcut, handler ]);
}
/**
* Indicates that the current note is in read-only mode, while an editing mode is available,
* and provides a way to switch to editing mode.