From 807eee938094d9f32e2cbf906cf84add591a2531 Mon Sep 17 00:00:00 2001 From: Nishant Mittal Date: Wed, 9 Jun 2021 01:39:13 +0530 Subject: [PATCH 1/4] feat: expose prompt to plugins as a command --- .eslintignore | 3 ++ .gitignore | 3 ++ packages/app-desktop/app.ts | 1 + .../app-desktop/gui/MainScreen/MainScreen.tsx | 1 + .../gui/MainScreen/commands/showPrompt.ts | 28 +++++++++++++++++++ packages/app-desktop/gui/menuCommandNames.ts | 1 + 6 files changed, 37 insertions(+) create mode 100644 packages/app-desktop/gui/MainScreen/commands/showPrompt.ts diff --git a/.eslintignore b/.eslintignore index 166a18fa081..f6427f7d898 100644 --- a/.eslintignore +++ b/.eslintignore @@ -356,6 +356,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 62e1ced6d74..31fdb1eeba5 100644 --- a/.gitignore +++ b/.gitignore @@ -342,6 +342,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 5c875364989..0e66fe22e35 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..30d1a2d9f72 --- /dev/null +++ b/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts @@ -0,0 +1,28 @@ +import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService'; +import { _ } from '@joplin/lib/locale'; + +export const declaration: CommandDeclaration = { + name: 'showPrompt', +}; + +export const runtime = (comp: any): CommandRuntime => { + return { + execute: async (_context: CommandContext, config: any) => { + return new Promise((resolve) => { + comp.setState({ + promptOptions: { + ...config, + label: _(config.label), + onClose: async (answer: any, buttonType: string) => { + resolve({ + answer: answer, + buttonType: buttonType, + }); + comp.setState({ promptOptions: null }); + }, + }, + }); + }); + }, + }; +}; diff --git a/packages/app-desktop/gui/menuCommandNames.ts b/packages/app-desktop/gui/menuCommandNames.ts index 8f0e3c78cfa..2e07dfde594 100644 --- a/packages/app-desktop/gui/menuCommandNames.ts +++ b/packages/app-desktop/gui/menuCommandNames.ts @@ -47,5 +47,6 @@ export default function() { 'toggleSafeMode', 'showShareNoteDialog', 'showShareFolderDialog', + 'showPrompt', ]; } From 33d1a967e63b0f3440e6db4bae7367db43742fb1 Mon Sep 17 00:00:00 2001 From: Nishant Mittal Date: Thu, 10 Jun 2021 01:27:09 +0530 Subject: [PATCH 2/4] address review comments --- .../gui/MainScreen/commands/showPrompt.ts | 14 ++++++++++---- packages/app-desktop/gui/menuCommandNames.ts | 1 - 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts b/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts index 30d1a2d9f72..d0706764371 100644 --- a/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts +++ b/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts @@ -1,24 +1,30 @@ import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService'; -import { _ } from '@joplin/lib/locale'; export const declaration: CommandDeclaration = { name: 'showPrompt', }; +interface PromptConfig { + label: string; + inputType?: 'dropdown' | 'datetime' | 'tags' | 'text'; + value?: any; + autocomplete?: any[]; + buttons?: string[]; +} + export const runtime = (comp: any): CommandRuntime => { return { - execute: async (_context: CommandContext, config: any) => { + execute: async (_context: CommandContext, config: PromptConfig) => { return new Promise((resolve) => { comp.setState({ promptOptions: { ...config, - label: _(config.label), onClose: async (answer: any, buttonType: string) => { + comp.setState({ promptOptions: null }); resolve({ answer: answer, buttonType: buttonType, }); - comp.setState({ promptOptions: null }); }, }, }); diff --git a/packages/app-desktop/gui/menuCommandNames.ts b/packages/app-desktop/gui/menuCommandNames.ts index 2e07dfde594..8f0e3c78cfa 100644 --- a/packages/app-desktop/gui/menuCommandNames.ts +++ b/packages/app-desktop/gui/menuCommandNames.ts @@ -47,6 +47,5 @@ export default function() { 'toggleSafeMode', 'showShareNoteDialog', 'showShareFolderDialog', - 'showPrompt', ]; } From 32832c8350390eb4256349cdc7ad000c52af613e Mon Sep 17 00:00:00 2001 From: Nishant Mittal Date: Thu, 10 Jun 2021 02:24:21 +0530 Subject: [PATCH 3/4] address review comments --- .../app-desktop/gui/MainScreen/commands/showPrompt.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts b/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts index d0706764371..97ebde1411d 100644 --- a/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts +++ b/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts @@ -4,9 +4,16 @@ export const declaration: CommandDeclaration = { name: 'showPrompt', }; +enum PromptInputType { + Dropdown = 'dropdown', + Datetime = 'datetime', + Tags = 'tags', + Text = 'text', +} + interface PromptConfig { label: string; - inputType?: 'dropdown' | 'datetime' | 'tags' | 'text'; + inputType?: PromptInputType; value?: any; autocomplete?: any[]; buttons?: string[]; From a414664ac40ec68328d07d3e03c193f301c2fcb1 Mon Sep 17 00:00:00 2001 From: Nishant Mittal Date: Thu, 10 Jun 2021 10:49:36 +0530 Subject: [PATCH 4/4] fix formatting --- packages/app-desktop/gui/MainScreen/commands/showPrompt.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts b/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts index 97ebde1411d..a9acaca5e50 100644 --- a/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts +++ b/packages/app-desktop/gui/MainScreen/commands/showPrompt.ts @@ -5,8 +5,8 @@ export const declaration: CommandDeclaration = { }; enum PromptInputType { - Dropdown = 'dropdown', - Datetime = 'datetime', + Dropdown = 'dropdown', + Datetime = 'datetime', Tags = 'tags', Text = 'text', }