From 20f67fe28f0ef30d40f89eee0e3399fe02153f13 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Tue, 26 Aug 2025 15:36:06 +0200 Subject: [PATCH] do not keep map state when switching between search panes (#293) Restores the behaviour the UI had before switching await from stores. --- src/components/Header.svelte | 4 ++-- src/components/Map.svelte | 21 ++++++++++++--------- src/state/MapState.svelte.js | 15 ++++++++++----- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/components/Header.svelte b/src/components/Header.svelte index a6eb154..6f92ced 100644 --- a/src/components/Header.svelte +++ b/src/components/Header.svelte @@ -94,8 +94,8 @@ {/if} diff --git a/src/components/Map.svelte b/src/components/Map.svelte index e259186..9923d09 100644 --- a/src/components/Map.svelte +++ b/src/components/Map.svelte @@ -37,6 +37,12 @@ ].join(','); } + function setMapState() { + mapState.viewboxStr = mapViewboxAsString(map); + mapState.center = map.getCenter(); + mapState.zoom = map.getZoom(); + } + function createMap(container) { const attribution = Nominatim_Config.Map_Tile_Attribution; @@ -44,8 +50,9 @@ attributionControl: false, scrollWheelZoom: true, // !L.Browser.touch, touchZoom: false, - center: mapState.center, - zoom: mapState.zoom + center: L.latLng(Nominatim_Config.Map_Default_Lat, + Nominatim_Config.Map_Default_Lon), + zoom: Nominatim_Config.Map_Default_Zoom }); if (typeof Nominatim_Config.Map_Default_Bounds !== 'undefined' && Nominatim_Config.Map_Default_Bounds) { @@ -56,7 +63,7 @@ L.control.attribution({ prefix: 'Leaflet' }).addTo(map); } - mapState.viewboxStr = mapViewboxAsString(map); + setMapState(); L.control.scale().addTo(map); @@ -73,12 +80,7 @@ new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map); } - map.on('move', () => { - mapState.center = map.getCenter(); - mapState.zoom = map.getZoom(); - mapState.viewboxStr = mapViewboxAsString(map); - }); - + map.on('move', setMapState); map.on('mousemove', (e) => { mapState.mousePos = e.latlng; }); map.on('click', (e) => { mapState.lastClick = e.latlng; }); } @@ -89,6 +91,7 @@ return { destroy: () => { + mapState.reset(); map.remove(); } }; diff --git a/src/state/MapState.svelte.js b/src/state/MapState.svelte.js index c30dad6..38c0c1d 100644 --- a/src/state/MapState.svelte.js +++ b/src/state/MapState.svelte.js @@ -1,12 +1,17 @@ -import {latLng } from 'leaflet'; - class MapState { - center = $state(latLng(Nominatim_Config.Map_Default_Lat, - Nominatim_Config.Map_Default_Lon)); - zoom = $state(Nominatim_Config.Map_Default_Zoom); + center = $state(); + zoom = $state(); viewboxStr = $state(); lastClick = $state(); mousePos = $state(); + + reset() { + this.center = undefined; + this.zoom = undefined; + this.viewboxStr = ''; + this.lastClick = undefined; + this.mousePos = undefined; + } } export const mapState = new MapState(); -- 2.39.5