2019-11-28 05:17:01 -09:00
|
|
|
import editMenu from '../menus/menus/edit';
|
|
|
|
|
import shellMenu from '../menus/menus/shell';
|
|
|
|
|
import {execCommand} from '../commands';
|
|
|
|
|
import {getDecoratedKeymaps} from '../plugins';
|
2019-12-24 06:56:23 -09:00
|
|
|
import {MenuItemConstructorOptions, BrowserWindow} from 'electron';
|
|
|
|
|
const separator: MenuItemConstructorOptions = {type: 'separator'};
|
2017-11-03 12:24:41 -08:00
|
|
|
|
2019-12-24 06:56:23 -09:00
|
|
|
const getCommandKeys = (keymaps: Record<string, string[]>): Record<string, string> =>
|
|
|
|
|
Object.keys(keymaps).reduce((commandKeys: Record<string, string>, command) => {
|
2017-11-14 14:55:21 -09:00
|
|
|
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
|
2019-12-24 06:56:23 -09:00
|
|
|
const filterCutCopy = (selection: string, menuItem: MenuItemConstructorOptions) => {
|
|
|
|
|
if (/^cut$|^copy$/.test(menuItem.role!) && !selection) {
|
2017-11-03 12:24:41 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return menuItem;
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-24 06:56:23 -09:00
|
|
|
export default (
|
|
|
|
|
createWindow: (fn?: (win: BrowserWindow) => void, options?: Record<string, any>) => BrowserWindow,
|
|
|
|
|
selection: string
|
|
|
|
|
) => {
|
2017-11-14 14:55:21 -09:00
|
|
|
const commandKeys = getCommandKeys(getDecoratedKeymaps());
|
2019-12-24 06:56:23 -09:00
|
|
|
const _shell = shellMenu(commandKeys, execCommand).submenu as MenuItemConstructorOptions[];
|
2017-11-03 12:50:00 -08:00
|
|
|
const _edit = editMenu(commandKeys, execCommand).submenu.filter(filterCutCopy.bind(null, selection));
|
2019-10-02 16:56:50 -08:00
|
|
|
return _edit
|
|
|
|
|
.concat(separator, _shell)
|
|
|
|
|
.filter(menuItem => !Object.prototype.hasOwnProperty.call(menuItem, 'enabled') || menuItem.enabled);
|
2017-11-03 12:24:41 -08:00
|
|
|
};
|