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-11-07 17:59:37 +02:00
|
|
|
const TemplateUtils = require('@joplin/lib/TemplateUtils');
|
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 {
|
2020-11-12 21:13:28 +02:00
|
|
|
execute: async (_context: CommandContext, template: string = null, isTodo: boolean = false) => {
|
2020-07-03 23:32:39 +02:00
|
|
|
const folderId = Setting.value('activeFolderId');
|
|
|
|
if (!folderId) return;
|
|
|
|
|
|
|
|
const body = template ? TemplateUtils.render(template) : '';
|
|
|
|
|
|
|
|
const defaultValues = Note.previewFieldsWithDefaultValues({ includeTimestamps: false });
|
|
|
|
|
|
|
|
let newNote = Object.assign({}, defaultValues, {
|
|
|
|
parent_id: folderId,
|
|
|
|
is_todo: isTodo ? 1 : 0,
|
|
|
|
body: body,
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
},
|
2020-10-18 22:52:10 +02:00
|
|
|
enabledCondition: 'oneFolderSelected && !inConflictFolder',
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
};
|