Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): give every embedded note pane a note context of its own

Elian Doraned2b9e72d631
diff --git a/apps/client/src/widgets/EmbeddedNotePane.tsx b/apps/client/src/widgets/EmbeddedNotePane.tsx
index a66bb2490e..d58c26502b 100644
--- a/apps/client/src/widgets/EmbeddedNotePane.tsx
+++ b/apps/client/src/widgets/EmbeddedNotePane.tsx
@@ -11,6 +11,7 @@ import NoteColorPicker from "../menus/custom-items/NoteColorPicker";
import linkContextMenu from "../menus/link_context_menu";
import { t } from "../services/i18n";
import link from "../services/link";
+import { randomString } from "../services/utils";
import ActionButton from "./react/ActionButton";
import Dropdown, { DropdownPanel } from "./react/Dropdown";
import { FormListItem } from "./react/FormList";
@@ -30,7 +31,8 @@ import { NoteContextContext, ParentComponent } from "./react/react_utils";
* the quick editor makes, so that the icon and title widgets work and a rename saves the usual way.
*
* One context for the pane rather than one per note: moving between notes is a note switch within a
- * standing pane, not a new pane.
+ * standing pane, not a new pane. Its ntxId is `ntxIdPrefix` plus a random suffix, so that two panes
+ * of one kind (two maps in two splits) register two contexts with the tab manager.
*
* The returned component stands between the host view's component and the pane's contents. The
* context was built here rather than by the tab manager, so it has no parent to raise events
@@ -44,10 +46,11 @@ import { NoteContextContext, ParentComponent } from "./react/react_utils";
*/
export function useEmbeddedNoteContext(
note: FNote | undefined,
- ntxId: string,
+ ntxIdPrefix: string,
{ floatingToolbar = true, skipRecentNotes = false }: EmbeddedNoteContextOptions = {}
) {
const parentComponent = useContext(ParentComponent);
+ const [ ntxId ] = useState(() => `${ntxIdPrefix}_${randomString(10)}`);
const [ noteContext ] = useState(() => new NoteContext(ntxId));
const [ component ] = useState(() => new Component());
useDetachedNoteContext(noteContext);
@@ -83,7 +86,7 @@ export function useEmbeddedNoteContext(
});
}, [ noteContext, note?.noteId ]);
- return { noteContext, component };
+ return { noteContext, component, ntxId };
}
interface EmbeddedNoteContextOptions {
diff --git a/apps/client/src/widgets/collections/calendar/EventPopover.tsx b/apps/client/src/widgets/collections/calendar/EventPopover.tsx
index 4a26596da2..b45fbe7c75 100644
--- a/apps/client/src/widgets/collections/calendar/EventPopover.tsx
+++ b/apps/client/src/widgets/collections/calendar/EventPopover.tsx
@@ -54,7 +54,7 @@ export default function EventPopover({ noteId, anchor, container, parentNote, is
onFollowLink(noteId: string): boolean;
}) {
const note = useNote(noteId);
- const { noteContext, component } = useEmbeddedNoteContext(note ?? undefined, POPOVER_NTX_ID);
+ const { noteContext, component, ntxId } = useEmbeddedNoteContext(note ?? undefined, POPOVER_NTX_ID_PREFIX);
/**
* Whether the card has been grown to the window (see the maximize in {@link EventDetails}, and
@@ -73,9 +73,9 @@ export default function EventPopover({ noteId, anchor, container, parentNote, is
* mounted, and not waited on. Every way out leads through here.
*/
const close = useCallback(() => {
- void announceEmbeddedNoteClosing(component, POPOVER_NTX_ID);
+ void announceEmbeddedNoteClosing(component, ntxId);
onClose();
- }, [ component, onClose ]);
+ }, [ component, ntxId, onClose ]);
if (!note) {
return null;
@@ -219,8 +219,8 @@ function EventSheet({ note, parentNote, isEditable, onClose, onFollowLink }: {
);
}
-/** The popover's own ntxId, as the geo pane and the quick editor have one of their own. */
-const POPOVER_NTX_ID = "_calendar-event-popover";
+/** The start of the ntxId of the popover's own note context. */
+const POPOVER_NTX_ID_PREFIX = "_calendar-event-popover";
/** The popover's contents: what heads it, and the event under that. */
function EventDetails({ note, parentNote, isEditable, maximized, setMaximized, onClose, onFollowLink }: {
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
index cc6edcbda0..52ef8a85c4 100644
--- a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
@@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import appContext from "../../../components/app_context";
import Component from "../../../components/component";
+import type NoteContext from "../../../components/note_context";
import type FNote from "../../../entities/fnote";
import attributes from "../../../services/attributes";
import linkContextMenu from "../../../menus/link_context_menu";
@@ -196,7 +197,8 @@ describe("DetailPane", () => {
mapComponent = undefined;
if (container) {
- render(null, container);
+ const mounted = container;
+ act(() => render(null, mounted));
container.remove();
container = undefined;
}
@@ -995,6 +997,38 @@ describe("DetailPane", () => {
expect(pane()?.querySelector(".note-detail-stub")?.textContent).toBe("Somewhere");
});
+ /** Commands name a note context by its ntxId, so two maps in two splits need two of them. */
+ it("registers a note context of its own in every map", async () => {
+ const registered: string[] = [];
+ (appContext.tabManager as unknown as Record<string, unknown>).registerDetachedContext =
+ (noteContext: NoteContext) => registered.push(noteContext.ntxId ?? "");
+ await openPane(fakeMap());
+
+ const second = document.createElement("div");
+ document.body.appendChild(second);
+ try {
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={new Component()}>
+ <ParentMap.Provider value={fakeMap() as never}>
+ <Harness notes={[]} placing={false} isReadOnly={false} />
+ </ParentMap.Provider>
+ </ParentComponent.Provider>,
+ second
+ );
+ });
+
+ expect(registered).toEqual([
+ expect.stringMatching(/^_geo-detail-pane_/),
+ expect.stringMatching(/^_geo-detail-pane_/)
+ ]);
+ expect(registered[0]).not.toBe(registered[1]);
+ } finally {
+ act(() => render(null, second));
+ second.remove();
+ }
+ });
+
/** A toolbar built for the width of a note does not fit a pane a third that wide. */
it("is asked for the floating toolbar rather than a bar of its own", async () => {
const map = fakeMap();
@@ -1038,7 +1072,7 @@ describe("DetailPane", () => {
await act(async () => { window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" })); });
- expect(editorAskedToSave).toHaveBeenCalledWith({ ntxIds: [ "_geo-detail-pane" ] });
+ expect(editorAskedToSave).toHaveBeenCalledWith({ ntxIds: [ expect.stringMatching(/^_geo-detail-pane_/) ] });
expect(pane()).toBeNull();
});
});
@@ -1139,7 +1173,7 @@ describe("DetailPane", () => {
expect(onRelocate).toHaveBeenCalledWith(note.noteId);
expect(pane()).toBeNull();
// Going away this way is still a close, so whatever was being written is saved first.
- expect(editorAskedToSave).toHaveBeenCalledWith({ ntxIds: [ "_geo-detail-pane" ] });
+ expect(editorAskedToSave).toHaveBeenCalledWith({ ntxIds: [ expect.stringMatching(/^_geo-detail-pane_/) ] });
});
it("is not offered at all on a map that cannot be edited", async () => {
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.tsx
index c0c680c227..fa13436ea1 100644
--- a/apps/client/src/widgets/collections/geomap/DetailPane.tsx
+++ b/apps/client/src/widgets/collections/geomap/DetailPane.tsx
@@ -93,7 +93,7 @@ export default function DetailPane({ notes, parentNote, placing, isReadOnly, sel
// geometry the way it follows a marker that moves.
const [ shapeValue ] = useNoteLabel(note, SHAPE_ATTRIBUTE);
const shape = shapeValue ? parseGeoShape(shapeValue) : null;
- const { noteContext, component: paneComponent } = useEmbeddedNoteContext(note, PANE_NTX_ID);
+ const { noteContext, component: paneComponent, ntxId } = useEmbeddedNoteContext(note, PANE_NTX_ID_PREFIX);
/**
* Lets the pane go, having given whatever is being edited in it the chance to save (see
@@ -101,9 +101,9 @@ export default function DetailPane({ notes, parentNote, placing, isReadOnly, sel
* thing: that is a note switch within the pane, and the context announces it.
*/
const closePane = useCallback(() => {
- void announceEmbeddedNoteClosing(paneComponent, PANE_NTX_ID);
+ void announceEmbeddedNoteClosing(paneComponent, ntxId);
onSelect(null);
- }, [ paneComponent, onSelect ]);
+ }, [ paneComponent, ntxId, onSelect ]);
/**
* Arms the map for this marker to be put somewhere else, and stands the pane down while it waits.
@@ -561,8 +561,8 @@ function MarkerContents({ note, parentNote, isReadOnly, onRelocate }: {
);
}
-/** The pane's own ntxId, as the quick editor has one of its own. */
-const PANE_NTX_ID = "_geo-detail-pane";
+/** The start of the ntxId of the pane's own note context. */
+const PANE_NTX_ID_PREFIX = "_geo-detail-pane";
/**
* What can be done with the marker: the ways of opening its note (see {@link OpenNoteActions}),
diff --git a/apps/client/src/widgets/type_widgets/text/TextEmbedEditor.tsx b/apps/client/src/widgets/type_widgets/text/TextEmbedEditor.tsx
index 5a2fcc5b2d..b6b44e3d0e 100644
--- a/apps/client/src/widgets/type_widgets/text/TextEmbedEditor.tsx
+++ b/apps/client/src/widgets/type_widgets/text/TextEmbedEditor.tsx
@@ -1,12 +1,11 @@
import "./TextEmbedEditor.css";
import { createPortal, type RefObject } from "preact";
-import { useLayoutEffect, useRef, useState } from "preact/hooks";
+import { useLayoutEffect, useRef } from "preact/hooks";
import type Component from "../../../components/component";
import type NoteContext from "../../../components/note_context";
import type FNote from "../../../entities/fnote";
-import { randomString } from "../../../services/utils";
import {
announceEmbeddedNoteClosing, EmbeddedNoteScope, useEmbeddedNoteContext
} from "../../EmbeddedNotePane";
@@ -36,13 +35,12 @@ export default function TextEmbedEditor({ note, hasFixedToolbar, onClose }: Text
const closingSave = getClosingSave(note.noteId);
if (closingSave) throw closingSave;
- const [ ntxId ] = useState(() => `_embed_${randomString(10)}`);
const rootRef = useRef<HTMLDivElement>(null);
const onCloseRef = useRef(onClose);
onCloseRef.current = onClose;
// Inside an editor with a fixed toolbar, that toolbar shows the buttons of this editor while
// it has the focus.
- const { noteContext, component } = useEmbeddedNoteContext(note, ntxId, {
+ const { noteContext, component, ntxId } = useEmbeddedNoteContext(note, "_embed", {
floatingToolbar: !hasFixedToolbar,
skipRecentNotes: true
});