1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-02 12:47:41 +02:00

Mobile: Plugin API: Implement the newNote command (#10524)

This commit is contained in:
Henry Heino 2024-06-04 01:52:52 -07:00 committed by GitHub
parent 0938dc9d52
commit f94c16b22e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 71 additions and 0 deletions

View File

@ -503,6 +503,8 @@ packages/app-desktop/utils/restartInSafeModeFromMain.test.js
packages/app-desktop/utils/restartInSafeModeFromMain.js packages/app-desktop/utils/restartInSafeModeFromMain.js
packages/app-mobile/PluginAssetsLoader.js packages/app-mobile/PluginAssetsLoader.js
packages/app-mobile/commands/index.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/openItem.js
packages/app-mobile/commands/openNote.js packages/app-mobile/commands/openNote.js
packages/app-mobile/commands/scrollToHash.js packages/app-mobile/commands/scrollToHash.js

2
.gitignore vendored
View File

@ -482,6 +482,8 @@ packages/app-desktop/utils/restartInSafeModeFromMain.test.js
packages/app-desktop/utils/restartInSafeModeFromMain.js packages/app-desktop/utils/restartInSafeModeFromMain.js
packages/app-mobile/PluginAssetsLoader.js packages/app-mobile/PluginAssetsLoader.js
packages/app-mobile/commands/index.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/openItem.js
packages/app-mobile/commands/openNote.js packages/app-mobile/commands/openNote.js
packages/app-mobile/commands/scrollToHash.js packages/app-mobile/commands/scrollToHash.js

View File

@ -1,9 +1,11 @@
// AUTO-GENERATED using `gulp buildScriptIndexes` // AUTO-GENERATED using `gulp buildScriptIndexes`
import * as newNote from './newNote';
import * as openItem from './openItem'; import * as openItem from './openItem';
import * as openNote from './openNote'; import * as openNote from './openNote';
import * as scrollToHash from './scrollToHash'; import * as scrollToHash from './scrollToHash';
const index: any[] = [ const index: any[] = [
newNote,
openItem, openItem,
openNote, openNote,
scrollToHash, scrollToHash,

View File

@ -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: '' },
]);
});
});

View File

@ -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, '');
},
};
};