Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(sort): re-sort when a child becomes a folder or stops being one
Elian Dorandafb02199c5d
diff --git a/packages/trilium-core/src/services/handlers.spec.ts b/packages/trilium-core/src/services/handlers.spec.tsindex b8c53fc53f..2033598835 100644--- a/packages/trilium-core/src/services/handlers.spec.ts+++ b/packages/trilium-core/src/services/handlers.spec.ts@@ -193,6 +193,49 @@ describe("handlers", () => {});});+ describe("folder re-sorting", () => {+ it("re-sorts the parents grouping folders when a note gains or loses its only child", () => {+ // The becca loader removes a deleted branch from its parent's children only once loaded.+ becca.loaded = true;+ buildNote({+ id: "fd-ff", "#sorted": "", "#sortFoldersFirst": "", children: [{ id: "fd-note" }]+ });+ buildNote({ id: "fd-key", "#sorted": "folders last, title" });+ buildNote({ id: "fd-plain", "#sorted": "title" });+ for (const parentNoteId of ["fd-key", "fd-plain"]) {+ new BBranch({ noteId: "fd-note", parentNoteId, branchId: `${parentNoteId}_fd-note` });+ }+ buildNote({ id: "fd-c1" });+ buildNote({ id: "fd-c2" });++ const first = new BBranch({+ noteId: "fd-c1", parentNoteId: "fd-note", branchId: "fd-note_fd-c1"+ });+ eventService.emit(eventService.ENTITY_CREATED, { entityName: "branches", entity: first });+ expect(sortNotesIfNeeded).toHaveBeenCalledWith("fd-ff");+ expect(sortNotesIfNeeded).toHaveBeenCalledWith("fd-key");+ expect(sortNotesIfNeeded).not.toHaveBeenCalledWith("fd-plain");+ sortNotesIfNeeded.mockClear();++ // A second child leaves `fd-note` a folder, so its parents keep their order.+ const second = new BBranch({+ noteId: "fd-c2", parentNoteId: "fd-note", branchId: "fd-note_fd-c2"+ });+ eventService.emit(eventService.ENTITY_CREATED, { entityName: "branches", entity: second });+ eventService.emit(eventService.ENTITY_DELETED, {+ entityName: "branches", entityId: second.branchId, entity: second+ });+ expect(sortNotesIfNeeded).not.toHaveBeenCalledWith("fd-ff");++ eventService.emit(eventService.ENTITY_DELETED, {+ entityName: "branches", entityId: first.branchId, entity: first+ });+ expect(sortNotesIfNeeded).toHaveBeenCalledWith("fd-ff");+ expect(sortNotesIfNeeded).toHaveBeenCalledWith("fd-key");+ expect(sortNotesIfNeeded).not.toHaveBeenCalledWith("fd-plain");+ });+ });+describe("ENTITY_DELETED", () => {it("reschedules a hidden-subtree check when a system ('_') note is deleted", () => {const sysNote = buildNote({ id: "_systemDeleted", title: "sys" });diff --git a/packages/trilium-core/src/services/handlers.ts b/packages/trilium-core/src/services/handlers.tsindex 589ae6ba56..230a7514cb 100644--- a/packages/trilium-core/src/services/handlers.ts+++ b/packages/trilium-core/src/services/handlers.ts@@ -138,6 +138,8 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>if (entity.parentNote?.hasLabel("sorted")) {treeService.sortNotesIfNeeded(entity.parentNoteId);}++ sortParentsGroupingFolders(entity.parentNoteId, 1);} else if (entityName === "notes") {runAttachedRelations(entity, "runOnNoteCreation", entity);}@@ -213,6 +215,28 @@ function handleMaybeSortingLabel(entity: BAttribute) {}}+/**+ * Re-sorts the parents that group folders once the note `noteId` has `childCount` children, that+ * is, once it has just become a folder or stopped being one.+ */+function sortParentsGroupingFolders(noteId: string, childCount: number) {+ const note = becca.notes[noteId];+ if (!note || note.children.length !== childCount) {+ return;+ }++ for (const parentNote of note.getParentNotes()) {+ const sorted = parentNote.getLabelValue("sorted");+ const groupsFolders = sorted !== null && (+ parentNote.isLabelTruthy("sortFoldersFirst")+ || parseSortCriteria(sorted).some(({ key, isLabel }) => !isLabel && key === "folders")+ );+ if (groupsFolders) {+ treeService.sortNotesIfNeeded(parentNote.noteId);+ }+ }+}+function sortsByLabel(sorted: string, labelName: string) {return labelName === "top"|| labelName === "bottom"@@ -254,6 +278,7 @@ eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) =>if (entityName === "branches") {runAttachedRelations(entity.getNote(), "runOnBranchDeletion", entity);+ sortParentsGroupingFolders(entity.parentNoteId, 0);}if (entityName === "notes" && entity.noteId.startsWith("_")) {