From 52d5c32950df64d70305f17528188e6c514d1c3b Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sun, 25 Oct 2020 17:22:59 +0000 Subject: [PATCH] Plugins: Add the openNote, openFolder and openTag commands --- .eslintignore | 3 +++ .gitignore | 3 +++ .ignore | 3 +++ ElectronClient/app.ts | 3 +++ ElectronClient/gui/MainScreen/MainScreen.tsx | 3 +++ .../gui/MainScreen/commands/openFolder.ts | 16 ++++++++++++ .../gui/MainScreen/commands/openNote.ts | 26 +++++++++++++++++++ .../gui/MainScreen/commands/openTag.ts | 16 ++++++++++++ ElectronClient/services/commands/types.ts | 1 + .../lib/services/CommandService.ts | 13 +++++++++- 10 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 ElectronClient/gui/MainScreen/commands/openFolder.ts create mode 100644 ElectronClient/gui/MainScreen/commands/openNote.ts create mode 100644 ElectronClient/gui/MainScreen/commands/openTag.ts diff --git a/.eslintignore b/.eslintignore index 9a73926a5..b03f12947 100644 --- a/.eslintignore +++ b/.eslintignore @@ -106,6 +106,9 @@ ElectronClient/gui/MainScreen/commands/moveToFolder.js ElectronClient/gui/MainScreen/commands/newFolder.js ElectronClient/gui/MainScreen/commands/newNote.js ElectronClient/gui/MainScreen/commands/newTodo.js +ElectronClient/gui/MainScreen/commands/openFolder.js +ElectronClient/gui/MainScreen/commands/openNote.js +ElectronClient/gui/MainScreen/commands/openTag.js ElectronClient/gui/MainScreen/commands/print.js ElectronClient/gui/MainScreen/commands/renameFolder.js ElectronClient/gui/MainScreen/commands/renameTag.js diff --git a/.gitignore b/.gitignore index b0100e9d2..176286743 100644 --- a/.gitignore +++ b/.gitignore @@ -100,6 +100,9 @@ ElectronClient/gui/MainScreen/commands/moveToFolder.js ElectronClient/gui/MainScreen/commands/newFolder.js ElectronClient/gui/MainScreen/commands/newNote.js ElectronClient/gui/MainScreen/commands/newTodo.js +ElectronClient/gui/MainScreen/commands/openFolder.js +ElectronClient/gui/MainScreen/commands/openNote.js +ElectronClient/gui/MainScreen/commands/openTag.js ElectronClient/gui/MainScreen/commands/print.js ElectronClient/gui/MainScreen/commands/renameFolder.js ElectronClient/gui/MainScreen/commands/renameTag.js diff --git a/.ignore b/.ignore index 8c678f45e..b584921f3 100644 --- a/.ignore +++ b/.ignore @@ -49,6 +49,9 @@ ElectronClient/gui/MainScreen/commands/moveToFolder.js ElectronClient/gui/MainScreen/commands/newFolder.js ElectronClient/gui/MainScreen/commands/newNote.js ElectronClient/gui/MainScreen/commands/newTodo.js +ElectronClient/gui/MainScreen/commands/openFolder.js +ElectronClient/gui/MainScreen/commands/openNote.js +ElectronClient/gui/MainScreen/commands/openTag.js ElectronClient/gui/MainScreen/commands/print.js ElectronClient/gui/MainScreen/commands/renameFolder.js ElectronClient/gui/MainScreen/commands/renameTag.js diff --git a/ElectronClient/app.ts b/ElectronClient/app.ts index eb846427e..d876f3b4a 100644 --- a/ElectronClient/app.ts +++ b/ElectronClient/app.ts @@ -61,6 +61,9 @@ const commands = [ require('./gui/MainScreen/commands/toggleSideBar'), require('./gui/MainScreen/commands/toggleVisiblePanes'), require('./gui/MainScreen/commands/toggleEditors'), + require('./gui/MainScreen/commands/openNote'), + require('./gui/MainScreen/commands/openFolder'), + require('./gui/MainScreen/commands/openTag'), require('./gui/NoteEditor/commands/focusElementNoteBody'), require('./gui/NoteEditor/commands/focusElementNoteTitle'), require('./gui/NoteEditor/commands/showLocalSearch'), diff --git a/ElectronClient/gui/MainScreen/MainScreen.tsx b/ElectronClient/gui/MainScreen/MainScreen.tsx index 343d56d31..e29155ab6 100644 --- a/ElectronClient/gui/MainScreen/MainScreen.tsx +++ b/ElectronClient/gui/MainScreen/MainScreen.tsx @@ -63,6 +63,9 @@ const commands = [ require('./commands/toggleNoteList'), require('./commands/toggleSideBar'), require('./commands/toggleVisiblePanes'), + require('./commands/openNote'), + require('./commands/openFolder'), + require('./commands/openTag'), ]; class MainScreenComponent extends React.Component { diff --git a/ElectronClient/gui/MainScreen/commands/openFolder.ts b/ElectronClient/gui/MainScreen/commands/openFolder.ts new file mode 100644 index 000000000..c2284a813 --- /dev/null +++ b/ElectronClient/gui/MainScreen/commands/openFolder.ts @@ -0,0 +1,16 @@ +import { CommandRuntime, CommandDeclaration, CommandContext } from 'lib/services/CommandService'; + +export const declaration:CommandDeclaration = { + name: 'openFolder', +}; + +export const runtime = ():CommandRuntime => { + return { + execute: async (context:CommandContext, folderId:string) => { + context.dispatch({ + type: 'FOLDER_SELECT', + id: folderId, + }); + }, + }; +}; diff --git a/ElectronClient/gui/MainScreen/commands/openNote.ts b/ElectronClient/gui/MainScreen/commands/openNote.ts new file mode 100644 index 000000000..e16bde15b --- /dev/null +++ b/ElectronClient/gui/MainScreen/commands/openNote.ts @@ -0,0 +1,26 @@ +import { CommandRuntime, CommandDeclaration, CommandContext } from 'lib/services/CommandService'; +const Note = require('lib/models/Note'); +const Folder = require('lib/models/Folder'); + +export const declaration:CommandDeclaration = { + name: 'openNote', +}; + +export const runtime = ():CommandRuntime => { + return { + execute: async (context:CommandContext, noteId:string, hash:string = null) => { + const note = await Note.load(noteId); + if (!note) throw new Error(`No such note: ${noteId}`); + + const folder = await Folder.load(note.parent_id); + if (!folder) throw new Error(`Note parent notebook does not exist: ${JSON.stringify(note)}`); + + context.dispatch({ + type: 'FOLDER_AND_NOTE_SELECT', + folderId: folder.id, + noteId: note.id, + hash, + }); + }, + }; +}; diff --git a/ElectronClient/gui/MainScreen/commands/openTag.ts b/ElectronClient/gui/MainScreen/commands/openTag.ts new file mode 100644 index 000000000..8c8a74164 --- /dev/null +++ b/ElectronClient/gui/MainScreen/commands/openTag.ts @@ -0,0 +1,16 @@ +import { CommandRuntime, CommandDeclaration, CommandContext } from 'lib/services/CommandService'; + +export const declaration:CommandDeclaration = { + name: 'openTag', +}; + +export const runtime = ():CommandRuntime => { + return { + execute: async (context:CommandContext, tagId:string) => { + context.dispatch({ + type: 'TAG_SELECT', + id: tagId, + }); + }, + }; +}; diff --git a/ElectronClient/services/commands/types.ts b/ElectronClient/services/commands/types.ts index fb565f08c..7e670923c 100644 --- a/ElectronClient/services/commands/types.ts +++ b/ElectronClient/services/commands/types.ts @@ -2,4 +2,5 @@ import { AppState } from '../../app'; export interface DesktopCommandContext { state: AppState, + dispatch: Function, } diff --git a/ReactNativeClient/lib/services/CommandService.ts b/ReactNativeClient/lib/services/CommandService.ts index 02b2385b1..d220d611c 100644 --- a/ReactNativeClient/lib/services/CommandService.ts +++ b/ReactNativeClient/lib/services/CommandService.ts @@ -11,6 +11,7 @@ type EnabledCondition = string; export interface CommandContext { // The state may also be of type "AppState" (used by the desktop app), which inherits from "State" (used by all apps) state: State, + dispatch: Function, } export interface CommandRuntime { @@ -203,10 +204,20 @@ export default class CommandService extends BaseService { delete command.runtime; } + private createContext():CommandContext { + return { + state: this.store_.getState(), + dispatch: (action:any) => { + this.store_.dispatch(action); + }, + }; + } + public async execute(commandName:string, ...args:any[]):Promise { const command = this.commandByName(commandName); this.logger().info('CommandService::execute:', commandName, args); - return command.runtime.execute({ state: this.store_.getState() }, ...args); + if (!command.runtime) throw new Error(`Cannot execute a command without a runtime: ${commandName}`); + return command.runtime.execute(this.createContext(), ...args); } public scheduleExecute(commandName:string, args:any) {