Skip to content

Build your personal knowledge base with Trilium Notes

fix(relation_map): close the note pane only on an Escape meant for it

Elian Doranb6425eef8b13
diff --git a/apps/client/src/widgets/type_widgets/relation_map/NotePane.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/NotePane.spec.tsx
index 95dff2344f..326cda0760 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/NotePane.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/NotePane.spec.tsx
@@ -87,10 +87,16 @@ describe("relation map NotePane", () => {
const paneRef = useRef<NotePaneHandle>(null);
handle = paneRef;
const mapApiRef = useRef(mapApi);
- return <NotePane
- paneRef={paneRef} noteIdsOnMap={noteIdsOnMap} mapApiRef={mapApiRef}
- isReadOnly={isReadOnly} selection={selection} onSelect={setSelection}
- />;
+ const hostRef = useRef<HTMLDivElement>(null);
+ return (
+ <div ref={hostRef} className="map-host">
+ <div className="map-canvas" tabIndex={0} />
+ <NotePane
+ paneRef={paneRef} hostRef={hostRef} noteIdsOnMap={noteIdsOnMap} mapApiRef={mapApiRef}
+ isReadOnly={isReadOnly} selection={selection} onSelect={setSelection}
+ />
+ </div>
+ );
}
/** Mounts the pane afresh, with the map's selection set to `selection`. */
@@ -136,9 +142,16 @@ describe("relation map NotePane", () => {
expect(pane()).toBeNull();
});
+ /** Presses Escape on `target`, and waits for the pane to decide whether the press was its own. */
+ async function pressEscape(target: Element | null | undefined) {
+ const event = new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true });
+ await act(async () => { target?.dispatchEvent(event); });
+ await settle();
+ }
+
it("closes on Escape and through the handle the map holds", async () => {
await mount();
- await act(async () => { window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" })); });
+ await pressEscape(container?.querySelector(".map-canvas"));
expect(pane()).toBeNull();
await mount({ selection: { noteId: "second" } });
@@ -148,6 +161,24 @@ describe("relation map NotePane", () => {
expect(pane()).toBeNull();
});
+ it("leaves an Escape pressed outside the map, or handled by what it was pressed in, to them", async () => {
+ await mount();
+ const dialog = document.createElement("div");
+ document.body.appendChild(dialog);
+ try {
+ await pressEscape(dialog);
+ expect(pane()).toBeTruthy();
+ } finally {
+ dialog.remove();
+ }
+
+ const popup = document.createElement("div");
+ popup.addEventListener("keydown", (e) => e.preventDefault());
+ pane()?.querySelector(".relation-map-note-pane-body")?.appendChild(popup);
+ await pressEscape(popup);
+ expect(pane()).toBeTruthy();
+ });
+
it("stays closed for a note that is not on the map, and closes once its note leaves it after asking the editor to save", async () => {
await mount({ noteIdsOnMap: [ "second" ] });
expect(pane()).toBeNull();
diff --git a/apps/client/src/widgets/type_widgets/relation_map/NotePane.tsx b/apps/client/src/widgets/type_widgets/relation_map/NotePane.tsx
index 0f77253fe5..b81466dd06 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/NotePane.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/NotePane.tsx
@@ -29,8 +29,10 @@ export interface NotePaneHandle {
* Panel at the trailing edge of a relation map that shows the selected note's title, promoted
* attributes and content for editing.
*/
-export default function NotePane({ paneRef, noteIdsOnMap, mapApiRef, isReadOnly, selection, onSelect }: {
+export default function NotePane({ paneRef, hostRef, noteIdsOnMap, mapApiRef, isReadOnly, selection, onSelect }: {
paneRef: RefObject<NotePaneHandle | null>;
+ /** The map the pane is in. Escape closes the pane only when pressed inside it. */
+ hostRef: RefObject<HTMLElement | null>;
/** IDs of the notes on the map. The pane closes when its note is not among them, and a link in
* the pane switches the pane only to a note among them. */
noteIdsOnMap: string[];
@@ -76,12 +78,17 @@ export default function NotePane({ paneRef, noteIdsOnMap, mapApiRef, isReadOnly,
if (!selection || isMobile()) return;
const onKeyDown = (e: KeyboardEvent) => {
- if (e.key === "Escape") closePane();
+ if (e.key !== "Escape" || !(e.target instanceof Node) || !hostRef.current?.contains(e.target)) return;
+ // Waits for the event to finish, so that an editor popup that handles Escape stays the
+ // only thing it closes.
+ setTimeout(() => {
+ if (!e.defaultPrevented) closePane();
+ });
};
// Capture phase, because `OverlayPanel` stops key presses inside it from bubbling.
window.addEventListener("keydown", onKeyDown, true);
return () => window.removeEventListener("keydown", onKeyDown, true);
- }, [ selection?.noteId, closePane ]);
+ }, [ selection?.noteId, hostRef, closePane ]);
if (!note) return null;
diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
index de6581e9e2..42366e39a9 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
@@ -220,6 +220,7 @@ export default function RelationMap({ note, noteContext, ntxId }: TypeWidgetProp
<NotePane
paneRef={paneRef}
+ hostRef={wrapperRef}
noteIdsOnMap={noteIdsOnMap}
mapApiRef={mapApiRef}
isReadOnly={isReadOnly}