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
style(geomap): dress the zoom controls as the image viewer's
Elian Doran9ad3b4ecd57e
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex ebe76ba37b..13ec1aed6f 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2749,6 +2749,7 @@"webgl-unavailable": "This map can't be drawn because WebGL isn't available. Check that hardware acceleration is enabled and that your graphics drivers are up to date.","zoom-in": "Zoom in","zoom-out": "Zoom out",+ "reset-zoom": "Reset zoom","fullscreen": "Fullscreen","exit-fullscreen": "Exit fullscreen","close-details": "Close",diff --git a/apps/client/src/widgets/collections/geomap/MapToolbar.css b/apps/client/src/widgets/collections/geomap/MapToolbar.cssindex cb6ffb35f9..6cd3b45286 100644--- a/apps/client/src/widgets/collections/geomap/MapToolbar.css+++ b/apps/client/src/widgets/collections/geomap/MapToolbar.css@@ -1,21 +1,36 @@/*- * The bar stands in the bottom corner, where every other bar floating over content in the app stands- * — the mind map's own (see MapToolbar.css there), the relation map's zoom buttons — rather than in- * the top corner MapLibre keeps its zoom buttons in. The corner is the map's to give: its attribution+ * The controls stand in the bottom corner, where every other set of zoom controls floating over+ * content in the app stands — the image viewer's, whose control group this is — rather than in the+ * top corner MapLibre keeps its zoom buttons in. The corner is the map's to give: its attribution* has been moved to the foot of the other side, beside the scale (see map.tsx).** Held off the edges by as much as everything else standing over the map is, which is settled for- * both in index.css, so the bar and the scale opposite it stand on the same line.+ * both in index.css, so the group and the scale opposite it stand on the same line.** Above the canvas whatever order the two end up in: MapLibre builds its part of the container* itself, so what Preact renders alongside is not reliably the later sibling. The number clears* MapLibre's own control corners, which sit at 2.*/.geo-map-container .geo-map-toolbar {+ position: absolute;bottom: var(--geo-map-inset);inset-inline-end: var(--geo-map-inset);z-index: 3;++ /* Dressed in the set the map's furniture shares, handed over under the names the control group+ reads (see the overlay buttons in theme-next/forms.css) — the palette itself is settled in+ index.css, which is also where a dark map darkens it. The hover pair has no overlay-button+ name of its own, so the group's is asked for directly. */+ --overlay-button-background: var(--geo-map-panel-background);+ --overlay-button-color: var(--geo-map-panel-text-color);+ --overlay-button-backdrop-filter: var(--dropdown-backdrop-filter, blur(10px));+ --opaque-icon-button-hover-background-color: var(--geo-map-panel-hover-background);+ --opaque-icon-button-hover-color: var(--geo-map-panel-hover-text-color);}-/* The bar is dressed in the set the map's furniture shares, handed over once for every bar standing- over this map — see the palette handoff in index.css, which is also where a dark map darkens it. */+/* A button that would do nothing says as much rather than merely failing to answer — the group's+ own styling leaves a disabled button looking ready (see OverlayToolbar.css for the same rule). */+.geo-map-container .geo-map-toolbar button:disabled {+ opacity: .4;+ pointer-events: none;+}diff --git a/apps/client/src/widgets/collections/geomap/MapToolbar.spec.tsx b/apps/client/src/widgets/collections/geomap/MapToolbar.spec.tsxindex e426dbd023..ca0422ea51 100644--- a/apps/client/src/widgets/collections/geomap/MapToolbar.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/MapToolbar.spec.tsx@@ -1,8 +1,9 @@/**- * The bar over a geo map, which used to be MapLibre's own controls — white boxes on a map that may- * well be dark, dressed in neither Trilium's buttons nor its colors. What is checked here is what- * they did for us: the two steps, a step that would carry the map past either end of the range it is- * allowed being refused rather than merely doing nothing, and the screen being given and taken back.+ * The controls over a geo map, which used to be MapLibre's own — white boxes on a map that may well+ * be dark, dressed in neither Trilium's buttons nor its colors. What is checked here is what they+ * did for us: the two steps, a step that would carry the map past either end of the range it is+ * allowed being refused rather than merely doing nothing, the readout between the steps saying how+ * close in the map is and taking it back out, and the screen being given and taken back.*/import type { Map as MapLibreGLMap } from "maplibre-gl";import { render } from "preact";@@ -10,15 +11,16 @@ import { act } from "preact/test-utils";import { beforeEach, describe, expect, it, vi } from "vitest";import { renderInto } from "../../../test/render";-import { ParentMap } from "./map";+import { DEFAULT_ZOOM, ParentMap } from "./map";import MapToolbar from "./MapToolbar";-/** The order the bar lays its buttons out in. */-const ZOOM_IN = 0;-const ZOOM_OUT = 1;-const FULLSCREEN = 2;+/** The order the group lays its buttons out in — the image viewer's, with the screen at the end. */+const ZOOM_OUT = 0;+const ZOOM_LEVEL = 1;+const ZOOM_IN = 2;+const FULLSCREEN = 3;-/** A map that zooms, says so, and stands somewhere — all this bar asks of one. */+/** A map that zooms, says so, and stands somewhere — all these controls ask of one. */function fakeMap({ zoom = 5, minZoom = 2, maxZoom = 22 } = {}) {const listeners = new Set<() => void>();const container = document.createElement("div");@@ -37,8 +39,8 @@ function fakeMap({ zoom = 5, minZoom = 2, maxZoom = 22 } = {}) {getContainer: () => container,zoomIn: vi.fn(() => setZoom(zoom + 1)),zoomOut: vi.fn(() => setZoom(zoom - 1)),- /** The map being zoomed by something other than these buttons — the wheel, or a pinch. */- zoomTo: setZoom,+ /** The map being zoomed to a level outright — the readout's reset, or a restored view. */+ zoomTo: vi.fn(setZoom),on: (event: string, listener: () => void) => { if (event === "zoom") listeners.add(listener); },off: (event: string, listener: () => void) => { if (event === "zoom") listeners.delete(listener); },get listenerCount() { return listeners.size; }@@ -56,7 +58,7 @@ beforeEach(() => {document.exitFullscreen = vi.fn(async () => {});});-/** Builds the bar over a map and settles it, so that it is listening before it is spoken to. */+/** Builds the controls over a map and settles them, so they are listening before being spoken to. */function renderToolbar(map: ReturnType<typeof fakeMap> | null) {let container: HTMLElement | undefined;act(() => {@@ -71,7 +73,7 @@ function renderToolbar(map: ReturnType<typeof fakeMap> | null) {}function buttons(container: HTMLElement) {- return [ ...container.querySelectorAll<HTMLButtonElement>(".tn-overlay-toolbar button") ];+ return [ ...container.querySelectorAll<HTMLButtonElement>(".tn-overlay-control-group button") ];}function press(container: HTMLElement, index: number) {@@ -79,12 +81,13 @@ function press(container: HTMLElement, index: number) {}describe("geo map MapToolbar", () => {- it("offers what the map's own controls did, in one row", () => {+ it("offers what the map's own controls did, laid out as the image viewer's group", () => {const container = renderToolbar(fakeMap());expect(buttons(container).map((button) => button.className)).toEqual([- expect.stringContaining("bx-zoom-in"),- expect.stringContaining("bx-zoom-out"),+ expect.stringContaining("bx-minus-circle"),+ expect.stringContaining("tn-overlay-text-button"),+ expect.stringContaining("bx-plus-circle"),expect.stringContaining("bx-fullscreen")]);});@@ -106,6 +109,26 @@ describe("geo map MapToolbar", () => {expect(map.zoomOut).toHaveBeenCalled();});+ it("says how close in the map is, following the wheel as much as the buttons", () => {+ const map = fakeMap({ zoom: 5 });+ const container = renderToolbar(map);+ expect(buttons(container)[ZOOM_LEVEL].textContent).toBe("5.0");++ // As the wheel or a pinch would.+ act(() => map.zoomTo(12.5));++ expect(buttons(container)[ZOOM_LEVEL].textContent).toBe("12.5");+ });++ it("takes the map back out to the whole world when the readout is pressed", () => {+ const map = fakeMap({ zoom: 15 });+ const container = renderToolbar(map);++ press(container, ZOOM_LEVEL);++ expect(map.zoomTo).toHaveBeenCalledWith(DEFAULT_ZOOM);+ });+it("disables the step that would carry the map past the range it is allowed", () => {const atTheTop = renderToolbar(fakeMap({ zoom: 22 }));expect(buttons(atTheTop)[ZOOM_IN].disabled).toBe(true);@@ -159,7 +182,7 @@ describe("geo map MapToolbar", () => {const container = renderToolbar(map);expect(map.listenerCount).toBe(1);- // A map torn down under a bar that stayed behind would go on being reported to.+ // A map torn down under controls that stayed behind would go on being reported to.act(() => { render(null, container); });expect(map.listenerCount).toBe(0);diff --git a/apps/client/src/widgets/collections/geomap/MapToolbar.tsx b/apps/client/src/widgets/collections/geomap/MapToolbar.tsxindex d9d328bf95..1b30241d63 100644--- a/apps/client/src/widgets/collections/geomap/MapToolbar.tsx+++ b/apps/client/src/widgets/collections/geomap/MapToolbar.tsx@@ -1,22 +1,22 @@import "./MapToolbar.css";import type { Map as MapLibreGLMap } from "maplibre-gl";-import { useContext, useEffect, useState } from "preact/hooks";+import { useContext, useEffect, useRef, useState } from "preact/hooks";import { t } from "../../../services/i18n";-import { useFullscreen } from "../../react/hooks";-import OverlayToolbar, { OverlayToolbarButton } from "../../react/OverlayToolbar";-import { ParentMap } from "./map";+import { useFullscreen, useStaticTooltip } from "../../react/hooks";+import { DEFAULT_ZOOM, ParentMap } from "./map";/**- * The bar standing in the corner of a geo map: how close in the map is drawn, and how much of the- * screen it is given.+ * The controls standing in the corner of a geo map: how close in the map is drawn, and how much of+ * the screen it is given.** MapLibre offers bars of its own for both (`NavigationControl`, which is what stood here, and* `FullscreenControl`), dressed in neither Trilium's buttons nor Trilium's colors — a white box with* hairline-separated squares in it, on a map that may well be dark. What they do is done here- * instead, on the surface every bar standing over a canvas shares (see {@link OverlayToolbar}),- * which is what the mind map's own bar stands on too.+ * instead, on the control group the image viewer's zoom buttons stand on (`tn-overlay-control-group`,+ * see {@link ImageViewer}): a step out, a step in, and between them a readout saying how close in the+ * map is — which, pressed, takes the map back out to the whole world.*/export default function MapToolbar() {const map = useContext(ParentMap);@@ -25,49 +25,81 @@ export default function MapToolbar() {// everything the bar above the map offers is on the map's right-click menu as well.const [ isFullscreen, toggleFullscreen ] = useFullscreen(map?.getContainer());+ const zoomOutRef = useRef<HTMLButtonElement>(null);+ const zoomLevelRef = useRef<HTMLButtonElement>(null);+ const zoomInRef = useRef<HTMLButtonElement>(null);+ const fullscreenRef = useRef<HTMLButtonElement>(null);++ // The group stands at the foot of the map, so its tooltips open away from that edge, where+ // they would otherwise fall off.+ useStaticTooltip(zoomOutRef, { title: t("geo-map.zoom-out"), placement: "top" });+ useStaticTooltip(zoomLevelRef, { title: t("geo-map.reset-zoom"), placement: "top" });+ useStaticTooltip(zoomInRef, { title: t("geo-map.zoom-in"), placement: "top" });+ useStaticTooltip(fullscreenRef, {+ title: isFullscreen ? t("geo-map.exit-fullscreen") : t("geo-map.fullscreen"),+ placement: "top"+ });+if (!map) return null;// Before the first report, which follows the very next tick: what the map already says it is.const current = zoom ?? map.getZoom();return (- // The bar stands at the foot of the map, so its tooltips open away from that edge, where- // they would otherwise fall off.- <OverlayToolbar className="geo-map-toolbar" titlePosition="top">- <OverlayToolbarButton- icon="bx bx-zoom-in"- text={t("geo-map.zoom-in")}- disabled={current >= map.getMaxZoom()}- onClick={() => map.zoomIn()}- />-- <OverlayToolbarButton- icon="bx bx-zoom-out"- text={t("geo-map.zoom-out")}+ <div+ className="geo-map-toolbar tn-overlay-control-group"+ /* Keep a press on the controls from reaching the canvas underneath, which would+ otherwise take it for the start of a drag. */+ onMouseDown={(e) => e.stopPropagation()}+ >+ <button+ ref={zoomOutRef}+ type="button"+ className="tn-overlay-icon-button bx bx-minus-circle"+ aria-label={t("geo-map.zoom-out")}disabled={current <= map.getMinZoom()}onClick={() => map.zoomOut()}/>-- <OverlayToolbarButton- icon={isFullscreen ? "bx bx-exit-fullscreen" : "bx bx-fullscreen"}- text={isFullscreen ? t("geo-map.exit-fullscreen") : t("geo-map.fullscreen")}+ <button+ ref={zoomLevelRef}+ type="button"+ className="tn-overlay-text-button geo-map-zoom-level"+ aria-label={t("geo-map.reset-zoom")}+ onClick={() => map.zoomTo(DEFAULT_ZOOM)}+ >+ {current.toFixed(1)}+ </button>+ <button+ ref={zoomInRef}+ type="button"+ className="tn-overlay-icon-button bx bx-plus-circle"+ aria-label={t("geo-map.zoom-in")}+ disabled={current >= map.getMaxZoom()}+ onClick={() => map.zoomIn()}+ />+ <button+ ref={fullscreenRef}+ type="button"+ className={`tn-overlay-icon-button bx ${isFullscreen ? "bx-exit-fullscreen" : "bx-fullscreen"}`}+ aria-label={isFullscreen ? t("geo-map.exit-fullscreen") : t("geo-map.fullscreen")}// Nothing here is measured across the change: the map keeps the middle of its view// through a resize of its own accord, and it is told of the new size by the view// itself (see `useElementSize` in map.tsx).onClick={() => void toggleFullscreen()}/>- </OverlayToolbar>+ </div>);}/*** How close in the map is drawn, followed as it changes — by these buttons, by the wheel, or by the- * view being restored. What it is read for is whether there is any room left to zoom, which is what- * leaves a button that would do nothing disabled instead of idle: MapLibre clamps a step past either- * end silently.+ * view being restored. Read for the readout between the steps, and for whether there is any room+ * left to zoom, which is what leaves a button that would do nothing disabled instead of idle:+ * MapLibre clamps a step past either end silently.*- * `zoom` rather than `zoomend`, so that a button reaching the end of the range is disabled as the- * map arrives there rather than a moment later — the two steps are animated.+ * `zoom` rather than `zoomend`, so that the readout counts through the animated step rather than+ * jumping at its end, and a button reaching the end of the range is disabled as the map arrives+ * there rather than a moment later.*/function useMapZoom(map: MapLibreGLMap | null) {const [ zoom, setZoom ] = useState<number | null>(null);diff --git a/apps/client/src/widgets/collections/geomap/index.css b/apps/client/src/widgets/collections/geomap/index.cssindex b4b76f972a..9e4e89632d 100644--- a/apps/client/src/widgets/collections/geomap/index.css+++ b/apps/client/src/widgets/collections/geomap/index.css@@ -24,17 +24,19 @@agree, and matching the room the mind map gives its bars (see MindMap.css). */--geo-map-inset: 20px;- /* How tall the bar of buttons in the corner stands, which is what anything else opened over that- corner has to end above (see DetailPane.css). The mind map's bar is the same height, being the- same bar (see MindMap.css). */- --geo-map-toolbar-height: 42px;+ /* How tall the controls in the corner stand, which is what anything else opened over that corner+ has to end above (see DetailPane.css). The image viewer's zoom controls are the same height,+ being the same group (see the overlay buttons in theme-next/forms.css); the fallback is that+ height where no theme names one. */+ --geo-map-toolbar-height: var(--overlay-button-height, 28px);/** What everything standing over the map is dressed in — our own bar and MapLibre's scale and* attribution alike, so that the three read as one set rather than as ours beside theirs. The* app's menu surface, which is what every panel floating over content in Trilium is drawn on- * (see OverlayToolbar.css); the map's furniture asks for it by these names, and the bar has them- * handed to it under the names the shared component reads (see MapToolbar.css).+ * (see OverlayToolbar.css); the map's furniture asks for it by these names, and what stands over+ * the map has them handed to it under the names its own surface reads (the editing bar below,+ * the zoom controls in MapToolbar.css).** Thinner than a menu's, though, and that is the whole of the difference: a menu is opened over a* page that is mostly one flat colour, where 85% of that colour over the rest of it is glass@@ -96,11 +98,12 @@}/*- * The bars standing over the map — the camera bar in its foot corner (MapToolbar) and the editing- * bar at its head (EditToolbar) — dressed in the set the map's furniture shares, handed over here- * under the names the shared bar reads (see OverlayToolbar.css).+ * The editing bar at the map's head (EditToolbar), dressed in the set the map's furniture shares,+ * handed over here under the names the shared bar reads (see OverlayToolbar.css). The zoom controls+ * in the foot corner wear the same set under different names, being the image viewer's control+ * group rather than a bar — their handoff is in MapToolbar.css.*- * Set on the bars rather than on the map, so that nothing else standing over the map is dressed for+ * Set on the bar 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* belongs to the app. The last pair marks a choice in force, which is the editing bar's + while the* map waits to be told a place.diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsxindex 22d898bd1a..f39d111e9b 100644--- a/apps/client/src/widgets/collections/geomap/index.tsx+++ b/apps/client/src/widgets/collections/geomap/index.tsx@@ -22,14 +22,13 @@ import DetailPane, { PaneSelection } from "./DetailPane";import EditToolbar from "./EditToolbar";import GhostPin from "./GhostPin";import { GpxTrack } from "./GpxTrack";-import Map, { GeoMouseEvent } from "./map";+import Map, { DEFAULT_ZOOM, GeoMouseEvent } from "./map";import { DEFAULT_MAP_LAYER_NAME, MAP_LAYERS, MapLayer } from "./map_layer";import MapToolbar from "./MapToolbar";import Markers, { LOCATION_ATTRIBUTE } from "./Markers";import Tooltips from "./Tooltips";const DEFAULT_COORDINATES: [number, number] = [3.878638227135724, 446.6630455551659];-const DEFAULT_ZOOM = 2;/*** The instruction toast that says what the map is waiting for. One id for both kinds of placement:diff --git a/apps/client/src/widgets/collections/geomap/map.tsx b/apps/client/src/widgets/collections/geomap/map.tsxindex 2666c2a3a4..59074406b0 100644--- a/apps/client/src/widgets/collections/geomap/map.tsx+++ b/apps/client/src/widgets/collections/geomap/map.tsx@@ -19,6 +19,10 @@ export interface GeoMouseEvent {export const ParentMap = createContext<MapLibreGLMap | null>(null);+/** The zoom a map opens at where no view has been saved: far enough out to see the whole world.+ * Also what the zoom readout returns the map to when pressed (see {@link MapToolbar}). */+export const DEFAULT_ZOOM = 2;+/*** Whether the map's style has finished loading, and so whether a source or a layer can be added to it.*@@ -187,7 +191,7 @@ export default function Map({ coordinates, zoom, layerData, viewportChanged, chi});// No navigation control of MapLibre's own: the zoom buttons are Trilium's (see MapToolbar),- // dressed as every other bar standing over a canvas in the app. No compass either — nothing+ // dressed as the image viewer's zoom controls are. No compass either — nothing// here persists a bearing, so the button would offer to undo a rotation the map never// remembers.setMap(mapInstance);