Skip to content

Build your personal knowledge base with Trilium Notes

fix(relation_map): keep the keyboard on the map

Elian Doran63652e5fb1f1
diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx
index 88e501225f..cf29948b8a 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx
@@ -28,6 +28,8 @@ vi.mock("react-zoom-pan-pinch", () => ({
TransformComponent: (props: { children?: ComponentChildren }) => <div>{props.children}</div>
}));
+import Component from "../../../components/component";
+import { ParentComponent } from "../../react/react_utils";
import type RelationMapApi from "./api";
import type { MapTransform } from "./api";
import { MapViewport, useCanvasClicks, useMapZoom, useRevealSelectedBox } from "./RelationMap";
@@ -237,6 +239,7 @@ describe("relation map zoom", () => {
const setTransform = vi.fn();
let boxes = [ { x: 0, y: 0, width: 1840, height: 100 } ];
let zoom: ReturnType<typeof useMapZoom> | undefined;
+ const component = new Component();
afterEach(() => {
if (container) {
@@ -250,7 +253,7 @@ describe("relation map zoom", () => {
function Harness({ loadedTransform }: { loadedTransform: MapTransform }) {
const [ viewport, setViewport ] = useState<HTMLDivElement | null>(null);
const mapApiRef = useRef({ setTransform } as unknown as RelationMapApi);
- zoom = useMapZoom({ viewport, loadedTransform, mapApiRef, getBoxes: () => boxes });
+ zoom = useMapZoom({ ntxId: "map", viewport, loadedTransform, mapApiRef, getBoxes: () => boxes });
return <MapViewport zoom={zoom} viewportRef={setViewport}><div className="note-box" /></MapViewport>;
}
@@ -258,7 +261,7 @@ describe("relation map zoom", () => {
container = document.createElement("div");
document.body.appendChild(container);
act(() => render(
- <Harness loadedTransform={loadedTransform} />,
+ <ParentComponent.Provider value={component}><Harness loadedTransform={loadedTransform} /></ParentComponent.Provider>,
container as HTMLElement));
}
@@ -304,6 +307,22 @@ describe("relation map zoom", () => {
expect(state()).toEqual({ positionX: 40, positionY: 251, scale: 0.5 });
});
+ it("takes the focus when its own note context asks for it, and when told to", () => {
+ mount({ x: 0, y: 0, scale: 1 });
+ const viewport = container?.querySelector(".relation-map-viewport");
+ const blur = () => act(() => { (document.activeElement as HTMLElement | null)?.blur(); });
+
+ act(() => { component.handleEvent("focusOnDetail", { ntxId: "other" }); });
+ expect(document.activeElement).not.toBe(viewport);
+
+ act(() => { component.handleEvent("focusOnDetail", { ntxId: "map" }); });
+ expect(document.activeElement).toBe(viewport);
+
+ blur();
+ act(() => zoom?.focus());
+ expect(document.activeElement).toBe(viewport);
+ });
+
it("steps the zoom", () => {
mount({ x: 0, y: 0, scale: 1 });
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 d759ac56d7..0549a242d9 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
@@ -16,7 +16,7 @@ import server from "../../../services/server";
import type { ShortcutHintDefinition } from "../../../services/shortcut_hints";
import toast from "../../../services/toast";
import { isMobile } from "../../../services/utils";
-import { useContextualShortcutHints, useEditorSpacedUpdate, useNoteLabelBoolean } from "../../react/hooks";
+import { useContextualShortcutHints, useEditorSpacedUpdate, useNoteLabelBoolean, useTriliumEvent } from "../../react/hooks";
import { useZoomPanPinch, useZoomPanWheel } from "../../react/zoom_pan";
import { useZoomPanKeyboard, ZOOM_PAN_HINTS } from "../../react/zoom_pan_keyboard";
import ShortcutHintButton from "../../shortcut_hints/shortcut_hint_button";
@@ -92,7 +92,7 @@ export default function RelationMap({ note, noteContext, ntxId }: TypeWidgetProp
});
const boxesRef = useRef<Map<string, Box>>(new Map());
- const mapZoom = useMapZoom({ viewport, loadedTransform, mapApiRef, getBoxes: () => boxesRef.current.values() });
+ const mapZoom = useMapZoom({ ntxId, viewport, loadedTransform, mapApiRef, getBoxes: () => boxesRef.current.values() });
const { getScale } = mapZoom;
const [ selection, setSelection ] = useState<PaneSelection | null>(null);
const noteIdsOnMap = useMemo(() => data?.notes.map((entry) => entry.noteId) ?? [], [ data ]);
@@ -103,8 +103,12 @@ export default function RelationMap({ note, noteContext, ntxId }: TypeWidgetProp
note,
ntxId,
mapApiRef,
- // Closes the pane, which covers part of the map where the note might be placed.
- onArm: () => paneRef.current?.close(),
+ // Closes the pane, which covers part of the map where the note might be placed, and takes
+ // the focus from the toolbar button so the keyboard still pans and zooms the map.
+ onArm: () => {
+ paneRef.current?.close();
+ mapZoom.focus();
+ },
onCreated: (noteId) => setSelection({ noteId, isNew: true })
});
const clickProps = useCanvasClicks({
@@ -224,7 +228,8 @@ export default function RelationMap({ note, noteContext, ntxId }: TypeWidgetProp
* 100%, and `fit` shows all of `getBoxes()` at once. The wheel zooms without the map being
* focused, since the map fills its pane and has no page to scroll.
*/
-export function useMapZoom({ viewport, loadedTransform, mapApiRef, getBoxes }: {
+export function useMapZoom({ ntxId, viewport, loadedTransform, mapApiRef, getBoxes }: {
+ ntxId: string | null | undefined;
/** The focusable element the keyboard and the wheel act on. */
viewport: HTMLDivElement | null;
loadedTransform: MapTransform | undefined;
@@ -263,7 +268,13 @@ export function useMapZoom({ viewport, loadedTransform, mapApiRef, getBoxes }: {
if (fitted) ref.current?.setTransform(fitted.x, fitted.y, fitted.scale, REVEAL_ANIMATION_MS);
}, [ ref, viewport, getBoxes ]);
- return { ...zoom, onTransform, getScale, moveBy, reset, fit };
+ // Focuses the viewport, so the keyboard pans and zooms the map.
+ const focus = useCallback(() => viewport?.focus({ preventScroll: true }), [ viewport ]);
+ useTriliumEvent("focusOnDetail", ({ ntxId: eventNtxId }) => {
+ if (eventNtxId === ntxId) focus();
+ });
+
+ return { ...zoom, onTransform, getScale, moveBy, reset, fit, focus };
}
/**