mirror of
https://github.com/zadam/trilium.git
synced 2025-02-24 15:05:31 +08:00
template subtree is now deep-duplicated on template assignment
This commit is contained in:
parent
314e0a453f
commit
1047aecfbd
2 changed files with 38 additions and 15 deletions
|
@ -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,18 +58,22 @@ 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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue