maplibrex-svelte 0.1.0 → 0.1.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/README.md +96 -4
- package/dist/components/MapLibre.svelte +4 -1
- package/dist/components/MapLibre.svelte.d.ts +4 -1
- package/dist/components/controls/CustomControl.svelte +2 -1
- package/dist/components/data/FeatureState.svelte +14 -3
- package/dist/components/data/FeatureState.svelte.d.ts +5 -1
- package/dist/components/overlays/Marker.svelte +2 -1
- package/dist/components/overlays/Popup.svelte +50 -14
- package/dist/components/sources/GeoJSONSource.svelte +7 -0
- package/dist/components/sources/GeoJSONSource.svelte.d.ts +7 -0
- package/dist/internal/source.js +22 -8
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Component-oriented [MapLibre GL JS](https://maplibre.org/) bindings for Svelte 5, written in TypeScript.
|
|
4
4
|
|
|
5
|
+
**[Documentation and live demos →](https://countlinkx-solutions.github.io/maplibrex-svelte/)** — every component on its own page, with a working map you can drive.
|
|
6
|
+
|
|
5
7
|
You describe the map as a component tree. The library keeps that tree and the imperative MapLibre instance in sync — adding, updating in place, and tearing down in the right order.
|
|
6
8
|
|
|
7
9
|
```svelte
|
|
@@ -10,6 +12,22 @@ You describe the map as a component tree. The library keeps that tree and the im
|
|
|
10
12
|
import { MapLibre, NavigationControl, GeoJSONSource, CircleLayer } from 'maplibrex-svelte';
|
|
11
13
|
|
|
12
14
|
let zoom = $state(4);
|
|
15
|
+
|
|
16
|
+
const cities: GeoJSON.FeatureCollection = {
|
|
17
|
+
type: 'FeatureCollection',
|
|
18
|
+
features: [
|
|
19
|
+
{
|
|
20
|
+
type: 'Feature',
|
|
21
|
+
properties: { name: 'Barcelona' },
|
|
22
|
+
geometry: { type: 'Point', coordinates: [2.17, 41.38] }
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
type: 'Feature',
|
|
26
|
+
properties: { name: 'Madrid' },
|
|
27
|
+
geometry: { type: 'Point', coordinates: [-3.7, 40.42] }
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
};
|
|
13
31
|
</script>
|
|
14
32
|
|
|
15
33
|
<div style="height: 400px">
|
|
@@ -39,7 +57,7 @@ import 'maplibre-gl/dist/maplibre-gl.css';
|
|
|
39
57
|
|
|
40
58
|
## Should you use this?
|
|
41
59
|
|
|
42
|
-
Two other libraries cover the same ground
|
|
60
|
+
Two other libraries cover the same ground. Both are mature, and both had releases in November 2026, when this comparison was written:
|
|
43
61
|
|
|
44
62
|
| | maplibre-gl | Svelte | Runtime dependencies |
|
|
45
63
|
| ------------------------------------------------------------------------ | ------------------------------------- | ------------- | ------------------------------------- |
|
|
@@ -47,7 +65,7 @@ Two other libraries cover the same ground, both mature and both published this m
|
|
|
47
65
|
| [`svelte-maplibre-gl`](https://www.npmjs.com/package/svelte-maplibre-gl) | peer (`^5.19 \|\| ^6`) | ≥5 | — |
|
|
48
66
|
| this one | peer (`^6`) | 5, runes only | none |
|
|
49
67
|
|
|
50
|
-
**If you want something proven, use one of those two.** They have real users, real issues filed, and a track record this package does not have yet. This one is at `0.1
|
|
68
|
+
**If you want something proven, use one of those two.** They have real users, real issues filed, and a track record this package does not have yet. This one is still at `0.1`, and the API has already moved once during its first week.
|
|
51
69
|
|
|
52
70
|
It exists for a narrower set of preferences: no runtime dependencies at all, MapLibre 6 and Svelte 5 runes only, event props derived from MapLibre's own event maps rather than enumerated by hand, and updates that are surgical by default — a paint change calls one setter, and recreation happens only where MapLibre exposes no alternative.
|
|
53
71
|
|
|
@@ -73,6 +91,27 @@ This repository does it with a small Vite plugin in `vite-plugins/maplibre-worke
|
|
|
73
91
|
|
|
74
92
|
Two things that look like fixes but are not: emitting only the worker with a `?url` import leaves its shared chunk missing, and a bare side-effect import of your setup module is legal to tree-shake under a narrow `sideEffects` list — export a function and call it instead.
|
|
75
93
|
|
|
94
|
+
## Content-Security-Policy
|
|
95
|
+
|
|
96
|
+
Since 0.1.1 the library writes no inline `style` attributes: the elements it keeps out of sight until MapLibre takes them over use the `hidden` attribute. Its one CSS rule, which sizes the map container, is ordinary Svelte component CSS and ends up wherever your build puts component styles (a CSS file in a Vite production build).
|
|
97
|
+
|
|
98
|
+
What MapLibre itself needs, checked against the `maplibre-gl` version this repository tests with (6.6):
|
|
99
|
+
|
|
100
|
+
| Directive | Why |
|
|
101
|
+
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
102
|
+
| `worker-src 'self'` | The worker, served from your own origin as in the Vite step above. A worker URL on another origin makes MapLibre start it from a `blob:` URL, which needs `worker-src blob:`. |
|
|
103
|
+
| `img-src data:` | `maplibre-gl.css` draws the control icons as `data:` SVG images. |
|
|
104
|
+
| `connect-src` | Every host your style, tiles, sprites and glyphs come from. MapLibre fetches them. |
|
|
105
|
+
| `style-src 'self'` | `maplibre-gl.css`, served as a stylesheet. Positions MapLibre sets from script, on markers and popups, go through `element.style`, which `style-src` does not restrict. |
|
|
106
|
+
|
|
107
|
+
Some setups need more. With `refreshExpiredTiles: false`, raster tiles load through image elements, so their hosts need `img-src`. `<VideoSource>` needs `media-src` for its video hosts.
|
|
108
|
+
|
|
109
|
+
Three things to check in your own app:
|
|
110
|
+
|
|
111
|
+
- A `style` prop on `<MapLibre>` is rendered as a `style` attribute in server-rendered HTML. Under a strict `style-src`, size the map with `class` instead.
|
|
112
|
+
- `vite dev` injects CSS through `<style>` elements, so test a strict policy against a production build.
|
|
113
|
+
- MapLibre assigns `innerHTML` in its attribution control, its scale control and the popup close button. Enforcing Trusted Types (`require-trusted-types-for 'script'`) breaks them unless you provide a default policy.
|
|
114
|
+
|
|
76
115
|
## Requirements
|
|
77
116
|
|
|
78
117
|
| | |
|
|
@@ -109,6 +148,31 @@ Changing `mapStyle` swaps the style in place rather than recreating the map; des
|
|
|
109
148
|
<MapLibre mapStyle={style} interactions={{ scrollZoom: false, dragRotate: allowRotation }} />
|
|
110
149
|
```
|
|
111
150
|
|
|
151
|
+
`options` passes any other `MapOptions` to the MapLibre `Map` constructor. It is read once, when the map is created, and later changes to it have no effect. Use `bind:map` for anything you need to change at runtime.
|
|
152
|
+
|
|
153
|
+
Errors MapLibre reports while the map runs, such as a style, tile, sprite or glyph request that fails or a style that does not validate, reach `onerror` with the `Error` in `event.error`. Without a handler, MapLibre writes them to the console.
|
|
154
|
+
|
|
155
|
+
```svelte
|
|
156
|
+
<MapLibre mapStyle={style} onerror={(event) => reportMapError(event.error)} />
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Move events fire even when nobody has touched the map.** `onmovestart`, `onmove`, `onmoveend` and the matching zoom, rotate and pitch events fire for every camera change, not only for gestures, and some arrive during setup:
|
|
160
|
+
|
|
161
|
+
- MapLibre jumps to the style's own camera when the style finishes loading, if you gave no `center`, `zoom` or `bounds`. It also fires a move whenever the map container changes size.
|
|
162
|
+
- `<MapLibre>` moves the map when you change `padding`, and when `center`, `zoom`, `bearing` or `pitch` disagree with where the map is.
|
|
163
|
+
- Your own `jumpTo`, `easeTo`, `flyTo` and `fitBounds` calls fire them too.
|
|
164
|
+
|
|
165
|
+
To react only to the person using the map, check `event.originalEvent`. Moves started by a gesture (drag, scroll, pinch, keyboard, and the glide after a drag) carry the DOM event that started them. Moves made from code carry none.
|
|
166
|
+
|
|
167
|
+
```svelte
|
|
168
|
+
<MapLibre
|
|
169
|
+
mapStyle={style}
|
|
170
|
+
onmoveend={(event) => {
|
|
171
|
+
if (event.originalEvent) saveView(event.target.getCenter(), event.target.getZoom());
|
|
172
|
+
}}
|
|
173
|
+
/>
|
|
174
|
+
```
|
|
175
|
+
|
|
112
176
|
### Controls
|
|
113
177
|
|
|
114
178
|
`<NavigationControl>`, `<ScaleControl>`, `<GeolocateControl>`, `<FullscreenControl>`, `<AttributionControl>`, `<GlobeControl>`, `<TerrainControl>`, `<LogoControl>`.
|
|
@@ -125,10 +189,38 @@ Each takes its native options as props plus `position`, and exposes `bind:contro
|
|
|
125
189
|
|
|
126
190
|
`<Control>` is the generic escape hatch for any third-party `IControl`.
|
|
127
191
|
|
|
192
|
+
**Attribution is written as HTML.** MapLibre puts the `customAttribution` you give `<AttributionControl>`, and the `attribution` of every source in the style, into the page as HTML. Before it does, it removes `<script>` elements, `on…` event-handler attributes, and `href` or `src` values that contain `javascript:` or `data:`. Nothing else is removed: links to any address, remote images (which can track whoever views the map), iframes, forms and `style` attributes all reach the page. Treat a style you did not write as untrusted HTML.
|
|
193
|
+
|
|
194
|
+
MapLibre also adds an attribution control of its own to every map, whether or not you render `<AttributionControl>`. Pass `options={{ attributionControl: false }}` to turn it off, and render `<AttributionControl>` only after doing so, or the map shows two. There is no option yet to show attributions as plain text: with a style from a source you do not trust, turn the default off, leave `<AttributionControl>` out, and show the credits the data requires as ordinary Svelte text, which is escaped:
|
|
195
|
+
|
|
196
|
+
```svelte
|
|
197
|
+
<MapLibre mapStyle={untrustedStyle} options={{ attributionControl: false }}>
|
|
198
|
+
<CustomControl position="bottom-right">
|
|
199
|
+
<small>{credits}</small>
|
|
200
|
+
</CustomControl>
|
|
201
|
+
</MapLibre>
|
|
202
|
+
```
|
|
203
|
+
|
|
128
204
|
### Sources
|
|
129
205
|
|
|
130
206
|
`<GeoJSONSource>`, `<VectorSource>`, `<RasterSource>`, `<RasterDEMSource>`, `<ImageSource>`, `<VideoSource>`, `<CanvasSource>`, and the generic `<Source>` which takes a raw `SourceSpecification`.
|
|
131
207
|
|
|
208
|
+
`<GeoJSONSource>` compares `data` by reference and never walks it, so a large collection costs nothing to compare. **Replace the data to update the map, do not mutate it.** A new object calls `setData`, even when it is equal to the old one. A mutation in place, such as `data.features.push(feature)` on a `$state` object, keeps the reference and never reaches the map. `$state.raw` suits data you replace, because it skips the deep proxy:
|
|
209
|
+
|
|
210
|
+
```svelte
|
|
211
|
+
<script lang="ts">
|
|
212
|
+
let points = $state.raw(initialPoints);
|
|
213
|
+
|
|
214
|
+
function add(feature: GeoJSON.Feature) {
|
|
215
|
+
points = { ...points, features: [...points.features, feature] };
|
|
216
|
+
}
|
|
217
|
+
</script>
|
|
218
|
+
|
|
219
|
+
<GeoJSONSource id="points" data={points}>
|
|
220
|
+
<CircleLayer id="points-dots" paint={{ 'circle-radius': 6 }} />
|
|
221
|
+
</GeoJSONSource>
|
|
222
|
+
```
|
|
223
|
+
|
|
132
224
|
### Layers
|
|
133
225
|
|
|
134
226
|
`<FillLayer>`, `<LineLayer>`, `<SymbolLayer>`, `<CircleLayer>`, `<HeatmapLayer>`, `<FillExtrusionLayer>`, `<RasterLayer>`, `<HillshadeLayer>`, `<ColorReliefLayer>`, `<BackgroundLayer>`, and the generic `<Layer>`.
|
|
@@ -202,7 +294,7 @@ Terrain, sky, light and projection are properties of the style rather than entri
|
|
|
202
294
|
|
|
203
295
|
### Data
|
|
204
296
|
|
|
205
|
-
`<FeatureState>` makes hover and selection declarative. Mount it while a feature is active; unmounting
|
|
297
|
+
`<FeatureState>` makes hover and selection declarative. Mount it while a feature is active; unmounting removes the keys it set, so there is no `setFeatureState` left without its matching `removeFeatureState`. It removes only its own keys, so a hover and a selection on the same feature can be two components that come and go independently:
|
|
206
298
|
|
|
207
299
|
```svelte
|
|
208
300
|
<GeoJSONSource id="counties" data={counties}>
|
|
@@ -271,7 +363,7 @@ The component surface is derived from the [official MapLibre GL JS examples](htt
|
|
|
271
363
|
|
|
272
364
|
## Documentation site
|
|
273
365
|
|
|
274
|
-
`npm run dev` serves a documentation site alongside the library: a page per component, generated from a single registry
|
|
366
|
+
`npm run dev` serves a documentation site alongside the library: a page per component with a live demo, generated from a single hand-written registry (`src/docs/registry.ts`), plus a playground. Nothing checks the registry against the component types yet, so update it with the component.
|
|
275
367
|
|
|
276
368
|
| Route | What it is |
|
|
277
369
|
| ------------------- | ------------------------------------------------------------------------------- |
|
|
@@ -40,7 +40,10 @@
|
|
|
40
40
|
interactions?: InteractionRequest;
|
|
41
41
|
/** How camera prop changes are applied. @defaultValue 'ease' */
|
|
42
42
|
cameraMode?: CameraMode;
|
|
43
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Escape hatch for any `MapOptions` this component does not surface.
|
|
45
|
+
* Read once, when the map is created: later changes have no effect.
|
|
46
|
+
*/
|
|
44
47
|
options?: Partial<Omit<MapOptions, 'container' | 'style'>>;
|
|
45
48
|
/** Read-only binding to the underlying instance. */
|
|
46
49
|
map?: MapLibreMap | null;
|
|
@@ -25,7 +25,10 @@ type Props = MapEventProps & {
|
|
|
25
25
|
interactions?: InteractionRequest;
|
|
26
26
|
/** How camera prop changes are applied. @defaultValue 'ease' */
|
|
27
27
|
cameraMode?: CameraMode;
|
|
28
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* Escape hatch for any `MapOptions` this component does not surface.
|
|
30
|
+
* Read once, when the map is created: later changes have no effect.
|
|
31
|
+
*/
|
|
29
32
|
options?: Partial<Omit<MapOptions, 'container' | 'style'>>;
|
|
30
33
|
/** Read-only binding to the underlying instance. */
|
|
31
34
|
map?: MapLibreMap | null;
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
});
|
|
38
38
|
</script>
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
<!-- `hidden`, not an inline style: a strict CSP blocks style attributes. -->
|
|
41
|
+
<div hidden aria-hidden="true">
|
|
41
42
|
<div bind:this={content} class={['maplibregl-ctrl', className]}>
|
|
42
43
|
{#if context.map}
|
|
43
44
|
{@render children({ map: context.map })}
|
|
@@ -10,7 +10,11 @@
|
|
|
10
10
|
sourceLayer?: string;
|
|
11
11
|
/** The feature id. Vector features need `promoteId` if their id is a property. */
|
|
12
12
|
id: string | number;
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Read from style expressions through `["feature-state", key]`. Unmounting
|
|
15
|
+
* removes these keys and no others, so several components can hold state
|
|
16
|
+
* on the same feature.
|
|
17
|
+
*/
|
|
14
18
|
state: Record<string, unknown>;
|
|
15
19
|
};
|
|
16
20
|
|
|
@@ -47,10 +51,17 @@
|
|
|
47
51
|
...(sourceLayer ? { sourceLayer } : {})
|
|
48
52
|
};
|
|
49
53
|
|
|
50
|
-
untrack(() =>
|
|
54
|
+
const keys = untrack(() => {
|
|
55
|
+
map.setFeatureState(target, next);
|
|
56
|
+
return Object.keys(next);
|
|
57
|
+
});
|
|
51
58
|
|
|
59
|
+
// Only the keys set here: other components may hold state on the same
|
|
60
|
+
// feature, such as a selection alongside a hover. A key that leaves
|
|
61
|
+
// `state` is removed by this cleanup and not set again by the next run.
|
|
52
62
|
return () => {
|
|
53
|
-
if (isStyleAlive(map))
|
|
63
|
+
if (!isStyleAlive(map)) return;
|
|
64
|
+
for (const key of keys) map.removeFeatureState(target, key);
|
|
54
65
|
};
|
|
55
66
|
});
|
|
56
67
|
</script>
|
|
@@ -5,7 +5,11 @@ type Props = {
|
|
|
5
5
|
sourceLayer?: string;
|
|
6
6
|
/** The feature id. Vector features need `promoteId` if their id is a property. */
|
|
7
7
|
id: string | number;
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Read from style expressions through `["feature-state", key]`. Unmounting
|
|
10
|
+
* removes these keys and no others, so several components can hold state
|
|
11
|
+
* on the same feature.
|
|
12
|
+
*/
|
|
9
13
|
state: Record<string, unknown>;
|
|
10
14
|
};
|
|
11
15
|
declare const FeatureState: import("svelte").Component<Props, {}, "">;
|
|
@@ -190,7 +190,8 @@
|
|
|
190
190
|
</script>
|
|
191
191
|
|
|
192
192
|
{#if usesCustomElement}
|
|
193
|
-
|
|
193
|
+
<!-- `hidden`, not an inline style: a strict CSP blocks style attributes. -->
|
|
194
|
+
<div hidden aria-hidden="true">
|
|
194
195
|
<div bind:this={contentNode} class={className}>
|
|
195
196
|
{#if marker}
|
|
196
197
|
{@render content?.({ marker })}
|
|
@@ -41,24 +41,43 @@
|
|
|
41
41
|
const markerContext = getMarkerContext();
|
|
42
42
|
const attachedToMarker = markerContext !== null;
|
|
43
43
|
|
|
44
|
-
// No setters exist for these, so a change means a new popup.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
44
|
+
// No setters exist for these, so a change means a new popup. MapLibre copies
|
|
45
|
+
// every key it is given over its defaults, `undefined` included, so an option
|
|
46
|
+
// the consumer did not set is left out rather than passed as `undefined`.
|
|
47
|
+
const constructorOnly = $derived(
|
|
48
|
+
Object.fromEntries(
|
|
49
|
+
Object.entries({
|
|
50
|
+
closeButton: rest.closeButton,
|
|
51
|
+
closeOnClick: rest.closeOnClick,
|
|
52
|
+
closeOnMove: rest.closeOnMove,
|
|
53
|
+
focusAfterOpen: rest.focusAfterOpen,
|
|
54
|
+
anchor: rest.anchor,
|
|
55
|
+
className: rest.className,
|
|
56
|
+
locationOccludedOpacity: rest.locationOccludedOpacity
|
|
57
|
+
}).filter(([, value]) => value !== undefined)
|
|
58
|
+
) as PopupOptions
|
|
59
|
+
);
|
|
54
60
|
const recreateKey = $derived(stableKey(constructorOnly));
|
|
55
61
|
|
|
56
62
|
let content = $state<HTMLDivElement | null>(null);
|
|
57
63
|
|
|
64
|
+
// Plain variables, not state: a teardown reads reactive values as they were
|
|
65
|
+
// before the change that triggered it, so it cannot tell a rebuild from an
|
|
66
|
+
// unmount through `recreateKey` itself.
|
|
67
|
+
let latestKey = '';
|
|
68
|
+
let rebuilding = false;
|
|
69
|
+
let reopenOnMarker = false;
|
|
70
|
+
|
|
71
|
+
// Pre-effects run before the effect below re-runs, so its teardown sees the
|
|
72
|
+
// new key here when a constructor-only prop changed.
|
|
73
|
+
$effect.pre(() => {
|
|
74
|
+
latestKey = recreateKey;
|
|
75
|
+
});
|
|
76
|
+
|
|
58
77
|
$effect(() => {
|
|
59
78
|
const map = context.map;
|
|
60
79
|
const node = content;
|
|
61
|
-
|
|
80
|
+
const key = recreateKey;
|
|
62
81
|
|
|
63
82
|
if (!map || !node) return;
|
|
64
83
|
|
|
@@ -80,8 +99,17 @@
|
|
|
80
99
|
popup = instance;
|
|
81
100
|
|
|
82
101
|
return () => {
|
|
102
|
+
// A rebuild must not look like a close: the old popup's `close` is not
|
|
103
|
+
// reported, and an open popup stays open in its replacement. An unmount
|
|
104
|
+
// or a new map leaves the key as it was and still reports the close.
|
|
105
|
+
rebuilding = latestKey !== key;
|
|
106
|
+
reopenOnMarker = rebuilding && attachedToMarker && instance.isOpen();
|
|
83
107
|
popup = null;
|
|
84
|
-
|
|
108
|
+
try {
|
|
109
|
+
instance.remove();
|
|
110
|
+
} finally {
|
|
111
|
+
rebuilding = false;
|
|
112
|
+
}
|
|
85
113
|
};
|
|
86
114
|
});
|
|
87
115
|
|
|
@@ -94,6 +122,13 @@
|
|
|
94
122
|
|
|
95
123
|
marker.setPopup(instance);
|
|
96
124
|
|
|
125
|
+
// A standalone replacement is reopened by the `open` effect below; an
|
|
126
|
+
// attached one only opens through its marker.
|
|
127
|
+
if (reopenOnMarker) {
|
|
128
|
+
reopenOnMarker = false;
|
|
129
|
+
untrack(() => marker.togglePopup());
|
|
130
|
+
}
|
|
131
|
+
|
|
97
132
|
return () => marker.setPopup(null);
|
|
98
133
|
});
|
|
99
134
|
|
|
@@ -132,7 +167,7 @@
|
|
|
132
167
|
open = true;
|
|
133
168
|
}),
|
|
134
169
|
instance.on('close', () => {
|
|
135
|
-
open = false;
|
|
170
|
+
if (!rebuilding) open = false;
|
|
136
171
|
})
|
|
137
172
|
];
|
|
138
173
|
|
|
@@ -175,7 +210,8 @@
|
|
|
175
210
|
);
|
|
176
211
|
</script>
|
|
177
212
|
|
|
178
|
-
|
|
213
|
+
<!-- `hidden`, not an inline style: a strict CSP blocks style attributes. -->
|
|
214
|
+
<div hidden aria-hidden="true">
|
|
179
215
|
<div bind:this={content}>
|
|
180
216
|
{#if popup}
|
|
181
217
|
{@render children?.({ popup })}
|
|
@@ -4,6 +4,13 @@
|
|
|
4
4
|
import Source from './Source.svelte';
|
|
5
5
|
|
|
6
6
|
type Props = Omit<GeoJSONSourceSpecification, 'type'> & {
|
|
7
|
+
/**
|
|
8
|
+
* Inline GeoJSON or a URL. Compared by reference, never walked: replace it
|
|
9
|
+
* to update the map, which calls `setData` even when the new value is
|
|
10
|
+
* equal. A mutation in place, including on a `$state` object, keeps the
|
|
11
|
+
* reference and does not reach the map.
|
|
12
|
+
*/
|
|
13
|
+
data: GeoJSONSourceSpecification['data'];
|
|
7
14
|
/** Unique within the style. Changing it recreates the source. */
|
|
8
15
|
id: string;
|
|
9
16
|
/** Layers nested here inherit this source id automatically. */
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import type { GeoJSONSourceSpecification } from 'maplibre-gl';
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
3
|
type Props = Omit<GeoJSONSourceSpecification, 'type'> & {
|
|
4
|
+
/**
|
|
5
|
+
* Inline GeoJSON or a URL. Compared by reference, never walked: replace it
|
|
6
|
+
* to update the map, which calls `setData` even when the new value is
|
|
7
|
+
* equal. A mutation in place, including on a `$state` object, keeps the
|
|
8
|
+
* reference and does not reach the map.
|
|
9
|
+
*/
|
|
10
|
+
data: GeoJSONSourceSpecification['data'];
|
|
4
11
|
/** Unique within the style. Changing it recreates the source. */
|
|
5
12
|
id: string;
|
|
6
13
|
/** Layers nested here inherit this source id automatically. */
|
package/dist/internal/source.js
CHANGED
|
@@ -12,14 +12,28 @@ export function planSourceUpdate(previous, next) {
|
|
|
12
12
|
return { kind: 'create' };
|
|
13
13
|
if (previous.type !== next.type)
|
|
14
14
|
return { kind: 'recreate' };
|
|
15
|
-
if (deepEqual(previous, next))
|
|
16
|
-
return { kind: 'noop' };
|
|
17
15
|
if (previous.type === 'geojson' && next.type === 'geojson') {
|
|
18
|
-
|
|
19
|
-
const nextWithoutData = { ...next, data: null };
|
|
20
|
-
if (deepEqual(withoutData, nextWithoutData) && next.data !== undefined) {
|
|
21
|
-
return { kind: 'set-data', data: next.data };
|
|
22
|
-
}
|
|
16
|
+
return planGeoJSONUpdate(previous, next);
|
|
23
17
|
}
|
|
24
|
-
return { kind: 'recreate' };
|
|
18
|
+
return deepEqual(previous, next) ? { kind: 'noop' } : { kind: 'recreate' };
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* GeoJSON data is compared by reference, never walked: a collection can be
|
|
22
|
+
* large, and a deep comparison would traverse it on every prop change. A new
|
|
23
|
+
* reference means new data and costs one `setData`, even when it is equal.
|
|
24
|
+
* The other options are small, so they keep the structural comparison.
|
|
25
|
+
*
|
|
26
|
+
* The flip side is that data mutated in place keeps its reference and never
|
|
27
|
+
* reaches the map, so consumers replace it instead.
|
|
28
|
+
*/
|
|
29
|
+
function planGeoJSONUpdate(previous, next) {
|
|
30
|
+
const { data: previousData, ...previousOptions } = previous;
|
|
31
|
+
const { data: nextData, ...nextOptions } = next;
|
|
32
|
+
if (!deepEqual(previousOptions, nextOptions))
|
|
33
|
+
return { kind: 'recreate' };
|
|
34
|
+
if (previousData === nextData)
|
|
35
|
+
return { kind: 'noop' };
|
|
36
|
+
if (nextData === undefined)
|
|
37
|
+
return { kind: 'recreate' };
|
|
38
|
+
return { kind: 'set-data', data: nextData };
|
|
25
39
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maplibrex-svelte",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Component-oriented Svelte 5 bindings for MapLibre GL JS, written in TypeScript.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Yendris Rogelio Cruz Mendoza",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/CountlinkX-Solutions/maplibrex-svelte/issues"
|
|
13
13
|
},
|
|
14
|
-
"homepage": "https://github.
|
|
14
|
+
"homepage": "https://countlinkx-solutions.github.io/maplibrex-svelte/",
|
|
15
15
|
"keywords": [
|
|
16
16
|
"svelte",
|
|
17
17
|
"sveltekit",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@eslint/js": "^10.0.1",
|
|
67
|
-
"@sveltejs/adapter-
|
|
67
|
+
"@sveltejs/adapter-static": "^3.0.10",
|
|
68
68
|
"@sveltejs/kit": "^2.70.3",
|
|
69
69
|
"@sveltejs/package": "^2.5.8",
|
|
70
70
|
"@sveltejs/vite-plugin-svelte": "^7.3.0",
|