2017-11-02 18:51:18 -08:00
|
|
|
import {remote} from 'electron';
|
2017-06-02 16:03:47 -08:00
|
|
|
|
2017-11-02 18:51:18 -08:00
|
|
|
const {getDecoratedKeymaps} = remote.require('./plugins');
|
|
|
|
|
|
|
|
|
|
let commands = {};
|
|
|
|
|
|
|
|
|
|
export const getRegisteredKeys = () => {
|
|
|
|
|
const keymaps = getDecoratedKeymaps();
|
2017-06-02 16:03:47 -08:00
|
|
|
|
2017-11-02 18:51:18 -08:00
|
|
|
return Object.keys(keymaps).reduce((result, actionName) => {
|
|
|
|
|
const commandKeys = keymaps[actionName];
|
|
|
|
|
commandKeys.forEach(shortcut => {
|
|
|
|
|
result[shortcut] = actionName;
|
|
|
|
|
});
|
|
|
|
|
return result;
|
|
|
|
|
}, {});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const registerCommandHandlers = cmds => {
|
|
|
|
|
if (!cmds) {
|
|
|
|
|
return;
|
2017-06-02 16:03:47 -08:00
|
|
|
}
|
|
|
|
|
|
2017-11-02 18:51:18 -08:00
|
|
|
commands = Object.assign(commands, cmds);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getCommandHandler = command => {
|
|
|
|
|
return commands[command];
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-18 07:17:20 -09:00
|
|
|
// Some commands are directly excuted by Electron menuItem role.
|
2017-11-02 18:51:18 -08:00
|
|
|
// They should not be prevented to reach Electron.
|
|
|
|
|
const roleCommands = [
|
|
|
|
|
'window:close',
|
|
|
|
|
'editor:undo',
|
|
|
|
|
'editor:redo',
|
|
|
|
|
'editor:cut',
|
|
|
|
|
'editor:copy',
|
|
|
|
|
'editor:paste',
|
|
|
|
|
'editor:selectAll',
|
|
|
|
|
'window:minimize',
|
|
|
|
|
'window:zoom',
|
|
|
|
|
'window:toggleFullScreen'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export const shouldPreventDefault = command => !roleCommands.includes(command);
|