2019-11-21 21:12:07 +01:00
|
|
|
import server from "./server.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../components/app_context.js";
|
2022-12-01 00:17:15 +01:00
|
|
|
import shortcutService from "./shortcuts.js";
|
2019-11-21 21:12:07 +01:00
|
|
|
|
|
|
|
const keyboardActionRepo = {};
|
|
|
|
|
|
|
|
const keyboardActionsLoaded = server.get('keyboard-actions').then(actions => {
|
2020-02-15 22:12:05 +01:00
|
|
|
actions = actions.filter(a => !!a.actionName); // filter out separators
|
|
|
|
|
2019-11-21 21:12:07 +01:00
|
|
|
for (const action of actions) {
|
2020-02-15 22:12:05 +01:00
|
|
|
action.effectiveShortcuts = action.effectiveShortcuts.filter(shortcut => !shortcut.startsWith("global:"));
|
|
|
|
|
2019-11-24 21:40:50 +01:00
|
|
|
keyboardActionRepo[action.actionName] = action;
|
2020-02-15 22:12:05 +01:00
|
|
|
}
|
2020-01-19 22:05:45 +01:00
|
|
|
|
2020-02-15 22:12:05 +01:00
|
|
|
return actions;
|
|
|
|
});
|
2020-01-19 22:05:45 +01:00
|
|
|
|
2021-07-05 09:44:41 +02:00
|
|
|
async function getActions() {
|
|
|
|
return await keyboardActionsLoaded;
|
|
|
|
}
|
|
|
|
|
2020-02-15 22:12:05 +01:00
|
|
|
async function getActionsForScope(scope) {
|
|
|
|
const actions = await keyboardActionsLoaded;
|
|
|
|
|
|
|
|
return actions.filter(action => action.scope === scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setupActionsForElement(scope, $el, component) {
|
|
|
|
const actions = await getActionsForScope(scope);
|
|
|
|
|
|
|
|
for (const action of actions) {
|
|
|
|
for (const shortcut of action.effectiveShortcuts) {
|
2022-12-01 00:17:15 +01:00
|
|
|
shortcutService.bindElShortcut($el, shortcut, () => component.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeNtxId}));
|
2020-02-15 22:12:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getActionsForScope("window").then(actions => {
|
|
|
|
for (const action of actions) {
|
2020-02-16 18:11:32 +01:00
|
|
|
for (const shortcut of action.effectiveShortcuts) {
|
2022-12-01 00:17:15 +01:00
|
|
|
shortcutService.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeNtxId}));
|
2020-01-19 22:05:45 +01:00
|
|
|
}
|
2019-11-21 21:12:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-22 22:35:59 +01:00
|
|
|
async function getAction(actionName, silent = false) {
|
2019-11-21 21:12:07 +01:00
|
|
|
await keyboardActionsLoaded;
|
|
|
|
|
|
|
|
const action = keyboardActionRepo[actionName];
|
|
|
|
|
|
|
|
if (!action) {
|
2019-11-22 22:35:59 +01:00
|
|
|
if (silent) {
|
|
|
|
console.log(`Cannot find action ${actionName}`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`Cannot find action ${actionName}`);
|
|
|
|
}
|
2019-11-21 21:12:07 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:24:07 +01:00
|
|
|
return action;
|
2019-11-21 21:12:07 +01:00
|
|
|
}
|
|
|
|
|
2019-11-24 10:14:30 +01:00
|
|
|
function updateDisplayedShortcuts($container) {
|
2020-02-27 23:12:22 +01:00
|
|
|
$container.find('kbd[data-command]').each(async (i, el) => {
|
|
|
|
const actionName = $(el).attr('data-command');
|
2019-11-22 22:35:59 +01:00
|
|
|
const action = await getAction(actionName, true);
|
|
|
|
|
|
|
|
if (action) {
|
2021-04-15 21:45:37 +02:00
|
|
|
const keyboardActions = action.effectiveShortcuts.join(', ');
|
|
|
|
|
|
|
|
if (keyboardActions || $(el).text() !== "not set") {
|
|
|
|
$(el).text(keyboardActions);
|
|
|
|
}
|
2019-11-22 22:35:59 +01:00
|
|
|
}
|
|
|
|
});
|
2019-11-24 10:14:30 +01:00
|
|
|
|
2020-05-05 23:58:52 +02:00
|
|
|
$container.find('[data-trigger-command]').each(async (i, el) => {
|
|
|
|
const actionName = $(el).attr('data-trigger-command');
|
2019-11-24 10:14:30 +01:00
|
|
|
const action = await getAction(actionName, true);
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
const title = $(el).attr('title');
|
|
|
|
const shortcuts = action.effectiveShortcuts.join(', ');
|
2022-12-20 23:46:44 +01:00
|
|
|
|
|
|
|
if (title?.includes(shortcuts)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newTitle = !title?.trim() ? shortcuts : `${title} (${shortcuts})`;
|
2019-11-24 10:14:30 +01:00
|
|
|
|
|
|
|
$(el).attr('title', newTitle);
|
|
|
|
}
|
|
|
|
});
|
2019-11-22 22:35:59 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:12:07 +01:00
|
|
|
export default {
|
2020-02-15 22:12:05 +01:00
|
|
|
updateDisplayedShortcuts,
|
2020-02-16 22:14:28 +01:00
|
|
|
setupActionsForElement,
|
2021-07-05 09:44:41 +02:00
|
|
|
getActions,
|
2022-12-01 13:24:34 +01:00
|
|
|
getActionsForScope
|
2020-09-01 23:18:28 +02:00
|
|
|
};
|