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
client/options: ensure the correct order of option items for existing databases
Adorian Doran6aa3e6383753
diff --git a/packages/commons/src/lib/hidden_subtree.ts b/packages/commons/src/lib/hidden_subtree.tsindex 5967c46fff..108bbe2ed5 100644--- a/packages/commons/src/lib/hidden_subtree.ts+++ b/packages/commons/src/lib/hidden_subtree.ts@@ -34,6 +34,13 @@ export interface HiddenSubtreeItem {icon?: string;attributes?: HiddenSubtreeAttribute[];children?: HiddenSubtreeItem[];+ /**+ * Holds the children in the order they are listed in, on every database rather than only on one+ * being set up: each is given a position from its place in the list. For a group whose order is+ * the app's to decide (the settings pages), not one the user arranges themselves (the launcher+ * bar), which would be undone at every start.+ */+ enforceChildOrder?: boolean;isExpanded?: boolean;baseSize?: string;growthFactor?: string;diff --git a/packages/trilium-core/src/services/hidden_subtree.spec.ts b/packages/trilium-core/src/services/hidden_subtree.spec.tsindex 481512485f..5414e738a5 100644--- a/packages/trilium-core/src/services/hidden_subtree.spec.ts+++ b/packages/trilium-core/src/services/hidden_subtree.spec.ts@@ -217,6 +217,52 @@ describe("hidden_subtree (real DB)", () => {});});+ describe("the order the settings pages are held in", () => {+ /** The settings pages under `_options`, read in the order the tree holds them. */+ function pageOrder() {+ return becca.notes["_options"].getChildBranches()+ .filter((branch) => !branch.isDeleted)+ .sort((a, b) => a.notePosition - b.notePosition)+ .map((branch) => branch.noteId);+ }++ it("puts them back in the declared order after one is moved, not only on a new database", () => {+ const before = pageOrder();+ expect(before[0]).toBe("_optionsAppearance");+ expect(before.length).toBeGreaterThan(1);++ // A database that already holds them in some other order: send the first page last.+ const moved = becca.notes["_options"].getChildBranches()+ .find((branch) => branch.noteId === "_optionsAppearance");+ getContext().init(() => {+ if (moved) {+ moved.notePosition = 999;+ moved.save();+ }+ });+ expect(pageOrder()[0]).not.toBe("_optionsAppearance");++ checkHiddenSubtree();++ expect(pageOrder()).toEqual(before);+ });++ it("leaves a group the user arranges alone, the launcher bar declaring no order", () => {+ const launchers = becca.notes["_lbRoot"].getChildBranches().filter((b) => !b.isDeleted);+ const moved = launchers[0];+ const rearranged = moved.notePosition + 5;++ getContext().init(() => {+ moved.notePosition = rearranged;+ moved.save();+ });++ checkHiddenSubtree();++ expect(moved.notePosition).toBe(rearranged);+ });+ });+describe("type and idempotency", () => {it("restores a note type that was changed away from the definition", () => {const options = becca.notes["_options"];diff --git a/packages/trilium-core/src/services/hidden_subtree.ts b/packages/trilium-core/src/services/hidden_subtree.tsindex 19795ff0c7..b6e07a370a 100644--- a/packages/trilium-core/src/services/hidden_subtree.ts+++ b/packages/trilium-core/src/services/hidden_subtree.ts@@ -294,6 +294,7 @@ function buildHiddenSubtreeDefinition(helpSubtree: HiddenSubtreeItem[]): HiddenStitle: t("hidden-subtree.options-title"),type: "book",icon: "bx-cog",+ enforceChildOrder: true,children: [// The order below is the order the pages are listed in. A database being// set up meets them here and gives each the next position as it is created;@@ -426,7 +427,11 @@ function isWithinHiddenSubtree(noteId: string): boolean {return noteId.startsWith("_") || noteId === "root";}-function checkHiddenSubtreeRecursively(parentNoteId: string, item: HiddenSubtreeItem, extraOpts: CheckHiddenExtraOpts = {}) {+function checkHiddenSubtreeRecursively(+ parentNoteId: string, item: HiddenSubtreeItem, extraOpts: CheckHiddenExtraOpts = {},+ /** Where the parent's own ordering puts this item, for a parent that holds its children in order. */+ orderedPosition?: number+) {if (!item.id || !item.type || !item.title) {throw new Error(`Item does not contain mandatory properties: ${JSON.stringify(item)}`);}@@ -558,8 +563,11 @@ function checkHiddenSubtreeRecursively(parentNoteId: string, item: HiddenSubtreeif (branch) {// in case of launchers the branch ID is not preserved and should not be relied upon - launchers which move between// visible and available will change branch since the branch's parent-child relationship is immutable- if (item.notePosition !== undefined && branch.notePosition !== item.notePosition) {- branch.notePosition = item.notePosition;+ // What the definition asks for: the item's own position, or the one its place in an ordered+ // parent implies. Written only where it differs, so a database already in order is untouched.+ const wantedPosition = item.notePosition ?? orderedPosition;+ if (wantedPosition !== undefined && branch.notePosition !== wantedPosition) {+ branch.notePosition = wantedPosition;branch.save();}@@ -616,8 +624,10 @@ function checkHiddenSubtreeRecursively(parentNoteId: string, item: HiddenSubtree}}- for (const child of item.children || []) {- checkHiddenSubtreeRecursively(item.id, child, extraOpts);+ for (const [ index, child ] of (item.children ?? []).entries()) {+ // Ten apart, as Trilium spaces positions, so there is room to drop something between two.+ const position = item.enforceChildOrder ? (index + 1) * 10 : undefined;+ checkHiddenSubtreeRecursively(item.id, child, extraOpts, position);}}