forked from vercel/hyper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-registry.ts
53 lines (44 loc) · 1.45 KB
/
command-registry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {remote} from 'electron';
import {HyperDispatch} from './hyper';
import {closeSearch} from './actions/sessions';
// TODO: Should be updates to new async API https://medium.com/@nornagon/electrons-remote-module-considered-harmful-70d69500f31
const {getDecoratedKeymaps} = remote.require('./plugins');
let commands: Record<string, (event: any, dispatch: HyperDispatch) => void> = {
'editor:search-close': (e, dispatch) => {
dispatch(closeSearch(undefined, e));
}
};
export const getRegisteredKeys = () => {
const keymaps = getDecoratedKeymaps();
return Object.keys(keymaps).reduce((result: Record<string, string>, actionName) => {
const commandKeys = keymaps[actionName];
commandKeys.forEach((shortcut: string) => {
result[shortcut] = actionName;
});
return result;
}, {});
};
export const registerCommandHandlers = (cmds: typeof commands) => {
if (!cmds) {
return;
}
commands = Object.assign(commands, cmds);
};
export const getCommandHandler = (command: string) => {
return commands[command];
};
// Some commands are directly excuted by Electron menuItem role.
// 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: string) => !roleCommands.includes(command);