2020-11-14 02:31:45 +02:00
|
|
|
import { MenuItemLocation } from '../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');
|
|
|
|
|
2022-04-11 17:49:32 +02:00
|
|
|
export interface MenuItem {
|
|
|
|
id?: string;
|
|
|
|
label?: string;
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2024-04-15 19:15:18 +02:00
|
|
|
click?: ()=> void;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
role?: any;
|
2024-04-15 19:15:18 +02:00
|
|
|
type?: 'normal'|'separator'|'submenu';
|
2020-11-12 21:29:22 +02:00
|
|
|
accelerator?: string;
|
2022-04-11 17:49:32 +02:00
|
|
|
checked?: boolean;
|
|
|
|
enabled?: boolean;
|
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 {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
[key: string]: any;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface MenuItemPropsCache {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
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,
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
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;
|
2023-08-22 12:58:53 +02:00
|
|
|
},
|
2020-10-09 19:35:46 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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(
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
(state: any) => state.array,
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-08-22 12:58:53 +02:00
|
|
|
(array: any[]) => array,
|
2020-10-09 19:35:46 +02:00
|
|
|
)({
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
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
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public 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();
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
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),
|
2021-09-09 15:44:16 +02:00
|
|
|
enabled: true,
|
2020-10-09 19:35:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (command.declaration.role) item.role = command.declaration.role;
|
|
|
|
|
|
|
|
if (this.keymapService && this.keymapService.acceleratorExists(commandName)) {
|
|
|
|
item.accelerator = this.keymapService.getAccelerator(commandName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
public commandToStatefulMenuItem(commandName: string, ...args: any[]): MenuItem {
|
2023-07-16 18:42:42 +02:00
|
|
|
const whenClauseContext = this.service.currentWhenClauseContext();
|
|
|
|
|
|
|
|
const menuItem = 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
|
|
|
});
|
2023-07-16 18:42:42 +02:00
|
|
|
menuItem.enabled = this.service.isEnabled(commandName, whenClauseContext);
|
|
|
|
return menuItem;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2021-12-31 08:50:32 +02:00
|
|
|
public commandsToMenuItems(commandNames: string[], onClick: Function, locale: string): MenuItems {
|
2023-02-05 13:32:28 +02:00
|
|
|
const key = `${this.keymapService.lastSaveTime}_${commandNames.join('_')}_${locale}`;
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-31 08:50:32 +02:00
|
|
|
this.menuItemCache_ = {
|
|
|
|
[key]: output,
|
|
|
|
};
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
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');
|
2021-09-09 15:44:16 +02:00
|
|
|
const whenClauseContext = this.service.currentWhenClauseContext();
|
2020-11-14 02:02:17 +02:00
|
|
|
|
|
|
|
for (const info of pluginViewInfos) {
|
|
|
|
if (info.view.location !== location) continue;
|
2021-09-09 15:44:16 +02:00
|
|
|
const menuItem = this.commandToStatefulMenuItem(info.view.commandName);
|
|
|
|
menuItem.enabled = this.service.isEnabled(info.view.commandName, whenClauseContext);
|
|
|
|
output.push(menuItem);
|
2020-11-14 02:02:17 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-14 02:02:17 +02:00
|
|
|
if (output.length) output.splice(0, 0, { type: 'separator' } as any);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|