mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 15:11:31 +08:00 
			
		
		
		
	improved template code with better heuristics on when to copy things from the template
This commit is contained in:
		
							parent
							
								
									4606e8d118
								
							
						
					
					
						commit
						ca77211b38
					
				@ -53,23 +53,19 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
 | 
				
			|||||||
        if (entity.type === 'relation' && entity.name === 'template') {
 | 
					        if (entity.type === 'relation' && entity.name === 'template') {
 | 
				
			||||||
            const note = repository.getNote(entity.noteId);
 | 
					            const note = repository.getNote(entity.noteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (!["text", "code"].includes(note.type)) {
 | 
					 | 
				
			||||||
                return;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            const content = note.getContent();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (content && content.trim().length > 0) {
 | 
					 | 
				
			||||||
                return;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            const templateNote = repository.getNote(entity.value);
 | 
					            const templateNote = repository.getNote(entity.value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (!templateNote) {
 | 
					            if (!templateNote) {
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (templateNote.isStringNote()) {
 | 
					            const content = note.getContent();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (!["text", "code"].includes(note.type)
 | 
				
			||||||
 | 
					                // if the note has already content we're not going to overwrite it with template's one
 | 
				
			||||||
 | 
					                && (!content || content.trim().length === 0)
 | 
				
			||||||
 | 
					                && templateNote.isStringNote()) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                const templateNoteContent = templateNote.getContent();
 | 
					                const templateNoteContent = templateNote.getContent();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (templateNoteContent) {
 | 
					                if (templateNoteContent) {
 | 
				
			||||||
@ -81,8 +77,12 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
 | 
				
			|||||||
                note.save();
 | 
					                note.save();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // we'll copy the children notes only if there's none so far
 | 
				
			||||||
 | 
					            // this protects against e.g. multiple assignment of template relation resulting in having multiple copies of the subtree
 | 
				
			||||||
 | 
					            if (note.getChildNotes().length === 0 && !note.isDescendantOfNote(templateNote.noteId)) {
 | 
				
			||||||
                noteService.duplicateSubtreeWithoutRoot(templateNote.noteId, note.noteId);
 | 
					                noteService.duplicateSubtreeWithoutRoot(templateNote.noteId, note.noteId);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        else if (entity.type === 'label' && entity.name === 'sorted') {
 | 
					        else if (entity.type === 'label' && entity.name === 'sorted') {
 | 
				
			||||||
            treeService.sortNotesByTitle(entity.noteId);
 | 
					            treeService.sortNotesByTitle(entity.noteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user