From f94c16b22e85b58af6a13410a4d285a3f1f5391e Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Tue, 4 Jun 2024 01:52:52 -0700 Subject: [PATCH] Mobile: Plugin API: Implement the `newNote` command (#10524) --- .eslintignore | 2 ++ .gitignore | 2 ++ packages/app-mobile/commands/index.ts | 2 ++ packages/app-mobile/commands/newNote.test.ts | 33 ++++++++++++++++++++ packages/app-mobile/commands/newNote.ts | 32 +++++++++++++++++++ 5 files changed, 71 insertions(+) create mode 100644 packages/app-mobile/commands/newNote.test.ts create mode 100644 packages/app-mobile/commands/newNote.ts diff --git a/.eslintignore b/.eslintignore index 394452039..ce45d2b3e 100644 --- a/.eslintignore +++ b/.eslintignore @@ -503,6 +503,8 @@ packages/app-desktop/utils/restartInSafeModeFromMain.test.js packages/app-desktop/utils/restartInSafeModeFromMain.js packages/app-mobile/PluginAssetsLoader.js packages/app-mobile/commands/index.js +packages/app-mobile/commands/newNote.test.js +packages/app-mobile/commands/newNote.js packages/app-mobile/commands/openItem.js packages/app-mobile/commands/openNote.js packages/app-mobile/commands/scrollToHash.js diff --git a/.gitignore b/.gitignore index 2949cf909..42146371b 100644 --- a/.gitignore +++ b/.gitignore @@ -482,6 +482,8 @@ packages/app-desktop/utils/restartInSafeModeFromMain.test.js packages/app-desktop/utils/restartInSafeModeFromMain.js packages/app-mobile/PluginAssetsLoader.js packages/app-mobile/commands/index.js +packages/app-mobile/commands/newNote.test.js +packages/app-mobile/commands/newNote.js packages/app-mobile/commands/openItem.js packages/app-mobile/commands/openNote.js packages/app-mobile/commands/scrollToHash.js diff --git a/packages/app-mobile/commands/index.ts b/packages/app-mobile/commands/index.ts index 0a54ea142..5a8834248 100644 --- a/packages/app-mobile/commands/index.ts +++ b/packages/app-mobile/commands/index.ts @@ -1,9 +1,11 @@ // AUTO-GENERATED using `gulp buildScriptIndexes` +import * as newNote from './newNote'; import * as openItem from './openItem'; import * as openNote from './openNote'; import * as scrollToHash from './scrollToHash'; const index: any[] = [ + newNote, openItem, openNote, scrollToHash, diff --git a/packages/app-mobile/commands/newNote.test.ts b/packages/app-mobile/commands/newNote.test.ts new file mode 100644 index 000000000..780dbd9c4 --- /dev/null +++ b/packages/app-mobile/commands/newNote.test.ts @@ -0,0 +1,33 @@ +import NavService from '@joplin/lib/services/NavService'; +import { runtime } from './newNote'; +import { setupDatabaseAndSynchronizer, switchClient } from '@joplin/lib/testing/test-utils'; +import Note from '@joplin/lib/models/Note'; +import Folder from '@joplin/lib/models/Folder'; +import Setting from '@joplin/lib/models/Setting'; + +describe('newNote', () => { + beforeEach(async () => { + await setupDatabaseAndSynchronizer(1); + await switchClient(1); + }); + test('should create and navigate to a new note', async () => { + const dispatchMock = jest.fn(); + NavService.dispatch = dispatchMock; + + // The command needs an active folder ID. + const activeFolder = await Folder.save({ title: 'folder' }); + Setting.setValue('activeFolderId', activeFolder.id); + + await runtime().execute(null, 'test note', true); + expect(dispatchMock).toHaveBeenCalledTimes(1); + + // Correct note should have been created + const noteId = dispatchMock.mock.lastCall[0].noteId; + expect(await Note.load(noteId)).toMatchObject({ body: 'test note', parent_id: activeFolder.id }); + + // Should have tried to navigate to the note. + expect(dispatchMock.mock.lastCall).toMatchObject([ + { noteId: noteId, noteHash: '' }, + ]); + }); +}); diff --git a/packages/app-mobile/commands/newNote.ts b/packages/app-mobile/commands/newNote.ts new file mode 100644 index 000000000..ef5560c99 --- /dev/null +++ b/packages/app-mobile/commands/newNote.ts @@ -0,0 +1,32 @@ +import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService'; +import Logger from '@joplin/utils/Logger'; +import goToNote from './util/goToNote'; +import Note from '@joplin/lib/models/Note'; +import Setting from '@joplin/lib/models/Setting'; + +const logger = Logger.create('newNoteCommand'); + +export const declaration: CommandDeclaration = { + name: 'newNote', +}; + +export const runtime = (): CommandRuntime => { + return { + execute: async (_context: CommandContext, body = '', todo = false) => { + const folderId = Setting.value('activeFolderId'); + if (!folderId) { + logger.warn('Not creating new note -- no active folder ID.'); + return; + } + + const note = await Note.save({ + body, + parent_id: folderId, + is_todo: todo ? 1 : 0, + }, { provisional: true }); + + logger.info(`Navigating to note ${note.id}`); + await goToNote(note.id, ''); + }, + }; +};