Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(geomap): stand the drawing tools across the head of a map with room for them
Elian Doran4983ff4ce558
diff --git a/apps/client/src/widgets/collections/geomap/DrawToolbar.spec.tsx b/apps/client/src/widgets/collections/geomap/DrawToolbar.spec.tsxindex 368aad5854..9da5a67a70 100644--- a/apps/client/src/widgets/collections/geomap/DrawToolbar.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/DrawToolbar.spec.tsx@@ -1,7 +1,8 @@/**- * The rail of drawing tools over a geo map (see DrawToolbar.tsx). What is checked is that every tool- * is offered, that a press arms the map and a press on an armed one stands it down, that the armed- * tool is worn held down, and that a map that may not be edited carries no rail at all.+ * The drawing tools over a geo map (see DrawToolbar.tsx). What is checked is that every tool is+ * offered, that a press arms the map and a press on an armed one stands it down, that the armed tool+ * is worn held down, that the width of the map settles where they stand, and that a map that may not+ * be edited carries none of them at all.*/import { act } from "preact/test-utils";import { describe, expect, it, vi } from "vitest";@@ -11,8 +12,21 @@ import type { DrawTool } from "./DrawShape";import DrawToolbar from "./DrawToolbar";import { ParentMap } from "./map";-/** Builds the rail over a map, which it asks nothing more of than being there at all. */-function renderRail({ map = {} as never, isReadOnly = false, drawingTool = null as DrawTool | null } = {}) {+/** A map of a given width, which is all the tools ask of one: where they stand follows from it. */+function fakeMap(width: number) {+ const container = document.createElement("div");+ Object.defineProperty(container, "clientWidth", { value: width, configurable: true });+ return { getContainer: () => container } as never;+}++/** A map with no room at its head, which is where the tools stand in a column down its side. */+const NARROW = 400;++/** A map roomy enough for the tools to stand across its head, clear of the search bar and the pane. */+const ROOMY = 1200;++/** Builds the tools over a map of the given width. */+function renderTools({ map = fakeMap(NARROW), isReadOnly = false, drawingTool = null as DrawTool | null } = {}) {const onToggleDrawing = vi.fn();let container: HTMLElement | undefined;act(() => {@@ -22,7 +36,7 @@ function renderRail({ map = {} as never, isReadOnly = false, drawingTool = null</ParentMap.Provider>);});- if (!container) throw new Error("the rail was not rendered");+ if (!container) throw new Error("the tools were not rendered");const all = () => [ ...container?.querySelectorAll<HTMLButtonElement>(".geo-draw-toolbar button") ?? [] ];return {@@ -33,7 +47,7 @@ function renderRail({ map = {} as never, isReadOnly = false, drawingTool = null};}-/** Every drawing tool on the rail, by the icon its button wears. */+/** Every drawing tool, by the icon its button wears. */const DRAW_TOOL_ICONS: { tool: DrawTool; icon: string }[] = [{ tool: "line", icon: "bx-vector" },{ tool: "polygon", icon: "bx-shape-polygon" },@@ -44,42 +58,51 @@ const DRAW_TOOL_ICONS: { tool: DrawTool; icon: string }[] = [describe("geo map DrawToolbar", () => {it("offers every drawing tool, wears the armed one as held down, and hands the arming to the map view", () => {for (const { tool, icon } of DRAW_TOOL_ICONS) {- const { button, onToggleDrawing } = renderRail();+ const { button, onToggleDrawing } = renderTools();expect(button(icon)?.classList.contains("active")).toBe(false);act(() => button(icon)?.click());expect(onToggleDrawing).toHaveBeenCalledWith(tool);- const armed = renderRail({ drawingTool: tool });+ const armed = renderTools({ drawingTool: tool });expect(armed.button(icon)?.classList.contains("active")).toBe(true);}});/*** A column down the leading edge rather than more buttons at the foot, which the scale and the- * camera group already fill at phone width (see DrawToolbar.tsx).+ * camera group already fill at this width (see DrawToolbar.tsx).*/- it("stands as a column at the middle of the leading edge", () => {- const { group, buttons } = renderRail();+ it("stands as a column at the middle of the leading edge of a narrow map", () => {+ const { group, buttons } = renderTools();expect(group()?.getAttribute("data-orientation")).toBe("vertical");expect(group()?.getAttribute("data-placement")).toBe("middle-start");expect(buttons()).toHaveLength(DRAW_TOOL_ICONS.length);});+ /** Where the head is wide enough to clear the search bar and the detail pane standing on it. */+ it("stands as a bar across the head of a roomy map", () => {+ const { group, buttons } = renderTools({ map: fakeMap(ROOMY) });++ expect(group()?.getAttribute("data-placement")).toBe("top-center");+ expect(group()?.hasAttribute("data-orientation")).toBe(false);+ expect(buttons()).toHaveLength(DRAW_TOOL_ICONS.length);+ });+/**- * Drawing is all the rail offers, so a map that may not be edited would carry four dead buttons+ * Drawing is all the group offers, so a map that may not be edited would carry four dead buttons* and nothing else. The editing bar shows its own refused, having somewhere to stand either way.*/it("comes off a map that may not be edited rather than standing there refused", () => {- const { group, buttons } = renderRail({ isReadOnly: true });+ const { group, buttons } = renderTools({ isReadOnly: true });expect(group()).toBeNull();expect(buttons()).toHaveLength(0);});it("stands aside where there is no map at all", () => {- const { buttons } = renderRail({ map: null as never });+ const { buttons } = renderTools({ map: null as never });expect(buttons()).toHaveLength(0);});diff --git a/apps/client/src/widgets/collections/geomap/DrawToolbar.tsx b/apps/client/src/widgets/collections/geomap/DrawToolbar.tsxindex dd8904a4cb..49480dcdf9 100644--- a/apps/client/src/widgets/collections/geomap/DrawToolbar.tsx+++ b/apps/client/src/widgets/collections/geomap/DrawToolbar.tsx@@ -1,4 +1,5 @@-import { useContext } from "preact/hooks";+import type { Map as MapLibreGLMap } from "maplibre-gl";+import { useContext, useLayoutEffect, useState } from "preact/hooks";import { t } from "../../../services/i18n";import OverlayControlGroup, { OverlayControlButton } from "../../react/OverlayControlGroup";@@ -15,12 +16,13 @@ interface DrawToolbarProps {}/**- * The drawing tools, in a column down the leading edge of the map.+ * The drawing tools, across the head of the map where there is room for them and in a column down+ * its leading edge where there is not.*- * A rail of their own rather than four more buttons on {@link EditToolbar}, because the foot of the+ * A group of their own rather than four more buttons on {@link EditToolbar}, because the foot of the* map is full: the scale holds one corner and {@link MapToolbar} the other, and what is left in the- * middle at phone width fits about three buttons. The middle of the leading edge is free at every- * width, and a column grows into the map's height rather than towards either corner.+ * middle at phone width fits about three buttons. The head is clear on a roomy map; on a narrow one+ * the leading edge is, and a column grows into the map's height rather than towards either corner.** Each button wears the shape its tool draws and no words, the shapes being what there is to say.* The armed tool is shown held down, and its tooltip says what a second press does, as the marker@@ -28,13 +30,19 @@ interface DrawToolbarProps {*/export default function DrawToolbar({ isReadOnly, drawingTool, onToggleDrawing }: DrawToolbarProps) {const map = useContext(ParentMap);+ const acrossTheHead = useRoomAcrossTheHead(map);- // No rail over a map that could not be drawn (see the WebGL fallback in map.tsx), and none over- // one that may not be edited: every button on it draws, so all four would be disabled at once.+ // No tools over a map that could not be drawn (see the WebGL fallback in map.tsx), and none+ // over one that may not be edited: every button draws, so all four would be disabled at once.if (!map || isReadOnly) return null;return (- <OverlayControlGroup className="geo-draw-toolbar" placement="middle-start" vertical overCanvas>+ <OverlayControlGroup+ className="geo-draw-toolbar"+ placement={acrossTheHead ? "top-center" : "middle-start"}+ vertical={!acrossTheHead}+ overCanvas+ >{DRAW_TOOLS.map(({ tool, icon, title }) => (<OverlayControlButtonkey={tool}@@ -48,7 +56,39 @@ export default function DrawToolbar({ isReadOnly, drawingTool, onToggleDrawing });}-/** The tools in the order they stand on the rail, each wearing the shape it draws. */+/** How wide the map must be for the tools to clear the search bar and the detail pane at its head,+ * a group centred between the insets keeping half of what it does not take on either side. */+const HEAD_BAR_MIN_WIDTH = 920;++/**+ * Whether the map has room for the tools across its head, followed as the map is resized. Measured+ * rather than asked of the device: a map sharing a desktop screen with another note is as narrow as+ * a phone's, and the search bar and the detail pane stand at the head of it either way.+ *+ * Measured before the paint, since the map arrives a render after the group does and the column is+ * what a group with nothing measured yet would be drawn as.+ */+function useRoomAcrossTheHead(map: MapLibreGLMap | null) {+ const [ hasRoom, setHasRoom ] = useState(false);++ useLayoutEffect(() => {+ if (!map) return;++ const container = map.getContainer();+ function measure() {+ setHasRoom(container.clientWidth >= HEAD_BAR_MIN_WIDTH);+ }++ measure();+ const observer = new ResizeObserver(measure);+ observer.observe(container);+ return () => observer.disconnect();+ }, [ map ]);++ return hasRoom;+}++/** The tools in the order they stand, each wearing the shape it draws. */const DRAW_TOOLS: { tool: DrawTool; icon: string; title: () => string }[] = [{ tool: "line", icon: "bx-vector", title: () => t("geo-map.draw-line") },{ tool: "polygon", icon: "bx-shape-polygon", title: () => t("geo-map.draw-polygon") },diff --git a/apps/client/src/widgets/collections/geomap/index.css b/apps/client/src/widgets/collections/geomap/index.cssindex 2c20a45d32..d2a712622d 100644--- a/apps/client/src/widgets/collections/geomap/index.css+++ b/apps/client/src/widgets/collections/geomap/index.css@@ -165,10 +165,10 @@ body[dir=rtl] .geo-map-container {/** Everything the control groups standing over the map have in common — the camera in the foot* corner (MapToolbar), the editing actions at the middle of the foot (EditToolbar), the drawing- * tools down the leading edge (DrawToolbar) — said here once for all of them: the room they keep- * from the map's edges, the layer they stand on, and the set the map's furniture is dressed in, all- * under the names the shared group reads (see the overlay buttons in theme-next/forms.css). What is- * left for each of them to say is where it stands.+ * tools at the head or down the leading edge (DrawToolbar) — said here once for all of them: the+ * room they keep from the map's edges, the layer they stand on, and the set the map's furniture is+ * dressed in, all under the names the shared group reads (see the overlay buttons in+ * theme-next/forms.css). What is left for each of them to say is where it stands.** Set on the groups rather than on the map, so that nothing else standing over the map is dressed* for a theme the reader did not choose — a marker's preview is the app's own note tooltip, and it