Skip to content

Build your personal knowledge base with Trilium Notes

fix(tree): keep collapsed ancestors closed on reload when not following the active note (closes #12220)

Elian Doran597b749ffc02
diff --git a/apps/client/src/widgets/note_tree.spec.ts b/apps/client/src/widgets/note_tree.spec.ts
index 0faf2a6667..f3c446d7c3 100644
--- a/apps/client/src/widgets/note_tree.spec.ts
+++ b/apps/client/src/widgets/note_tree.spec.ts
@@ -2,9 +2,11 @@ import $ from "jquery";
import { afterEach, describe, expect, it, vi } from "vitest";
import appContext from "../components/app_context.js";
+import type { EventData } from "../components/app_context.js";
import type TabManager from "../components/tab_manager.js";
import hoistedNoteService from "../services/hoisted_note.js";
import noteCreateService from "../services/note_create.js";
+import options from "../services/options.js";
import server from "../services/server.js";
import treeService from "../services/tree.js";
import ws from "../services/ws.js";
@@ -135,6 +137,54 @@ describe("NoteTreeWidget", () => {
// The popup's own hoisted note is passed explicitly — not the active tab's.
expect(isHoisted).toHaveBeenCalledWith("_taskStates");
});
+
+ describe("restoring the active node after an entity reload", () => {
+ async function reload(follow: boolean) {
+ const widget = await renderWidget();
+ vi.spyOn(options, "is").mockImplementation((name) => name === "treeScrollFollowNavigation" && follow);
+ vi.spyOn(widget, "activityDetected").mockImplementation(() => {});
+ vi.spyOn(widget, "getActiveNode").mockReturnValue({ hasFocus: () => false } as Fancytree.FancytreeNode);
+ vi.spyOn(treeService, "getNotePath").mockReturnValue("root/parent/child");
+
+ const tree = { options: { activeVisible: true } };
+ (widget as unknown as { tree: unknown }).tree = tree;
+ let activeVisibleOnActivation: boolean | undefined;
+ const node = {
+ data: { noteId: "child" },
+ setActive: vi.fn(() => {
+ activeVisibleOnActivation = tree.options.activeVisible;
+ })
+ };
+ const getNodeFromPath = vi.spyOn(widget, "getNodeFromPath")
+ .mockResolvedValue(node as unknown as Fancytree.FancytreeNode);
+
+ await widget.entitiesReloadedEvent({
+ loadResults: {
+ isEmptyForTree: () => false,
+ getAttributeRows: () => [],
+ getBranchRows: () => [],
+ getNoteIds: () => [],
+ getNoteReorderings: () => []
+ }
+ } as unknown as EventData<"entitiesReloaded">);
+
+ return { getNodeFromPath, node, activeVisibleOnActivation };
+ }
+
+ it("keeps collapsed ancestors collapsed when the tree does not follow the active note (#12220)", async () => {
+ const { getNodeFromPath, node, activeVisibleOnActivation } = await reload(false);
+ expect(getNodeFromPath).toHaveBeenCalledWith("root/parent/child", false, false);
+ expect(node.setActive).toHaveBeenCalled();
+ expect(activeVisibleOnActivation).toBe(false);
+ });
+
+ it("expands to the active note when the tree follows it", async () => {
+ const { getNodeFromPath, node, activeVisibleOnActivation } = await reload(true);
+ expect(getNodeFromPath).toHaveBeenCalledWith("root/parent/child", true, false);
+ expect(node.setActive).toHaveBeenCalled();
+ expect(activeVisibleOnActivation).toBe(true);
+ });
+ });
});
describe("the drop marker's distance from the row boundary", () => {
diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts
index bbf3ea0438..6e6968b98a 100644
--- a/apps/client/src/widgets/note_tree.ts
+++ b/apps/client/src/widgets/note_tree.ts
@@ -1422,7 +1422,8 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
return;
}
- let node: Fancytree.FancytreeNode | null | undefined = await this.expandToNote(activeNotePath, false);
+ const follow = this.treeScrollFollowNavigation;
+ let node: Fancytree.FancytreeNode | null | undefined = await this.getNodeFromPath(activeNotePath, follow, false);
if (node && node.data.noteId !== treeService.getNoteIdFromUrl(activeNotePath)) {
// if the active note has been moved elsewhere then it won't be found by the path,
@@ -1446,7 +1447,14 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
this.tree.$container.focus();
}
- await node.setActive(true, { noEvents: true, noFocus: !activeNodeFocused });
+ // fancytree's `activeVisible` expands the ancestors of a node it activates.
+ const prevActiveVisible = this.tree.options.activeVisible;
+ this.tree.options.activeVisible = follow;
+ try {
+ await node.setActive(true, { noEvents: true, noFocus: !activeNodeFocused });
+ } finally {
+ this.tree.options.activeVisible = prevActiveVisible;
+ }
}
sortChildren(node: Fancytree.FancytreeNode) {