@classic-homes/maps-svelte 0.5.2 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/components/base/MapError.svelte +1 -1
- package/dist/lib/components/controls/BasemapSwitcher.svelte +49 -6
- package/dist/lib/components/controls/LayerManager.svelte +1 -1
- package/dist/lib/components/controls/LegendGroup.svelte +1 -1
- package/dist/lib/components/core/Map.svelte +57 -61
- package/dist/lib/components/core/Marker.svelte +7 -0
- package/dist/lib/components/core/Source.svelte +98 -20
- package/dist/lib/components/core/sourceRegistry.d.ts +20 -0
- package/dist/lib/components/core/sourceRegistry.js +26 -0
- package/dist/lib/components/layers/useLayer.svelte.js +95 -42
- package/package.json +1 -1
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
{#if onRetry}
|
|
48
48
|
<button
|
|
49
49
|
onclick={onRetry}
|
|
50
|
-
class="mt-2 rounded-md border border-input bg-background px-4 py-2 text-sm font-medium shadow-sm
|
|
50
|
+
class="mt-2 rounded-md border border-input bg-background px-4 py-2 text-sm font-medium shadow-sm hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
|
|
51
51
|
>
|
|
52
52
|
Retry
|
|
53
53
|
</button>
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { getContext, untrack } from 'svelte';
|
|
2
|
+
import { getContext, tick, untrack } from 'svelte';
|
|
3
3
|
import type { BasemapSwitcherProps, BasemapDefinition } from '@classic-homes/maps-core';
|
|
4
4
|
import {
|
|
5
5
|
FREE_BASEMAPS,
|
|
6
6
|
getAvailableBasemaps,
|
|
7
|
+
getGridNavigationIndex,
|
|
7
8
|
resolveBasemapStyle,
|
|
8
9
|
resolveBasemapThumbnail,
|
|
9
10
|
} from '@classic-homes/maps-core';
|
|
@@ -40,6 +41,9 @@
|
|
|
40
41
|
let internalActiveId = $state<string | undefined>(undefined);
|
|
41
42
|
let containerElement: HTMLDivElement;
|
|
42
43
|
let toggleButton: HTMLButtonElement;
|
|
44
|
+
const optionButtons: (HTMLButtonElement | null)[] = $state([]);
|
|
45
|
+
// Roving tab stop: the one option in the radiogroup that Tab reaches
|
|
46
|
+
let tabStopIndex = $state(0);
|
|
43
47
|
let panelStyle = $state('');
|
|
44
48
|
let panelColumns = $state(1);
|
|
45
49
|
let compactPanel = $state(false);
|
|
@@ -110,6 +114,39 @@
|
|
|
110
114
|
const currentActiveId = $derived(
|
|
111
115
|
activeBasemapId ?? internalActiveId ?? availableBasemaps()[0]?.id
|
|
112
116
|
);
|
|
117
|
+
const activeIndex = $derived(
|
|
118
|
+
Math.max(
|
|
119
|
+
0,
|
|
120
|
+
availableBasemaps().findIndex((basemap) => basemap.id === currentActiveId)
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
// On open, the checked option takes the tab stop and focus. Only isOpen is
|
|
125
|
+
// tracked: re-focusing whenever the active basemap changes would pull focus
|
|
126
|
+
// around while the panel is in use.
|
|
127
|
+
$effect(() => {
|
|
128
|
+
if (!isOpen) return;
|
|
129
|
+
const index = untrack(() => activeIndex);
|
|
130
|
+
tabStopIndex = index;
|
|
131
|
+
// The options render with this same update; focus once they are in the DOM
|
|
132
|
+
tick().then(() => optionButtons[index]?.focus());
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Arrow keys move focus between options; Enter/Space selects. Selecting on
|
|
136
|
+
// arrow (the automatic radiogroup variant) would swap the map style and close
|
|
137
|
+
// the panel on the first key press.
|
|
138
|
+
function handleOptionsKeyDown(e: KeyboardEvent) {
|
|
139
|
+
const next = getGridNavigationIndex(
|
|
140
|
+
e.key,
|
|
141
|
+
tabStopIndex,
|
|
142
|
+
availableBasemaps().length,
|
|
143
|
+
panelColumns
|
|
144
|
+
);
|
|
145
|
+
if (next === null) return;
|
|
146
|
+
e.preventDefault();
|
|
147
|
+
tabStopIndex = next;
|
|
148
|
+
optionButtons[next]?.focus();
|
|
149
|
+
}
|
|
113
150
|
|
|
114
151
|
// Close on outside click
|
|
115
152
|
$effect(() => {
|
|
@@ -195,22 +232,28 @@
|
|
|
195
232
|
style={panelStyle}
|
|
196
233
|
>
|
|
197
234
|
<div
|
|
198
|
-
role="
|
|
235
|
+
role="radiogroup"
|
|
199
236
|
aria-label="Select basemap"
|
|
237
|
+
tabindex="-1"
|
|
200
238
|
class="grid gap-1.5"
|
|
201
239
|
style="grid-template-columns: repeat({panelColumns}, minmax(0, 1fr));"
|
|
240
|
+
onkeydown={handleOptionsKeyDown}
|
|
202
241
|
>
|
|
203
|
-
{#each availableBasemaps() as basemap (basemap.id)}
|
|
242
|
+
{#each availableBasemaps() as basemap, index (basemap.id)}
|
|
204
243
|
{@const isActive = basemap.id === currentActiveId}
|
|
205
244
|
{@const thumbUrl = resolveBasemapThumbnail(basemap, apiKeys)}
|
|
206
245
|
<button
|
|
246
|
+
bind:this={optionButtons[index]}
|
|
207
247
|
type="button"
|
|
208
|
-
|
|
248
|
+
role="radio"
|
|
249
|
+
aria-checked={isActive}
|
|
250
|
+
tabindex={index === tabStopIndex ? 0 : -1}
|
|
209
251
|
class={cn(
|
|
210
252
|
'flex min-h-[44px] min-w-0 flex-col items-center gap-1 p-1 rounded cursor-pointer',
|
|
211
|
-
'hover:bg-muted
|
|
253
|
+
'hover:bg-muted',
|
|
212
254
|
isActive && 'ring-2 ring-primary'
|
|
213
255
|
)}
|
|
256
|
+
onfocus={() => (tabStopIndex = index)}
|
|
214
257
|
onclick={() => handleSelect(basemap)}
|
|
215
258
|
title={basemap.name}
|
|
216
259
|
>
|
|
@@ -239,7 +282,7 @@
|
|
|
239
282
|
class={cn(
|
|
240
283
|
'w-[44px] h-[44px] flex items-center justify-center',
|
|
241
284
|
'bg-card rounded shadow-lg border border-black dark:border-gray-400',
|
|
242
|
-
'hover:bg-muted
|
|
285
|
+
'hover:bg-muted cursor-pointer',
|
|
243
286
|
'text-foreground'
|
|
244
287
|
)}
|
|
245
288
|
onclick={handleToggle}
|
|
@@ -954,7 +954,7 @@
|
|
|
954
954
|
class={cn(
|
|
955
955
|
'w-[40px] h-[40px] flex items-center justify-center',
|
|
956
956
|
'bg-card rounded shadow-lg border border-black dark:border-gray-400',
|
|
957
|
-
'hover:bg-muted
|
|
957
|
+
'hover:bg-muted cursor-pointer',
|
|
958
958
|
'text-foreground'
|
|
959
959
|
)}
|
|
960
960
|
onclick={handleToggle}
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
type="button"
|
|
105
105
|
class={cn(
|
|
106
106
|
'w-full flex items-center justify-between px-3 py-2 font-medium text-card-foreground',
|
|
107
|
-
'cursor-pointer hover:bg-muted
|
|
107
|
+
'cursor-pointer hover:bg-muted',
|
|
108
108
|
isFirst && 'rounded-t-lg',
|
|
109
109
|
isLast && !isExpanded && 'rounded-b-lg'
|
|
110
110
|
)}
|
|
@@ -391,78 +391,74 @@
|
|
|
391
391
|
};
|
|
392
392
|
};
|
|
393
393
|
|
|
394
|
-
//
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
394
|
+
// Handlers are always attached and read the callback props when the
|
|
395
|
+
// event fires: a callback passed after mount still gets wired, and one
|
|
396
|
+
// removed later is skipped instead of throwing on every event. Each one
|
|
397
|
+
// returns early when no callback is set, so an unused mousemove or move does
|
|
398
|
+
// not query features or bounds on every frame.
|
|
399
|
+
newMap.on('click', (e) => {
|
|
400
|
+
if (!onClick) return;
|
|
401
|
+
onClick({
|
|
402
|
+
type: 'click',
|
|
403
|
+
...createEventParams(e),
|
|
404
|
+
features: newMap.queryRenderedFeatures(
|
|
405
|
+
e.point
|
|
406
|
+
) as unknown as MapClickEventParams['features'],
|
|
404
407
|
});
|
|
405
|
-
}
|
|
408
|
+
});
|
|
406
409
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
});
|
|
410
|
+
newMap.on('dblclick', (e) => {
|
|
411
|
+
if (!onDblClick) return;
|
|
412
|
+
onDblClick({
|
|
413
|
+
type: 'dblclick',
|
|
414
|
+
...createEventParams(e),
|
|
415
|
+
features: newMap.queryRenderedFeatures(
|
|
416
|
+
e.point
|
|
417
|
+
) as unknown as MapClickEventParams['features'],
|
|
416
418
|
});
|
|
417
|
-
}
|
|
419
|
+
});
|
|
418
420
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
});
|
|
421
|
+
newMap.on('contextmenu', (e) => {
|
|
422
|
+
if (!onContextMenu) return;
|
|
423
|
+
onContextMenu({
|
|
424
|
+
type: 'contextmenu',
|
|
425
|
+
...createEventParams(e),
|
|
426
|
+
features: newMap.queryRenderedFeatures(
|
|
427
|
+
e.point
|
|
428
|
+
) as unknown as MapClickEventParams['features'],
|
|
428
429
|
});
|
|
429
|
-
}
|
|
430
|
+
});
|
|
430
431
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
});
|
|
432
|
+
newMap.on('mousemove', (e) => {
|
|
433
|
+
if (!onMouseMove) return;
|
|
434
|
+
onMouseMove({
|
|
435
|
+
type: 'mousemove',
|
|
436
|
+
...createEventParams(e),
|
|
437
|
+
features: newMap.queryRenderedFeatures(
|
|
438
|
+
e.point
|
|
439
|
+
) as unknown as MapHoverEventParams['features'],
|
|
440
440
|
});
|
|
441
|
-
}
|
|
441
|
+
});
|
|
442
442
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
}
|
|
443
|
+
newMap.on('move', () => {
|
|
444
|
+
if (!onMove) return;
|
|
445
|
+
onMove({ type: 'move', ...createMoveParams() } as MapMoveEventParams);
|
|
446
|
+
});
|
|
448
447
|
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
}
|
|
448
|
+
newMap.on('movestart', () => {
|
|
449
|
+
if (!onMoveStart) return;
|
|
450
|
+
onMoveStart({ type: 'movestart', ...createMoveParams() } as MapMoveEventParams);
|
|
451
|
+
});
|
|
454
452
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
453
|
+
newMap.on('moveend', () => {
|
|
454
|
+
if (!onMoveEnd) return;
|
|
455
|
+
onMoveEnd({ type: 'moveend', ...createMoveParams() } as MapMoveEventParams);
|
|
456
|
+
});
|
|
460
457
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
}
|
|
458
|
+
newMap.on('zoom', () => {
|
|
459
|
+
if (!onZoom) return;
|
|
460
|
+
onZoom({ type: 'zoom', ...createMoveParams() } as MapMoveEventParams);
|
|
461
|
+
});
|
|
466
462
|
} catch (err) {
|
|
467
463
|
const errorMessage = err instanceof Error ? err.message : 'Failed to initialize map';
|
|
468
464
|
mapError = errorMessage;
|
|
@@ -188,6 +188,13 @@
|
|
|
188
188
|
}
|
|
189
189
|
});
|
|
190
190
|
|
|
191
|
+
// Update offset (matches maps-react: an unset offset keeps MapLibre's default)
|
|
192
|
+
$effect(() => {
|
|
193
|
+
if (marker && offset) {
|
|
194
|
+
marker.setOffset(offset as maplibregl.PointLike);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
191
198
|
// Move rendered children content into markerElement for MapLibre
|
|
192
199
|
$effect(() => {
|
|
193
200
|
if (contentRef && markerElement && children) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { getContext, onDestroy } from 'svelte';
|
|
2
|
+
import { getContext, onDestroy, untrack } from 'svelte';
|
|
3
3
|
import type maplibregl from 'maplibre-gl';
|
|
4
4
|
import type {
|
|
5
5
|
GeoJSONFeatureCollection,
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
fingerprintEqual,
|
|
13
13
|
} from '@classic-homes/maps-core';
|
|
14
14
|
import type { MapContextValue } from '../../composables/useMapContext.svelte';
|
|
15
|
+
import { getSourceRegistry } from './sourceRegistry.js';
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* Source component props - extends MapLibre source specifications
|
|
@@ -108,6 +109,65 @@
|
|
|
108
109
|
// Track style version for re-initialization after basemap changes
|
|
109
110
|
let lastKnownStyleVersion = -1;
|
|
110
111
|
|
|
112
|
+
// The id and configuration of the source this component put on the map.
|
|
113
|
+
// MapLibre cannot change a source's id, type, tiles or cluster options in
|
|
114
|
+
// place, so a change to any of them replaces the source.
|
|
115
|
+
let addedSourceId: string | null = null;
|
|
116
|
+
let addedConfigKey: string | null = null;
|
|
117
|
+
|
|
118
|
+
/** Every prop that shapes the source, except `data` (updated via setData) */
|
|
119
|
+
function getConfigKey(): string {
|
|
120
|
+
return JSON.stringify([
|
|
121
|
+
id,
|
|
122
|
+
type,
|
|
123
|
+
url ?? null,
|
|
124
|
+
tiles ?? null,
|
|
125
|
+
tileSize ?? null,
|
|
126
|
+
minzoom ?? null,
|
|
127
|
+
maxzoom ?? null,
|
|
128
|
+
attribution ?? null,
|
|
129
|
+
bounds ?? null,
|
|
130
|
+
scheme ?? null,
|
|
131
|
+
promoteId ?? null,
|
|
132
|
+
generateId ?? null,
|
|
133
|
+
cluster ?? null,
|
|
134
|
+
clusterRadius ?? null,
|
|
135
|
+
clusterMaxZoom ?? null,
|
|
136
|
+
clusterMinPoints ?? null,
|
|
137
|
+
clusterProperties ?? null,
|
|
138
|
+
lineMetrics ?? null,
|
|
139
|
+
tolerance ?? null,
|
|
140
|
+
buffer ?? null,
|
|
141
|
+
]);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
type LayerSnapshot = { spec: maplibregl.LayerSpecification; beforeId?: string };
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Remove a source and the layers drawn from it. Returns those layers with
|
|
148
|
+
* the layer each sat below, so they can be put back on a replacement.
|
|
149
|
+
*/
|
|
150
|
+
function removeSourceAndLayers(map: maplibregl.Map, sourceId: string): LayerSnapshot[] {
|
|
151
|
+
const removed: LayerSnapshot[] = [];
|
|
152
|
+
try {
|
|
153
|
+
const styleLayers = map.getStyle()?.layers ?? [];
|
|
154
|
+
const uses = (layer: maplibregl.LayerSpecification) =>
|
|
155
|
+
'source' in layer && layer.source === sourceId;
|
|
156
|
+
styleLayers.forEach((layer, index) => {
|
|
157
|
+
if (!uses(layer)) return;
|
|
158
|
+
// The next layer that stays on the map keeps the original order
|
|
159
|
+
const below = styleLayers.slice(index + 1).find((l) => !uses(l));
|
|
160
|
+
removed.push({ spec: layer, beforeId: below?.id });
|
|
161
|
+
if (map.getLayer(layer.id)) map.removeLayer(layer.id);
|
|
162
|
+
});
|
|
163
|
+
if (map.getSource(sourceId)) map.removeSource(sourceId);
|
|
164
|
+
} catch {
|
|
165
|
+
// Ignore cleanup errors
|
|
166
|
+
}
|
|
167
|
+
untrack(() => getSourceRegistry(map).removed(sourceId));
|
|
168
|
+
return removed;
|
|
169
|
+
}
|
|
170
|
+
|
|
111
171
|
$effect(() => {
|
|
112
172
|
const sv = mapContext.styleVersion ?? 0;
|
|
113
173
|
if (lastKnownStyleVersion === -1) {
|
|
@@ -124,9 +184,27 @@
|
|
|
124
184
|
// Add source effect
|
|
125
185
|
$effect(() => {
|
|
126
186
|
const { map, loaded } = mapContext;
|
|
187
|
+
const configKey = getConfigKey();
|
|
188
|
+
|
|
189
|
+
if (!map || !loaded) return;
|
|
190
|
+
|
|
191
|
+
// A changed configuration replaces the source. Its layers are put back on
|
|
192
|
+
// the new one, since their hooks still consider them added; on an id
|
|
193
|
+
// change they stay off, and layers re-pointed at the new id add themselves.
|
|
194
|
+
let layersToRestore: LayerSnapshot[] = [];
|
|
195
|
+
if (sourceAdded && addedSourceId !== null && addedConfigKey !== configKey) {
|
|
196
|
+
const removed = removeSourceAndLayers(map, addedSourceId);
|
|
197
|
+
if (addedSourceId === id) layersToRestore = removed;
|
|
198
|
+
addedSourceId = null;
|
|
199
|
+
addedConfigKey = null;
|
|
200
|
+
lastDataFingerprint = null;
|
|
201
|
+
sourceAdded = false;
|
|
202
|
+
}
|
|
127
203
|
|
|
128
|
-
if (
|
|
204
|
+
if (sourceAdded) return;
|
|
129
205
|
if (map.getSource(id)) {
|
|
206
|
+
addedSourceId = id;
|
|
207
|
+
addedConfigKey = configKey;
|
|
130
208
|
sourceAdded = true;
|
|
131
209
|
return;
|
|
132
210
|
}
|
|
@@ -218,7 +296,22 @@
|
|
|
218
296
|
return;
|
|
219
297
|
}
|
|
220
298
|
|
|
299
|
+
addedSourceId = id;
|
|
300
|
+
addedConfigKey = configKey;
|
|
221
301
|
sourceAdded = true;
|
|
302
|
+
|
|
303
|
+
for (const { spec, beforeId } of layersToRestore) {
|
|
304
|
+
try {
|
|
305
|
+
map.addLayer(spec, beforeId && map.getLayer(beforeId) ? beforeId : undefined);
|
|
306
|
+
} catch (err) {
|
|
307
|
+
console.error(`[maps-svelte] Failed to restore layer "${spec.id}" on "${id}":`, err);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Layers drawn from this id that are not back yet (they came off with a
|
|
312
|
+
// source this or another Source removed) add themselves on the new
|
|
313
|
+
// version; the ones restored above are found in place
|
|
314
|
+
untrack(() => getSourceRegistry(map).added(id));
|
|
222
315
|
} catch (err) {
|
|
223
316
|
console.error(`[maps-svelte] Failed to add source "${id}":`, err);
|
|
224
317
|
}
|
|
@@ -337,24 +430,9 @@
|
|
|
337
430
|
|
|
338
431
|
const { map } = mapContext;
|
|
339
432
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const style = map.getStyle();
|
|
344
|
-
if (style?.layers) {
|
|
345
|
-
style.layers.forEach((layer) => {
|
|
346
|
-
if ('source' in layer && layer.source === id) {
|
|
347
|
-
map.removeLayer(layer.id);
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (map.getSource(id)) {
|
|
353
|
-
map.removeSource(id);
|
|
354
|
-
}
|
|
355
|
-
} catch (err) {
|
|
356
|
-
// Ignore cleanup errors
|
|
357
|
-
}
|
|
433
|
+
// The id this component added, which can trail the prop by one update
|
|
434
|
+
if (map && sourceAdded && addedSourceId !== null) {
|
|
435
|
+
removeSourceAndLayers(map, addedSourceId);
|
|
358
436
|
}
|
|
359
437
|
});
|
|
360
438
|
</script>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which sources each map holds, by id, with a version that changes every time
|
|
3
|
+
* Source adds one. Removing a source removes the layers drawn from it: on an id
|
|
4
|
+
* change, or when a Source unmounts and mounts again. A layer reads the version
|
|
5
|
+
* for its own `source` id, so it adds itself back once a source with that id is
|
|
6
|
+
* on the map again.
|
|
7
|
+
*
|
|
8
|
+
* Keyed by the map instance rather than held in context, so any provider of
|
|
9
|
+
* the map context (Map, or a test's) shares one registry per map.
|
|
10
|
+
*/
|
|
11
|
+
export interface SourceRegistry {
|
|
12
|
+
/** The current version for a source id (reactive); 0 when Source has not added it */
|
|
13
|
+
version(id: string): number;
|
|
14
|
+
/** Source added `id` to the map */
|
|
15
|
+
added(id: string): void;
|
|
16
|
+
/** Source removed `id` from the map */
|
|
17
|
+
removed(id: string): void;
|
|
18
|
+
}
|
|
19
|
+
/** The registry for one map instance, created on first use */
|
|
20
|
+
export declare function getSourceRegistry(map: object): SourceRegistry;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SvelteMap } from 'svelte/reactivity';
|
|
2
|
+
function createSourceRegistry() {
|
|
3
|
+
const versions = new SvelteMap();
|
|
4
|
+
// One counter for every id, so a remove followed by an add never lands back
|
|
5
|
+
// on the version a layer last saw
|
|
6
|
+
let counter = 0;
|
|
7
|
+
return {
|
|
8
|
+
version: (id) => versions.get(id) ?? 0,
|
|
9
|
+
added(id) {
|
|
10
|
+
versions.set(id, ++counter);
|
|
11
|
+
},
|
|
12
|
+
removed(id) {
|
|
13
|
+
versions.delete(id);
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const registries = new WeakMap();
|
|
18
|
+
/** The registry for one map instance, created on first use */
|
|
19
|
+
export function getSourceRegistry(map) {
|
|
20
|
+
let registry = registries.get(map);
|
|
21
|
+
if (!registry) {
|
|
22
|
+
registry = createSourceRegistry();
|
|
23
|
+
registries.set(map, registry);
|
|
24
|
+
}
|
|
25
|
+
return registry;
|
|
26
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getContext, onDestroy } from 'svelte';
|
|
2
2
|
import { getChangedPaintProps, serializeFilter } from '@classic-homes/maps-core';
|
|
3
|
+
import { getSourceRegistry } from '../core/sourceRegistry.js';
|
|
3
4
|
/**
|
|
4
5
|
* Svelte 5 composable to manage layer lifecycle and events
|
|
5
6
|
*/
|
|
@@ -10,8 +11,13 @@ export function useLayer(getOptions) {
|
|
|
10
11
|
let sourceDataHandler = null;
|
|
11
12
|
let lastHoverCallTime = 0;
|
|
12
13
|
let lastMousePosition = null;
|
|
13
|
-
//
|
|
14
|
-
|
|
14
|
+
// The layer this hook put on the map (or adopted), and the structural options
|
|
15
|
+
// it was built from. Plain variables: they are bookkeeping, not reactive state.
|
|
16
|
+
let addedLayerId = null;
|
|
17
|
+
let addedLayerKey = null;
|
|
18
|
+
// The source registry version the layer was added against. A newer one means
|
|
19
|
+
// its source was re-created, which took the layer off the map with it.
|
|
20
|
+
let addedSourceVersion = 0;
|
|
15
21
|
// Track last applied values to prevent redundant MapLibre API calls
|
|
16
22
|
let lastAppliedVisible = undefined;
|
|
17
23
|
let lastAppliedPaint = undefined;
|
|
@@ -28,20 +34,36 @@ export function useLayer(getOptions) {
|
|
|
28
34
|
}
|
|
29
35
|
if (sv !== lastKnownStyleVersion) {
|
|
30
36
|
lastKnownStyleVersion = sv;
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
eventHandlerCleanup();
|
|
34
|
-
eventHandlerCleanup = null;
|
|
35
|
-
}
|
|
37
|
+
// Re-initialize: the handler effect drops its handlers when layerAdded
|
|
38
|
+
// goes false and re-attaches once the add effect sets it again
|
|
36
39
|
layerAdded = false;
|
|
37
|
-
|
|
38
|
-
lastAppliedVisible = undefined;
|
|
39
|
-
lastAppliedPaint = undefined;
|
|
40
|
-
lastAppliedFilterSerialized = '';
|
|
41
|
-
lastAppliedLayout = undefined;
|
|
42
|
-
lastAppliedZoomRange = undefined;
|
|
40
|
+
resetAppliedState();
|
|
43
41
|
}
|
|
44
42
|
});
|
|
43
|
+
function resetAppliedState() {
|
|
44
|
+
hoveredFeatureId = null;
|
|
45
|
+
lastAppliedVisible = undefined;
|
|
46
|
+
lastAppliedPaint = undefined;
|
|
47
|
+
lastAppliedFilterSerialized = '';
|
|
48
|
+
lastAppliedLayout = undefined;
|
|
49
|
+
lastAppliedZoomRange = undefined;
|
|
50
|
+
}
|
|
51
|
+
/** Remove the layer this hook added, if it is still on the map */
|
|
52
|
+
function removeAddedLayer(map) {
|
|
53
|
+
if (addedLayerId === null)
|
|
54
|
+
return;
|
|
55
|
+
try {
|
|
56
|
+
if (map.getLayer(addedLayerId)) {
|
|
57
|
+
map.removeLayer(addedLayerId);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Ignore cleanup errors
|
|
62
|
+
}
|
|
63
|
+
addedLayerId = null;
|
|
64
|
+
addedLayerKey = null;
|
|
65
|
+
addedSourceVersion = 0;
|
|
66
|
+
}
|
|
45
67
|
const createFeatureParams = (e, options, map) => {
|
|
46
68
|
const feature = e.features?.[0];
|
|
47
69
|
if (!feature)
|
|
@@ -63,9 +85,30 @@ export function useLayer(getOptions) {
|
|
|
63
85
|
const { map, loaded } = mapContext;
|
|
64
86
|
if (!map || !loaded)
|
|
65
87
|
return;
|
|
66
|
-
const { id, source, sourceLayer, type, visible = true, minZoom, maxZoom, filter, beforeId, paint = {}, layout = {},
|
|
88
|
+
const { id, source, sourceLayer, type, visible = true, minZoom, maxZoom, filter, beforeId, paint = {}, layout = {}, } = options;
|
|
89
|
+
// id, source, sourceLayer, type and beforeId cannot be changed in place:
|
|
90
|
+
// when one changes, the old layer comes off and a new one is added. Without
|
|
91
|
+
// this, an id change left the old layer on the map for good (unmount only
|
|
92
|
+
// removed the current id) and a new source or beforeId was ignored.
|
|
93
|
+
const layerKey = JSON.stringify([id, source, sourceLayer ?? null, type, beforeId ?? null]);
|
|
94
|
+
if (addedLayerKey !== null && addedLayerKey !== layerKey) {
|
|
95
|
+
removeAddedLayer(map);
|
|
96
|
+
layerAdded = false;
|
|
97
|
+
resetAppliedState();
|
|
98
|
+
}
|
|
99
|
+
// Changes each time a Source adds this layer's source (see sourceRegistry),
|
|
100
|
+
// so the effect re-runs when the source is back, wherever the Source sits
|
|
101
|
+
const sourceVersion = getSourceRegistry(map).version(source);
|
|
67
102
|
// Check if source is available
|
|
68
103
|
if (!map.getSource(source)) {
|
|
104
|
+
// The source went away and took the layer with it (a Source id change or
|
|
105
|
+
// unmount): forget it, so the layer is added again when the source returns
|
|
106
|
+
if (addedLayerId !== null && !map.getLayer(addedLayerId)) {
|
|
107
|
+
addedLayerId = null;
|
|
108
|
+
addedLayerKey = null;
|
|
109
|
+
layerAdded = false;
|
|
110
|
+
resetAppliedState();
|
|
111
|
+
}
|
|
69
112
|
let timeoutId = null;
|
|
70
113
|
const checkSource = () => {
|
|
71
114
|
if (map.getSource(source)) {
|
|
@@ -100,18 +143,25 @@ export function useLayer(getOptions) {
|
|
|
100
143
|
}
|
|
101
144
|
addLayer();
|
|
102
145
|
function addLayer() {
|
|
103
|
-
|
|
146
|
+
// A newer source version than the one the layer was added against: the
|
|
147
|
+
// source was re-created, and the layer must go back on unless Source
|
|
148
|
+
// already restored it
|
|
149
|
+
const sourceRecreated = addedLayerId !== null && addedSourceVersion !== sourceVersion;
|
|
150
|
+
// layerAdded is read first, always: it is what re-runs this effect after
|
|
151
|
+
// a style change, and short-circuiting past it drops that dependency
|
|
152
|
+
if ((layerAdded && !sourceRecreated) || map.getLayer(id)) {
|
|
153
|
+
// Already on the map: a styleVersion bump can land after the layer
|
|
154
|
+
// survived the style swap, or a second bump after the first re-added
|
|
155
|
+
// it. Adopt it; setting layerAdded re-attaches the handlers.
|
|
156
|
+
addedLayerId = id;
|
|
157
|
+
addedLayerKey = layerKey;
|
|
158
|
+
addedSourceVersion = sourceVersion;
|
|
104
159
|
layerAdded = true;
|
|
105
|
-
// The layer is on the map but this hook may not own its events: a
|
|
106
|
-
// styleVersion bump drops the handlers and marks the layer as not added,
|
|
107
|
-
// and if the layer survived (or a second bump lands after the first one
|
|
108
|
-
// re-added it) this branch is the only path back. Without the re-attach,
|
|
109
|
-
// the layer renders but every onClick/onHover is silently dead.
|
|
110
|
-
if (!eventHandlerCleanup) {
|
|
111
|
-
eventHandlerCleanup = setupEventHandlers(map, id, source, sourceLayer, onClick, onHover);
|
|
112
|
-
}
|
|
113
160
|
return;
|
|
114
161
|
}
|
|
162
|
+
// The re-created source has no feature state, hovered or otherwise
|
|
163
|
+
if (sourceRecreated)
|
|
164
|
+
hoveredFeatureId = null;
|
|
115
165
|
try {
|
|
116
166
|
const layerConfig = {
|
|
117
167
|
id,
|
|
@@ -136,15 +186,30 @@ export function useLayer(getOptions) {
|
|
|
136
186
|
layerConfig.filter = filter;
|
|
137
187
|
}
|
|
138
188
|
map.addLayer(layerConfig, beforeId);
|
|
189
|
+
addedLayerId = id;
|
|
190
|
+
addedLayerKey = layerKey;
|
|
191
|
+
addedSourceVersion = sourceVersion;
|
|
139
192
|
layerAdded = true;
|
|
140
|
-
// Set up event handlers and store cleanup function
|
|
141
|
-
eventHandlerCleanup = setupEventHandlers(map, id, source, sourceLayer, onClick, onHover);
|
|
142
193
|
}
|
|
143
194
|
catch (err) {
|
|
144
195
|
console.error(`[maps-svelte] Failed to add layer "${id}":`, err);
|
|
145
196
|
}
|
|
146
197
|
}
|
|
147
198
|
});
|
|
199
|
+
// Event handlers. Their own effect, so an onClick/onHover passed (or changed)
|
|
200
|
+
// after the layer was added is wired up too; they used to be bound once, in
|
|
201
|
+
// the add path. The deriveds keep paint or filter updates from re-binding.
|
|
202
|
+
const handlerLayerId = $derived(getOptions().id);
|
|
203
|
+
const handlerSource = $derived(getOptions().source);
|
|
204
|
+
const handlerSourceLayer = $derived(getOptions().sourceLayer);
|
|
205
|
+
const handlerOnClick = $derived(getOptions().onClick);
|
|
206
|
+
const handlerOnHover = $derived(getOptions().onHover);
|
|
207
|
+
$effect(() => {
|
|
208
|
+
const { map } = mapContext;
|
|
209
|
+
if (!map || !layerAdded)
|
|
210
|
+
return;
|
|
211
|
+
return setupEventHandlers(map, handlerLayerId, handlerSource, handlerSourceLayer, handlerOnClick, handlerOnHover);
|
|
212
|
+
});
|
|
148
213
|
// Update visibility (only when changed)
|
|
149
214
|
$effect(() => {
|
|
150
215
|
const options = getOptions();
|
|
@@ -379,28 +444,16 @@ export function useLayer(getOptions) {
|
|
|
379
444
|
}
|
|
380
445
|
};
|
|
381
446
|
}
|
|
382
|
-
// Cleanup on destroy
|
|
447
|
+
// Cleanup on destroy (the handler effect detaches its handlers itself)
|
|
383
448
|
onDestroy(() => {
|
|
384
449
|
const { map } = mapContext;
|
|
385
|
-
const options = getOptions();
|
|
386
|
-
// Remove event handlers first (before removing the layer)
|
|
387
|
-
if (eventHandlerCleanup) {
|
|
388
|
-
eventHandlerCleanup();
|
|
389
|
-
eventHandlerCleanup = null;
|
|
390
|
-
}
|
|
391
450
|
if (sourceDataHandler && map) {
|
|
392
451
|
map.off('sourcedata', sourceDataHandler);
|
|
393
452
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
catch {
|
|
401
|
-
// Ignore cleanup errors
|
|
402
|
-
}
|
|
403
|
-
}
|
|
453
|
+
// The id this hook added, not the current prop: they differ when the add
|
|
454
|
+
// effect has not yet re-run after an id change
|
|
455
|
+
if (map)
|
|
456
|
+
removeAddedLayer(map);
|
|
404
457
|
});
|
|
405
458
|
return {
|
|
406
459
|
get layerAdded() {
|