From c5c38a323f793e8a340760fc5ad4a1095f8588ee Mon Sep 17 00:00:00 2001 From: Nishant Mittal Date: Thu, 10 Jun 2021 16:26:16 -0700 Subject: [PATCH] Desktop: Expose prompt to plugins as a command (#5058) --- .eslintignore | 3 ++ .gitignore | 3 ++ packages/app-desktop/app.ts | 1 + .../app-desktop/gui/MainScreen/MainScreen.tsx | 1 + .../gui/MainScreen/commands/showPrompt.ts | 41 +++++++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 packages/app-desktop/gui/MainScreen/commands/showPrompt.ts diff --git a/.eslintignore b/.eslintignore index 43d5398466c..708d9a9d692 100644 --- a/.eslintignore +++ b/.eslintignore @@ -278,6 +278,9 @@ packages/app-desktop/gui/MainScreen/commands/showNoteContentProperties.js.map packages/app-desktop/gui/MainScreen/commands/showNoteProperties.d.ts packages/app-desktop/gui/MainScreen/commands/showNoteProperties.js packages/app-desktop/gui/MainScreen/commands/showNoteProperties.js.map +packages/app-desktop/gui/MainScreen/commands/showPrompt.d.ts +packages/app-desktop/gui/MainScreen/commands/showPrompt.js +packages/app-desktop/gui/MainScreen/commands/showPrompt.js.map packages/app-desktop/gui/MainScreen/commands/showShareFolderDialog.d.ts packages/app-desktop/gui/MainScreen/commands/showShareFolderDialog.js packages/app-desktop/gui/MainScreen/commands/showShareFolderDialog.js.map diff --git a/.gitignore b/.gitignore index 0ae10962935..c53761af969 100644 --- a/.gitignore +++ b/.gitignore @@ -264,6 +264,9 @@ packages/app-desktop/gui/MainScreen/commands/showNoteContentProperties.js.map packages/app-desktop/gui/MainScreen/commands/showNoteProperties.d.ts packages/app-desktop/gui/MainScreen/commands/showNoteProperties.js packages/app-desktop/gui/MainScreen/commands/showNoteProperties.js.map +packages/app-desktop/gui/MainScreen/commands/showPrompt.d.ts +packages/app-desktop/gui/MainScreen/commands/showPrompt.js +packages/app-desktop/gui/MainScreen/commands/showPrompt.js.map packages/app-desktop/gui/MainScreen/commands/showShareFolderDialog.d.ts packages/app-desktop/gui/MainScreen/commands/showShareFolderDialog.js packages/app-desktop/gui/MainScreen/commands/showShareFolderDialog.js.map diff --git a/packages/app-desktop/app.ts b/packages/app-desktop/app.ts index ae6aee24b17..e9bb475910f 100644 --- a/packages/app-desktop/app.ts +++ b/packages/app-desktop/app.ts @@ -74,6 +74,7 @@ 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 c4c9154d845..5658408fca8 100644 --- a/packages/app-desktop/gui/MainScreen/MainScreen.tsx +++ b/packages/app-desktop/gui/MainScreen/MainScreen.tsx @@ -135,6 +135,7 @@ const commands = [ 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/showPrompt.ts b/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts new file mode 100644 index 00000000000..a9acaca5e50 --- /dev/null +++ b/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts @@ -0,0 +1,41 @@ +import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService'; + +export const declaration: CommandDeclaration = { + name: 'showPrompt', +}; + +enum PromptInputType { + Dropdown = 'dropdown', + Datetime = 'datetime', + Tags = 'tags', + Text = 'text', +} + +interface PromptConfig { + label: string; + inputType?: PromptInputType; + value?: any; + autocomplete?: any[]; + buttons?: string[]; +} + +export const runtime = (comp: any): CommandRuntime => { + return { + execute: async (_context: CommandContext, config: PromptConfig) => { + return new Promise((resolve) => { + comp.setState({ + promptOptions: { + ...config, + onClose: async (answer: any, buttonType: string) => { + comp.setState({ promptOptions: null }); + resolve({ + answer: answer, + buttonType: buttonType, + }); + }, + }, + }); + }); + }, + }; +};