Skip to content

Build your personal knowledge base with Trilium Notes

Merge branch 'stable'

zadam29eb5a843531
diff --cc src/services/notes.js
index eba8f5da1b,344a60f62b..effd510978
--- a/src/services/notes.js
+++ b/src/services/notes.js
@@@ -34,82 -49,65 +34,82 @@@ async function triggerNoteTitleChanged(
await eventService.emit(eventService.NOTE_TITLE_CHANGED, note);
}
-/**
- * FIXME: noteData has mandatory property "target", it might be better to add it as parameter to reflect this
- */
-async function createNewNote(parentNoteId, noteData) {
- let newNotePos;
+function deriveMime(type, mime) {
+ if (!type) {
+ throw new Error(`Note type is a required param`);
+ }
- if (noteData.notePosition !== undefined) {
- newNotePos = noteData.notePosition;
+ if (mime) {
+ return mime;
}
- else {
- newNotePos = await getNewNotePosition(parentNoteId, noteData);
+
+ if (type === 'text') {
+ mime = 'text/html';
+ } else if (type === 'code') {
+ mime = 'text/plain';
+ } else if (['relation-map', 'search'].includes(type)) {
+ mime = 'application/json';
+ } else if (type === 'render') {
+ mime = '';
}
- const parentNote = await repository.getNote(parentNoteId);
+ return mime;
+}
- if (!parentNote) {
- throw new Error(`Parent note ${parentNoteId} not found.`);
- }
+async function copyChildAttributes(parentNote, childNote) {
- for (const attr of await parentNote.getAttributes()) {
++ for (const attr of await parentNote.getOwnedAttributes()) {
+ if (attr.name.startsWith("child:")) {
+ await new Attribute({
+ noteId: childNote.noteId,
+ type: attr.type,
+ name: attr.name.substr(6),
+ value: attr.value,
+ position: attr.position,
+ isInheritable: attr.isInheritable
+ }).save();
- if (!noteData.type) {
- if (parentNote.type === 'text' || parentNote.type === 'code') {
- noteData.type = parentNote.type;
- noteData.mime = parentNote.mime;
- }
- else {
- // inheriting note type makes sense only for text and code
- noteData.type = 'text';
- noteData.mime = 'text/html';
+ childNote.invalidateAttributeCache();
}
}
+}
- if (!noteData.mime) {
- if (noteData.type === 'text') {
- noteData.mime = 'text/html';
- }
- else if (noteData.type === 'code') {
- noteData.mime = 'text/plain';
- }
- else if (noteData.type === 'relation-map' || noteData.type === 'search') {
- noteData.mime = 'application/json';
- }
+/**
+ * Following object properties are mandatory:
+ * - {string} parentNoteId
+ * - {string} title
+ * - {*} content
+ * - {string} type - text, code, file, image, search, book, relation-map
+ *
+ * Following are optional (have defaults)
+ * - {string} mime - value is derived from default mimes for type
+ * - {boolean} isProtected - default is false
+ * - {boolean} isExpanded - default is false
+ * - {string} prefix - default is empty string
+ * - {integer} notePosition - default is last existing notePosition in a parent + 10
+ *
+ * @param params
+ * @return {Promise<{note: Note, branch: Branch}>}
+ */
+async function createNewNote(params) {
+ const parentNote = await repository.getNote(params.parentNoteId);
+
+ if (!parentNote) {
+ throw new Error(`Parent note ${params.parentNoteId} not found.`);
}
- noteData.type = noteData.type || parentNote.type;
- noteData.mime = noteData.mime || parentNote.mime;
+ if (!params.title || params.title.trim().length === 0) {
+ throw new Error(`Note title must not be empty`);
+ }
const note = await new Note({
- noteId: noteData.noteId, // optionally can force specific noteId
- title: noteData.title,
- isProtected: noteData.isProtected,
- type: noteData.type || 'text',
- mime: noteData.mime || 'text/html'
+ noteId: params.noteId, // optionally can force specific noteId
+ title: params.title,
+ isProtected: !!params.isProtected,
+ type: params.type,
+ mime: deriveMime(params.type, params.mime)
}).save();
- if (note.isStringNote() || this.type === 'render') { // render to just make sure it's not null
- noteData.content = noteData.content || "";
- }
-
- await note.setContent(noteData.content);
+ await note.setContent(params.content);
const branch = await new Branch({
noteId: note.noteId,
diff --cc src/tools/generate_document.js
index bf250ca77a,80c2b63184..2d0613755e
--- a/src/tools/generate_document.js
+++ b/src/tools/generate_document.js
@@@ -50,12 -34,7 +34,12 @@@ async function start()
const content = loremIpsum({ count: paragraphCount, units: 'paragraphs', sentenceLowerBound: 1, sentenceUpperBound: 15,
paragraphLowerBound: 3, paragraphUpperBound: 10, format: 'html' });
- const {note} = await noteService.createNote(getRandomNoteId(), title, content);
+ const {note} = await noteService.createNewNote({
- parentNoteId: getRandomParentNoteId(),
++ parentNoteId: getRandomNoteId(),
+ title,
+ content,
+ type: 'text'
+ });
console.log(`Created note ${i}: ${title}`);