From 00b39e46715c2afbb971fa7a40c5e43705a7c636 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sun, 27 Jun 2021 14:14:11 +0100 Subject: [PATCH] Plugins: Add support for gotoAnything command so that it can be called from plugins --- .eslintignore | 3 +++ .gitignore | 3 +++ packages/app-desktop/app.ts | 5 +++-- .../app-desktop/gui/MainScreen/MainScreen.tsx | 15 +++++++------- .../gui/MainScreen/commands/gotoAnything.ts | 20 +++++++++++++++++++ packages/app-desktop/gui/MenuBar.tsx | 1 + packages/app-desktop/plugins/GotoAnything.tsx | 14 +++++++++++++ packages/lib/services/PluginManager.js | 2 +- 8 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 packages/app-desktop/gui/MainScreen/commands/gotoAnything.ts diff --git a/.eslintignore b/.eslintignore index e1986ea51..f05614e22 100644 --- a/.eslintignore +++ b/.eslintignore @@ -224,6 +224,9 @@ packages/app-desktop/gui/MainScreen/commands/editAlarm.js.map packages/app-desktop/gui/MainScreen/commands/exportPdf.d.ts packages/app-desktop/gui/MainScreen/commands/exportPdf.js packages/app-desktop/gui/MainScreen/commands/exportPdf.js.map +packages/app-desktop/gui/MainScreen/commands/gotoAnything.d.ts +packages/app-desktop/gui/MainScreen/commands/gotoAnything.js +packages/app-desktop/gui/MainScreen/commands/gotoAnything.js.map packages/app-desktop/gui/MainScreen/commands/hideModalMessage.d.ts packages/app-desktop/gui/MainScreen/commands/hideModalMessage.js packages/app-desktop/gui/MainScreen/commands/hideModalMessage.js.map diff --git a/.gitignore b/.gitignore index 44d1406c6..5a6b77ccf 100644 --- a/.gitignore +++ b/.gitignore @@ -210,6 +210,9 @@ packages/app-desktop/gui/MainScreen/commands/editAlarm.js.map packages/app-desktop/gui/MainScreen/commands/exportPdf.d.ts packages/app-desktop/gui/MainScreen/commands/exportPdf.js packages/app-desktop/gui/MainScreen/commands/exportPdf.js.map +packages/app-desktop/gui/MainScreen/commands/gotoAnything.d.ts +packages/app-desktop/gui/MainScreen/commands/gotoAnything.js +packages/app-desktop/gui/MainScreen/commands/gotoAnything.js.map packages/app-desktop/gui/MainScreen/commands/hideModalMessage.d.ts packages/app-desktop/gui/MainScreen/commands/hideModalMessage.js packages/app-desktop/gui/MainScreen/commands/hideModalMessage.js.map diff --git a/packages/app-desktop/app.ts b/packages/app-desktop/app.ts index e301e5a2b..6fe023eb9 100644 --- a/packages/app-desktop/app.ts +++ b/packages/app-desktop/app.ts @@ -48,6 +48,7 @@ const CssUtils = require('@joplin/lib/CssUtils'); const commands = [ require('./gui/MainScreen/commands/editAlarm'), require('./gui/MainScreen/commands/exportPdf'), + require('./gui/MainScreen/commands/gotoAnything'), require('./gui/MainScreen/commands/hideModalMessage'), require('./gui/MainScreen/commands/moveToFolder'), require('./gui/MainScreen/commands/newFolder'), @@ -59,7 +60,6 @@ const commands = [ require('./gui/MainScreen/commands/openTag'), require('./gui/MainScreen/commands/print'), require('./gui/MainScreen/commands/renameFolder'), - require('./gui/MainScreen/commands/showShareFolderDialog'), require('./gui/MainScreen/commands/renameTag'), require('./gui/MainScreen/commands/search'), require('./gui/MainScreen/commands/selectTemplate'), @@ -67,6 +67,8 @@ const commands = [ require('./gui/MainScreen/commands/showModalMessage'), require('./gui/MainScreen/commands/showNoteContentProperties'), require('./gui/MainScreen/commands/showNoteProperties'), + require('./gui/MainScreen/commands/showPrompt'), + require('./gui/MainScreen/commands/showShareFolderDialog'), require('./gui/MainScreen/commands/showShareNoteDialog'), require('./gui/MainScreen/commands/showSpellCheckerMenu'), require('./gui/MainScreen/commands/toggleEditors'), @@ -74,7 +76,6 @@ const commands = [ require('./gui/MainScreen/commands/toggleNoteList'), require('./gui/MainScreen/commands/toggleSideBar'), require('./gui/MainScreen/commands/toggleVisiblePanes'), - require('./gui/MainScreen/commands/showPrompt'), require('./gui/NoteEditor/commands/focusElementNoteBody'), require('./gui/NoteEditor/commands/focusElementNoteTitle'), require('./gui/NoteEditor/commands/showLocalSearch'), diff --git a/packages/app-desktop/gui/MainScreen/MainScreen.tsx b/packages/app-desktop/gui/MainScreen/MainScreen.tsx index b6da990bc..6d912be97 100644 --- a/packages/app-desktop/gui/MainScreen/MainScreen.tsx +++ b/packages/app-desktop/gui/MainScreen/MainScreen.tsx @@ -111,15 +111,18 @@ const defaultLayout: LayoutItem = { const commands = [ require('./commands/editAlarm'), require('./commands/exportPdf'), + require('./commands/gotoAnything'), require('./commands/hideModalMessage'), require('./commands/moveToFolder'), - require('./commands/newNote'), require('./commands/newFolder'), + require('./commands/newNote'), require('./commands/newSubFolder'), require('./commands/newTodo'), + require('./commands/openFolder'), + require('./commands/openNote'), + require('./commands/openTag'), require('./commands/print'), require('./commands/renameFolder'), - require('./commands/showShareFolderDialog'), require('./commands/renameTag'), require('./commands/search'), require('./commands/selectTemplate'), @@ -127,17 +130,15 @@ const commands = [ require('./commands/showModalMessage'), require('./commands/showNoteContentProperties'), require('./commands/showNoteProperties'), + require('./commands/showPrompt'), + require('./commands/showShareFolderDialog'), require('./commands/showShareNoteDialog'), require('./commands/showSpellCheckerMenu'), require('./commands/toggleEditors'), + require('./commands/toggleLayoutMoveMode'), require('./commands/toggleNoteList'), require('./commands/toggleSideBar'), require('./commands/toggleVisiblePanes'), - require('./commands/toggleLayoutMoveMode'), - require('./commands/openNote'), - require('./commands/openFolder'), - require('./commands/openTag'), - require('./commands/showPrompt'), ]; class MainScreenComponent extends React.Component { diff --git a/packages/app-desktop/gui/MainScreen/commands/gotoAnything.ts b/packages/app-desktop/gui/MainScreen/commands/gotoAnything.ts new file mode 100644 index 000000000..a8e0bf520 --- /dev/null +++ b/packages/app-desktop/gui/MainScreen/commands/gotoAnything.ts @@ -0,0 +1,20 @@ +import { CommandRuntime, CommandDeclaration } from '@joplin/lib/services/CommandService'; +const PluginManager = require('@joplin/lib/services/PluginManager'); + +export const declaration: CommandDeclaration = { + name: 'gotoAnything', +}; + +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(); + }); + }, + }; +}; diff --git a/packages/app-desktop/gui/MenuBar.tsx b/packages/app-desktop/gui/MenuBar.tsx index 8c131b981..205c21573 100644 --- a/packages/app-desktop/gui/MenuBar.tsx +++ b/packages/app-desktop/gui/MenuBar.tsx @@ -802,6 +802,7 @@ function useMenu(props: Props) { // 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); diff --git a/packages/app-desktop/plugins/GotoAnything.tsx b/packages/app-desktop/plugins/GotoAnything.tsx index 45a473d2e..97ddb90b4 100644 --- a/packages/app-desktop/plugins/GotoAnything.tsx +++ b/packages/app-desktop/plugins/GotoAnything.tsx @@ -79,12 +79,15 @@ class Dialog extends React.PureComponent { private itemListRef: any; private listUpdateIID_: any; private markupToHtml_: any; + private userCallback_: any = null; constructor(props: Props) { super(props); const startString = props?.userData?.startString ? props?.userData?.startString : ''; + this.userCallback_ = props?.userData?.callback; + this.state = { query: startString, results: [], @@ -398,6 +401,14 @@ class Dialog extends React.PureComponent { open: false, }); + if (this.userCallback_) { + this.userCallback_.resolve({ + type: this.state.listType, + item: { ...item }, + }); + return; + } + if (item.type === BaseModel.TYPE_COMMAND) { void CommandService.instance().execute(item.id, ...item.commandArgs); void focusEditorIfEditorCommand(item.id, CommandService.instance()); @@ -593,6 +604,9 @@ GotoAnything.manifest = { startString: ':', }, }, + { + id: 'controlledApi', + }, ], }; diff --git a/packages/lib/services/PluginManager.js b/packages/lib/services/PluginManager.js index d6d45d0e8..2c8d1963e 100644 --- a/packages/lib/services/PluginManager.js +++ b/packages/lib/services/PluginManager.js @@ -96,7 +96,7 @@ class PluginManager { }); }; - item.accelerator = menuItems[i].accelerator(); + if (menuItems[i].accelerator) item.accelerator = menuItems[i].accelerator(); menuItems[i] = item; }