hyper/app/utils/map-keys.js
CHaBou 2af575c3c0 Multiple keymaps and mousetrap (#2412)
* WIP

* WIP

* Wip

* Wip

* wip

* Refactor without normalize and plugin

* Replace extendKeymaps by decorateKeymaps

* WIP

* Add mousetrap

* Add first command over rpc

* More commands

* Add all commands

* Begin to hook commands

* Working multiple keymaps

* Use redux action to trigger command

* Use forked version of Mousetrap to capture key events

* Fix lint

* Add command in redux action to debug purpose

* ExecCommand from menu click

* Remove unused files

* Fix xterm should ignore catched events

* Re-enable IntelliSense checking

* Remove unused runes dep
2017-11-02 19:51:18 -07:00

41 lines
1.3 KiB
JavaScript

const generatePrefixedCommand = (command, shortcuts) => {
const result = {};
const baseCmd = command.replace(/:prefix$/, '');
for (let i = 1; i <= 9; i++) {
// 9 is a special number because it means 'last'
const index = i === 9 ? 'last' : i;
const prefixedShortcuts = shortcuts.map(shortcut => `${shortcut}+${i}`);
result[`${baseCmd}:${index}`] = prefixedShortcuts;
}
return result;
};
module.exports = config => {
return Object.keys(config).reduce((keymap, command) => {
if (!command) {
return;
}
// We can have different keys for a same command.
const shortcuts = Array.isArray(config[command]) ? config[command] : [config[command]];
const fixedShortcuts = [];
shortcuts.forEach(shortcut => {
let newShortcut = shortcut;
if (newShortcut.indexOf('cmd') !== -1) {
// Mousetrap use `command` and not `cmd`
//eslint-disable-next-line no-console
console.warn('Your config use deprecated `cmd` in key combination. Please use `command` instead.');
newShortcut = newShortcut.replace('cmd', 'command');
}
fixedShortcuts.push(newShortcut);
});
if (command.endsWith(':prefix')) {
return Object.assign(keymap, generatePrefixedCommand(command, fixedShortcuts));
}
keymap[command] = fixedShortcuts;
return keymap;
}, {});
};