@classic-homes/maps-svelte 0.5.2 → 0.6.0
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 +90 -19
- package/dist/lib/components/layers/useLayer.svelte.js +67 -41
- 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) {
|
|
@@ -108,6 +108,64 @@
|
|
|
108
108
|
// Track style version for re-initialization after basemap changes
|
|
109
109
|
let lastKnownStyleVersion = -1;
|
|
110
110
|
|
|
111
|
+
// The id and configuration of the source this component put on the map.
|
|
112
|
+
// MapLibre cannot change a source's id, type, tiles or cluster options in
|
|
113
|
+
// place, so a change to any of them replaces the source.
|
|
114
|
+
let addedSourceId: string | null = null;
|
|
115
|
+
let addedConfigKey: string | null = null;
|
|
116
|
+
|
|
117
|
+
/** Every prop that shapes the source, except `data` (updated via setData) */
|
|
118
|
+
function getConfigKey(): string {
|
|
119
|
+
return JSON.stringify([
|
|
120
|
+
id,
|
|
121
|
+
type,
|
|
122
|
+
url ?? null,
|
|
123
|
+
tiles ?? null,
|
|
124
|
+
tileSize ?? null,
|
|
125
|
+
minzoom ?? null,
|
|
126
|
+
maxzoom ?? null,
|
|
127
|
+
attribution ?? null,
|
|
128
|
+
bounds ?? null,
|
|
129
|
+
scheme ?? null,
|
|
130
|
+
promoteId ?? null,
|
|
131
|
+
generateId ?? null,
|
|
132
|
+
cluster ?? null,
|
|
133
|
+
clusterRadius ?? null,
|
|
134
|
+
clusterMaxZoom ?? null,
|
|
135
|
+
clusterMinPoints ?? null,
|
|
136
|
+
clusterProperties ?? null,
|
|
137
|
+
lineMetrics ?? null,
|
|
138
|
+
tolerance ?? null,
|
|
139
|
+
buffer ?? null,
|
|
140
|
+
]);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
type LayerSnapshot = { spec: maplibregl.LayerSpecification; beforeId?: string };
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Remove a source and the layers drawn from it. Returns those layers with
|
|
147
|
+
* the layer each sat below, so they can be put back on a replacement.
|
|
148
|
+
*/
|
|
149
|
+
function removeSourceAndLayers(map: maplibregl.Map, sourceId: string): LayerSnapshot[] {
|
|
150
|
+
const removed: LayerSnapshot[] = [];
|
|
151
|
+
try {
|
|
152
|
+
const styleLayers = map.getStyle()?.layers ?? [];
|
|
153
|
+
const uses = (layer: maplibregl.LayerSpecification) =>
|
|
154
|
+
'source' in layer && layer.source === sourceId;
|
|
155
|
+
styleLayers.forEach((layer, index) => {
|
|
156
|
+
if (!uses(layer)) return;
|
|
157
|
+
// The next layer that stays on the map keeps the original order
|
|
158
|
+
const below = styleLayers.slice(index + 1).find((l) => !uses(l));
|
|
159
|
+
removed.push({ spec: layer, beforeId: below?.id });
|
|
160
|
+
if (map.getLayer(layer.id)) map.removeLayer(layer.id);
|
|
161
|
+
});
|
|
162
|
+
if (map.getSource(sourceId)) map.removeSource(sourceId);
|
|
163
|
+
} catch {
|
|
164
|
+
// Ignore cleanup errors
|
|
165
|
+
}
|
|
166
|
+
return removed;
|
|
167
|
+
}
|
|
168
|
+
|
|
111
169
|
$effect(() => {
|
|
112
170
|
const sv = mapContext.styleVersion ?? 0;
|
|
113
171
|
if (lastKnownStyleVersion === -1) {
|
|
@@ -124,9 +182,27 @@
|
|
|
124
182
|
// Add source effect
|
|
125
183
|
$effect(() => {
|
|
126
184
|
const { map, loaded } = mapContext;
|
|
185
|
+
const configKey = getConfigKey();
|
|
186
|
+
|
|
187
|
+
if (!map || !loaded) return;
|
|
188
|
+
|
|
189
|
+
// A changed configuration replaces the source. Its layers are put back on
|
|
190
|
+
// the new one, since their hooks still consider them added; on an id
|
|
191
|
+
// change they stay off, and layers re-pointed at the new id add themselves.
|
|
192
|
+
let layersToRestore: LayerSnapshot[] = [];
|
|
193
|
+
if (sourceAdded && addedSourceId !== null && addedConfigKey !== configKey) {
|
|
194
|
+
const removed = removeSourceAndLayers(map, addedSourceId);
|
|
195
|
+
if (addedSourceId === id) layersToRestore = removed;
|
|
196
|
+
addedSourceId = null;
|
|
197
|
+
addedConfigKey = null;
|
|
198
|
+
lastDataFingerprint = null;
|
|
199
|
+
sourceAdded = false;
|
|
200
|
+
}
|
|
127
201
|
|
|
128
|
-
if (
|
|
202
|
+
if (sourceAdded) return;
|
|
129
203
|
if (map.getSource(id)) {
|
|
204
|
+
addedSourceId = id;
|
|
205
|
+
addedConfigKey = configKey;
|
|
130
206
|
sourceAdded = true;
|
|
131
207
|
return;
|
|
132
208
|
}
|
|
@@ -218,7 +294,17 @@
|
|
|
218
294
|
return;
|
|
219
295
|
}
|
|
220
296
|
|
|
297
|
+
addedSourceId = id;
|
|
298
|
+
addedConfigKey = configKey;
|
|
221
299
|
sourceAdded = true;
|
|
300
|
+
|
|
301
|
+
for (const { spec, beforeId } of layersToRestore) {
|
|
302
|
+
try {
|
|
303
|
+
map.addLayer(spec, beforeId && map.getLayer(beforeId) ? beforeId : undefined);
|
|
304
|
+
} catch (err) {
|
|
305
|
+
console.error(`[maps-svelte] Failed to restore layer "${spec.id}" on "${id}":`, err);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
222
308
|
} catch (err) {
|
|
223
309
|
console.error(`[maps-svelte] Failed to add source "${id}":`, err);
|
|
224
310
|
}
|
|
@@ -337,24 +423,9 @@
|
|
|
337
423
|
|
|
338
424
|
const { map } = mapContext;
|
|
339
425
|
|
|
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
|
-
}
|
|
426
|
+
// The id this component added, which can trail the prop by one update
|
|
427
|
+
if (map && sourceAdded && addedSourceId !== null) {
|
|
428
|
+
removeSourceAndLayers(map, addedSourceId);
|
|
358
429
|
}
|
|
359
430
|
});
|
|
360
431
|
</script>
|
|
@@ -10,8 +10,10 @@ export function useLayer(getOptions) {
|
|
|
10
10
|
let sourceDataHandler = null;
|
|
11
11
|
let lastHoverCallTime = 0;
|
|
12
12
|
let lastMousePosition = null;
|
|
13
|
-
//
|
|
14
|
-
|
|
13
|
+
// The layer this hook put on the map (or adopted), and the structural options
|
|
14
|
+
// it was built from. Plain variables: they are bookkeeping, not reactive state.
|
|
15
|
+
let addedLayerId = null;
|
|
16
|
+
let addedLayerKey = null;
|
|
15
17
|
// Track last applied values to prevent redundant MapLibre API calls
|
|
16
18
|
let lastAppliedVisible = undefined;
|
|
17
19
|
let lastAppliedPaint = undefined;
|
|
@@ -28,20 +30,35 @@ export function useLayer(getOptions) {
|
|
|
28
30
|
}
|
|
29
31
|
if (sv !== lastKnownStyleVersion) {
|
|
30
32
|
lastKnownStyleVersion = sv;
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
eventHandlerCleanup();
|
|
34
|
-
eventHandlerCleanup = null;
|
|
35
|
-
}
|
|
33
|
+
// Re-initialize: the handler effect drops its handlers when layerAdded
|
|
34
|
+
// goes false and re-attaches once the add effect sets it again
|
|
36
35
|
layerAdded = false;
|
|
37
|
-
|
|
38
|
-
lastAppliedVisible = undefined;
|
|
39
|
-
lastAppliedPaint = undefined;
|
|
40
|
-
lastAppliedFilterSerialized = '';
|
|
41
|
-
lastAppliedLayout = undefined;
|
|
42
|
-
lastAppliedZoomRange = undefined;
|
|
36
|
+
resetAppliedState();
|
|
43
37
|
}
|
|
44
38
|
});
|
|
39
|
+
function resetAppliedState() {
|
|
40
|
+
hoveredFeatureId = null;
|
|
41
|
+
lastAppliedVisible = undefined;
|
|
42
|
+
lastAppliedPaint = undefined;
|
|
43
|
+
lastAppliedFilterSerialized = '';
|
|
44
|
+
lastAppliedLayout = undefined;
|
|
45
|
+
lastAppliedZoomRange = undefined;
|
|
46
|
+
}
|
|
47
|
+
/** Remove the layer this hook added, if it is still on the map */
|
|
48
|
+
function removeAddedLayer(map) {
|
|
49
|
+
if (addedLayerId === null)
|
|
50
|
+
return;
|
|
51
|
+
try {
|
|
52
|
+
if (map.getLayer(addedLayerId)) {
|
|
53
|
+
map.removeLayer(addedLayerId);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Ignore cleanup errors
|
|
58
|
+
}
|
|
59
|
+
addedLayerId = null;
|
|
60
|
+
addedLayerKey = null;
|
|
61
|
+
}
|
|
45
62
|
const createFeatureParams = (e, options, map) => {
|
|
46
63
|
const feature = e.features?.[0];
|
|
47
64
|
if (!feature)
|
|
@@ -63,7 +80,17 @@ export function useLayer(getOptions) {
|
|
|
63
80
|
const { map, loaded } = mapContext;
|
|
64
81
|
if (!map || !loaded)
|
|
65
82
|
return;
|
|
66
|
-
const { id, source, sourceLayer, type, visible = true, minZoom, maxZoom, filter, beforeId, paint = {}, layout = {},
|
|
83
|
+
const { id, source, sourceLayer, type, visible = true, minZoom, maxZoom, filter, beforeId, paint = {}, layout = {}, } = options;
|
|
84
|
+
// id, source, sourceLayer, type and beforeId cannot be changed in place:
|
|
85
|
+
// when one changes, the old layer comes off and a new one is added. Without
|
|
86
|
+
// this, an id change left the old layer on the map for good (unmount only
|
|
87
|
+
// removed the current id) and a new source or beforeId was ignored.
|
|
88
|
+
const layerKey = JSON.stringify([id, source, sourceLayer ?? null, type, beforeId ?? null]);
|
|
89
|
+
if (addedLayerKey !== null && addedLayerKey !== layerKey) {
|
|
90
|
+
removeAddedLayer(map);
|
|
91
|
+
layerAdded = false;
|
|
92
|
+
resetAppliedState();
|
|
93
|
+
}
|
|
67
94
|
// Check if source is available
|
|
68
95
|
if (!map.getSource(source)) {
|
|
69
96
|
let timeoutId = null;
|
|
@@ -101,15 +128,12 @@ export function useLayer(getOptions) {
|
|
|
101
128
|
addLayer();
|
|
102
129
|
function addLayer() {
|
|
103
130
|
if (layerAdded || map.getLayer(id)) {
|
|
131
|
+
// Already on the map: a styleVersion bump can land after the layer
|
|
132
|
+
// survived the style swap, or a second bump after the first re-added
|
|
133
|
+
// it. Adopt it; setting layerAdded re-attaches the handlers.
|
|
134
|
+
addedLayerId = id;
|
|
135
|
+
addedLayerKey = layerKey;
|
|
104
136
|
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
137
|
return;
|
|
114
138
|
}
|
|
115
139
|
try {
|
|
@@ -136,15 +160,29 @@ export function useLayer(getOptions) {
|
|
|
136
160
|
layerConfig.filter = filter;
|
|
137
161
|
}
|
|
138
162
|
map.addLayer(layerConfig, beforeId);
|
|
163
|
+
addedLayerId = id;
|
|
164
|
+
addedLayerKey = layerKey;
|
|
139
165
|
layerAdded = true;
|
|
140
|
-
// Set up event handlers and store cleanup function
|
|
141
|
-
eventHandlerCleanup = setupEventHandlers(map, id, source, sourceLayer, onClick, onHover);
|
|
142
166
|
}
|
|
143
167
|
catch (err) {
|
|
144
168
|
console.error(`[maps-svelte] Failed to add layer "${id}":`, err);
|
|
145
169
|
}
|
|
146
170
|
}
|
|
147
171
|
});
|
|
172
|
+
// Event handlers. Their own effect, so an onClick/onHover passed (or changed)
|
|
173
|
+
// after the layer was added is wired up too; they used to be bound once, in
|
|
174
|
+
// the add path. The deriveds keep paint or filter updates from re-binding.
|
|
175
|
+
const handlerLayerId = $derived(getOptions().id);
|
|
176
|
+
const handlerSource = $derived(getOptions().source);
|
|
177
|
+
const handlerSourceLayer = $derived(getOptions().sourceLayer);
|
|
178
|
+
const handlerOnClick = $derived(getOptions().onClick);
|
|
179
|
+
const handlerOnHover = $derived(getOptions().onHover);
|
|
180
|
+
$effect(() => {
|
|
181
|
+
const { map } = mapContext;
|
|
182
|
+
if (!map || !layerAdded)
|
|
183
|
+
return;
|
|
184
|
+
return setupEventHandlers(map, handlerLayerId, handlerSource, handlerSourceLayer, handlerOnClick, handlerOnHover);
|
|
185
|
+
});
|
|
148
186
|
// Update visibility (only when changed)
|
|
149
187
|
$effect(() => {
|
|
150
188
|
const options = getOptions();
|
|
@@ -379,28 +417,16 @@ export function useLayer(getOptions) {
|
|
|
379
417
|
}
|
|
380
418
|
};
|
|
381
419
|
}
|
|
382
|
-
// Cleanup on destroy
|
|
420
|
+
// Cleanup on destroy (the handler effect detaches its handlers itself)
|
|
383
421
|
onDestroy(() => {
|
|
384
422
|
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
423
|
if (sourceDataHandler && map) {
|
|
392
424
|
map.off('sourcedata', sourceDataHandler);
|
|
393
425
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
catch {
|
|
401
|
-
// Ignore cleanup errors
|
|
402
|
-
}
|
|
403
|
-
}
|
|
426
|
+
// The id this hook added, not the current prop: they differ when the add
|
|
427
|
+
// effect has not yet re-run after an id change
|
|
428
|
+
if (map)
|
|
429
|
+
removeAddedLayer(map);
|
|
404
430
|
});
|
|
405
431
|
return {
|
|
406
432
|
get layerAdded() {
|