2020-11-07 17:59:37 +02:00
|
|
|
import { utils, CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import Note from '@joplin/lib/models/Note';
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const declaration: CommandDeclaration = {
|
2020-07-03 23:32:39 +02:00
|
|
|
name: 'newNote',
|
|
|
|
label: () => _('New note'),
|
|
|
|
iconName: 'fa-file',
|
|
|
|
};
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const runtime = (): CommandRuntime => {
|
2020-07-03 23:32:39 +02:00
|
|
|
return {
|
2023-06-30 10:11:26 +02:00
|
|
|
execute: async (_context: CommandContext, body = '', isTodo = false) => {
|
2020-07-03 23:32:39 +02:00
|
|
|
const folderId = Setting.value('activeFolderId');
|
|
|
|
if (!folderId) return;
|
|
|
|
|
|
|
|
const defaultValues = Note.previewFieldsWithDefaultValues({ includeTimestamps: false });
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
let newNote = { ...defaultValues, parent_id: folderId,
|
2020-07-03 23:32:39 +02:00
|
|
|
is_todo: isTodo ? 1 : 0,
|
2023-06-01 13:02:36 +02:00
|
|
|
body: body };
|
2020-07-03 23:32:39 +02:00
|
|
|
|
|
|
|
newNote = await Note.save(newNote, { provisional: true });
|
|
|
|
|
2021-02-01 23:44:13 +02:00
|
|
|
void Note.updateGeolocation(newNote.id);
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
utils.store.dispatch({
|
|
|
|
type: 'NOTE_SELECT',
|
|
|
|
id: newNote.id,
|
|
|
|
});
|
2024-01-28 19:57:41 +02:00
|
|
|
|
|
|
|
// Immediately sort the note list so that the new note is positioned correctly before
|
|
|
|
// scrolling to it.
|
|
|
|
utils.store.dispatch({
|
|
|
|
type: 'NOTE_SORT',
|
|
|
|
});
|
2020-07-03 23:32:39 +02:00
|
|
|
},
|
2023-07-16 18:42:42 +02:00
|
|
|
enabledCondition: 'oneFolderSelected && !inConflictFolder && !folderIsReadOnly',
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
};
|