2019-11-22 04:12:07 +08:00
|
|
|
import server from "./server.js";
|
|
|
|
import utils from "./utils.js";
|
|
|
|
|
|
|
|
class KeyboardAction {
|
|
|
|
constructor(params) {
|
|
|
|
/** @property {string} */
|
|
|
|
this.actionName = params.actionName;
|
|
|
|
/** @property {string[]} */
|
|
|
|
this.defaultShortcuts = params.defaultShortcuts;
|
|
|
|
/** @property {string[]} */
|
|
|
|
this.effectiveShortcuts = params.effectiveShortcuts;
|
|
|
|
/** @property {string} */
|
|
|
|
this.description = params.description;
|
|
|
|
}
|
|
|
|
|
|
|
|
addShortcut(shortcut) {
|
|
|
|
this.effectiveShortcuts.push(shortcut);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string|string[]} shortcuts
|
|
|
|
*/
|
|
|
|
replaceShortcuts(shortcuts) {
|
|
|
|
this.effectiveShortcuts = Array.isArray(shortcuts) ? shortcuts : [shortcuts];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyboardActionRepo = {};
|
|
|
|
|
|
|
|
const keyboardActionsLoaded = server.get('keyboard-actions').then(actions => {
|
|
|
|
for (const action of actions) {
|
|
|
|
keyboardActionRepo[action.actionName] = new KeyboardAction(action);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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 () => {
|
|
|
|
const treeService = (await import("./tree.js")).default;
|
|
|
|
|
|
|
|
treeService.activateNote(shortcutForNotes[shortcut]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-24 17:40:18 +08:00
|
|
|
function setGlobalActionHandler(actionName, handler) {
|
2019-11-22 04:12:07 +08:00
|
|
|
keyboardActionsLoaded.then(() => {
|
|
|
|
const action = keyboardActionRepo[actionName];
|
|
|
|
|
|
|
|
if (!action) {
|
|
|
|
throw new Error(`Cannot find keyboard action '${actionName}'`);
|
|
|
|
}
|
|
|
|
|
|
|
|
action.handler = handler;
|
|
|
|
|
|
|
|
for (const shortcut of action.effectiveShortcuts) {
|
|
|
|
if (shortcut) {
|
|
|
|
utils.bindGlobalShortcut(shortcut, handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-22 05:24:07 +08:00
|
|
|
const action = getAction(actionName);
|
|
|
|
|
|
|
|
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) {
|
2019-11-23 05:35:59 +08:00
|
|
|
$container.find('kbd[data-kb-action]').each(async (i, el) => {
|
|
|
|
const actionName = $(el).attr('data-kb-action');
|
|
|
|
const action = await getAction(actionName, true);
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
$(el).text(action.effectiveShortcuts.join(', '));
|
|
|
|
}
|
|
|
|
});
|
2019-11-24 17:14:30 +08:00
|
|
|
|
|
|
|
$container.find('button[data-kb-action],a.icon-action[data-kb-action]').each(async (i, el) => {
|
|
|
|
const actionName = $(el).attr('data-kb-action');
|
|
|
|
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-24 17:14:30 +08:00
|
|
|
$(() => updateDisplayedShortcuts($(document)));
|
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
|
|
|
setGlobalActionHandler,
|
|
|
|
setElementActionHandler,
|
2019-11-22 05:24:07 +08:00
|
|
|
triggerAction,
|
2019-11-23 05:35:59 +08:00
|
|
|
getAction,
|
2019-11-24 17:14:30 +08:00
|
|
|
updateDisplayedShortcuts
|
2019-11-22 04:12:07 +08:00
|
|
|
};
|