hyper/lib/command-registry.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-07-22 05:57:32 -08:00
import {ipcRenderer} from './utils/ipc';
2023-07-25 01:39:51 -08:00
import type {HyperDispatch} from '../typings/hyper';
import {closeSearch} from './actions/sessions';
let commands: Record<string, (event: any, dispatch: HyperDispatch) => void> = {
'editor:search-close': (e, dispatch) => {
dispatch(closeSearch(undefined, e));
window.focusActiveTerm();
}
};
2023-07-22 05:57:32 -08:00
export const getRegisteredKeys = async () => {
const keymaps = await ipcRenderer.invoke('getDecoratedKeymaps');
return Object.keys(keymaps).reduce((result: Record<string, string>, actionName) => {
const commandKeys = keymaps[actionName];
2020-06-19 04:51:34 -08:00
commandKeys.forEach((shortcut) => {
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);