mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	avoid resorting children on every child add, fixes #1480
This commit is contained in:
		| @@ -75,15 +75,17 @@ class NoteShort { | |||||||
|         this.parentToBranch[parentNoteId] = branchId; |         this.parentToBranch[parentNoteId] = branchId; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     addChild(childNoteId, branchId) { |     addChild(childNoteId, branchId, sort = true) { | ||||||
|         if (!this.children.includes(childNoteId)) { |         if (!this.children.includes(childNoteId)) { | ||||||
|             this.children.push(childNoteId); |             this.children.push(childNoteId); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         this.childToBranch[childNoteId] = branchId; |         this.childToBranch[childNoteId] = branchId; | ||||||
|  |  | ||||||
|  |         if (sort) { | ||||||
|             this.sortChildren(); |             this.sortChildren(); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     sortChildren() { |     sortChildren() { | ||||||
|         const branchIdPos = {}; |         const branchIdPos = {}; | ||||||
|   | |||||||
| @@ -87,6 +87,8 @@ class TreeCache { | |||||||
|         const branchRows = resp.branches; |         const branchRows = resp.branches; | ||||||
|         const attributeRows = resp.attributes; |         const attributeRows = resp.attributes; | ||||||
|  |  | ||||||
|  |         const noteIdsToSort = new Set(); | ||||||
|  |  | ||||||
|         for (const noteRow of noteRows) { |         for (const noteRow of noteRows) { | ||||||
|             const {noteId} = noteRow; |             const {noteId} = noteRow; | ||||||
|  |  | ||||||
| @@ -153,7 +155,9 @@ class TreeCache { | |||||||
|             const parentNote = this.notes[branch.parentNoteId]; |             const parentNote = this.notes[branch.parentNoteId]; | ||||||
|  |  | ||||||
|             if (parentNote) { |             if (parentNote) { | ||||||
|                 parentNote.addChild(branch.noteId, branch.branchId); |                 parentNote.addChild(branch.noteId, branch.branchId, false); | ||||||
|  |  | ||||||
|  |                 noteIdsToSort.add(parentNote.noteId); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -178,6 +182,11 @@ class TreeCache { | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         // sort all of them at once, this avoids repeated sorts (#1480) | ||||||
|  |         for (const noteId of noteIdsToSort) { | ||||||
|  |             this.notes[noteId].sortChildren(); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async reloadNotes(noteIds) { |     async reloadNotes(noteIds) { | ||||||
|   | |||||||
| @@ -57,7 +57,7 @@ function getTree(req) { | |||||||
|     const noteIds = sql.getColumn(` |     const noteIds = sql.getColumn(` | ||||||
|         WITH RECURSIVE |         WITH RECURSIVE | ||||||
|             treeWithDescendants(noteId, isExpanded) AS ( |             treeWithDescendants(noteId, isExpanded) AS ( | ||||||
|                 SELECT noteId, 1 FROM branches WHERE parentNoteId = ? AND isDeleted = 0 |                 SELECT noteId, isExpanded FROM branches WHERE parentNoteId = ? AND isDeleted = 0 | ||||||
|                 UNION |                 UNION | ||||||
|                 SELECT branches.noteId, branches.isExpanded FROM branches |                 SELECT branches.noteId, branches.isExpanded FROM branches | ||||||
|                   JOIN treeWithDescendants ON branches.parentNoteId = treeWithDescendants.noteId |                   JOIN treeWithDescendants ON branches.parentNoteId = treeWithDescendants.noteId | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user