From b204014a114b54f6b9d7189c510e389579e4c238 Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 31 May 2022 23:27:45 +0200 Subject: [PATCH] create note from template WIP --- src/public/app/services/note_create.js | 3 ++- src/public/app/services/tree_context_menu.js | 13 +++++++++---- src/services/notes.js | 8 ++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/public/app/services/note_create.js b/src/public/app/services/note_create.js index 0132f438a..1357406af 100644 --- a/src/public/app/services/note_create.js +++ b/src/public/app/services/note_create.js @@ -43,7 +43,8 @@ async function createNote(parentNotePath, options = {}) { content: options.content || "", isProtected: options.isProtected, type: options.type, - mime: options.mime + mime: options.mime, + templateNoteId: options.templateNoteId }); if (options.saveSelection && utils.isCKEditorInitialized()) { diff --git a/src/public/app/services/tree_context_menu.js b/src/public/app/services/tree_context_menu.js index 60003dd0a..30dc5821d 100644 --- a/src/public/app/services/tree_context_menu.js +++ b/src/public/app/services/tree_context_menu.js @@ -48,7 +48,10 @@ class TreeContextMenu { for (const templateNote of templateNotes) { items.push({ title: templateNote.title, - uiIcon: templateNote.getIcon() + uiIcon: templateNote.getIcon(), + command: command, + type: templateNote.type, + templateNoteId: templateNote.noteId }); } } @@ -127,7 +130,7 @@ class TreeContextMenu { ].filter(row => row !== null); } - async selectMenuItemHandler({command, type}) { + async selectMenuItemHandler({command, type, templateNoteId}) { const notePath = treeService.getNotePath(this.node); if (command === 'openInTab') { @@ -141,7 +144,8 @@ class TreeContextMenu { target: 'after', targetBranchId: this.node.data.branchId, type: type, - isProtected: isProtected + isProtected: isProtected, + templateNoteId: templateNoteId }); } else if (command === "insertChildNote") { @@ -149,7 +153,8 @@ class TreeContextMenu { noteCreateService.createNote(parentNotePath, { type: type, - isProtected: this.node.data.isProtected + isProtected: this.node.data.isProtected, + templateNoteId: templateNoteId }); } else if (command === 'openNoteInSplit') { diff --git a/src/services/notes.js b/src/services/notes.js index 0a23c72eb..8168dc0de 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -155,6 +155,14 @@ function createNewNote(params) { scanForLinks(note); + if (params.templateNoteId) { + if (!becca.getNote(params.templateNoteId)) { + throw new Error(`Template note '${params.templateNoteId}' does not exist.`); + } + + note.addRelation('template', params.templateNoteId); + } + copyChildAttributes(parentNote, note); triggerNoteTitleChanged(note);