Skip to content

Build your personal knowledge base with Trilium Notes

fix(tree): don't crash when scrolling to an unrendered node, closes #10407

Elian Doran9c355b1af7b4
diff --git a/apps/client/src/test/setup.ts b/apps/client/src/test/setup.ts
index f15c07567c..89ad69d622 100644
--- a/apps/client/src/test/setup.ts
+++ b/apps/client/src/test/setup.ts
@@ -1,5 +1,5 @@
-import { beforeAll, vi } from "vitest";
import $ from "jquery";
+import { beforeAll, vi } from "vitest";
injectGlobals();
@@ -11,6 +11,8 @@ beforeAll(() => {
function injectGlobals() {
const uncheckedWindow = window as any;
uncheckedWindow.$ = $;
+ // some libraries (e.g. jquery.fancytree's ui-deps) expect the jQuery global, same as src/index.ts
+ uncheckedWindow.jQuery = $;
uncheckedWindow.WebSocket = () => {};
uncheckedWindow.glob = {
isMainWindow: true
@@ -24,7 +26,7 @@ function mockWebsocket() {
// Do nothing.
}
}
- }
+ };
}
function mockServer() {
@@ -44,7 +46,7 @@ function mockServer() {
branches: [],
notes: [],
attributes: []
- }
+ };
}
console.warn(`Unsupported GET to mocked server: ${url}`);
@@ -52,7 +54,7 @@ function mockServer() {
async post(url: string, data: object) {
if (url === "tree/load") {
- throw new Error(`A module tried to load from the server the following notes: ${((data as any).noteIds || []).join(",")}\nThis is not supported, use Froca mocking instead and ensure the note exist in the mock.`)
+ throw new Error(`A module tried to load from the server the following notes: ${((data as any).noteIds || []).join(",")}\nThis is not supported, use Froca mocking instead and ensure the note exist in the mock.`);
}
}
}
diff --git a/apps/client/src/widgets/note_tree.spec.ts b/apps/client/src/widgets/note_tree.spec.ts
new file mode 100644
index 0000000000..c8bab340f0
--- /dev/null
+++ b/apps/client/src/widgets/note_tree.spec.ts
@@ -0,0 +1,51 @@
+import "./note_tree.js";
+
+import $ from "jquery";
+import { describe, expect, it } from "vitest";
+
+describe("fancytree scrollIntoView patch", () => {
+ it("resolves instead of crashing for a node without rendered markup (#10407)", async () => {
+ const $tree = $("<div>").appendTo(document.body);
+
+ try {
+ $tree.fancytree({
+ source: [
+ {
+ title: "parent",
+ key: "parent",
+ expanded: true,
+ children: [{ title: "child", key: "child" }]
+ }
+ ]
+ });
+
+ const tree: Fancytree.Fancytree = $tree.fancytree("getTree");
+
+ // The rendered node scrolls normally.
+ await tree.getNodeByKey("child").scrollIntoView();
+
+ // Simulate a batchUpdate() window: rendering is suspended while nodes are
+ // recreated (as entitiesReloadedEvent does via node.load(true) during a sync).
+ tree.enableUpdate(false);
+ const parent = tree.getNodeByKey("parent");
+ parent.removeChildren();
+ parent.addChildren({ title: "recreated child", key: "recreated" });
+ // node.load() restores the expanded flag the same way after a forced reload
+ await parent.setExpanded(true, { noAnimation: true, noEvents: true });
+
+ const recreated = tree.getNodeByKey("recreated");
+ expect(recreated.span).toBeFalsy(); // no markup was created,
+ expect(recreated.isVisible()).toBe(true); // yet fancytree considers the node visible
+
+ // Without the patch both of these threw
+ // "TypeError: Cannot read properties of undefined (reading 'top')".
+ await recreated.scrollIntoView();
+ await recreated.setActive(true, { noEvents: true, noFocus: true });
+
+ tree.enableUpdate(true);
+ expect(recreated.span).toBeTruthy(); // re-enabling updates renders the node
+ } finally {
+ $tree.remove();
+ }
+ });
+});
diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts
index 2502526543..b9f82209e3 100644
--- a/apps/client/src/widgets/note_tree.ts
+++ b/apps/client/src/widgets/note_tree.ts
@@ -1966,3 +1966,31 @@ function buildEnhanceTitle() {
}
};
}
+
+type ScrollIntoViewFn = (this: Fancytree.FancytreeNode, effects?: boolean | object, options?: object) => JQueryPromise<unknown>;
+
+/**
+ * jquery.fancytree's `FancytreeNode.scrollIntoView()` reads `$(this.span).offset().top`, which throws
+ * "Cannot read properties of undefined (reading 'top')" for a node that has no DOM markup — e.g. nodes
+ * recreated by `load(true)` while rendering is suspended by `batchUpdate()` during a large sync update.
+ * Its `isVisible()` guard only checks the ancestors' expanded flags, not the actual markup, and the
+ * exception then aborts the whole tab activation (see #10407). Skip the scroll instead, mirroring the
+ * library's own early return for invisible nodes; the node gets rendered (and styled as active) by the
+ * full `tree.render()` that re-enabling updates triggers anyway.
+ */
+function patchScrollIntoViewCrash() {
+ const { _FancytreeNodeClass } = $.ui.fancytree as Fancytree.FancytreeStatic & {
+ _FancytreeNodeClass: { prototype: Fancytree.FancytreeNode };
+ };
+ const originalScrollIntoView = _FancytreeNodeClass.prototype.scrollIntoView as ScrollIntoViewFn;
+
+ _FancytreeNodeClass.prototype.scrollIntoView = function (this: Fancytree.FancytreeNode, effects?: boolean | object, options?: object) {
+ if (!this.span) {
+ return $.Deferred().resolveWith(this).promise();
+ }
+
+ return originalScrollIntoView.call(this, effects, options);
+ };
+}
+
+patchScrollIntoViewCrash();