Skip to content

Commit 2bc5285

Browse files
committed
Prevent recentering map on config changes
Fixes #4
1 parent cf8d40d commit 2bc5285

1 file changed

Lines changed: 32 additions & 72 deletions

File tree

src/map-view.ts

Lines changed: 32 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export class MapView extends BasesView {
7676
// Internal rendering data
7777
private map: Map | null = null;
7878
private markers: MapMarker[] = [];
79+
private bounds: LngLatBounds | null = null;
7980
private coordinatesProp: BasesPropertyId | null = null;
8081
private markerIconProp: BasesPropertyId | null = null;
8182
private markerColorProp: BasesPropertyId | null = null;
@@ -155,11 +156,27 @@ export class MapView extends BasesView {
155156

156157
this.map.addControl(new CustomZoomControl(), 'top-right');
157158

158-
// Ensure the center is set after map loads (in case the style loading overrides it)
159+
// Ensure the center and zoom are set after map loads (in case the style loading overrides it)
159160
this.map.on('load', () => {
160-
// Only set center if we have non-default coordinates
161-
if (this.center[0] !== 0 || this.center[1] !== 0) {
162-
this.map?.setCenter([this.center[1], this.center[0]]); // MapLibre uses [lng, lat]
161+
if (!this.map) return;
162+
163+
const hasConfiguredCenter = this.center[0] !== 0 || this.center[1] !== 0;
164+
const hasConfiguredZoom = this.config.get('defaultZoom') && Number.isNumber(this.config.get('defaultZoom'));
165+
166+
// Set center based on configuration
167+
if (hasConfiguredCenter) {
168+
this.map.setCenter([this.center[1], this.center[0]]); // MapLibre uses [lng, lat]
169+
}
170+
else if (this.bounds) {
171+
this.map.setCenter(this.bounds.getCenter()); // Center on markers
172+
}
173+
174+
// Set zoom based on configuration
175+
if (hasConfiguredZoom) {
176+
this.map.setZoom(this.defaultZoom); // Use configured zoom
177+
}
178+
else if (this.bounds) {
179+
this.map.fitBounds(this.bounds, { padding: 20 }); // Fit all markers
163180
}
164181
});
165182

@@ -172,10 +189,6 @@ export class MapView extends BasesView {
172189
evt.preventDefault();
173190
this.showMapContextMenu(evt);
174191
});
175-
176-
if (this.data) {
177-
this.updateMarkers();
178-
}
179192
}
180193

181194
private destroyMap(): void {
@@ -189,23 +202,20 @@ export class MapView extends BasesView {
189202
this.map = null;
190203
}
191204
this.markers = [];
205+
this.bounds = null;
192206
}
193207

194208
public onDataUpdated(): void {
195209
this.containerEl.removeClass('is-loading');
196210
this.loadConfig();
197211
this.initializeMap();
198-
this.display();
199-
}
200212

201-
private display() {
202-
if (this.map) {
213+
if (this.map && this.data) {
203214
this.updateMarkers();
204215
}
205216
}
206217

207218
private loadConfig(): void {
208-
209219
// Load property configurations
210220
this.coordinatesProp = this.config.getAsPropertyId('coordinates');
211221
this.markerIconProp = this.config.getAsPropertyId('markerIcon');
@@ -402,27 +412,17 @@ export class MapView extends BasesView {
402412
}
403413

404414
private updateMarkers(): void {
415+
// Clear existing markers
416+
for (const markerData of this.markers) {
417+
markerData.marker.remove();
418+
}
419+
405420
if (!this.map || !this.data || !this.coordinatesProp) {
406-
this.clearMarkers();
407421
return;
408422
}
409423

410-
// Clear existing markers
411-
this.clearMarkers();
412-
413424
// Create markers for entries with valid coordinates
414-
this.createMarkersFromData();
415-
416-
// Update map view based on markers
417-
this.updateMapView();
418-
419-
// Apply pending map state if available
420-
this.applyPendingMapState();
421-
}
422-
423-
private createMarkersFromData(): void {
424-
const validMarkers: MapMarker[] = [];
425-
425+
const validMarkers: MapMarker[] = this.markers = [];
426426
for (const entry of this.data.data) {
427427
const coordinates = this.extractCoordinates(entry);
428428
if (coordinates) {
@@ -437,47 +437,14 @@ export class MapView extends BasesView {
437437
}
438438
}
439439

440-
this.markers = validMarkers;
441-
}
442-
443-
private updateMapView(): void {
444-
if (!this.map) return;
445-
446-
const hasConfiguredCenter = this.center[0] !== 0 || this.center[1] !== 0;
447-
const hasConfiguredZoom = this.config.get('defaultZoom') && Number.isNumber(this.config.get('defaultZoom'));
448-
449-
if (this.markers.length === 0) {
450-
// No markers - use configured defaults
451-
this.map.setCenter([this.center[1], this.center[0]]); // MapLibre uses [lng, lat]
452-
this.map.setZoom(this.defaultZoom);
453-
return;
454-
}
455-
456440
// Calculate bounds for all markers
457-
const bounds = new LngLatBounds();
458-
this.markers.forEach(markerData => {
441+
const bounds = this.bounds = new LngLatBounds();
442+
validMarkers.forEach(markerData => {
459443
const [lat, lng] = markerData.coordinates;
460444
bounds.extend([lng, lat]);
461445
});
462446

463-
// Set center based on configuration
464-
if (hasConfiguredCenter) {
465-
this.map.setCenter([this.center[1], this.center[0]]); // Use configured center
466-
}
467-
else {
468-
this.map.setCenter(bounds.getCenter()); // Center on markers
469-
}
470-
471-
// Set zoom based on configuration
472-
if (hasConfiguredZoom) {
473-
this.map.setZoom(this.defaultZoom); // Use configured zoom
474-
}
475-
else {
476-
this.map.fitBounds(bounds, { padding: 20 }); // Fit all markers
477-
}
478-
}
479-
480-
private applyPendingMapState(): void {
447+
// Apply pending map state if available
481448
if (this.pendingMapState && this.map) {
482449
const { center, zoom } = this.pendingMapState;
483450
if (center) {
@@ -789,13 +756,6 @@ export class MapView extends BasesView {
789756
return false;
790757
}
791758

792-
private clearMarkers(): void {
793-
for (const markerData of this.markers) {
794-
markerData.marker.remove();
795-
}
796-
this.markers = [];
797-
}
798-
799759
private showPopup(entry: BasesEntry, coordinates: [number, number]): void {
800760
if (!this.map) return;
801761

0 commit comments

Comments
 (0)