2023-07-22 05:57:32 -08:00
|
|
|
import {ipcRenderer} from './utils/ipc';
|
2023-06-26 01:29:50 -08:00
|
|
|
import type {HyperDispatch} from './hyper';
|
2020-03-22 01:27:40 -08:00
|
|
|
import {closeSearch} from './actions/sessions';
|
2017-11-02 18:51:18 -08:00
|
|
|
|
2020-03-22 01:27:40 -08:00
|
|
|
let commands: Record<string, (event: any, dispatch: HyperDispatch) => void> = {
|
|
|
|
|
'editor:search-close': (e, dispatch) => {
|
|
|
|
|
dispatch(closeSearch(undefined, e));
|
2021-09-16 20:40:27 -08:00
|
|
|
window.focusActiveTerm();
|
2020-03-22 01:27:40 -08:00
|
|
|
}
|
|
|
|
|
};
|
2017-11-02 18:51:18 -08:00
|
|
|
|
2023-07-22 05:57:32 -08:00
|
|
|
export const getRegisteredKeys = async () => {
|
|
|
|
|
const keymaps = await ipcRenderer.invoke('getDecoratedKeymaps');
|
2017-06-02 16:03:47 -08:00
|
|
|
|
2019-11-11 06:21:42 -09:00
|
|
|
return Object.keys(keymaps).reduce((result: Record<string, string>, actionName) => {
|
2017-11-02 18:51:18 -08:00
|
|
|
const commandKeys = keymaps[actionName];
|
2020-06-19 04:51:34 -08:00
|
|
|
commandKeys.forEach((shortcut) => {
|
2017-11-02 18:51:18 -08:00
|
|
|
result[shortcut] = actionName;
|
|
|
|
|
});
|
|
|
|
|
return result;
|
|
|
|
|
}, {});
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-11 06:21:42 -09:00
|
|
|
export const registerCommandHandlers = (cmds: typeof commands) => {
|
2017-11-02 18:51:18 -08:00
|
|
|
if (!cmds) {
|
|
|
|
|
return;
|
2017-06-02 16:03:47 -08:00
|
|
|
}
|
|
|
|
|
|
2017-11-02 18:51:18 -08:00
|
|
|
commands = Object.assign(commands, cmds);
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-11 06:21:42 -09:00
|
|
|
export const getCommandHandler = (command: string) => {
|
2017-11-02 18:51:18 -08:00
|
|
|
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'
|
|
|
|
|
];
|
|
|
|
|
|
2019-11-11 06:21:42 -09:00
|
|
|
export const shouldPreventDefault = (command: string) => !roleCommands.includes(command);
|