2021-08-18 12:54:28 +02:00
|
|
|
import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
2021-06-27 15:14:11 +02:00
|
|
|
const PluginManager = require('@joplin/lib/services/PluginManager');
|
|
|
|
|
2021-08-18 12:54:28 +02:00
|
|
|
export enum UiType {
|
|
|
|
GotoAnything = 'gotoAnything',
|
|
|
|
CommandPalette = 'commandPalette',
|
|
|
|
ControlledApi = 'controlledApi',
|
|
|
|
}
|
|
|
|
|
2021-06-27 15:14:11 +02:00
|
|
|
export const declaration: CommandDeclaration = {
|
|
|
|
name: 'gotoAnything',
|
2021-08-18 12:54:28 +02:00
|
|
|
label: () => _('Goto Anything...'),
|
2021-06-27 15:14:11 +02:00
|
|
|
};
|
|
|
|
|
2021-08-18 12:54:28 +02:00
|
|
|
function menuItemById(id: string) {
|
|
|
|
return PluginManager.instance().menuItems().find((i: any) => i.id === id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The way this command is implemented is a bit hacky due to the PluginManager
|
|
|
|
// layer. This manager is no longer needed but hasn't been refactored yet, so in
|
|
|
|
// the meantime we access the GotoAnything actions by grabbing the menu item
|
|
|
|
// calling the click() handler.
|
2021-06-27 15:14:11 +02:00
|
|
|
export const runtime = (): CommandRuntime => {
|
|
|
|
return {
|
2021-08-18 12:54:28 +02:00
|
|
|
execute: async (_context: CommandContext, uiType: UiType = UiType.GotoAnything) => {
|
|
|
|
if (uiType === UiType.GotoAnything) {
|
|
|
|
menuItemById('gotoAnything').click();
|
|
|
|
} else if (uiType === UiType.CommandPalette) {
|
|
|
|
menuItemById('commandPalette').click();
|
|
|
|
} else if (uiType === UiType.ControlledApi) {
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2021-08-18 12:54:28 +02:00
|
|
|
return new Promise((resolve: Function, reject: Function) => {
|
|
|
|
const menuItem = PluginManager.instance().menuItems().find((i: any) => i.id === 'controlledApi');
|
|
|
|
menuItem.userData = {
|
|
|
|
callback: { resolve, reject },
|
|
|
|
};
|
|
|
|
menuItem.click();
|
|
|
|
});
|
|
|
|
}
|
2023-08-01 16:38:59 +02:00
|
|
|
return null;
|
2021-06-27 15:14:11 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|