Skip to content

Build your personal knowledge base with Trilium Notes

fix(mind_map): destroy the instance a rebuild replaces

Elian Doran635e189284c7
diff --git a/apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.ts b/apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.tsx
similarity index 79%
rename from apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.ts
rename to apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.tsx
index 82c75c8833..7a93985314 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.ts
+++ b/apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.tsx
@@ -4,11 +4,46 @@
// sanitization-dependent specs under jsdom, which matches real-browser behavior.
import i18next from "i18next";
import { DARK_THEME, MindElixirInstance, THEME as LIGHT_THEME } from "mind-elixir";
-import { beforeAll, describe, expect, it } from "vitest";
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { beforeAll, describe, expect, it, vi } from "vitest";
+import { renderInto } from "../../../test/render";
import englishTranslation from "../../../translations/en/translation.json";
-import { buildTheme, localizeBuiltLabels, renameOverlayLabel, sanitizeMindMapData } from "./MindMap.js";
+import { buildTheme, localizeBuiltLabels, MindElixir, renameOverlayLabel, sanitizeMindMapData } from "./MindMap.js";
+
+const builtMaps = vi.hoisted(() => [] as { destroy: () => void; destroyed: boolean }[]);
+
+vi.mock("mind-elixir", async (importOriginal) => {
+ const original = await importOriginal<typeof import("mind-elixir")>();
+
+ /** Records each instance built and whether it was destroyed. */
+ class FakeMindElixir {
+ nodes = document.createElement("me-nodes");
+ currentNodes = [];
+ theme = { name: "Latte" };
+ bus = { addListener: vi.fn(), removeListener: vi.fn() };
+ destroyed = false;
+
+ constructor() {
+ builtMaps.push(this);
+ }
+
+ getData() { return {}; }
+ init() {}
+ linkDiv() {}
+ destroy() { this.destroyed = true; }
+ }
+
+ return { ...original, default: FakeMindElixir };
+});
+
+vi.mock("../../react/hooks", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../../react/hooks")>()),
+ useColorScheme: () => "light",
+ useTriliumOption: () => [ "en" ]
+}));
describe("sanitizeMindMapData", () => {
it("strips XSS/RCE vectors from dangerouslySetInnerHTML (GHSA-rj57-j38v-3577)", () => {
@@ -203,3 +238,29 @@ describe("renameOverlayLabel", () => {
expect([ label.style.left, label.style.top ]).toEqual([ "", "" ]);
});
});
+
+describe("MindElixir", () => {
+ const liveMaps = () => builtMaps.filter((map) => !map.destroyed);
+
+ /** Builds the map and settles it, so that its effects have run before it is looked at. */
+ function renderMap(editable: boolean) {
+ let container: HTMLElement | undefined;
+ act(() => { container = renderInto(<MindElixir editable={editable} />); });
+ if (!container) throw new Error("the map was not rendered");
+ return container;
+ }
+
+ it("destroys the instance it replaces, and the last one when it unmounts", () => {
+ builtMaps.length = 0;
+ const container = renderMap(true);
+ expect(liveMaps()).toHaveLength(1);
+
+ const first = liveMaps()[0];
+ act(() => render(<MindElixir editable={false} />, container));
+ expect(first.destroyed).toBe(true);
+ expect(liveMaps()).toHaveLength(1);
+
+ act(() => render(null, container));
+ expect(liveMaps()).toHaveLength(0);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
index d2e14cef8c..ca75b95256 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
@@ -198,7 +198,7 @@ export default function MindMap({ note, ntxId, noteContext }: TypeWidgetProps) {
);
}
-function MindElixir({ children, containerRef: externalContainerRef, containerProps, apiRef: externalApiRef, onChange, onSelectionChange, editable }: MindElixirProps) {
+export function MindElixir({ children, containerRef: externalContainerRef, containerProps, apiRef: externalApiRef, onChange, onSelectionChange, editable }: MindElixirProps) {
const containerRef = useSyncedRef<HTMLDivElement>(externalContainerRef, null);
const apiRef = useRef<MindElixirInstance>(null);
const [ locale ] = useTriliumOption("locale");
@@ -211,6 +211,7 @@ function MindElixir({ children, containerRef: externalContainerRef, containerPro
function reinitialize() {
if (!containerRef.current) return;
+ apiRef.current?.destroy();
const mind = new VanillaMindElixir({
el: containerRef.current,
editable,
@@ -238,12 +239,11 @@ function MindElixir({ children, containerRef: externalContainerRef, containerPro
setInstanceVersion((version) => version + 1);
}
- useEffect(() => {
- reinitialize();
- return () => {
- apiRef.current?.destroy();
- apiRef.current = null;
- };
+ // The instance is built by the effect that follows `editable` and `locale`, which also runs on
+ // mount; this one only tears it down.
+ useEffect(() => () => {
+ apiRef.current?.destroy();
+ apiRef.current = null;
}, []);
// React to theme changes.