Skip to content

Build your personal knowledge base with Trilium Notes

fix(tree): expand and collapse the virtual branches of a subtree

Elian Doraned4ac241ef25
diff --git a/packages/trilium-core/src/routes/api/branches.spec.ts b/packages/trilium-core/src/routes/api/branches.spec.ts
index da3fd64b75..f75bbd8d65 100644
--- a/packages/trilium-core/src/routes/api/branches.spec.ts
+++ b/packages/trilium-core/src/routes/api/branches.spec.ts
@@ -1,6 +1,10 @@
import { beforeAll, describe, expect, it } from "vitest";
+import becca from "../../becca/becca";
+import { load } from "../../becca/becca_loader";
+import { getContext } from "../../services/context";
import { getSql } from "../../services/sql/index";
+import { registerVirtualNoteProvider, unregisterVirtualNoteProvider } from "../../services/virtual_notes";
import { createTextNote } from "../../test/api_fixtures";
import { CoreApiTester } from "../../test/api_tester";
@@ -30,6 +34,7 @@ function setExpanded(branchIds: string[], isExpanded: number) {
"UPDATE branches SET isExpanded = ? WHERE branchId = ?",
[ isExpanded, branchId ]
);
+ becca.getBranchOrThrow(branchId).isExpanded = !!isExpanded;
}
}
@@ -287,6 +292,45 @@ describe("Branches API (core)", () => {
expect(getBranchRow(branchId)?.isExpanded).toBe(0);
}
});
+
+ it("reaches the virtual subtree below a note, and a subtree that is virtual itself", async () => {
+ const anchor = await createTextNote(api, { title: "Virtual anchor" });
+ const persistedChild = await createTextNote(api, { parentNoteId: anchor.noteId, title: "Persisted child" });
+ registerVirtualNoteProvider({
+ namespace: "_vexp",
+ parentNoteId: anchor.noteId,
+ getSubtree: () => [{
+ id: "_vexp",
+ title: "Virtual root",
+ type: "book",
+ children: [{
+ id: "_vexpBook",
+ title: "Virtual book",
+ type: "book",
+ children: [{ id: "_vexpPage", title: "Virtual page", type: "text" }]
+ }]
+ }]
+ });
+
+ try {
+ getContext().init(() => load());
+ const virtualBranchIds = [ `${anchor.noteId}__vexp`, "_vexp__vexpBook", "_vexpBook__vexpPage" ];
+ const isExpanded = (branchId: string) => becca.getBranch(branchId)?.isExpanded;
+
+ const expand = await api.put<{ branchIds: string[] }>(`/api/branches/${anchor.branchId}/expanded-subtree/1`);
+ expect(new Set(expand.body.branchIds)).toEqual(new Set([ anchor.branchId, persistedChild.branchId, ...virtualBranchIds ]));
+ expect(getBranchRow(persistedChild.branchId)?.isExpanded).toBe(1);
+ expect(virtualBranchIds.map(isExpanded)).toEqual([ true, true, true ]);
+
+ const collapse = await api.put<{ branchIds: string[] }>(`/api/branches/_vexp__vexpBook/expanded-subtree/0`);
+ expect(new Set(collapse.body.branchIds)).toEqual(new Set([ "_vexp__vexpBook", "_vexpBook__vexpPage" ]));
+ expect(virtualBranchIds.map(isExpanded)).toEqual([ true, false, false ]);
+ expect(getBranchRow(persistedChild.branchId)?.isExpanded).toBe(1);
+ } finally {
+ unregisterVirtualNoteProvider("_vexp");
+ getContext().init(() => load());
+ }
+ });
});
describe("prefixes", () => {
diff --git a/packages/trilium-core/src/routes/api/branches.ts b/packages/trilium-core/src/routes/api/branches.ts
index fdb0c123db..fbc8096a93 100644
--- a/packages/trilium-core/src/routes/api/branches.ts
+++ b/packages/trilium-core/src/routes/api/branches.ts
@@ -4,6 +4,7 @@ import eventService from "../../services/events.js";
import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
+import type BBranch from "../../becca/entities/bbranch.js";
import entityChangesService from "../../services/entity_changes.js";
import { getLog } from "../../services/log.js";
import TaskContext from "../../services/task_context.js";
@@ -175,44 +176,37 @@ function setExpanded(req: Request<{ branchId: string, expanded: string }>) {
function setExpandedForSubtree(req: Request<{ branchId: string, expanded: string }>) {
const { branchId } = req.params;
- const expanded = req.params.expanded === "1" ? 1 : 0;
- const sql = getSql();
-
- // The state filter sits outside the recursion so that the walk reaches every descendant;
- // `none_root` is excluded because the root is always expanded.
- const branchIds = sql.getColumn<string>(/*sql*/`
- WITH RECURSIVE
- tree(branchId, noteId) AS (
- SELECT branchId, noteId FROM branches WHERE branchId = ?
- UNION
- SELECT branches.branchId, branches.noteId
- FROM branches
- JOIN tree ON branches.parentNoteId = tree.noteId
- WHERE branches.isDeleted = 0
- )
- SELECT tree.branchId
- FROM tree
- JOIN branches USING (branchId)
- WHERE branches.isExpanded != ?
- AND tree.branchId != 'none_root'`,
- [ branchId, expanded ]
- );
+ const isExpanded = req.params.expanded === "1";
+
+ // Walks becca rather than the `branches` table, so that the virtual branches a provider
+ // injects are reached too; `none_root` is skipped because the root is always expanded.
+ const changedBranches: BBranch[] = [];
+ const visitedNoteIds = new Set<string>();
+ const startBranch = becca.branches[branchId];
+ const pending = startBranch ? [ startBranch ] : [];
+
+ for (let branch = pending.pop(); branch; branch = pending.pop()) {
+ if (branch.branchId !== "none_root" && branch.isExpanded !== isExpanded) {
+ changedBranches.push(branch);
+ }
- sql.executeMany(
- /*sql*/`UPDATE branches SET isExpanded = ${expanded} WHERE branchId IN (???)`,
- branchIds
- );
+ if (!visitedNoteIds.has(branch.noteId)) {
+ visitedNoteIds.add(branch.noteId);
+ pending.push(...branch.childNote.getChildBranches().filter((child) => !!child));
+ }
+ }
- for (const branchId of branchIds) {
- const branch = becca.branches[branchId];
+ getSql().executeMany(
+ /*sql*/`UPDATE branches SET isExpanded = ${isExpanded ? 1 : 0} WHERE branchId IN (???)`,
+ changedBranches.filter((branch) => !branch.isVirtual).map((branch) => branch.branchId)
+ );
- if (branch) {
- branch.isExpanded = !!expanded;
- }
+ for (const branch of changedBranches) {
+ branch.isExpanded = isExpanded;
}
return {
- branchIds
+ branchIds: changedBranches.map((branch) => branch.branchId)
};
}