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:
parent
0938dc9d52
commit
f94c16b22e
@ -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
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -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
|
||||
|
@ -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,
|
||||
|
33
packages/app-mobile/commands/newNote.test.ts
Normal file
33
packages/app-mobile/commands/newNote.test.ts
Normal 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: '' },
|
||||
]);
|
||||
});
|
||||
});
|
32
packages/app-mobile/commands/newNote.ts
Normal file
32
packages/app-mobile/commands/newNote.ts
Normal 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, '');
|
||||
},
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user