2019-11-21 21:12:07 +01:00
|
|
|
import server from "./server.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import appContext, { type CommandNames } from "../components/app_context.js";
|
2022-12-01 00:17:15 +01:00
|
|
|
import shortcutService from "./shortcuts.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type Component from "../components/component.js";
|
2025-07-30 19:50:07 +03:00
|
|
|
import type { ActionKeyboardShortcut } from "@triliumnext/commons";
|
2019-11-21 21:12:07 +01:00
|
|
|
|
2025-07-30 19:50:07 +03:00
|
|
|
const keyboardActionRepo: Record<string, ActionKeyboardShortcut> = {};
|
2024-12-21 17:55:22 +02:00
|
|
|
|
2025-07-30 19:50:07 +03:00
|
|
|
const keyboardActionsLoaded = server.get<ActionKeyboardShortcut[]>("keyboard-actions").then((actions) => {
|
2025-01-09 18:07:02 +02:00
|
|
|
actions = actions.filter((a) => !!a.actionName); // filter out separators
|
2020-02-15 22:12:05 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
for (const action of actions) {
|
2025-07-30 19:50:07 +03:00
|
|
|
action.effectiveShortcuts = (action.effectiveShortcuts ?? []).filter((shortcut) => !shortcut.startsWith("global:"));
|
2020-02-15 22:12:05 +01:00
|
|
|
|
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-02-15 22:12:05 +01:00
|
|
|
});
|
2020-01-19 22:05:45 +01:00
|
|
|
|
2021-07-05 09:44:41 +02:00
|
|
|
async function getActions() {
|
2025-01-09 18:07:02 +02:00
|
|
|
return await keyboardActionsLoaded;
|
2021-07-05 09:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-21 17:55:22 +02:00
|
|
|
async function getActionsForScope(scope: string) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const actions = await keyboardActionsLoaded;
|
2020-02-15 22:12:05 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return actions.filter((action) => action.scope === scope);
|
2020-02-15 22:12:05 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-21 17:55:22 +02:00
|
|
|
async function setupActionsForElement(scope: string, $el: JQuery<HTMLElement>, component: Component) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const actions = await getActionsForScope(scope);
|
2020-02-15 22:12:05 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
for (const action of actions) {
|
2025-07-30 19:50:07 +03:00
|
|
|
for (const shortcut of action.effectiveShortcuts ?? []) {
|
2025-01-09 18:07:02 +02:00
|
|
|
shortcutService.bindElShortcut($el, shortcut, () => component.triggerCommand(action.actionName, { ntxId: appContext.tabManager.activeNtxId }));
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-15 22:12:05 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
getActionsForScope("window").then((actions) => {
|
|
|
|
|
for (const action of actions) {
|
2025-07-30 19:50:07 +03:00
|
|
|
for (const shortcut of action.effectiveShortcuts ?? []) {
|
2025-01-09 18:07:02 +02:00
|
|
|
shortcutService.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName, { ntxId: appContext.tabManager.activeNtxId }));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-21 21:12:07 +01:00
|
|
|
});
|
|
|
|
|
|
2024-12-21 17:55:22 +02:00
|
|
|
async function getAction(actionName: string, silent = false) {
|
2025-01-09 18:07:02 +02:00
|
|
|
await keyboardActionsLoaded;
|
2019-11-21 21:12:07 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const action = keyboardActionRepo[actionName];
|
2019-11-21 21:12:07 +01:00
|
|
|
|
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}'`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-21 21:12:07 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return action;
|
2019-11-21 21:12:07 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-29 00:47:47 +03:00
|
|
|
export function getActionSync(actionName: string) {
|
|
|
|
|
return keyboardActionRepo[actionName];
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 17:55:22 +02:00
|
|
|
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) {
|
2025-07-30 19:50:07 +03:00
|
|
|
const keyboardActions = (action.effectiveShortcuts ?? []).join(", ");
|
2025-01-09 18:07:02 +02:00
|
|
|
|
|
|
|
|
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");
|
2025-07-30 19:50:07 +03:00
|
|
|
const shortcuts = (action.effectiveShortcuts ?? []).join(", ");
|
2025-01-09 18:07:02 +02:00
|
|
|
|
|
|
|
|
if (title?.includes(shortcuts)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newTitle = !title?.trim() ? shortcuts : `${title} (${shortcuts})`;
|
|
|
|
|
|
|
|
|
|
$(el).attr("title", newTitle);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-11-22 22:35:59 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-21 21:12:07 +01:00
|
|
|
export default {
|
2025-01-09 18:07:02 +02:00
|
|
|
updateDisplayedShortcuts,
|
|
|
|
|
setupActionsForElement,
|
2025-06-01 15:27:50 +08:00
|
|
|
getAction,
|
2025-01-09 18:07:02 +02:00
|
|
|
getActions,
|
|
|
|
|
getActionsForScope
|
2020-09-01 23:18:28 +02:00
|
|
|
};
|