Skip to content

Build your personal knowledge base with Trilium Notes

fix(splits): detach a closed split from the widget tree

Elian Doran43017a0ebe1a
diff --git a/apps/client/src/widgets/containers/split_note_container.spec.ts b/apps/client/src/widgets/containers/split_note_container.spec.ts
new file mode 100644
index 0000000000..bbc3f089cf
--- /dev/null
+++ b/apps/client/src/widgets/containers/split_note_container.spec.ts
@@ -0,0 +1,56 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import appContext from "../../components/app_context";
+import NoteContext from "../../components/note_context";
+import type TabManager from "../../components/tab_manager";
+import LoadResults from "../../services/load_results";
+import NoteWrapperWidget from "../note_wrapper";
+import SplitNoteContainer from "./split_note_container";
+
+describe("SplitNoteContainer", () => {
+ let noteContexts: NoteContext[];
+
+ beforeEach(() => {
+ noteContexts = [];
+
+ // Only the lookups the container makes while opening and closing splits.
+ appContext.tabManager = {
+ activeNtxId: null,
+ getActiveMainContext: () => noteContexts[0],
+ getNoteContextById: (ntxId: string) => noteContexts.find((c) => c.ntxId === ntxId),
+ activateNoteContext: () => {}
+ } as unknown as TabManager;
+ });
+
+ async function openSplits(ntxIds: string[]) {
+ const container = new SplitNoteContainer(() => new NoteWrapperWidget());
+ container.render();
+
+ for (const ntxId of ntxIds) {
+ const noteContext = new NoteContext(ntxId);
+ noteContexts.push(noteContext);
+ await container.newNoteContextCreatedEvent({ noteContext });
+ }
+
+ return container;
+ }
+
+ it("stops delivering events to a split once its tab is closed", async () => {
+ const container = await openSplits([ "split-a", "split-b" ]);
+ const [ staysOpen, getsClosed ] = container.children;
+
+ // Stands in for what useTriliumEvent registers on a widget: the board's entitiesReloaded
+ // subscription is one of these, and it is what redraws a board nobody can see.
+ const openHandler = vi.fn();
+ const closedHandler = vi.fn();
+ staysOpen.registerHandler("entitiesReloaded", openHandler);
+ getsClosed.registerHandler("entitiesReloaded", closedHandler);
+
+ container.noteContextRemovedEvent({ ntxIds: [ "split-b" ] });
+ await container.handleEvent("entitiesReloaded", { loadResults: new LoadResults([]) });
+
+ expect(openHandler).toHaveBeenCalledOnce();
+ expect(closedHandler).not.toHaveBeenCalled();
+ expect(container.children).toHaveLength(1);
+ });
+});
diff --git a/apps/client/src/widgets/containers/split_note_container.ts b/apps/client/src/widgets/containers/split_note_container.ts
index 0eb4c0502d..b7690bcf62 100644
--- a/apps/client/src/widgets/containers/split_note_container.ts
+++ b/apps/client/src/widgets/containers/split_note_container.ts
@@ -187,12 +187,20 @@ export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {
}
noteContextRemovedEvent({ ntxIds }: EventData<"noteContextRemoved">) {
- this.children = this.children.filter((c) => !ntxIds.includes(c.ntxId ?? ""));
-
for (const ntxId of ntxIds) {
this.$widget.find(`[data-ntx-id="${ntxId}"]`).remove();
const widget = this.widgets[ntxId];
+ if (!widget) {
+ continue;
+ }
+
+ // Detach through this map, not through a `widget.ntxId` lookup: the widgets are whatever
+ // the factory builds (a NoteWrapperWidget on both layouts), and none of them carries an
+ // ntxId -- only the DOM node gets one, as `data-ntx-id`. A filter over `children` reading
+ // `c.ntxId` therefore matches nothing, leaving the closed split in the tree and its
+ // widgets handling events for a pane the user cannot see.
+ this.removeChild(widget);
recursiveCleanup(widget);
delete this.widgets[ntxId];
}