mirror of
https://github.com/zadam/trilium.git
synced 2025-12-04 22:44:25 +01:00
Merge a1c03143342078c68301cc7bc51a68ce4840c5dc into a867a25d5f09e410d0c73b7fbeab18b4641f484a
This commit is contained in:
commit
3c5f084031
@ -1,11 +1,11 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import {beforeEach, describe, expect, it, vi} from "vitest";
|
||||||
import { note, NoteBuilder } from "../test/becca_mocking.js";
|
import {note, NoteBuilder} from "../test/becca_mocking.js";
|
||||||
import becca from "../becca/becca.js";
|
import becca from "../becca/becca.js";
|
||||||
import BBranch from "../becca/entities/bbranch.js";
|
import BBranch from "../becca/entities/bbranch.js";
|
||||||
import BNote from "../becca/entities/bnote.js";
|
import BNote from "../becca/entities/bnote.js";
|
||||||
import tree from "./tree.js";
|
import tree from "./tree.js";
|
||||||
import cls from "./cls.js";
|
import cls from "./cls.js";
|
||||||
import { buildNote } from "../test/becca_easy_mocking.js";
|
import {buildNote} from "../test/becca_easy_mocking.js";
|
||||||
|
|
||||||
describe("Tree", () => {
|
describe("Tree", () => {
|
||||||
let rootNote!: NoteBuilder;
|
let rootNote!: NoteBuilder;
|
||||||
@ -48,6 +48,23 @@ describe("Tree", () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it("sorts notes by title (base case)", () => {
|
||||||
|
|
||||||
|
const note = buildNote({
|
||||||
|
children: [
|
||||||
|
{title: "1"},
|
||||||
|
{title: "2"},
|
||||||
|
{title: "3"},
|
||||||
|
],
|
||||||
|
"#sorted": "",
|
||||||
|
});
|
||||||
|
cls.init(() => {
|
||||||
|
tree.sortNotesIfNeeded(note.noteId);
|
||||||
|
});
|
||||||
|
const orderedTitles = note.children.map((child) => child.title);
|
||||||
|
expect(orderedTitles).toStrictEqual(["1", "2", "3"]);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
it("custom sort order is idempotent", () => {
|
it("custom sort order is idempotent", () => {
|
||||||
rootNote.label("sorted", "order");
|
rootNote.label("sorted", "order");
|
||||||
@ -56,13 +73,15 @@ describe("Tree", () => {
|
|||||||
for (let i = 0; i <= 5; i++) {
|
for (let i = 0; i <= 5; i++) {
|
||||||
rootNote.child(note(String(i)).label("order", String(i)));
|
rootNote.child(note(String(i)).label("order", String(i)));
|
||||||
}
|
}
|
||||||
|
rootNote.child(note("top").label("top"));
|
||||||
|
rootNote.child(note("bottom").label("bottom"));
|
||||||
|
|
||||||
// Add a few values which have no defined order.
|
// Add a few values which have no defined order.
|
||||||
for (let i = 6; i < 10; i++) {
|
for (let i = 6; i < 10; i++) {
|
||||||
rootNote.child(note(String(i)));
|
rootNote.child(note(String(i)));
|
||||||
}
|
}
|
||||||
|
|
||||||
const expectedOrder = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ];
|
const expectedOrder = ["top", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "bottom"];
|
||||||
|
|
||||||
// Sort a few times to ensure that the resulting order is the same.
|
// Sort a few times to ensure that the resulting order is the same.
|
||||||
for (let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
@ -78,12 +97,12 @@ describe("Tree", () => {
|
|||||||
it("pins to the top and bottom", () => {
|
it("pins to the top and bottom", () => {
|
||||||
const note = buildNote({
|
const note = buildNote({
|
||||||
children: [
|
children: [
|
||||||
{ title: "bottom", "#bottom": "" },
|
{title: "bottom", "#bottom": ""},
|
||||||
{ title: "5" },
|
{title: "5"},
|
||||||
{ title: "3" },
|
{title: "3"},
|
||||||
{ title: "2" },
|
{title: "2"},
|
||||||
{ title: "1" },
|
{title: "1"},
|
||||||
{ title: "top", "#top": "" }
|
{title: "top", "#top": ""}
|
||||||
],
|
],
|
||||||
"#sorted": ""
|
"#sorted": ""
|
||||||
});
|
});
|
||||||
@ -91,18 +110,18 @@ describe("Tree", () => {
|
|||||||
tree.sortNotesIfNeeded(note.noteId);
|
tree.sortNotesIfNeeded(note.noteId);
|
||||||
});
|
});
|
||||||
const orderedTitles = note.children.map((child) => child.title);
|
const orderedTitles = note.children.map((child) => child.title);
|
||||||
expect(orderedTitles).toStrictEqual([ "top", "1", "2", "3", "5", "bottom" ]);
|
expect(orderedTitles).toStrictEqual(["top", "1", "2", "3", "5", "bottom"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("pins to the top and bottom in reverse order", () => {
|
it("pins to the top and bottom in reverse order", () => {
|
||||||
const note = buildNote({
|
const note = buildNote({
|
||||||
children: [
|
children: [
|
||||||
{ title: "bottom", "#bottom": "" },
|
{title: "bottom", "#bottom": ""},
|
||||||
{ title: "1" },
|
{title: "1"},
|
||||||
{ title: "2" },
|
{title: "2"},
|
||||||
{ title: "3" },
|
{title: "3"},
|
||||||
{ title: "5" },
|
{title: "5"},
|
||||||
{ title: "top", "#top": "" }
|
{title: "top", "#top": ""}
|
||||||
],
|
],
|
||||||
"#sorted": "",
|
"#sorted": "",
|
||||||
"#sortDirection": "desc"
|
"#sortDirection": "desc"
|
||||||
@ -111,6 +130,50 @@ describe("Tree", () => {
|
|||||||
tree.sortNotesIfNeeded(note.noteId);
|
tree.sortNotesIfNeeded(note.noteId);
|
||||||
});
|
});
|
||||||
const orderedTitles = note.children.map((child) => child.title);
|
const orderedTitles = note.children.map((child) => child.title);
|
||||||
expect(orderedTitles).toStrictEqual([ "top", "5", "3", "2", "1", "bottom" ]);
|
expect(orderedTitles).toStrictEqual(["top", "5", "3", "2", "1", "bottom"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("keeps folder notes on top when #sortFolderFirst is set, but not above #top", () => {
|
||||||
|
const note = buildNote({
|
||||||
|
children: [
|
||||||
|
{title: "bottom", "#bottom": ""},
|
||||||
|
{title: "1"},
|
||||||
|
{title: "2"},
|
||||||
|
{title: "p1", children: [{title: "1.1"}, {title: "1.2"}]},
|
||||||
|
{title: "p2", children: [{title: "2.1"}, {title: "2.2"}]},
|
||||||
|
{title: "3"},
|
||||||
|
{title: "5"},
|
||||||
|
{title: "top", "#top": ""}
|
||||||
|
],
|
||||||
|
"#sorted": "",
|
||||||
|
"#sortFoldersFirst": ""
|
||||||
|
});
|
||||||
|
cls.init(() => {
|
||||||
|
tree.sortNotesIfNeeded(note.noteId);
|
||||||
|
});
|
||||||
|
const orderedTitles = note.children.map((child) => child.title);
|
||||||
|
expect(orderedTitles).toStrictEqual(["top", "p1", "p2", "1", "2", "3", "5", "bottom"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("sorts notes accordingly when #sortNatural is set", () => {
|
||||||
|
const note = buildNote({
|
||||||
|
children: [
|
||||||
|
{title: "bottom", "#bottom": ""},
|
||||||
|
{title: "1"},
|
||||||
|
{title: "2"},
|
||||||
|
{title: "10"},
|
||||||
|
{title: "20"},
|
||||||
|
{title: "3"},
|
||||||
|
{title: "top", "#top": ""}
|
||||||
|
],
|
||||||
|
"#sorted": "",
|
||||||
|
"#sortNatural": ""
|
||||||
|
});
|
||||||
|
cls.init(() => {
|
||||||
|
tree.sortNotesIfNeeded(note.noteId);
|
||||||
|
});
|
||||||
|
const orderedTitles = note.children.map((child) => child.title);
|
||||||
|
expect(orderedTitles).toStrictEqual(["top", "1", "2", "3", "10", "20", "bottom"]);
|
||||||
|
}
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|||||||
@ -98,15 +98,6 @@ function sortNotes(parentNoteId: string, customSortBy: string = "title", reverse
|
|||||||
}
|
}
|
||||||
|
|
||||||
notes.sort((a, b) => {
|
notes.sort((a, b) => {
|
||||||
if (foldersFirst) {
|
|
||||||
const aHasChildren = a.hasChildren();
|
|
||||||
const bHasChildren = b.hasChildren();
|
|
||||||
|
|
||||||
if ((aHasChildren && !bHasChildren) || (!aHasChildren && bHasChildren)) {
|
|
||||||
// exactly one note of the two is a directory, so the sorting will be done based on this status
|
|
||||||
return aHasChildren ? -1 : 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function fetchValue(note: BNote, key: string) {
|
function fetchValue(note: BNote, key: string) {
|
||||||
let rawValue: string | null;
|
let rawValue: string | null;
|
||||||
@ -154,6 +145,16 @@ function sortNotes(parentNoteId: string, customSortBy: string = "title", reverse
|
|||||||
return compare(bottomBEl, bottomAEl) * (reverse ? -1 : 1);
|
return compare(bottomBEl, bottomAEl) * (reverse ? -1 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (foldersFirst) {
|
||||||
|
const aHasChildren = a.hasChildren();
|
||||||
|
const bHasChildren = b.hasChildren();
|
||||||
|
|
||||||
|
if ((aHasChildren && !bHasChildren) || (!aHasChildren && bHasChildren)) {
|
||||||
|
// exactly one note of the two is a directory, so the sorting will be done based on this status
|
||||||
|
return aHasChildren ? -1 : 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const customAEl = fetchValue(a, customSortBy) ?? fetchValue(a, "title") as string;
|
const customAEl = fetchValue(a, customSortBy) ?? fetchValue(a, "title") as string;
|
||||||
const customBEl = fetchValue(b, customSortBy) ?? fetchValue(b, "title") as string;
|
const customBEl = fetchValue(b, customSortBy) ?? fetchValue(b, "title") as string;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user