From 1047aecfbdc7527b461a53d0dba978f0b4751dd1 Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 19 Nov 2020 14:29:26 +0100 Subject: [PATCH] template subtree is now deep-duplicated on template assignment --- src/services/handlers.js | 16 ++++++++++------ src/services/notes.js | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/services/handlers.js b/src/services/handlers.js index 035a027e1..08fc66577 100644 --- a/src/services/handlers.js +++ b/src/services/handlers.js @@ -1,7 +1,7 @@ const eventService = require('./events'); const scriptService = require('./script'); const treeService = require('./tree'); -const log = require('./log'); +const noteService = require('./notes'); const repository = require('./repository'); const Attribute = require('../entities/attribute'); @@ -58,17 +58,21 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) => return; } - const targetNote = repository.getNote(entity.value); + const templateNote = repository.getNote(entity.value); - if (!targetNote || !targetNote.isStringNote()) { + if (!templateNote) { return; } - const targetNoteContent = targetNote.getContent(); + if (templateNote.isStringNote()) { + const templateNoteContent = templateNote.getContent(); - if (targetNoteContent) { - note.setContent(targetNoteContent); + if (templateNoteContent) { + note.setContent(templateNoteContent); + } } + + noteService.duplicateSubtreeWithoutRoot(templateNote.noteId, note.noteId); } else if (entity.type === 'label' && entity.name === 'sorted') { treeService.sortNotesAlphabetically(entity.noteId); diff --git a/src/services/notes.js b/src/services/notes.js index 567211692..3f4eddf56 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -726,21 +726,16 @@ function replaceByMap(str, mapObj) { return str.replace(re, matched => mapObj[matched]); } -function duplicateSubtree(noteId, newParentNoteId) { - if (noteId === 'root') { +function duplicateSubtree(origNoteId, newParentNoteId) { + if (origNoteId === 'root') { throw new Error('Duplicating root is not possible'); } - const origNote = repository.getNote(noteId); + const origNote = repository.getNote(origNoteId); // might be null if orig note is not in the target newParentNoteId const origBranch = origNote.getBranches().find(branch => branch.parentNoteId === newParentNoteId); - const noteIdMapping = {}; - - // pregenerate new noteIds since we'll need to fix relation references even for not yet created notes - for (const origNoteId of origNote.getDescendantNoteIds()) { - noteIdMapping[origNoteId] = utils.newEntityId(); - } + const noteIdMapping = getNoteIdMapping(origNote); const res = duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping); @@ -750,6 +745,19 @@ function duplicateSubtree(noteId, newParentNoteId) { return res; } +function duplicateSubtreeWithoutRoot(origNoteId, newNoteId) { + if (origNoteId === 'root') { + throw new Error('Duplicating root is not possible'); + } + + const origNote = repository.getNote(origNoteId); + const noteIdMapping = getNoteIdMapping(origNote); + + for (const childBranch of origNote.getChildBranches()) { + duplicateSubtreeInner(childBranch.getNote(), childBranch, newNoteId, noteIdMapping); + } +} + function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping) { if (origNote.isProtected && !protectedSessionService.isProtectedSessionAvailable()) { throw new Error(`Cannot duplicate note=${origNote.noteId} because it is protected and protected session is not available`); @@ -802,6 +810,16 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp }; } +function getNoteIdMapping(origNote) { + const noteIdMapping = {}; + + // pregenerate new noteIds since we'll need to fix relation references even for not yet created notes + for (const origNoteId of origNote.getDescendantNoteIds()) { + noteIdMapping[origNoteId] = utils.newEntityId(); + } + return noteIdMapping; +} + sqlInit.dbReady.then(() => { // first cleanup kickoff 5 minutes after startup setTimeout(cls.wrap(eraseDeletedNotes), 5 * 60 * 1000); @@ -818,6 +836,7 @@ module.exports = { protectNoteRecursively, scanForLinks, duplicateSubtree, + duplicateSubtreeWithoutRoot, getUndeletedParentBranches, triggerNoteTitleChanged };