diff --git a/.eslintignore b/.eslintignore index 04660bc7f..22d159540 100644 --- a/.eslintignore +++ b/.eslintignore @@ -228,6 +228,9 @@ packages/app-desktop/gui/KeymapConfig/utils/useKeymap.js.map packages/app-desktop/gui/MainScreen/MainScreen.d.ts packages/app-desktop/gui/MainScreen/MainScreen.js packages/app-desktop/gui/MainScreen/MainScreen.js.map +packages/app-desktop/gui/MainScreen/commands/commandPalette.d.ts +packages/app-desktop/gui/MainScreen/commands/commandPalette.js +packages/app-desktop/gui/MainScreen/commands/commandPalette.js.map packages/app-desktop/gui/MainScreen/commands/editAlarm.d.ts packages/app-desktop/gui/MainScreen/commands/editAlarm.js packages/app-desktop/gui/MainScreen/commands/editAlarm.js.map diff --git a/.gitignore b/.gitignore index e4ab7aaab..d3d9d5144 100644 --- a/.gitignore +++ b/.gitignore @@ -213,6 +213,9 @@ packages/app-desktop/gui/KeymapConfig/utils/useKeymap.js.map packages/app-desktop/gui/MainScreen/MainScreen.d.ts packages/app-desktop/gui/MainScreen/MainScreen.js packages/app-desktop/gui/MainScreen/MainScreen.js.map +packages/app-desktop/gui/MainScreen/commands/commandPalette.d.ts +packages/app-desktop/gui/MainScreen/commands/commandPalette.js +packages/app-desktop/gui/MainScreen/commands/commandPalette.js.map packages/app-desktop/gui/MainScreen/commands/editAlarm.d.ts packages/app-desktop/gui/MainScreen/commands/editAlarm.js packages/app-desktop/gui/MainScreen/commands/editAlarm.js.map diff --git a/packages/app-desktop/app.ts b/packages/app-desktop/app.ts index b2183b0ac..07ad76555 100644 --- a/packages/app-desktop/app.ts +++ b/packages/app-desktop/app.ts @@ -46,6 +46,7 @@ const commands = [ require('./gui/MainScreen/commands/editAlarm'), require('./gui/MainScreen/commands/exportPdf'), require('./gui/MainScreen/commands/gotoAnything'), + require('./gui/MainScreen/commands/commandPalette'), require('./gui/MainScreen/commands/hideModalMessage'), require('./gui/MainScreen/commands/moveToFolder'), require('./gui/MainScreen/commands/newFolder'), diff --git a/packages/app-desktop/gui/KeymapConfig/utils/getLabel.ts b/packages/app-desktop/gui/KeymapConfig/utils/getLabel.ts index 1d0f17f03..9042257e2 100644 --- a/packages/app-desktop/gui/KeymapConfig/utils/getLabel.ts +++ b/packages/app-desktop/gui/KeymapConfig/utils/getLabel.ts @@ -16,16 +16,12 @@ const getLabel = (commandName: string): string => { return _('Quit'); case 'zoomActualSize': return _('Actual Size'); - case 'gotoAnything': - return _('Goto Anything...'); case 'help': return _('Website and documentation'); case 'hideApp': return _('Hide Joplin'); case 'closeWindow': return _('Close Window'); - case 'commandPalette': - return _('Command palette'); case 'config': return shim.isMac() ? _('Preferences') : _('Options'); } diff --git a/packages/app-desktop/gui/MainScreen/MainScreen.tsx b/packages/app-desktop/gui/MainScreen/MainScreen.tsx index 7660f6432..8f5b3d1f9 100644 --- a/packages/app-desktop/gui/MainScreen/MainScreen.tsx +++ b/packages/app-desktop/gui/MainScreen/MainScreen.tsx @@ -114,6 +114,7 @@ const commands = [ require('./commands/editAlarm'), require('./commands/exportPdf'), require('./commands/gotoAnything'), + require('./commands/commandPalette'), require('./commands/hideModalMessage'), require('./commands/moveToFolder'), require('./commands/newFolder'), diff --git a/packages/app-desktop/gui/MainScreen/commands/commandPalette.ts b/packages/app-desktop/gui/MainScreen/commands/commandPalette.ts new file mode 100644 index 000000000..59fb5ea27 --- /dev/null +++ b/packages/app-desktop/gui/MainScreen/commands/commandPalette.ts @@ -0,0 +1,16 @@ +import CommandService, { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService'; +import { _ } from '@joplin/lib/locale'; +import { UiType } from './gotoAnything'; + +export const declaration: CommandDeclaration = { + name: 'commandPalette', + label: () => _('Command palette...'), +}; + +export const runtime = (): CommandRuntime => { + return { + execute: async (_context: CommandContext) => { + void CommandService.instance().execute('gotoAnything', UiType.CommandPalette); + }, + }; +}; diff --git a/packages/app-desktop/gui/MainScreen/commands/gotoAnything.ts b/packages/app-desktop/gui/MainScreen/commands/gotoAnything.ts index a8e0bf520..8309219f2 100644 --- a/packages/app-desktop/gui/MainScreen/commands/gotoAnything.ts +++ b/packages/app-desktop/gui/MainScreen/commands/gotoAnything.ts @@ -1,20 +1,42 @@ -import { CommandRuntime, CommandDeclaration } from '@joplin/lib/services/CommandService'; +import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService'; +import { _ } from '@joplin/lib/locale'; const PluginManager = require('@joplin/lib/services/PluginManager'); +export enum UiType { + GotoAnything = 'gotoAnything', + CommandPalette = 'commandPalette', + ControlledApi = 'controlledApi', +} + export const declaration: CommandDeclaration = { name: 'gotoAnything', + label: () => _('Goto Anything...'), }; +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. export const runtime = (): CommandRuntime => { return { - execute: async () => { - 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(); - }); + 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) { + 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(); + }); + } }, }; }; diff --git a/packages/app-desktop/gui/MenuBar.tsx b/packages/app-desktop/gui/MenuBar.tsx index 395e65787..cf75e5944 100644 --- a/packages/app-desktop/gui/MenuBar.tsx +++ b/packages/app-desktop/gui/MenuBar.tsx @@ -24,7 +24,6 @@ import { reg } from '@joplin/lib/registry'; const packageInfo = require('../packageInfo.js'); const { clipboard } = require('electron'); const Menu = bridge().Menu; -const PluginManager = require('@joplin/lib/services/PluginManager'); const menuUtils = new MenuUtils(CommandService.instance()); @@ -751,16 +750,8 @@ function useMenu(props: Props) { rootMenus[key].submenu = cleanUpSeparators(rootMenus[key].submenu); } - { - // This is for GotoAnything only - should be refactored since this plugin manager is not used otherwise - const pluginMenuItems = PluginManager.instance().menuItems(); - for (const item of pluginMenuItems) { - if (!item.parent) continue; - const itemParent = rootMenus[item.parent] ? rootMenus[item.parent] : 'tools'; - itemParent.submenu.push(separator()); - itemParent.submenu.push(item); - } - } + rootMenus.go.submenu.push(menuItemDic.gotoAnything); + rootMenus.tools.submenu.push(menuItemDic.commandPalette); for (const view of props.pluginMenuItems) { const location: MenuItemLocation = view.location; diff --git a/packages/app-desktop/gui/menuCommandNames.ts b/packages/app-desktop/gui/menuCommandNames.ts index 8f0e3c78c..e2ce733ae 100644 --- a/packages/app-desktop/gui/menuCommandNames.ts +++ b/packages/app-desktop/gui/menuCommandNames.ts @@ -47,5 +47,7 @@ export default function() { 'toggleSafeMode', 'showShareNoteDialog', 'showShareFolderDialog', + 'gotoAnything', + 'commandPalette', ]; } diff --git a/packages/app-desktop/plugins/GotoAnything.tsx b/packages/app-desktop/plugins/GotoAnything.tsx index 9183b8aac..ebcc84ab8 100644 --- a/packages/app-desktop/plugins/GotoAnything.tsx +++ b/packages/app-desktop/plugins/GotoAnything.tsx @@ -416,6 +416,8 @@ class Dialog extends React.PureComponent { }); if (this.userCallback_) { + logger.info('gotoItem: user callback', item); + this.userCallback_.resolve({ type: this.state.listType, item: { ...item }, @@ -424,6 +426,7 @@ class Dialog extends React.PureComponent { } if (item.type === BaseModel.TYPE_COMMAND) { + logger.info('gotoItem: execute command', item); void CommandService.instance().execute(item.id, ...item.commandArgs); void focusEditorIfEditorCommand(item.id, CommandService.instance()); return; @@ -442,6 +445,8 @@ class Dialog extends React.PureComponent { } if (this.state.listType === BaseModel.TYPE_NOTE) { + logger.info('gotoItem: note', item); + this.props.dispatch({ type: 'FOLDER_AND_NOTE_SELECT', folderId: item.parent_id, @@ -450,11 +455,15 @@ class Dialog extends React.PureComponent { CommandService.instance().scheduleExecute('focusElement', 'noteBody'); } else if (this.state.listType === BaseModel.TYPE_TAG) { + logger.info('gotoItem: tag', item); + this.props.dispatch({ type: 'TAG_SELECT', id: item.id, }); } else if (this.state.listType === BaseModel.TYPE_FOLDER) { + logger.info('gotoItem: folder', item); + this.props.dispatch({ type: 'FOLDER_SELECT', id: item.id, @@ -602,6 +611,7 @@ GotoAnything.manifest = { name: PLUGIN_NAME, menuItems: [ { + id: 'gotoAnything', name: 'main', parent: 'go', label: _('Goto Anything...'), @@ -609,6 +619,7 @@ GotoAnything.manifest = { screens: ['Main'], }, { + id: 'commandPalette', name: 'main', parent: 'tools', label: _('Command palette'),