From a863da1dcea9ae248a00d5139d9bb2f4904b06db Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 6 Jan 2023 15:07:18 +0100 Subject: [PATCH] becca should also keep correct branch order, e.g. for export, #2346 --- src/becca/becca_loader.js | 12 +++++++++++- src/becca/entities/bnote.js | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/becca/becca_loader.js b/src/becca/becca_loader.js index 8af9dd656..c1294cdc7 100644 --- a/src/becca/becca_loader.js +++ b/src/becca/becca_loader.js @@ -34,7 +34,11 @@ function load() { new BNote().update(row).init(); } - for (const row of sql.getRawRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`)) { + const branchRows = sql.getRawRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`); + // in-memory sort is faster than in the DB + branchRows.sort((a, b) => a.notePosition - b.notePosition); + + for (const row of branchRows) { new BBranch().update(row).init(); } @@ -164,6 +168,12 @@ function branchUpdated(branch) { childNote.flatTextCache = null; childNote.sortParents(); } + + const parentNote = becca.notes[branch.parentNoteId]; + + if (parentNote) { + parentNote.sortChildren(); + } } function attributeDeleted(attributeId) { diff --git a/src/becca/entities/bnote.js b/src/becca/entities/bnote.js index 1e34315dd..3aa685b95 100644 --- a/src/becca/entities/bnote.js +++ b/src/becca/entities/bnote.js @@ -730,6 +730,21 @@ class BNote extends AbstractBeccaEntity { .filter(note => !!note); } + sortChildren() { + if (this.children.length === 0) { + return; + } + + const becca = this.becca; + + this.children.sort((a, b) => { + const aBranch = becca.getBranchFromChildAndParent(a.noteId, this.noteId); + const bBranch = becca.getBranchFromChildAndParent(b.noteId, this.noteId); + + return aBranch?.notePosition < bBranch?.notePosition ? -1 : 1; + }); + } + /** * This is used for: * - fast searching