mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	template subtree is now deep-duplicated on template assignment
This commit is contained in:
		
							parent
							
								
									314e0a453f
								
							
						
					
					
						commit
						1047aecfbd
					
				| @ -1,7 +1,7 @@ | |||||||
| const eventService = require('./events'); | const eventService = require('./events'); | ||||||
| const scriptService = require('./script'); | const scriptService = require('./script'); | ||||||
| const treeService = require('./tree'); | const treeService = require('./tree'); | ||||||
| const log = require('./log'); | const noteService = require('./notes'); | ||||||
| const repository = require('./repository'); | const repository = require('./repository'); | ||||||
| const Attribute = require('../entities/attribute'); | const Attribute = require('../entities/attribute'); | ||||||
| 
 | 
 | ||||||
| @ -58,18 +58,22 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) => | |||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             const targetNote = repository.getNote(entity.value); |             const templateNote = repository.getNote(entity.value); | ||||||
| 
 | 
 | ||||||
|             if (!targetNote || !targetNote.isStringNote()) { |             if (!templateNote) { | ||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             const targetNoteContent = targetNote.getContent(); |             if (templateNote.isStringNote()) { | ||||||
|  |                 const templateNoteContent = templateNote.getContent(); | ||||||
| 
 | 
 | ||||||
|             if (targetNoteContent) { |                 if (templateNoteContent) { | ||||||
|                 note.setContent(targetNoteContent); |                     note.setContent(templateNoteContent); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  | 
 | ||||||
|  |             noteService.duplicateSubtreeWithoutRoot(templateNote.noteId, note.noteId); | ||||||
|  |         } | ||||||
|         else if (entity.type === 'label' && entity.name === 'sorted') { |         else if (entity.type === 'label' && entity.name === 'sorted') { | ||||||
|             treeService.sortNotesAlphabetically(entity.noteId); |             treeService.sortNotesAlphabetically(entity.noteId); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -726,21 +726,16 @@ function replaceByMap(str, mapObj) { | |||||||
|     return str.replace(re, matched => mapObj[matched]); |     return str.replace(re, matched => mapObj[matched]); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function duplicateSubtree(noteId, newParentNoteId) { | function duplicateSubtree(origNoteId, newParentNoteId) { | ||||||
|     if (noteId === 'root') { |     if (origNoteId === 'root') { | ||||||
|         throw new Error('Duplicating root is not possible'); |         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
 |     // might be null if orig note is not in the target newParentNoteId
 | ||||||
|     const origBranch = origNote.getBranches().find(branch => branch.parentNoteId === newParentNoteId); |     const origBranch = origNote.getBranches().find(branch => branch.parentNoteId === newParentNoteId); | ||||||
| 
 | 
 | ||||||
|     const noteIdMapping = {}; |     const noteIdMapping = getNoteIdMapping(origNote); | ||||||
| 
 |  | ||||||
|     // 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 res = duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping); |     const res = duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping); | ||||||
| 
 | 
 | ||||||
| @ -750,6 +745,19 @@ function duplicateSubtree(noteId, newParentNoteId) { | |||||||
|     return res; |     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) { | function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping) { | ||||||
|     if (origNote.isProtected && !protectedSessionService.isProtectedSessionAvailable()) { |     if (origNote.isProtected && !protectedSessionService.isProtectedSessionAvailable()) { | ||||||
|         throw new Error(`Cannot duplicate note=${origNote.noteId} because it is protected and protected session is not available`); |         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(() => { | sqlInit.dbReady.then(() => { | ||||||
|     // first cleanup kickoff 5 minutes after startup
 |     // first cleanup kickoff 5 minutes after startup
 | ||||||
|     setTimeout(cls.wrap(eraseDeletedNotes), 5 * 60 * 1000); |     setTimeout(cls.wrap(eraseDeletedNotes), 5 * 60 * 1000); | ||||||
| @ -818,6 +836,7 @@ module.exports = { | |||||||
|     protectNoteRecursively, |     protectNoteRecursively, | ||||||
|     scanForLinks, |     scanForLinks, | ||||||
|     duplicateSubtree, |     duplicateSubtree, | ||||||
|  |     duplicateSubtreeWithoutRoot, | ||||||
|     getUndeletedParentBranches, |     getUndeletedParentBranches, | ||||||
|     triggerNoteTitleChanged |     triggerNoteTitleChanged | ||||||
| }; | }; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam