2019-11-22 04:12:07 +08:00
|
|
|
import server from "./server.js";
|
|
|
|
import utils from "./utils.js";
|
2020-01-20 05:05:45 +08:00
|
|
|
import appContext from "./app_context.js";
|
2019-11-22 04:12:07 +08:00
|
|
|
|
|
|
|
const keyboardActionRepo = {};
|
|
|
|
|
|
|
|
const keyboardActionsLoaded = server.get('keyboard-actions').then(actions => {
|
2020-02-16 05:12:05 +08:00
|
|
|
actions = actions.filter(a => !!a.actionName); // filter out separators
|
|
|
|
|
2019-11-22 04:12:07 +08:00
|
|
|
for (const action of actions) {
|
2020-02-16 05:12:05 +08:00
|
|
|
action.effectiveShortcuts = action.effectiveShortcuts.filter(shortcut => !shortcut.startsWith("global:"));
|
|
|
|
|
2019-11-25 04:40:50 +08:00
|
|
|
keyboardActionRepo[action.actionName] = action;
|
2020-02-16 05:12:05 +08:00
|
|
|
}
|
2020-01-20 05:05:45 +08:00
|
|
|
|
2020-02-16 05:12:05 +08:00
|
|
|
return actions;
|
|
|
|
});
|
2020-01-20 05:05:45 +08:00
|
|
|
|
2020-02-16 05:12:05 +08: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) {
|
|
|
|
utils.bindElShortcut($el, shortcut, () => component.triggerCommand(action.actionName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getActionsForScope("window").then(actions => {
|
|
|
|
for (const action of actions) {
|
2020-02-17 01:11:32 +08:00
|
|
|
for (const shortcut of action.effectiveShortcuts) {
|
2020-02-16 18:22:37 +08:00
|
|
|
utils.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName));
|
2020-01-20 05:05:45 +08:00
|
|
|
}
|
2019-11-22 04:12:07 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-25 01:32:18 +08:00
|
|
|
server.get('keyboard-shortcuts-for-notes').then(shortcutForNotes => {
|
|
|
|
for (const shortcut in shortcutForNotes) {
|
|
|
|
utils.bindGlobalShortcut(shortcut, async () => {
|
2020-02-08 04:08:55 +08:00
|
|
|
appContext.tabManager.getActiveTabContext().setNote(shortcutForNotes[shortcut]);
|
2019-11-25 01:32:18 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-24 17:40:18 +08:00
|
|
|
function setElementActionHandler($el, actionName, handler) {
|
|
|
|
keyboardActionsLoaded.then(() => {
|
|
|
|
const action = keyboardActionRepo[actionName];
|
|
|
|
|
|
|
|
if (!action) {
|
|
|
|
throw new Error(`Cannot find keyboard action '${actionName}'`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// not setting action.handler since this is not global
|
|
|
|
|
|
|
|
for (const shortcut of action.effectiveShortcuts) {
|
|
|
|
if (shortcut) {
|
|
|
|
utils.bindElShortcut($el, shortcut, handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-22 04:12:07 +08:00
|
|
|
async function triggerAction(actionName) {
|
2019-11-25 04:40:50 +08:00
|
|
|
const action = await getAction(actionName);
|
2019-11-22 05:24:07 +08:00
|
|
|
|
|
|
|
if (!action.handler) {
|
|
|
|
throw new Error(`Action ${actionName} has no handler`);
|
|
|
|
}
|
|
|
|
|
|
|
|
await action.handler();
|
|
|
|
}
|
|
|
|
|
2019-11-23 05:35:59 +08:00
|
|
|
async function getAction(actionName, silent = false) {
|
2019-11-22 04:12:07 +08:00
|
|
|
await keyboardActionsLoaded;
|
|
|
|
|
|
|
|
const action = keyboardActionRepo[actionName];
|
|
|
|
|
|
|
|
if (!action) {
|
2019-11-23 05:35:59 +08:00
|
|
|
if (silent) {
|
|
|
|
console.log(`Cannot find action ${actionName}`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`Cannot find action ${actionName}`);
|
|
|
|
}
|
2019-11-22 04:12:07 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 05:24:07 +08:00
|
|
|
return action;
|
2019-11-22 04:12:07 +08:00
|
|
|
}
|
|
|
|
|
2019-11-24 17:14:30 +08:00
|
|
|
function updateDisplayedShortcuts($container) {
|
2020-02-17 02:54:11 +08:00
|
|
|
$container.find('kbd[data-kb-command]').each(async (i, el) => {
|
|
|
|
const actionName = $(el).attr('data-kb-command');
|
2019-11-23 05:35:59 +08:00
|
|
|
const action = await getAction(actionName, true);
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
$(el).text(action.effectiveShortcuts.join(', '));
|
|
|
|
}
|
|
|
|
});
|
2019-11-24 17:14:30 +08:00
|
|
|
|
2020-02-17 02:54:11 +08:00
|
|
|
$container.find('button[data-kb-command],a.icon-action[data-kb-command],.kb-in-title').each(async (i, el) => {
|
|
|
|
const actionName = $(el).attr('data-kb-command');
|
2019-11-24 17:14:30 +08:00
|
|
|
const action = await getAction(actionName, true);
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
const title = $(el).attr('title');
|
|
|
|
const shortcuts = action.effectiveShortcuts.join(', ');
|
|
|
|
const newTitle = !title || !title.trim() ? shortcuts : `${title} (${shortcuts})`;
|
|
|
|
|
|
|
|
$(el).attr('title', newTitle);
|
|
|
|
}
|
|
|
|
});
|
2019-11-23 05:35:59 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 04:12:07 +08:00
|
|
|
export default {
|
2019-11-24 17:40:18 +08:00
|
|
|
setElementActionHandler,
|
2019-11-22 05:24:07 +08:00
|
|
|
triggerAction,
|
2019-11-23 05:35:59 +08:00
|
|
|
getAction,
|
2020-02-16 05:12:05 +08:00
|
|
|
updateDisplayedShortcuts,
|
2020-02-17 05:14:28 +08:00
|
|
|
setupActionsForElement,
|
|
|
|
getActionsForScope
|
2019-11-22 04:12:07 +08:00
|
|
|
};
|