@classic-homes/maps-svelte 0.6.0 → 0.7.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/README.md +17 -1
- package/dist/lib/components/controls/BasemapSwitcher.svelte +1 -1
- package/dist/lib/components/controls/CoordinateDisplay.svelte +1 -1
- package/dist/lib/components/controls/LayerManager.svelte +13 -3
- package/dist/lib/components/core/Map.svelte +6 -1
- package/dist/lib/components/core/Marker.svelte +1 -1
- package/dist/lib/components/core/Popup.svelte +1 -1
- package/dist/lib/components/core/Source.svelte +9 -2
- package/dist/lib/components/core/sourceRegistry.d.ts +20 -0
- package/dist/lib/components/core/sourceRegistry.js +26 -0
- package/dist/lib/components/deck/DeckOverlay.svelte +1 -1
- package/dist/lib/components/layers/useLayer.svelte.d.ts +1 -1
- package/dist/lib/components/layers/useLayer.svelte.js +28 -1
- package/dist/lib/composables/useMapContext.svelte.d.ts +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -8,6 +8,22 @@ Svelte 5 components for MapLibre GL maps in the Classic Theme design system.
|
|
|
8
8
|
npm install @classic-homes/maps-svelte maplibre-gl
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
### MapLibre 6: set the worker URL
|
|
12
|
+
|
|
13
|
+
MapLibre 6 loads its tile worker from a separate file, which it cannot find on
|
|
14
|
+
its own under a bundler. Set the URL once at app startup, before the first map
|
|
15
|
+
renders; without it every map fails with "Worker failed to load".
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { setWorkerUrl } from 'maplibre-gl';
|
|
19
|
+
import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url'; // Vite
|
|
20
|
+
|
|
21
|
+
setWorkerUrl(workerUrl);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
With webpack: `setWorkerUrl(new URL('maplibre-gl/dist/maplibre-gl-worker.mjs', import.meta.url).toString())`.
|
|
25
|
+
MapLibre 5 bundles its worker and needs none of this.
|
|
26
|
+
|
|
11
27
|
## Features
|
|
12
28
|
|
|
13
29
|
- **Svelte 5 Runes**: Modern `$state`, `$derived`, `$effect` patterns
|
|
@@ -425,7 +441,7 @@ import type {
|
|
|
425
441
|
## Peer Dependencies
|
|
426
442
|
|
|
427
443
|
- `svelte` >= 5.0.0
|
|
428
|
-
- `maplibre-gl`
|
|
444
|
+
- `maplibre-gl` ^5.0.0 || ^6.11.0
|
|
429
445
|
|
|
430
446
|
## License
|
|
431
447
|
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
} from '@classic-homes/maps-core';
|
|
11
11
|
import '@classic-homes/maps-core/styles/basemap-switcher.css';
|
|
12
12
|
import { cn } from '../../utils.js';
|
|
13
|
-
import type maplibregl from 'maplibre-gl';
|
|
13
|
+
import type * as maplibregl from 'maplibre-gl';
|
|
14
14
|
import { useControlPosition } from '../../composables/useControlPosition.svelte.js';
|
|
15
15
|
|
|
16
16
|
interface MapContext {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { getContext } from 'svelte';
|
|
3
|
-
import type maplibregl from 'maplibre-gl';
|
|
3
|
+
import type * as maplibregl from 'maplibre-gl';
|
|
4
4
|
import type { CoordinateDisplayProps, LngLat } from '@classic-homes/maps-core';
|
|
5
5
|
import { formatCoordinates, EDGE_MARGIN, STACK_GAP } from '@classic-homes/maps-core';
|
|
6
6
|
import { cn } from '../../utils.js';
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
import '@classic-homes/maps-core/styles/layer-manager.css';
|
|
19
19
|
import { Switch } from '@classic-homes/theme-svelte';
|
|
20
20
|
import { cn } from '../../utils.js';
|
|
21
|
-
import type maplibregl from 'maplibre-gl';
|
|
21
|
+
import type * as maplibregl from 'maplibre-gl';
|
|
22
22
|
import { useControlPosition } from '../../composables/useControlPosition.svelte.js';
|
|
23
23
|
|
|
24
24
|
interface MapContext {
|
|
@@ -336,7 +336,11 @@
|
|
|
336
336
|
if (map.getLayer(layerId) && layerThemes.get(layerId) !== theme) {
|
|
337
337
|
for (const [prop, value] of Object.entries(paint)) {
|
|
338
338
|
try {
|
|
339
|
-
map.setPaintProperty(
|
|
339
|
+
map.setPaintProperty(
|
|
340
|
+
layerId,
|
|
341
|
+
prop as Parameters<maplibregl.Map['setPaintProperty']>[1],
|
|
342
|
+
value as Parameters<maplibregl.Map['setPaintProperty']>[2]
|
|
343
|
+
);
|
|
340
344
|
} catch {
|
|
341
345
|
// Ignore
|
|
342
346
|
}
|
|
@@ -383,7 +387,13 @@
|
|
|
383
387
|
}, 0); // End of setTimeout callback
|
|
384
388
|
});
|
|
385
389
|
|
|
386
|
-
|
|
390
|
+
/**
|
|
391
|
+
* MapLibre 6 types `setPaintProperty`'s name parameter as a union of known
|
|
392
|
+
* paint properties, so this returns the literals rather than `string`.
|
|
393
|
+
*/
|
|
394
|
+
type OpacityProperty = 'fill-opacity' | 'line-opacity' | 'circle-opacity' | 'icon-opacity';
|
|
395
|
+
|
|
396
|
+
function getOpacityProperty(renderType: string): OpacityProperty | null {
|
|
387
397
|
switch (renderType) {
|
|
388
398
|
case 'fill':
|
|
389
399
|
return 'fill-opacity';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { onMount, onDestroy, setContext } from 'svelte';
|
|
3
3
|
import type { Snippet } from 'svelte';
|
|
4
|
-
import maplibregl from 'maplibre-gl';
|
|
4
|
+
import * as maplibregl from 'maplibre-gl';
|
|
5
5
|
import maplibreStyles from 'maplibre-gl/dist/maplibre-gl.css?inline';
|
|
6
6
|
import controlStyles from '@classic-homes/maps-core/styles/controls.css?inline';
|
|
7
7
|
import markerStyles from '@classic-homes/maps-core/styles/marker.css?inline';
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
import {
|
|
17
17
|
generateMapDescription,
|
|
18
18
|
createPositioningCoordinator,
|
|
19
|
+
warnIfWorkerUrlMissing,
|
|
19
20
|
type PositioningCoordinator,
|
|
20
21
|
} from '@classic-homes/maps-core';
|
|
21
22
|
|
|
@@ -177,6 +178,10 @@
|
|
|
177
178
|
document.head.appendChild(sheet);
|
|
178
179
|
}
|
|
179
180
|
try {
|
|
181
|
+
// MapLibre 6 cannot load its worker under a bundler unless the app set
|
|
182
|
+
// its URL; its own error names the symptom, this warning names the fix.
|
|
183
|
+
warnIfWorkerUrlMissing(maplibregl);
|
|
184
|
+
|
|
180
185
|
const newMap = new maplibregl.Map({
|
|
181
186
|
container: containerElement,
|
|
182
187
|
style: mapStyle as maplibregl.StyleSpecification | string,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { getContext, onDestroy } from 'svelte';
|
|
3
3
|
import type { Snippet } from 'svelte';
|
|
4
|
-
import maplibregl from 'maplibre-gl';
|
|
4
|
+
import * as maplibregl from 'maplibre-gl';
|
|
5
5
|
import type { MarkerProps, LngLat } from '@classic-homes/maps-core';
|
|
6
6
|
import {
|
|
7
7
|
applyMarkerProtection,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { getContext, onDestroy, tick } from 'svelte';
|
|
3
3
|
import type { Snippet } from 'svelte';
|
|
4
|
-
import maplibregl from 'maplibre-gl';
|
|
4
|
+
import * as maplibregl from 'maplibre-gl';
|
|
5
5
|
import type { PopupProps } from '@classic-homes/maps-core';
|
|
6
6
|
import { normalizePadding, panToFitPopup } from '@classic-homes/maps-core/utils';
|
|
7
7
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { getContext, onDestroy } from 'svelte';
|
|
3
|
-
import type maplibregl from 'maplibre-gl';
|
|
2
|
+
import { getContext, onDestroy, untrack } from 'svelte';
|
|
3
|
+
import type * as maplibregl from 'maplibre-gl';
|
|
4
4
|
import type {
|
|
5
5
|
GeoJSONFeatureCollection,
|
|
6
6
|
ClusterConfig,
|
|
@@ -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
|
|
@@ -163,6 +164,7 @@
|
|
|
163
164
|
} catch {
|
|
164
165
|
// Ignore cleanup errors
|
|
165
166
|
}
|
|
167
|
+
untrack(() => getSourceRegistry(map).removed(sourceId));
|
|
166
168
|
return removed;
|
|
167
169
|
}
|
|
168
170
|
|
|
@@ -305,6 +307,11 @@
|
|
|
305
307
|
console.error(`[maps-svelte] Failed to restore layer "${spec.id}" on "${id}":`, err);
|
|
306
308
|
}
|
|
307
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));
|
|
308
315
|
} catch (err) {
|
|
309
316
|
console.error(`[maps-svelte] Failed to add source "${id}":`, err);
|
|
310
317
|
}
|
|
@@ -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,4 +1,4 @@
|
|
|
1
|
-
import type maplibregl from 'maplibre-gl';
|
|
1
|
+
import type * as maplibregl from 'maplibre-gl';
|
|
2
2
|
import type { BaseLayerProps, FeatureClickEventParams, FeatureHoverEventParams } from '@classic-homes/maps-core';
|
|
3
3
|
/**
|
|
4
4
|
* Extended click event params that include the map instance
|
|
@@ -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
|
*/
|
|
@@ -14,6 +15,9 @@ export function useLayer(getOptions) {
|
|
|
14
15
|
// it was built from. Plain variables: they are bookkeeping, not reactive state.
|
|
15
16
|
let addedLayerId = null;
|
|
16
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;
|
|
17
21
|
// Track last applied values to prevent redundant MapLibre API calls
|
|
18
22
|
let lastAppliedVisible = undefined;
|
|
19
23
|
let lastAppliedPaint = undefined;
|
|
@@ -58,6 +62,7 @@ export function useLayer(getOptions) {
|
|
|
58
62
|
}
|
|
59
63
|
addedLayerId = null;
|
|
60
64
|
addedLayerKey = null;
|
|
65
|
+
addedSourceVersion = 0;
|
|
61
66
|
}
|
|
62
67
|
const createFeatureParams = (e, options, map) => {
|
|
63
68
|
const feature = e.features?.[0];
|
|
@@ -91,8 +96,19 @@ export function useLayer(getOptions) {
|
|
|
91
96
|
layerAdded = false;
|
|
92
97
|
resetAppliedState();
|
|
93
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);
|
|
94
102
|
// Check if source is available
|
|
95
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
|
+
}
|
|
96
112
|
let timeoutId = null;
|
|
97
113
|
const checkSource = () => {
|
|
98
114
|
if (map.getSource(source)) {
|
|
@@ -127,15 +143,25 @@ export function useLayer(getOptions) {
|
|
|
127
143
|
}
|
|
128
144
|
addLayer();
|
|
129
145
|
function addLayer() {
|
|
130
|
-
|
|
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)) {
|
|
131
153
|
// Already on the map: a styleVersion bump can land after the layer
|
|
132
154
|
// survived the style swap, or a second bump after the first re-added
|
|
133
155
|
// it. Adopt it; setting layerAdded re-attaches the handlers.
|
|
134
156
|
addedLayerId = id;
|
|
135
157
|
addedLayerKey = layerKey;
|
|
158
|
+
addedSourceVersion = sourceVersion;
|
|
136
159
|
layerAdded = true;
|
|
137
160
|
return;
|
|
138
161
|
}
|
|
162
|
+
// The re-created source has no feature state, hovered or otherwise
|
|
163
|
+
if (sourceRecreated)
|
|
164
|
+
hoveredFeatureId = null;
|
|
139
165
|
try {
|
|
140
166
|
const layerConfig = {
|
|
141
167
|
id,
|
|
@@ -162,6 +188,7 @@ export function useLayer(getOptions) {
|
|
|
162
188
|
map.addLayer(layerConfig, beforeId);
|
|
163
189
|
addedLayerId = id;
|
|
164
190
|
addedLayerKey = layerKey;
|
|
191
|
+
addedSourceVersion = sourceVersion;
|
|
165
192
|
layerAdded = true;
|
|
166
193
|
}
|
|
167
194
|
catch (err) {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* It provides access to the underlying MapLibre GL map instance for
|
|
6
6
|
* advanced operations like cluster expansion.
|
|
7
7
|
*/
|
|
8
|
-
import type maplibregl from 'maplibre-gl';
|
|
8
|
+
import type * as maplibregl from 'maplibre-gl';
|
|
9
9
|
import type { PositioningCoordinator } from '@classic-homes/maps-core';
|
|
10
10
|
/**
|
|
11
11
|
* Map context interface provided by the Map component
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@classic-homes/maps-svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Svelte MapLibre components for Classic Theme",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"maps",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@deck.gl/core": "^9.0.0",
|
|
48
48
|
"@deck.gl/layers": "^9.0.0",
|
|
49
49
|
"@deck.gl/mapbox": "^9.0.0",
|
|
50
|
-
"maplibre-gl": "^
|
|
50
|
+
"maplibre-gl": "^5.0.0 || ^6.11.0",
|
|
51
51
|
"svelte": "^5.0.0"
|
|
52
52
|
},
|
|
53
53
|
"peerDependenciesMeta": {
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"@sveltejs/package": "^2.3.0",
|
|
69
69
|
"@sveltejs/vite-plugin-svelte": "^7.0.0",
|
|
70
70
|
"jsdom": "^28.1.0",
|
|
71
|
-
"maplibre-gl": "^
|
|
71
|
+
"maplibre-gl": "^6.14.0",
|
|
72
72
|
"svelte": "^5.0.0",
|
|
73
73
|
"svelte-check": "^4.0.0",
|
|
74
74
|
"typescript": "^5.3.0",
|