Skip to content

Build your personal knowledge base with Trilium Notes

avoid resorting children on every child add, fixes #1480

zadam4633c68a0c5e
diff --git a/src/public/app/entities/note_short.js b/src/public/app/entities/note_short.js
index 8f6b2260d5..d339d63a39 100644
--- a/src/public/app/entities/note_short.js
+++ b/src/public/app/entities/note_short.js
@@ -75,14 +75,16 @@ class NoteShort {
this.parentToBranch[parentNoteId] = branchId;
}
- addChild(childNoteId, branchId) {
+ addChild(childNoteId, branchId, sort = true) {
if (!this.children.includes(childNoteId)) {
this.children.push(childNoteId);
}
this.childToBranch[childNoteId] = branchId;
- this.sortChildren();
+ if (sort) {
+ this.sortChildren();
+ }
}
sortChildren() {
diff --git a/src/public/app/services/tree_cache.js b/src/public/app/services/tree_cache.js
index e3649d18fd..22c332ddd8 100644
--- a/src/public/app/services/tree_cache.js
+++ b/src/public/app/services/tree_cache.js
@@ -87,6 +87,8 @@ class TreeCache {
const branchRows = resp.branches;
const attributeRows = resp.attributes;
+ const noteIdsToSort = new Set();
+
for (const noteRow of noteRows) {
const {noteId} = noteRow;
@@ -153,7 +155,9 @@ class TreeCache {
const parentNote = this.notes[branch.parentNoteId];
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) {
diff --git a/src/routes/api/tree.js b/src/routes/api/tree.js
index 57e1ad915f..e6b810834a 100644
--- a/src/routes/api/tree.js
+++ b/src/routes/api/tree.js
@@ -57,7 +57,7 @@ function getTree(req) {
const noteIds = sql.getColumn(`
WITH RECURSIVE
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
SELECT branches.noteId, branches.isExpanded FROM branches
JOIN treeWithDescendants ON branches.parentNoteId = treeWithDescendants.noteId