hyper/app/ui/contextmenu.ts

43 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-25 09:30:19 -08:00
import type {MenuItemConstructorOptions, BrowserWindow} from 'electron';
import {execCommand} from '../commands';
import {getProfiles} from '../config';
2019-11-28 05:17:01 -09:00
import editMenu from '../menus/menus/edit';
import shellMenu from '../menus/menus/shell';
import {getDecoratedKeymaps} from '../plugins';
2023-07-25 09:30:19 -08:00
const separator: MenuItemConstructorOptions = {type: 'separator'};
2017-11-03 12:24:41 -08:00
const getCommandKeys = (keymaps: Record<string, string[]>): Record<string, string> =>
Object.keys(keymaps).reduce((commandKeys: Record<string, string>, command) => {
return Object.assign(commandKeys, {
[command]: keymaps[command][0]
});
}, {});
2017-11-03 12:50:00 -08:00
2017-11-03 12:24:41 -08:00
// only display cut/copy when there's a cursor selection
const filterCutCopy = (selection: string, menuItem: MenuItemConstructorOptions) => {
if (/^cut$|^copy$/.test(menuItem.role!) && !selection) {
2017-11-03 12:24:41 -08:00
return;
}
return menuItem;
};
2023-07-25 08:11:02 -08:00
const contextMenuTemplate = (
createWindow: (fn?: (win: BrowserWindow) => void, options?: Record<string, any>) => BrowserWindow,
selection: string
) => {
const commandKeys = getCommandKeys(getDecoratedKeymaps());
const _shell = shellMenu(
commandKeys,
(command, focusedWindow) => execCommand(command, focusedWindow as BrowserWindow | undefined),
getProfiles().map((p) => p.name)
).submenu as MenuItemConstructorOptions[];
2017-11-03 12:50:00 -08:00
const _edit = editMenu(commandKeys, execCommand).submenu.filter(filterCutCopy.bind(null, selection));
return _edit
.concat(separator, _shell)
2020-03-25 02:15:08 -08:00
.filter((menuItem) => !Object.prototype.hasOwnProperty.call(menuItem, 'enabled') || menuItem.enabled);
2017-11-03 12:24:41 -08:00
};
2023-07-25 08:11:02 -08:00
export default contextMenuTemplate;