2020-11-14 02:02:17 +02:00
|
|
|
import { MenuItemLocation } from '../../plugin_types/services/plugins/api/types';
|
2020-10-09 19:35:46 +02:00
|
|
|
import CommandService from '../CommandService';
|
|
|
|
import KeymapService from '../KeymapService';
|
2020-11-14 02:02:17 +02:00
|
|
|
import { PluginStates, utils as pluginUtils } from '../plugins/reducer';
|
2020-10-09 19:35:46 +02:00
|
|
|
import propsHaveChanged from './propsHaveChanged';
|
|
|
|
const { createSelectorCreator, defaultMemoize } = require('reselect');
|
|
|
|
const { createCachedSelector } = require('re-reselect');
|
|
|
|
|
|
|
|
interface MenuItem {
|
2020-11-12 21:29:22 +02:00
|
|
|
id: string;
|
|
|
|
label: string;
|
|
|
|
click: Function;
|
|
|
|
role?: any;
|
|
|
|
accelerator?: string;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface MenuItems {
|
2020-11-12 21:29:22 +02:00
|
|
|
[key: string]: MenuItem;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface MenuItemProps {
|
2020-11-12 21:29:22 +02:00
|
|
|
[key: string]: any;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface MenuItemPropsCache {
|
2020-11-12 21:29:22 +02:00
|
|
|
[key: string]: any;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface MenuItemCache {
|
2020-11-12 21:29:22 +02:00
|
|
|
[key: string]: MenuItems;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const createShallowObjectEqualSelector = createSelectorCreator(
|
|
|
|
defaultMemoize,
|
2020-11-12 21:13:28 +02:00
|
|
|
(prev: any, next: any) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (Object.keys(prev).length !== Object.keys(next).length) return false;
|
|
|
|
for (const n in prev) {
|
|
|
|
if (prev[n] !== next[n]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// This selector ensures that for the given command names, the same toolbar
|
|
|
|
// button array is returned if the underlying toolbar buttons have not changed.
|
|
|
|
const selectObjectByCommands = createCachedSelector(
|
2020-11-12 21:13:28 +02:00
|
|
|
(state: any) => state.array,
|
|
|
|
(array: any[]) => array
|
2020-10-09 19:35:46 +02:00
|
|
|
)({
|
2020-11-12 21:13:28 +02:00
|
|
|
keySelector: (_state: any, commandNames: string[]) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
return commandNames.join('_');
|
|
|
|
},
|
|
|
|
selectorCreator: createShallowObjectEqualSelector,
|
|
|
|
});
|
|
|
|
|
|
|
|
export default class MenuUtils {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private service_: CommandService;
|
|
|
|
private menuItemCache_: MenuItemCache = {};
|
|
|
|
private menuItemPropsCache_: MenuItemPropsCache = {};
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
constructor(service: CommandService) {
|
2020-10-09 19:35:46 +02:00
|
|
|
this.service_ = service;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private get service(): CommandService {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.service_;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private get keymapService(): KeymapService {
|
2020-10-09 19:35:46 +02:00
|
|
|
return KeymapService.instance();
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public commandToMenuItem(commandName: string, onClick: Function): MenuItem {
|
2020-10-09 19:35:46 +02:00
|
|
|
const command = this.service.commandByName(commandName);
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const item: MenuItem = {
|
2020-10-09 19:35:46 +02:00
|
|
|
id: command.declaration.name,
|
|
|
|
label: this.service.label(commandName),
|
|
|
|
click: () => onClick(command.declaration.name),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (command.declaration.role) item.role = command.declaration.role;
|
|
|
|
|
|
|
|
if (this.keymapService && this.keymapService.acceleratorExists(commandName)) {
|
|
|
|
item.accelerator = this.keymapService.getAccelerator(commandName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public commandToStatefulMenuItem(commandName: string, ...args: any[]): MenuItem {
|
2020-10-11 20:37:12 +02:00
|
|
|
return this.commandToMenuItem(commandName, () => {
|
2020-10-18 22:52:10 +02:00
|
|
|
return this.service.execute(commandName, ...args);
|
2020-10-09 19:35:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public commandsToMenuItems(commandNames: string[], onClick: Function): MenuItems {
|
|
|
|
const key: string = `${this.keymapService.lastSaveTime}_${commandNames.join('_')}`;
|
2020-10-09 19:35:46 +02:00
|
|
|
if (this.menuItemCache_[key]) return this.menuItemCache_[key];
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const output: MenuItems = {};
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
for (const commandName of commandNames) {
|
|
|
|
output[commandName] = this.commandToMenuItem(commandName, onClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.menuItemCache_[key] = output;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public commandsToMenuItemProps(commandNames: string[], whenClauseContext: any): MenuItemProps {
|
|
|
|
const output: MenuItemProps = {};
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
for (const commandName of commandNames) {
|
2020-10-18 22:52:10 +02:00
|
|
|
const newProps = {
|
|
|
|
enabled: this.service.isEnabled(commandName, whenClauseContext),
|
|
|
|
};
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
if (newProps === null || propsHaveChanged(this.menuItemPropsCache_[commandName], newProps)) {
|
|
|
|
output[commandName] = newProps;
|
|
|
|
this.menuItemPropsCache_[commandName] = newProps;
|
|
|
|
} else {
|
|
|
|
output[commandName] = this.menuItemPropsCache_[commandName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selectObjectByCommands({ array: output }, commandNames);
|
|
|
|
}
|
|
|
|
|
2020-11-14 02:02:17 +02:00
|
|
|
public pluginContextMenuItems(plugins: PluginStates, location: MenuItemLocation): MenuItem[] {
|
|
|
|
const output: MenuItem[] = [];
|
|
|
|
const pluginViewInfos = pluginUtils.viewInfosByType(plugins, 'menuItem');
|
|
|
|
|
|
|
|
for (const info of pluginViewInfos) {
|
|
|
|
if (info.view.location !== location) continue;
|
|
|
|
output.push(this.commandToStatefulMenuItem(info.view.commandName));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output.length) output.splice(0, 0, { type: 'separator' } as any);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|