Files
Trilium/src/public/app/services/keyboard_actions.ts

121 lines
3.5 KiB
TypeScript
Raw Normal View History

import server from "./server.js";
import appContext, { type CommandNames } from "../components/app_context.js";
import shortcutService from "./shortcuts.js";
import Component from "../components/component.js";
const keyboardActionRepo: Record<string, Action> = {};
// TODO: Deduplicate with server.
export interface Action {
2025-01-09 18:07:02 +02:00
actionName: CommandNames;
effectiveShortcuts: string[];
scope: string;
}
2025-01-09 18:07:02 +02:00
const keyboardActionsLoaded = server.get<Action[]>("keyboard-actions").then((actions) => {
actions = actions.filter((a) => !!a.actionName); // filter out separators
2025-01-09 18:07:02 +02:00
for (const action of actions) {
action.effectiveShortcuts = action.effectiveShortcuts.filter((shortcut) => !shortcut.startsWith("global:"));
2025-01-09 18:07:02 +02:00
keyboardActionRepo[action.actionName] = action;
}
2020-01-19 22:05:45 +01:00
2025-01-09 18:07:02 +02:00
return actions;
});
2020-01-19 22:05:45 +01:00
async function getActions() {
2025-01-09 18:07:02 +02:00
return await keyboardActionsLoaded;
}
async function getActionsForScope(scope: string) {
2025-01-09 18:07:02 +02:00
const actions = await keyboardActionsLoaded;
2025-01-09 18:07:02 +02:00
return actions.filter((action) => action.scope === scope);
}
async function setupActionsForElement(scope: string, $el: JQuery<HTMLElement>, component: Component) {
2025-01-09 18:07:02 +02:00
const actions = await getActionsForScope(scope);
2025-01-09 18:07:02 +02:00
for (const action of actions) {
for (const shortcut of action.effectiveShortcuts) {
shortcutService.bindElShortcut($el, shortcut, () => component.triggerCommand(action.actionName, { ntxId: appContext.tabManager.activeNtxId }));
}
}
}
2025-01-09 18:07:02 +02:00
getActionsForScope("window").then((actions) => {
for (const action of actions) {
for (const shortcut of action.effectiveShortcuts) {
shortcutService.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName, { ntxId: appContext.tabManager.activeNtxId }));
}
}
});
async function getAction(actionName: string, silent = false) {
2025-01-09 18:07:02 +02:00
await keyboardActionsLoaded;
2025-01-09 18:07:02 +02:00
const action = keyboardActionRepo[actionName];
2025-01-09 18:07:02 +02:00
if (!action) {
if (silent) {
console.debug(`Cannot find action '${actionName}'`);
} else {
throw new Error(`Cannot find action '${actionName}'`);
}
}
2025-01-09 18:07:02 +02:00
return action;
}
function updateDisplayedShortcuts($container: JQuery<HTMLElement>) {
2025-01-09 18:07:02 +02:00
//@ts-ignore
//TODO: each() does not support async callbacks.
$container.find("kbd[data-command]").each(async (i, el) => {
const actionName = $(el).attr("data-command");
if (!actionName) {
return;
}
const action = await getAction(actionName, true);
if (action) {
const keyboardActions = action.effectiveShortcuts.join(", ");
if (keyboardActions || $(el).text() !== "not set") {
$(el).text(keyboardActions);
}
}
});
//@ts-ignore
//TODO: each() does not support async callbacks.
$container.find("[data-trigger-command]").each(async (i, el) => {
const actionName = $(el).attr("data-trigger-command");
if (!actionName) {
return;
}
const action = await getAction(actionName, true);
if (action) {
const title = $(el).attr("title");
const shortcuts = action.effectiveShortcuts.join(", ");
if (title?.includes(shortcuts)) {
return;
}
const newTitle = !title?.trim() ? shortcuts : `${title} (${shortcuts})`;
$(el).attr("title", newTitle);
}
});
}
export default {
2025-01-09 18:07:02 +02:00
updateDisplayedShortcuts,
setupActionsForElement,
getActions,
getActionsForScope
};