1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-16 00:14:34 +02:00

Plugins: Add support for enabledConditions when creating menu item from command

This commit is contained in:
Laurent Cozic
2021-09-09 14:44:16 +01:00
parent 0a54854f54
commit 9260b2a9ab

View File

@ -12,6 +12,7 @@ interface MenuItem {
click: Function; click: Function;
role?: any; role?: any;
accelerator?: string; accelerator?: string;
enabled: boolean;
} }
interface MenuItems { interface MenuItems {
@ -78,6 +79,7 @@ export default class MenuUtils {
id: command.declaration.name, id: command.declaration.name,
label: this.service.label(commandName), label: this.service.label(commandName),
click: () => onClick(command.declaration.name), click: () => onClick(command.declaration.name),
enabled: true,
}; };
if (command.declaration.role) item.role = command.declaration.role; if (command.declaration.role) item.role = command.declaration.role;
@ -132,10 +134,13 @@ export default class MenuUtils {
public pluginContextMenuItems(plugins: PluginStates, location: MenuItemLocation): MenuItem[] { public pluginContextMenuItems(plugins: PluginStates, location: MenuItemLocation): MenuItem[] {
const output: MenuItem[] = []; const output: MenuItem[] = [];
const pluginViewInfos = pluginUtils.viewInfosByType(plugins, 'menuItem'); const pluginViewInfos = pluginUtils.viewInfosByType(plugins, 'menuItem');
const whenClauseContext = this.service.currentWhenClauseContext();
for (const info of pluginViewInfos) { for (const info of pluginViewInfos) {
if (info.view.location !== location) continue; if (info.view.location !== location) continue;
output.push(this.commandToStatefulMenuItem(info.view.commandName)); const menuItem = this.commandToStatefulMenuItem(info.view.commandName);
menuItem.enabled = this.service.isEnabled(info.view.commandName, whenClauseContext);
output.push(menuItem);
} }
if (output.length) output.splice(0, 0, { type: 'separator' } as any); if (output.length) output.splice(0, 0, { type: 'separator' } as any);