Skip to content

Build your personal knowledge base with Trilium Notes

refactoring of note creation APIs WIP

zadam60231de0edea
diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js
index dbbaa84be2..e938f188aa 100644
--- a/src/public/javascripts/services/tree.js
+++ b/src/public/javascripts/services/tree.js
@@ -649,11 +649,9 @@ async function createNote(node, parentNoteId, target, extraOptions = {}) {
const newNoteName = extraOptions.title || "new note";
- const {note, branch} = await server.post('notes/' + parentNoteId + '/children', {
+ const {note, branch} = await server.post(`notes/${parentNoteId}/children?target=${target}&targetBranchId=${node.data.branchId}`, {
title: newNoteName,
content: extraOptions.content,
- target: target,
- target_branchId: node.data.branchId,
isProtected: extraOptions.isProtected,
type: extraOptions.type
});
diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js
index 2107760740..330bf8182a 100644
--- a/src/routes/api/notes.js
+++ b/src/routes/api/notes.js
@@ -56,7 +56,9 @@ async function createNote(req) {
const params = Object.assign({}, req.body); // clone
params.parentNoteId = req.params.parentNoteId;
- const { note, branch } = await noteService.createNewNote(params);
+ const { target, targetBranchId } = req.query;
+
+ const { note, branch } = await noteService.createNewNoteWithTarget(target, targetBranchId, params);
note.cssClass = (await note.getLabels("cssClass")).map(label => label.value).join(" ");
diff --git a/src/services/notes.js b/src/services/notes.js
index 4cd3e081d5..7f2f89045e 100644
--- a/src/services/notes.js
+++ b/src/services/notes.js
@@ -76,7 +76,7 @@ async function copyChildAttributes(parentNote, childNote) {
* - {string} parentNoteId
* - {string} title
* - {*} content
- * - {string} type
+ * - {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
@@ -128,6 +128,28 @@ async function createNewNote(params) {
};
}
+async function createNewNoteWithTarget(target, targetBranchId, params) {
+ if (target === 'into') {
+ return await createNewNote(params);
+ }
+ else if (target === 'after') {
+ const afterNote = await sql.getRow('SELECT notePosition FROM branches WHERE branchId = ?', [noteData.target_branchId]);
+
+ // not updating utcDateModified to avoig having to sync whole rows
+ await sql.execute('UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0',
+ [params.parentNoteId, afterNote.notePosition]);
+
+ params.notePosition = afterNote.notePosition + 10;
+
+ await createNewNote(params);
+
+ await syncTableService.addNoteReorderingSync(params.parentNoteId);
+ }
+ else {
+ throw new Error(`Unknown target ${target}`);
+ }
+}
+
// methods below should be probably just backend API methods
async function createJsonNote(parentNoteId, title, content = {}, params = {}) {
params.parentNoteId = parentNoteId;
@@ -512,6 +534,7 @@ sqlInit.dbReady.then(() => {
module.exports = {
createNewNote,
+ createNewNoteWithTarget,
updateNote,
deleteBranch,
protectNoteRecursively,