Skip to content

Build your personal knowledge base with Trilium Notes

fix(tree): #top/#bottom reversed in desc order (closes #7716)

Elian Doranc9f648fcb8c4
diff --git a/apps/server/src/services/tree.spec.ts b/apps/server/src/services/tree.spec.ts
index e7538ce693..1efd9acbeb 100644
--- a/apps/server/src/services/tree.spec.ts
+++ b/apps/server/src/services/tree.spec.ts
@@ -5,6 +5,7 @@ import BBranch from "../becca/entities/bbranch.js";
import BNote from "../becca/entities/bnote.js";
import tree from "./tree.js";
import cls from "./cls.js";
+import { buildNote } from "../test/becca_easy_mocking.js";
describe("Tree", () => {
let rootNote!: NoteBuilder;
@@ -73,4 +74,43 @@ describe("Tree", () => {
expect(order).toStrictEqual(expectedOrder);
}
});
+
+ it("pins to the top and bottom", () => {
+ const note = buildNote({
+ children: [
+ { title: "bottom", "#bottom": "" },
+ { title: "5" },
+ { title: "3" },
+ { title: "2" },
+ { title: "1" },
+ { title: "top", "#top": "" }
+ ],
+ "#sorted": ""
+ });
+ cls.init(() => {
+ tree.sortNotesIfNeeded(note.noteId);
+ });
+ const orderedTitles = note.children.map((child) => child.title);
+ expect(orderedTitles).toStrictEqual([ "top", "1", "2", "3", "5", "bottom" ]);
+ });
+
+ it("pins to the top and bottom in reverse order", () => {
+ const note = buildNote({
+ children: [
+ { title: "bottom", "#bottom": "" },
+ { title: "1" },
+ { title: "2" },
+ { title: "3" },
+ { title: "5" },
+ { title: "top", "#top": "" }
+ ],
+ "#sorted": "",
+ "#sortDirection": "desc"
+ });
+ cls.init(() => {
+ tree.sortNotesIfNeeded(note.noteId);
+ });
+ const orderedTitles = note.children.map((child) => child.title);
+ expect(orderedTitles).toStrictEqual([ "top", "5", "3", "2", "1", "bottom" ]);
+ });
});
diff --git a/apps/server/src/services/tree.ts b/apps/server/src/services/tree.ts
index 6211c5cd02..05c9ecdd9b 100644
--- a/apps/server/src/services/tree.ts
+++ b/apps/server/src/services/tree.ts
@@ -136,8 +136,8 @@ function sortNotes(parentNoteId: string, customSortBy: string = "title", reverse
const topBEl = fetchValue(b, "top");
if (topAEl !== topBEl) {
- if (topAEl === null) return 1;
- if (topBEl === null) return -1;
+ if (topAEl === null) return reverse ? -1 : 1;
+ if (topBEl === null) return reverse ? 1 : -1;
// since "top" should not be reversible, we'll reverse it once more to nullify this effect
return compare(topAEl, topBEl) * (reverse ? -1 : 1);
@@ -147,8 +147,8 @@ function sortNotes(parentNoteId: string, customSortBy: string = "title", reverse
const bottomBEl = fetchValue(b, "bottom");
if (bottomAEl !== bottomBEl) {
- if (bottomAEl === null) return -1;
- if (bottomBEl === null) return 1;
+ if (bottomAEl === null) return reverse ? 1 : -1;
+ if (bottomBEl === null) return reverse ? -1 : 1;
// since "bottom" should not be reversible, we'll reverse it once more to nullify this effect
return compare(bottomBEl, bottomAEl) * (reverse ? -1 : 1);