From 2346230d36a9a8eab7c40a7ec6a508bffd42a61b Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 4 Sep 2025 14:26:29 +0300 Subject: [PATCH] chore(react/collections/geomap): save state --- .../src/widgets/collections/geomap/index.tsx | 22 ++++++++++++++++++- .../src/widgets/collections/geomap/map.tsx | 18 ++++++++++++++- .../widgets/view_widgets/geo_view/index.ts | 9 +++----- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsx index 39081c5ed..8bdc8b7a8 100644 --- a/apps/client/src/widgets/collections/geomap/index.tsx +++ b/apps/client/src/widgets/collections/geomap/index.tsx @@ -1,9 +1,10 @@ import Map from "./map"; import "./index.css"; import { ViewModeProps } from "../interface"; -import { useNoteLabel } from "../../react/hooks"; +import { useNoteLabel, useSpacedUpdate } from "../../react/hooks"; import { DEFAULT_MAP_LAYER_NAME } from "./map_layer"; import { LatLng } from "leaflet"; +import { useEffect, useRef } from "preact/hooks"; const DEFAULT_COORDINATES: [number, number] = [3.878638227135724, 446.6630455551659]; const DEFAULT_ZOOM = 2; @@ -17,6 +18,19 @@ interface MapData { export default function GeoView({ note, viewStorage }: ViewModeProps) { const [ layerName ] = useNoteLabel(note, "map:style"); + const viewOptions = useRef(); + const spacedUpdate = useSpacedUpdate(() => { + viewStorage.store({ + view: viewOptions.current + }); + }, 5000); + + // Clean up on note change. + useEffect(() => { + viewStorage.restore().then(data => { + viewOptions.current = data?.view; + }); + }, [ note ]); return (
@@ -24,6 +38,12 @@ export default function GeoView({ note, viewStorage }: ViewModeProps) { coordinates={DEFAULT_COORDINATES} zoom={DEFAULT_ZOOM} layerName={layerName ?? DEFAULT_MAP_LAYER_NAME} + viewportChanged={(coordinates, zoom) => { + if (!viewOptions.current) return; + viewOptions.current.center = coordinates; + viewOptions.current.zoom = zoom; + spacedUpdate.scheduleUpdate(); + }} />
); diff --git a/apps/client/src/widgets/collections/geomap/map.tsx b/apps/client/src/widgets/collections/geomap/map.tsx index 4452be824..8dd6d1fc0 100644 --- a/apps/client/src/widgets/collections/geomap/map.tsx +++ b/apps/client/src/widgets/collections/geomap/map.tsx @@ -7,9 +7,10 @@ interface MapProps { coordinates: LatLng | [number, number]; zoom: number; layerName: string; + viewportChanged: (coordinates: LatLng, zoom: number) => void; } -export default function Map({ coordinates, zoom, layerName }: MapProps) { +export default function Map({ coordinates, zoom, layerName, viewportChanged }: MapProps) { const mapRef = useRef(null); const containerRef = useRef(null); @@ -60,5 +61,20 @@ export default function Map({ coordinates, zoom, layerName }: MapProps) { mapRef.current.setView(coordinates, zoom); }, [ mapRef, coordinates, zoom ]); + // Viewport callback. + useEffect(() => { + const map = mapRef.current; + if (!map) return; + + const updateFn = () => viewportChanged(map.getBounds().getCenter(), map.getZoom()); + map.on("moveend", updateFn); + map.on("zoomend", updateFn); + + return () => { + map.off("moveend", updateFn); + map.off("zoomend", updateFn); + }; + }, [ mapRef, viewportChanged ]); + return
; } diff --git a/apps/client/src/widgets/view_widgets/geo_view/index.ts b/apps/client/src/widgets/view_widgets/geo_view/index.ts index fa23b6e0e..60e9facd0 100644 --- a/apps/client/src/widgets/view_widgets/geo_view/index.ts +++ b/apps/client/src/widgets/view_widgets/geo_view/index.ts @@ -75,9 +75,6 @@ export default class GeoView extends ViewMode { this.#restoreViewportAndZoom(); const isEditable = !this.isReadOnly; - const updateFn = () => this.spacedUpdate.scheduleUpdate(); - map.on("moveend", updateFn); - map.on("zoomend", updateFn); map.on("click", (e) => this.#onMapClicked(e)) map.on("contextmenu", (e) => openMapContextMenu(this.parentNote.noteId, e, isEditable)); @@ -117,13 +114,13 @@ export default class GeoView extends ViewMode { if (map) { data = { view: { - center: map.getBounds().getCenter(), - zoom: map.getZoom() + center: , + zoom: } }; } - this.viewStorage.store(data); + } async #reloadMarkers() {