@superrouteca/smap-react 1.0.6 → 1.1.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/CHANGELOG.md +15 -0
- package/README.md +109 -7
- package/dist/index.cjs +399 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +156 -14
- package/dist/index.d.ts +156 -14
- package/dist/index.js +379 -23
- package/dist/index.js.map +1 -1
- package/package.json +11 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.1.0 — 2026-10-27
|
|
4
|
+
|
|
5
|
+
- Standard style layers: `<Source>` and `<StyleLayer>` take Style Specification v8 sources and layers unchanged; a
|
|
6
|
+
change of `paint`, `layout` or `filter` is set in place, a source is added before its layers and removed after them.
|
|
7
|
+
- `slot` on `<Points>`, `<Lines>`, `<Polygons>`, `<Data>`, `<StyleLayer>` and the external layers places a layer
|
|
8
|
+
among the basemap's layers (`bottom`, `roads`, `labels`, `top`); without one a layer draws as in 1.0.
|
|
9
|
+
- `<SMap basemap={…}>` shows, hides and recolours the basemap by class; `style="custom:<id>"` uses a saved custom style.
|
|
10
|
+
- External layers: `<RasterLayer>` (XYZ, TileJSON), `<WmsLayer>`, `<WmtsLayer>`, `<VectorTiles>` (vector tiles and
|
|
11
|
+
PMTiles) and `<ArcGisLayer>`.
|
|
12
|
+
- Data files: `<Data>` draws KML, KMZ, GPX, encoded polylines, WKT, WKB, CSV, TopoJSON and GeoJSON; `formats.read`
|
|
13
|
+
reads them without a map.
|
|
14
|
+
- Layer descriptions: `<Layers json>` loads the description every sMap SDK writes with `toJSON()`.
|
|
15
|
+
- `<DeckLayer>` draws deck.gl layers interleaved with the map; `useRasterUrl()` gives a raster tile URL template.
|
|
16
|
+
- `@superrouteca/smap` is now a peer dependency, 1.1.0 or later.
|
|
17
|
+
|
|
3
18
|
## 1.0.6 — 2026-10-27
|
|
4
19
|
|
|
5
20
|
- With the Web SDK 1.0.6: numbers drawn into pins close up, vehicle markers.
|
package/README.md
CHANGED
|
@@ -5,11 +5,14 @@
|
|
|
5
5
|
|
|
6
6
|
# sMap for React: `@superrouteca/smap-react`
|
|
7
7
|
|
|
8
|
-
React components for maps from Superroute's sMap service: `<SMap>`, `<Points>`, `<Lines>`, `<Polygons
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
React components for maps from Superroute's sMap service: `<SMap>`, `<Points>`, `<Lines>`, `<Polygons>`, the
|
|
9
|
+
standard style layers (`<Source>`, `<StyleLayer>`), external layers (`<RasterLayer>`, `<WmsLayer>`, `<WmtsLayer>`,
|
|
10
|
+
`<VectorTiles>`, `<ArcGisLayer>`), data files (`<Data>`), layer descriptions (`<Layers>`), deck.gl layers
|
|
11
|
+
(`<DeckLayer>`) and the `useSMap()` hook, over [`@superrouteca/smap`](../js). Each component makes one call of the
|
|
12
|
+
SDK and removes what it added when it unmounts.
|
|
13
|
+
|
|
14
|
+
- Version 1.1.0, MIT licence (see `LICENSE` at the repository root)
|
|
15
|
+
- React 18 or later and `@superrouteca/smap` 1.1.0 or later (a peer dependency); TypeScript types included
|
|
13
16
|
- Server rendering (Next.js App Router included): the components are client components and the map is created only
|
|
14
17
|
in the browser, after mounting
|
|
15
18
|
|
|
@@ -54,7 +57,8 @@ export function Deliveries({ stops, route, zones }) {
|
|
|
54
57
|
| Prop | |
|
|
55
58
|
|---|---|
|
|
56
59
|
| `apiKey`, `token`, `getToken` | access, see below |
|
|
57
|
-
| `style` | `light`, `white`, `grayscale`, `dark`, `black` (or a style URL); a change keeps the layers |
|
|
60
|
+
| `style` | `light`, `white`, `grayscale`, `dark`, `black`, a saved custom style `custom:sms_…` (or a style URL); a change keeps the layers |
|
|
61
|
+
| `basemap` | basemap overrides by class, see below; a new value replaces the previous ones, removing the prop clears them |
|
|
58
62
|
| `language` | label language (`en`, `fr`, `zh-Hans`, …); a change keeps the layers |
|
|
59
63
|
| `center`, `zoom`, `bearing`, `pitch` | the camera; it follows a change |
|
|
60
64
|
| `buildings3D` | buildings in 3D |
|
|
@@ -72,9 +76,107 @@ again (its layers are added again).
|
|
|
72
76
|
|
|
73
77
|
`<Points>`, `<Lines>` and `<Polygons>` take `id`, `data` (GeoJSON, or an array of `{lng, lat, …properties}`),
|
|
74
78
|
`options` (as `addPoints` / `addLines` / `addPolygons` of `@superrouteca/smap`), `onTap(properties, {lat, lng},
|
|
75
|
-
feature)` and `
|
|
79
|
+
feature)`, `visible` and `slot` (`bottom`, `roads`, `labels` or `top`: where the layer draws among the basemap's
|
|
80
|
+
layers; without one it draws on top, in the order added, as in 1.0). A layer is added when the map is ready and removed when the component unmounts; new `data`
|
|
76
81
|
replaces the data and keeps the look, new `options` draw the layer again. Higher `zIndex` draws on top.
|
|
77
82
|
|
|
83
|
+
## Basemap and custom styles
|
|
84
|
+
|
|
85
|
+
`basemap` shows, hides and recolours the basemap by class — `water`, `land`, `parks`, `roads` (and `roads.highway`,
|
|
86
|
+
`roads.major`, `roads.minor`, `roads.paths`), `rail`, `buildings`, `boundaries`, `pois`, `labels` (and
|
|
87
|
+
`labels.places`, `labels.roads`, `labels.water`, `labels.addresses`), `transit`. Each class takes `visible`, `color`,
|
|
88
|
+
`casing`, `halo`, `opacity` and `width` (a factor, 0.25 to 4); an override on `roads` applies to every `roads.*`
|
|
89
|
+
class, and a child class wins over its parent. The overrides are kept across style and language changes.
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
<SMap apiKey="smk_…" style="grayscale" basemap={{ water: { color: '#9cc0e6' }, pois: { visible: false }, roads: { width: 1.2 } }} />
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
A custom style saved in the Superroute developer center is used by its id: `style="custom:sms_…"`.
|
|
96
|
+
|
|
97
|
+
## Standard style layers
|
|
98
|
+
|
|
99
|
+
`<Source>` and `<StyleLayer>` take Style Specification v8 objects unchanged, so a layer copied from Mapbox Studio,
|
|
100
|
+
Maputnik or a Mapbox GL JS page works as it is. Style layers inside `<Source>` draw that source (their
|
|
101
|
+
`source` is filled in) and are added once it is; a layer outside any `<Source>` can draw the basemap's source
|
|
102
|
+
(`basemap`) or a source added elsewhere.
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
const parcels = { type: 'vector', tiles: ['https://tiles.example/{z}/{x}/{y}.pbf'], attribution: '© Example' };
|
|
106
|
+
|
|
107
|
+
<SMap apiKey="smk_…">
|
|
108
|
+
<Source id="parcels" source={parcels}>
|
|
109
|
+
<StyleLayer slot="roads" layer={{ id: 'parcel-lines', type: 'line', 'source-layer': 'parcels',
|
|
110
|
+
paint: { 'line-color': '#64748b', 'line-width': 1 } }} />
|
|
111
|
+
</Source>
|
|
112
|
+
</SMap>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
| Component | Props |
|
|
116
|
+
|---|---|
|
|
117
|
+
| `<Source>` | `id`, `source` (a Style Spec source); a new value (compared by value) adds it again with its layers |
|
|
118
|
+
| `<StyleLayer>` | `layer` (a Style Spec layer), `before` (a layer id to draw under) or `slot`, `visible`. A change of `paint`, `layout` or `filter` is set in place; any other change adds the layer again |
|
|
119
|
+
|
|
120
|
+
## External layers
|
|
121
|
+
|
|
122
|
+
Tiles from other services, each shown with the attribution it names beside the map data credit:
|
|
123
|
+
|
|
124
|
+
| Component | Props |
|
|
125
|
+
|---|---|
|
|
126
|
+
| `<RasterLayer>` | `id`, `tiles` (`{z}/{x}/{y}` templates) or `url` (TileJSON), `tileSize`, `attribution`, `slot`, `opacity` |
|
|
127
|
+
| `<WmsLayer>` | `id`, `url`, `layers`, `styles`, `format`, `transparent`, `version`, `attribution`, `slot` (OGC WMS, one GetMap per tile) |
|
|
128
|
+
| `<WmtsLayer>` | `id`, `url` (GetCapabilities or a REST template), `layer`, `tileMatrixSet`, `style`, `format`, `attribution`, `slot` |
|
|
129
|
+
| `<VectorTiles>` | `id`, `tiles`, `url` (TileJSON) or `pmtiles`, `layers` (Style Spec layers), `attribution` |
|
|
130
|
+
| `<ArcGisLayer>` | `id`, `url` (an ArcGIS REST MapServer or VectorTileServer), `attribution`, `slot` |
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
<WmsLayer id="flood" url="https://maps.example/wms" layers="flood_zones" transparent slot="labels" attribution="© Example" />
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
A change of any prop adds the layer again.
|
|
137
|
+
|
|
138
|
+
## Data files
|
|
139
|
+
|
|
140
|
+
`<Data>` reads KML, KMZ (Google Earth, My Maps; their styles become simplestyle properties), GPX, encoded polylines
|
|
141
|
+
(`polyline` precision 5, `polyline6`), WKT, WKB, CSV with latitude / longitude columns, TopoJSON and GeoJSON, on the
|
|
142
|
+
device, and draws them: points as the layer `<id>`, lines as `<id>:lines`, areas as `<id>:areas`. `data` is text,
|
|
143
|
+
bytes, a `Blob` / `File` or a parsed object; `format` is guessed from the content when not given; `lat` / `lng` name
|
|
144
|
+
the CSV columns (guessed from common headings when not given). It takes `options`, `slot`, `onTap`, `visible`, `onLoad`, `onError`, `popup`
|
|
145
|
+
and `popupOptions` like the other layers. GeoJSON properties of the simplestyle specification (`marker-color`,
|
|
146
|
+
`stroke`, `fill`, …) are drawn when a layer gives no colour of its own.
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
function Upload() {
|
|
150
|
+
const [file, setFile] = useState<File | null>(null);
|
|
151
|
+
return (
|
|
152
|
+
<>
|
|
153
|
+
<input type="file" onChange={(e) => setFile(e.target.files?.[0] ?? null)} />
|
|
154
|
+
<SMap apiKey="smk_…">{file && <Data id="upload" data={file} />}</SMap>
|
|
155
|
+
</>
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`formats.read(input, { format, lat, lng })` gives the GeoJSON FeatureCollection without a map. Through the ref (or
|
|
161
|
+
`useSMap()`): `toGeoJSON(layerId)` and `toKML(layerId)` export a layer, and `convert(input, { format })` converts a
|
|
162
|
+
large file on the map service instead of the device.
|
|
163
|
+
|
|
164
|
+
## Layer descriptions
|
|
165
|
+
|
|
166
|
+
`toJSON()` (through the ref) writes the map's layers as one JSON description that every sMap SDK reads, so a map built
|
|
167
|
+
on the web opens the same in an app. `<Layers json={description} />` loads one (`loadLayers`) and removes its layers
|
|
168
|
+
and sources when it unmounts.
|
|
169
|
+
|
|
170
|
+
## Raster tiles
|
|
171
|
+
|
|
172
|
+
`useRasterUrl(style, { lang, retina })` gives a `{z}/{x}/{y}` raster tile URL template of a scheme or a custom style,
|
|
173
|
+
for tools that read raster tiles only (null until the map is ready).
|
|
174
|
+
|
|
175
|
+
## deck.gl layers
|
|
176
|
+
|
|
177
|
+
`<DeckLayer layer={new ArcLayer({ id: 'arcs', … })} />` draws a deck.gl layer interleaved with the map. A new layer
|
|
178
|
+
instance with the same id updates it, as deck.gl expects; install deck.gl yourself.
|
|
179
|
+
|
|
78
180
|
## Popups
|
|
79
181
|
|
|
80
182
|
`<Points>`, `<Lines>` and `<Polygons>` take `popup` and `popupOptions`; `<SMap>` takes `popup` (the default popup
|
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
9
|
var __export = (target, all) => {
|
|
8
10
|
for (var name in all)
|
|
@@ -16,16 +18,36 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
18
|
}
|
|
17
19
|
return to;
|
|
18
20
|
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
19
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
30
|
|
|
21
31
|
// src/index.ts
|
|
22
32
|
var index_exports = {};
|
|
23
33
|
__export(index_exports, {
|
|
34
|
+
ArcGisLayer: () => ArcGisLayer,
|
|
35
|
+
Data: () => Data,
|
|
36
|
+
DeckLayer: () => DeckLayer,
|
|
37
|
+
Layers: () => Layers,
|
|
24
38
|
Lines: () => Lines,
|
|
25
39
|
Points: () => Points,
|
|
26
40
|
Polygons: () => Polygons,
|
|
41
|
+
RasterLayer: () => RasterLayer,
|
|
27
42
|
SMap: () => SMap,
|
|
43
|
+
Source: () => Source,
|
|
44
|
+
StyleLayer: () => StyleLayer,
|
|
45
|
+
VectorTiles: () => VectorTiles,
|
|
46
|
+
WmsLayer: () => WmsLayer,
|
|
47
|
+
WmtsLayer: () => WmtsLayer,
|
|
48
|
+
formats: () => formats2,
|
|
28
49
|
popupTemplate: () => import_smap2.popupTemplate,
|
|
50
|
+
useRasterUrl: () => useRasterUrl,
|
|
29
51
|
useSMap: () => useSMap
|
|
30
52
|
});
|
|
31
53
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -61,7 +83,7 @@ var SMap = (0, import_react.forwardRef)(function SMap2(props, ref) {
|
|
|
61
83
|
const latest = (0, import_react.useRef)(props);
|
|
62
84
|
latest.current = props;
|
|
63
85
|
const applied = (0, import_react.useRef)(
|
|
64
|
-
{ style: "", language: "", camera: {}, projection: "", buildings: false }
|
|
86
|
+
{ style: "", language: "", camera: {}, projection: "", buildings: false, basemap: key(void 0) }
|
|
65
87
|
);
|
|
66
88
|
(0, import_react.useImperativeHandle)(ref, () => map, [map]);
|
|
67
89
|
const nodesKey = key(props.nodes);
|
|
@@ -111,7 +133,8 @@ var SMap = (0, import_react.forwardRef)(function SMap2(props, ref) {
|
|
|
111
133
|
language: key(p.language ?? null),
|
|
112
134
|
camera: { center: p.center, zoom: p.zoom, bearing: p.bearing, pitch: p.pitch },
|
|
113
135
|
projection: p.projection ?? "mercator",
|
|
114
|
-
buildings: false
|
|
136
|
+
buildings: false,
|
|
137
|
+
basemap: key(void 0)
|
|
115
138
|
};
|
|
116
139
|
created.ready.then(() => {
|
|
117
140
|
ready = true;
|
|
@@ -119,6 +142,15 @@ var SMap = (0, import_react.forwardRef)(function SMap2(props, ref) {
|
|
|
119
142
|
created.remove();
|
|
120
143
|
return;
|
|
121
144
|
}
|
|
145
|
+
const overrides = latest.current.basemap;
|
|
146
|
+
if (overrides !== void 0) {
|
|
147
|
+
applied.current.basemap = key(overrides);
|
|
148
|
+
try {
|
|
149
|
+
created.setBasemap(overrides);
|
|
150
|
+
} catch (e) {
|
|
151
|
+
latest.current.onError?.(e);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
122
154
|
setMap(created);
|
|
123
155
|
latest.current.onReady?.(created);
|
|
124
156
|
}, (e) => {
|
|
@@ -171,6 +203,18 @@ var SMap = (0, import_react.forwardRef)(function SMap2(props, ref) {
|
|
|
171
203
|
applied.current.projection = projection;
|
|
172
204
|
map.setProjection(projection);
|
|
173
205
|
}, [map, props.projection]);
|
|
206
|
+
const basemapKey = key(props.basemap);
|
|
207
|
+
(0, import_react.useEffect)(() => {
|
|
208
|
+
if (!map || applied.current.basemap === basemapKey) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
applied.current.basemap = basemapKey;
|
|
212
|
+
try {
|
|
213
|
+
map.setBasemap(latest.current.basemap ?? {});
|
|
214
|
+
} catch (e) {
|
|
215
|
+
latest.current.onError?.(e);
|
|
216
|
+
}
|
|
217
|
+
}, [map, basemapKey]);
|
|
174
218
|
(0, import_react.useEffect)(() => {
|
|
175
219
|
const on = !!props.buildings3D;
|
|
176
220
|
if (!map || applied.current.buildings === on) {
|
|
@@ -198,37 +242,46 @@ var import_react2 = require("react");
|
|
|
198
242
|
function optionsKey(options) {
|
|
199
243
|
return JSON.stringify(options ?? {}, (_k, v) => typeof v === "function" ? "[fn]" : v);
|
|
200
244
|
}
|
|
245
|
+
function layerOptions(latest, hasTap, hasPopup) {
|
|
246
|
+
const o = { ...latest.current.options || {} };
|
|
247
|
+
if (hasTap) {
|
|
248
|
+
o.onClick = (feature, event) => {
|
|
249
|
+
const p = latest.current;
|
|
250
|
+
p.options?.onClick?.(feature, event);
|
|
251
|
+
p.onTap?.(feature.properties || {}, { lat: event.lngLat.lat, lng: event.lngLat.lng }, feature);
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
if (hasPopup) {
|
|
255
|
+
const given = latest.current.popup ?? latest.current.options?.popup;
|
|
256
|
+
o.popup = typeof given === "string" ? given : (properties, feature) => {
|
|
257
|
+
const p = latest.current;
|
|
258
|
+
const f = p.popup ?? p.options?.popup;
|
|
259
|
+
return typeof f === "function" ? f(properties, feature) : f;
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
if (latest.current.slot) {
|
|
263
|
+
o.slot = latest.current.slot;
|
|
264
|
+
}
|
|
265
|
+
if (latest.current.popupOptions) {
|
|
266
|
+
o.popupOptions = { ...o.popupOptions || {}, ...latest.current.popupOptions };
|
|
267
|
+
}
|
|
268
|
+
return o;
|
|
269
|
+
}
|
|
201
270
|
function useLayer(kind, props) {
|
|
202
271
|
const map = useSMap();
|
|
203
|
-
const { id, data, options, onTap, visible = true, popupOptions } = props;
|
|
272
|
+
const { id, data, options, onTap, visible = true, popupOptions, slot } = props;
|
|
204
273
|
const latest = (0, import_react2.useRef)(props);
|
|
205
274
|
latest.current = props;
|
|
206
275
|
const shown = (0, import_react2.useRef)(null);
|
|
207
276
|
const shownVisible = (0, import_react2.useRef)(true);
|
|
208
277
|
const hasTap = !!onTap || typeof options?.onClick === "function";
|
|
209
278
|
const hasPopup = !!props.popup || typeof options?.popup === "function";
|
|
210
|
-
const looks = optionsKey(options) + optionsKey(popupOptions);
|
|
279
|
+
const looks = optionsKey(options) + optionsKey(popupOptions) + (slot ?? "");
|
|
211
280
|
(0, import_react2.useEffect)(() => {
|
|
212
281
|
if (!map) {
|
|
213
282
|
return void 0;
|
|
214
283
|
}
|
|
215
|
-
const o =
|
|
216
|
-
if (hasTap) {
|
|
217
|
-
o.onClick = (feature, event) => {
|
|
218
|
-
const p = latest.current;
|
|
219
|
-
p.options?.onClick?.(feature, event);
|
|
220
|
-
p.onTap?.(feature.properties || {}, { lat: event.lngLat.lat, lng: event.lngLat.lng }, feature);
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
if (hasPopup) {
|
|
224
|
-
o.popup = (properties, feature) => {
|
|
225
|
-
const p = latest.current;
|
|
226
|
-
return (p.popup ?? p.options?.popup)?.(properties, feature);
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
if (latest.current.popupOptions) {
|
|
230
|
-
o.popupOptions = { ...o.popupOptions || {}, ...latest.current.popupOptions };
|
|
231
|
-
}
|
|
284
|
+
const o = layerOptions(latest, hasTap, hasPopup);
|
|
232
285
|
const d = latest.current.data;
|
|
233
286
|
map[kind](id, d, o);
|
|
234
287
|
if (latest.current.visible === false) {
|
|
@@ -270,15 +323,340 @@ function Polygons(props) {
|
|
|
270
323
|
return useLayer("addPolygons", props);
|
|
271
324
|
}
|
|
272
325
|
|
|
326
|
+
// src/interop.tsx
|
|
327
|
+
var import_react3 = require("react");
|
|
328
|
+
var sdk = __toESM(require("@superrouteca/smap"), 1);
|
|
329
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
330
|
+
function quietly(f) {
|
|
331
|
+
try {
|
|
332
|
+
f();
|
|
333
|
+
} catch {
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
var SourceContext = (0, import_react3.createContext)(null);
|
|
337
|
+
function Source(props) {
|
|
338
|
+
const map = useSMap();
|
|
339
|
+
const { id, source, children } = props;
|
|
340
|
+
const latest = (0, import_react3.useRef)(source);
|
|
341
|
+
latest.current = source;
|
|
342
|
+
const inside = (0, import_react3.useRef)(/* @__PURE__ */ new Set());
|
|
343
|
+
const [shown, setShown] = (0, import_react3.useState)(null);
|
|
344
|
+
const sourceKey = optionsKey(source);
|
|
345
|
+
(0, import_react3.useEffect)(() => {
|
|
346
|
+
if (!map) {
|
|
347
|
+
return void 0;
|
|
348
|
+
}
|
|
349
|
+
map.addSource(id, latest.current);
|
|
350
|
+
setShown((s) => ({ map, generation: (s?.generation ?? 0) + 1 }));
|
|
351
|
+
return () => {
|
|
352
|
+
if (removedMaps.has(map)) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
inside.current.forEach((layerId) => quietly(() => map.removeLayer(layerId)));
|
|
356
|
+
inside.current.clear();
|
|
357
|
+
quietly(() => map.removeSource(id));
|
|
358
|
+
};
|
|
359
|
+
}, [map, id, sourceKey]);
|
|
360
|
+
const scope = (0, import_react3.useMemo)(() => ({
|
|
361
|
+
id,
|
|
362
|
+
register: (layerId) => {
|
|
363
|
+
inside.current.add(layerId);
|
|
364
|
+
return () => inside.current.delete(layerId);
|
|
365
|
+
}
|
|
366
|
+
}), [id]);
|
|
367
|
+
if (!map || !shown || shown.map !== map) {
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(SourceContext.Provider, { value: scope, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react3.Fragment, { children }, shown.generation) });
|
|
371
|
+
}
|
|
372
|
+
function effectiveLayer(layer, sourceId, visible) {
|
|
373
|
+
const out = { ...layer };
|
|
374
|
+
if (out.source === void 0 && sourceId !== void 0) {
|
|
375
|
+
out.source = sourceId;
|
|
376
|
+
}
|
|
377
|
+
if (!visible) {
|
|
378
|
+
out.layout = { ...layer.layout || {}, visibility: "none" };
|
|
379
|
+
}
|
|
380
|
+
return out;
|
|
381
|
+
}
|
|
382
|
+
function structureKey(layer, before, slot) {
|
|
383
|
+
const { paint: _p, layout: _l, filter: _f, ...rest } = layer;
|
|
384
|
+
return optionsKey(rest) + "|" + (before ?? "") + "|" + (slot ?? "");
|
|
385
|
+
}
|
|
386
|
+
function applyDiff(prev, next, set) {
|
|
387
|
+
const a = prev || {};
|
|
388
|
+
const b = next || {};
|
|
389
|
+
for (const name of /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
390
|
+
if (optionsKey(a[name]) !== optionsKey(b[name])) {
|
|
391
|
+
set(name, b[name]);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function StyleLayer(props) {
|
|
396
|
+
const map = useSMap();
|
|
397
|
+
const scope = (0, import_react3.useContext)(SourceContext);
|
|
398
|
+
const { layer, before, slot, visible = true } = props;
|
|
399
|
+
const current = effectiveLayer(layer, scope?.id, visible);
|
|
400
|
+
const latest = (0, import_react3.useRef)(current);
|
|
401
|
+
latest.current = current;
|
|
402
|
+
const shown = (0, import_react3.useRef)(null);
|
|
403
|
+
const structure = structureKey(current, before, slot);
|
|
404
|
+
(0, import_react3.useEffect)(() => {
|
|
405
|
+
if (!map) {
|
|
406
|
+
return void 0;
|
|
407
|
+
}
|
|
408
|
+
const l = latest.current;
|
|
409
|
+
const options = {};
|
|
410
|
+
if (before !== void 0) {
|
|
411
|
+
options.before = before;
|
|
412
|
+
}
|
|
413
|
+
if (slot !== void 0) {
|
|
414
|
+
options.slot = slot;
|
|
415
|
+
}
|
|
416
|
+
map.addStyleLayer(l, options);
|
|
417
|
+
shown.current = { map, layer: l };
|
|
418
|
+
const unregister = scope?.register(l.id);
|
|
419
|
+
return () => {
|
|
420
|
+
shown.current = null;
|
|
421
|
+
const stillMine = unregister ? unregister() : true;
|
|
422
|
+
if (stillMine && !removedMaps.has(map)) {
|
|
423
|
+
quietly(() => map.removeLayer(l.id));
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
}, [map, structure, scope]);
|
|
427
|
+
const looks = optionsKey(current.paint) + optionsKey(current.layout) + optionsKey(current.filter);
|
|
428
|
+
(0, import_react3.useEffect)(() => {
|
|
429
|
+
const s = shown.current;
|
|
430
|
+
if (!map || !s || s.map !== map) {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const next = latest.current;
|
|
434
|
+
const prev = s.layer;
|
|
435
|
+
if (prev === next) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
applyDiff(prev.paint, next.paint, (name, value) => map.setPaint(next.id, name, value));
|
|
439
|
+
applyDiff(prev.layout, next.layout, (name, value) => map.setLayout(next.id, name, value));
|
|
440
|
+
if (optionsKey(prev.filter) !== optionsKey(next.filter)) {
|
|
441
|
+
map.setFilter(next.id, next.filter ?? null);
|
|
442
|
+
}
|
|
443
|
+
s.layer = next;
|
|
444
|
+
}, [map, looks]);
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
function useAddedLayer(method, id, options, remove) {
|
|
448
|
+
const map = useSMap();
|
|
449
|
+
const latest = (0, import_react3.useRef)(options);
|
|
450
|
+
latest.current = options;
|
|
451
|
+
const removeLatest = (0, import_react3.useRef)(remove);
|
|
452
|
+
removeLatest.current = remove;
|
|
453
|
+
const looks = optionsKey(options);
|
|
454
|
+
(0, import_react3.useEffect)(() => {
|
|
455
|
+
if (!map) {
|
|
456
|
+
return void 0;
|
|
457
|
+
}
|
|
458
|
+
map[method](id, latest.current);
|
|
459
|
+
const removeNow = removeLatest.current;
|
|
460
|
+
return () => {
|
|
461
|
+
if (!removedMaps.has(map)) {
|
|
462
|
+
removeNow(map);
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
}, [map, method, id, looks]);
|
|
466
|
+
return null;
|
|
467
|
+
}
|
|
468
|
+
function split(props) {
|
|
469
|
+
const { id, ...options } = props;
|
|
470
|
+
return [id, options];
|
|
471
|
+
}
|
|
472
|
+
function RasterLayer(props) {
|
|
473
|
+
const [id, options] = split(props);
|
|
474
|
+
return useAddedLayer("addRasterLayer", id, options, (map) => quietly(() => map.removeLayer(id)));
|
|
475
|
+
}
|
|
476
|
+
function WmsLayer(props) {
|
|
477
|
+
const [id, options] = split(props);
|
|
478
|
+
return useAddedLayer("addWmsLayer", id, options, (map) => quietly(() => map.removeLayer(id)));
|
|
479
|
+
}
|
|
480
|
+
function WmtsLayer(props) {
|
|
481
|
+
const [id, options] = split(props);
|
|
482
|
+
return useAddedLayer("addWmtsLayer", id, options, (map) => quietly(() => map.removeLayer(id)));
|
|
483
|
+
}
|
|
484
|
+
function VectorTiles(props) {
|
|
485
|
+
const [id, options] = split(props);
|
|
486
|
+
const layerIds = (options.layers || []).map((l) => l.id);
|
|
487
|
+
return useAddedLayer("addVectorTiles", id, options, (map) => {
|
|
488
|
+
layerIds.forEach((l) => quietly(() => map.removeLayer(l)));
|
|
489
|
+
quietly(() => map.removeLayer(id));
|
|
490
|
+
quietly(() => map.removeSource(id));
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
function ArcGisLayer(props) {
|
|
494
|
+
const [id, options] = split(props);
|
|
495
|
+
return useAddedLayer("addArcGisLayer", id, options, (map) => quietly(() => map.removeLayer(id)));
|
|
496
|
+
}
|
|
497
|
+
var dataLayerIds = (id) => [id, `${id}:lines`, `${id}:areas`];
|
|
498
|
+
function Data(props) {
|
|
499
|
+
const map = useSMap();
|
|
500
|
+
const { id, data, format, lat, lng, options, slot, onTap, visible = true, popupOptions } = props;
|
|
501
|
+
const latest = (0, import_react3.useRef)(props);
|
|
502
|
+
latest.current = props;
|
|
503
|
+
const shownVisible = (0, import_react3.useRef)(true);
|
|
504
|
+
const hasTap = !!onTap || typeof options?.onClick === "function";
|
|
505
|
+
const hasPopup = !!props.popup || typeof options?.popup === "function";
|
|
506
|
+
const looks = optionsKey(options) + optionsKey(popupOptions) + (slot ?? "") + "|" + (format ?? "") + "|" + (lat ?? "") + "|" + (lng ?? "");
|
|
507
|
+
(0, import_react3.useEffect)(() => {
|
|
508
|
+
if (!map) {
|
|
509
|
+
return void 0;
|
|
510
|
+
}
|
|
511
|
+
const p = latest.current;
|
|
512
|
+
const o = layerOptions(latest, hasTap, hasPopup);
|
|
513
|
+
if (p.format !== void 0) {
|
|
514
|
+
o.format = p.format;
|
|
515
|
+
}
|
|
516
|
+
if (p.lat !== void 0) {
|
|
517
|
+
o.lat = p.lat;
|
|
518
|
+
}
|
|
519
|
+
if (p.lng !== void 0) {
|
|
520
|
+
o.lng = p.lng;
|
|
521
|
+
}
|
|
522
|
+
const ids = dataLayerIds(id);
|
|
523
|
+
let alive = true;
|
|
524
|
+
Promise.resolve(map.addData(id, p.data, o)).then(() => {
|
|
525
|
+
if (removedMaps.has(map)) {
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (!alive) {
|
|
529
|
+
ids.forEach((l) => quietly(() => map.removeLayer(l)));
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
if (latest.current.visible === false) {
|
|
533
|
+
map.layers().filter((l) => ids.includes(l)).forEach((l) => map.setVisible(l, false));
|
|
534
|
+
}
|
|
535
|
+
latest.current.onLoad?.();
|
|
536
|
+
}, (e) => {
|
|
537
|
+
if (alive) {
|
|
538
|
+
latest.current.onError?.(e);
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
shownVisible.current = p.visible !== false;
|
|
542
|
+
return () => {
|
|
543
|
+
alive = false;
|
|
544
|
+
if (removedMaps.has(map)) {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
ids.forEach((l) => quietly(() => map.removeLayer(l)));
|
|
548
|
+
};
|
|
549
|
+
}, [map, id, data, looks, hasTap, hasPopup]);
|
|
550
|
+
(0, import_react3.useEffect)(() => {
|
|
551
|
+
if (!map || shownVisible.current === visible) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
shownVisible.current = visible;
|
|
555
|
+
const ids = dataLayerIds(id);
|
|
556
|
+
map.layers().filter((l) => ids.includes(l)).forEach((l) => map.setVisible(l, visible));
|
|
557
|
+
}, [map, id, visible]);
|
|
558
|
+
return null;
|
|
559
|
+
}
|
|
560
|
+
function Layers(props) {
|
|
561
|
+
const map = useSMap();
|
|
562
|
+
const latest = (0, import_react3.useRef)(props.json);
|
|
563
|
+
latest.current = props.json;
|
|
564
|
+
const jsonKey = typeof props.json === "string" ? props.json : optionsKey(props.json);
|
|
565
|
+
(0, import_react3.useEffect)(() => {
|
|
566
|
+
if (!map) {
|
|
567
|
+
return void 0;
|
|
568
|
+
}
|
|
569
|
+
const json = latest.current;
|
|
570
|
+
map.loadLayers(json);
|
|
571
|
+
const d = typeof json === "string" ? JSON.parse(json) : json;
|
|
572
|
+
return () => {
|
|
573
|
+
if (removedMaps.has(map)) {
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
[...d.layers || []].reverse().forEach((l) => quietly(() => map.removeLayer(l.id)));
|
|
577
|
+
Object.keys(d.sources || {}).forEach((s) => quietly(() => map.removeSource(s)));
|
|
578
|
+
};
|
|
579
|
+
}, [map, jsonKey]);
|
|
580
|
+
return null;
|
|
581
|
+
}
|
|
582
|
+
function DeckLayer(props) {
|
|
583
|
+
const map = useSMap();
|
|
584
|
+
const { layer } = props;
|
|
585
|
+
const latest = (0, import_react3.useRef)(layer);
|
|
586
|
+
latest.current = layer;
|
|
587
|
+
const shown = (0, import_react3.useRef)(null);
|
|
588
|
+
(0, import_react3.useEffect)(() => {
|
|
589
|
+
if (!map) {
|
|
590
|
+
return void 0;
|
|
591
|
+
}
|
|
592
|
+
map.addDeckLayer(latest.current);
|
|
593
|
+
shown.current = { map, layer: latest.current };
|
|
594
|
+
return () => {
|
|
595
|
+
shown.current = null;
|
|
596
|
+
if (!removedMaps.has(map)) {
|
|
597
|
+
quietly(() => map.removeDeckLayer(layer.id));
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
}, [map, layer.id]);
|
|
601
|
+
(0, import_react3.useEffect)(() => {
|
|
602
|
+
const s = shown.current;
|
|
603
|
+
if (!map || !s || s.map !== map || s.layer === layer) {
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
s.layer = layer;
|
|
607
|
+
map.addDeckLayer(layer);
|
|
608
|
+
}, [map, layer]);
|
|
609
|
+
return null;
|
|
610
|
+
}
|
|
611
|
+
function useRasterUrl(style, options) {
|
|
612
|
+
const map = useSMap();
|
|
613
|
+
const lang = options?.lang;
|
|
614
|
+
const retina = options?.retina;
|
|
615
|
+
return (0, import_react3.useMemo)(() => {
|
|
616
|
+
if (!map) {
|
|
617
|
+
return null;
|
|
618
|
+
}
|
|
619
|
+
const o = {};
|
|
620
|
+
if (lang !== void 0) {
|
|
621
|
+
o.lang = lang;
|
|
622
|
+
}
|
|
623
|
+
if (retina !== void 0) {
|
|
624
|
+
o.retina = retina;
|
|
625
|
+
}
|
|
626
|
+
return map.rasterUrl(style, o);
|
|
627
|
+
}, [map, style, lang, retina]);
|
|
628
|
+
}
|
|
629
|
+
var formats2 = {
|
|
630
|
+
read(input, options) {
|
|
631
|
+
const f = sdk.formats;
|
|
632
|
+
if (!f) {
|
|
633
|
+
throw new Error("sMap: formats need @superrouteca/smap 1.1.0 or later");
|
|
634
|
+
}
|
|
635
|
+
return f.read(input, options);
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
|
|
273
639
|
// src/index.ts
|
|
274
640
|
var import_smap2 = require("@superrouteca/smap");
|
|
275
641
|
// Annotate the CommonJS export names for ESM import in node:
|
|
276
642
|
0 && (module.exports = {
|
|
643
|
+
ArcGisLayer,
|
|
644
|
+
Data,
|
|
645
|
+
DeckLayer,
|
|
646
|
+
Layers,
|
|
277
647
|
Lines,
|
|
278
648
|
Points,
|
|
279
649
|
Polygons,
|
|
650
|
+
RasterLayer,
|
|
280
651
|
SMap,
|
|
652
|
+
Source,
|
|
653
|
+
StyleLayer,
|
|
654
|
+
VectorTiles,
|
|
655
|
+
WmsLayer,
|
|
656
|
+
WmtsLayer,
|
|
657
|
+
formats,
|
|
281
658
|
popupTemplate,
|
|
659
|
+
useRasterUrl,
|
|
282
660
|
useSMap
|
|
283
661
|
});
|
|
284
662
|
//# sourceMappingURL=index.cjs.map
|