@superrouteca/smap-react 1.2.15 → 1.5.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/index.js CHANGED
@@ -10,7 +10,9 @@ import {
10
10
  useRef,
11
11
  useState
12
12
  } from "react";
13
- import { Map as BaseMap } from "@superrouteca/smap";
13
+ import {
14
+ Map as BaseMap
15
+ } from "@superrouteca/smap";
14
16
  import { jsx, jsxs } from "react/jsx-runtime";
15
17
  var MapContext = createContext(null);
16
18
  var removedMaps = /* @__PURE__ */ new WeakSet();
@@ -69,7 +71,9 @@ var SMap = forwardRef(function SMap2(props, ref) {
69
71
  projection: p.projection,
70
72
  popup: p.popup,
71
73
  popupLoading: p.popupLoading,
72
- tools: p.tools
74
+ tools: p.tools,
75
+ cooperativeGestures: p.cooperativeGestures,
76
+ unavailableNotice: p.unavailableNotice
73
77
  };
74
78
  for (const k of Object.keys(options)) {
75
79
  if (options[k] === void 0) {
@@ -78,6 +82,7 @@ var SMap = forwardRef(function SMap2(props, ref) {
78
82
  }
79
83
  let alive = true;
80
84
  let ready = false;
85
+ let removedEarly = false;
81
86
  let created;
82
87
  try {
83
88
  created = new BaseMap(options);
@@ -94,10 +99,19 @@ var SMap = forwardRef(function SMap2(props, ref) {
94
99
  basemap: key(void 0)
95
100
  };
96
101
  created.onToolsChange((e) => latest.current.onToolsChange?.(e));
102
+ if (typeof created.on === "function") {
103
+ created.on("smap.unavailable", (e) => {
104
+ if (alive) {
105
+ latest.current.onUnavailable?.(e);
106
+ }
107
+ });
108
+ }
97
109
  created.ready.then(() => {
98
110
  ready = true;
99
111
  if (!alive) {
100
- created.remove();
112
+ if (!removedEarly) {
113
+ created.remove();
114
+ }
101
115
  return;
102
116
  }
103
117
  const overrides = latest.current.basemap;
@@ -122,6 +136,13 @@ var SMap = forwardRef(function SMap2(props, ref) {
122
136
  setMap(null);
123
137
  if (ready) {
124
138
  created.remove();
139
+ } else if (created.unavailable) {
140
+ removedEarly = true;
141
+ try {
142
+ created.remove();
143
+ } catch (e) {
144
+ }
145
+ container.querySelectorAll(":scope > .smap-unavailable").forEach((n) => n.remove());
125
146
  }
126
147
  };
127
148
  }, [props.apiKey, props.token, props.baseUrl, nodesKey, props.nearest, mapOptionsKey, hasGetToken]);
@@ -200,8 +221,74 @@ import { useEffect as useEffect2, useRef as useRef2 } from "react";
200
221
  function optionsKey(options) {
201
222
  return JSON.stringify(options ?? {}, (_k, v) => typeof v === "function" ? "[fn]" : v);
202
223
  }
224
+ var isPlain = (v) => {
225
+ if (!v || typeof v !== "object") {
226
+ return false;
227
+ }
228
+ if (Array.isArray(v)) {
229
+ return true;
230
+ }
231
+ const proto = Object.getPrototypeOf(v);
232
+ return proto === Object.prototype || proto === null;
233
+ };
234
+ function at(root, path) {
235
+ let v = root;
236
+ for (const k of path) {
237
+ if (!v || typeof v !== "object") {
238
+ return void 0;
239
+ }
240
+ v = v[k];
241
+ }
242
+ return v;
243
+ }
244
+ function liveFunctions(value, read, path = []) {
245
+ if (typeof value === "function") {
246
+ const first = value;
247
+ return ((...args) => {
248
+ const f = at(read(), path);
249
+ return (typeof f === "function" ? f : first)(...args);
250
+ });
251
+ }
252
+ if (!isPlain(value)) {
253
+ return value;
254
+ }
255
+ if (Array.isArray(value)) {
256
+ return value.map((v, i) => liveFunctions(v, read, [...path, i]));
257
+ }
258
+ const out = {};
259
+ for (const k of Object.keys(value)) {
260
+ out[k] = liveFunctions(value[k], read, [...path, k]);
261
+ }
262
+ return out;
263
+ }
264
+ var POINTER = ["onHover", "onDblClick", "onContextMenu"];
265
+ function handlersKey(p) {
266
+ return ["onTap", "popup", ...POINTER, "onClusterClick"].map((k) => p[k] ? "1" : "0").join("");
267
+ }
203
268
  function layerOptions(latest, hasTap, hasPopup) {
204
- const o = { ...latest.current.options || {} };
269
+ const o = liveFunctions({ ...latest.current.options || {} }, () => latest.current.options);
270
+ for (const name of POINTER) {
271
+ if (!latest.current[name]) {
272
+ continue;
273
+ }
274
+ o[name] = (feature, event) => {
275
+ const p = latest.current;
276
+ const own = p.options?.[name];
277
+ if (typeof own === "function") {
278
+ own(feature, event);
279
+ }
280
+ p[name]?.(feature, event);
281
+ };
282
+ }
283
+ if (latest.current.onClusterClick && o.cluster) {
284
+ const cluster = o.cluster === true ? {} : { ...o.cluster };
285
+ const own = cluster.onClick;
286
+ cluster.onClick = (feature, event, info) => {
287
+ own?.(feature, event, info);
288
+ latest.current.onClusterClick?.(info, feature, event);
289
+ };
290
+ o.cluster = cluster;
291
+ }
205
292
  if (hasTap) {
206
293
  o.onClick = (feature, event) => {
207
294
  const p = latest.current;
@@ -225,6 +312,45 @@ function layerOptions(latest, hasTap, hasPopup) {
225
312
  }
226
313
  return o;
227
314
  }
315
+ var stateKey = (v) => JSON.stringify(v === void 0 ? null : v);
316
+ function useFeatureStates(map, id, states, drawn) {
317
+ const applied = useRef2({ map: null, id: "", drawn: "", states: {} });
318
+ const latest = useRef2(states);
319
+ latest.current = states;
320
+ const statesKey = stateKey(states ?? {});
321
+ useEffect2(() => {
322
+ if (!map || removedMaps.has(map)) {
323
+ return;
324
+ }
325
+ const a = applied.current;
326
+ if (a.map !== map || a.id !== id || a.drawn !== drawn) {
327
+ applied.current = { map, id, drawn, states: {} };
328
+ }
329
+ const next = latest.current || {};
330
+ const prev = applied.current.states;
331
+ for (const fid of Object.keys(prev)) {
332
+ for (const name of Object.keys(prev[fid])) {
333
+ if (!next[fid] || !(name in next[fid])) {
334
+ map.removeFeatureState(id, fid, name);
335
+ }
336
+ }
337
+ }
338
+ for (const fid of Object.keys(next)) {
339
+ const changed = {};
340
+ let any = false;
341
+ for (const [name, v] of Object.entries(next[fid] || {})) {
342
+ if (!prev[fid] || !(name in prev[fid]) || stateKey(prev[fid][name]) !== stateKey(v)) {
343
+ changed[name] = v;
344
+ any = true;
345
+ }
346
+ }
347
+ if (any) {
348
+ map.setFeatureState(id, fid, changed);
349
+ }
350
+ }
351
+ applied.current.states = JSON.parse(statesKey);
352
+ }, [map, id, statesKey, drawn]);
353
+ }
228
354
  function useLayer(kind, props) {
229
355
  const map = useSMap();
230
356
  const { id, data, options, onTap, visible = true, popupOptions, slot } = props;
@@ -234,7 +360,7 @@ function useLayer(kind, props) {
234
360
  const shownVisible = useRef2(true);
235
361
  const hasTap = !!onTap || typeof options?.onClick === "function";
236
362
  const hasPopup = !!props.popup || typeof options?.popup === "function";
237
- const looks = optionsKey(options) + optionsKey(popupOptions) + (slot ?? "");
363
+ const looks = optionsKey(options) + optionsKey(popupOptions) + (slot ?? "") + handlersKey(props);
238
364
  useEffect2(() => {
239
365
  if (!map) {
240
366
  return void 0;
@@ -254,6 +380,7 @@ function useLayer(kind, props) {
254
380
  }
255
381
  };
256
382
  }, [map, id, looks, hasTap, hasPopup]);
383
+ useFeatureStates(map, id, props.featureStates, `${looks}|${hasTap}|${hasPopup}`);
257
384
  useEffect2(() => {
258
385
  const s = shown.current;
259
386
  if (!map || !s || s.map !== map || s.data === data) {
@@ -280,6 +407,9 @@ function Lines(props) {
280
407
  function Polygons(props) {
281
408
  return useLayer("addPolygons", props);
282
409
  }
410
+ function Circles(props) {
411
+ return useLayer("addCircles", props);
412
+ }
283
413
 
284
414
  // src/tools.tsx
285
415
  import { useEffect as useEffect3, useRef as useRef3, useState as useState2 } from "react";
@@ -340,6 +470,30 @@ function Tool(props) {
340
470
  }
341
471
  return box ? createPortal(props.children, box) : null;
342
472
  }
473
+ function SearchTool(props) {
474
+ const map = useSMap();
475
+ const latest = useRef3(props);
476
+ latest.current = props;
477
+ const { id, search: _s, onPick: _p, ...rest } = props;
478
+ const looks = optionsKey2(rest);
479
+ useEffect3(() => {
480
+ if (!map) {
481
+ return void 0;
482
+ }
483
+ const { id: _i, search: _a, onPick: _b, ...o } = latest.current;
484
+ map.addTool(id, "search", {
485
+ ...o,
486
+ search: (text) => latest.current.search(text),
487
+ onPick: (result) => latest.current.onPick?.(result)
488
+ });
489
+ return () => {
490
+ if (!removedMaps.has(map)) {
491
+ map.removeTool(id);
492
+ }
493
+ };
494
+ }, [map, id, looks]);
495
+ return null;
496
+ }
343
497
 
344
498
  // src/features.tsx
345
499
  import { useEffect as useEffect4, useRef as useRef4, useState as useState3 } from "react";
@@ -347,7 +501,7 @@ import { createPortal as createPortal2 } from "react-dom";
347
501
  import { Fragment, jsx as jsx3 } from "react/jsx-runtime";
348
502
  function CustomMarkers(props) {
349
503
  const map = useSMap();
350
- const { id, data, render: _render, ...rest } = props;
504
+ const { id, data, render: _render, featureStates, ...rest } = props;
351
505
  const latest = useRef4(props);
352
506
  latest.current = props;
353
507
  const [entries, setEntries] = useState3(/* @__PURE__ */ new Map());
@@ -375,8 +529,9 @@ function CustomMarkers(props) {
375
529
  setEntries(new Map(held.current));
376
530
  });
377
531
  };
532
+ const { featureStates: _s, render: _r, ...given } = latest.current;
378
533
  const options = {
379
- ...latest.current,
534
+ ...liveFunctions(given, () => latest.current),
380
535
  render: (properties, feature) => {
381
536
  const el = document.createElement("div");
382
537
  held.current.set(keyOf(properties, feature), { el, properties, feature });
@@ -425,6 +580,7 @@ function CustomMarkers(props) {
425
580
  }
426
581
  }
427
582
  }, [map, id, data]);
583
+ useFeatureStates(map, id, featureStates, looks);
428
584
  return /* @__PURE__ */ jsx3(Fragment, { children: [...entries.entries()].map(([k, e]) => createPortal2(latest.current.render(e.properties, e.feature), e.el, `${id}\0${k}`)) });
429
585
  }
430
586
  function useNativeLayer(add, id, looks, data) {
@@ -453,12 +609,19 @@ function useNativeLayer(add, id, looks, data) {
453
609
  map.setData(id, data);
454
610
  }, [map, id, data]);
455
611
  }
612
+ function useLatest(value) {
613
+ const ref = useRef4(value);
614
+ ref.current = value;
615
+ return ref;
616
+ }
456
617
  function Heatmap(props) {
457
- useNativeLayer((m) => m.addHeatmap(props.id, props.data, props.options), props.id, optionsKey(props.options), props.data);
618
+ const latest = useLatest(props);
619
+ useNativeLayer((m) => m.addHeatmap(props.id, props.data, liveFunctions(props.options, () => latest.current.options)), props.id, optionsKey(props.options), props.data);
458
620
  return null;
459
621
  }
460
622
  function Models(props) {
461
- useNativeLayer((m) => m.addModels(props.id, props.data, props.options), props.id, optionsKey(props.options), props.data);
623
+ const latest = useLatest(props);
624
+ useNativeLayer((m) => m.addModels(props.id, props.data, liveFunctions(props.options, () => latest.current.options)), props.id, optionsKey(props.options), props.data);
462
625
  return null;
463
626
  }
464
627
  function CanvasTiles(props) {
@@ -471,14 +634,18 @@ function DrawTool(props) {
471
634
  const map = useSMap();
472
635
  const latest = useRef4(props);
473
636
  latest.current = props;
474
- const { onChange: _c, onTool: _t, ...rest } = props;
475
- const looks = optionsKey(rest);
637
+ const { onChange: _c, onTool: _t, onDrawing: _d, ...rest } = props;
638
+ const looks = optionsKey(rest) + (props.onDrawing ? "|drawing" : "");
476
639
  useEffect4(() => {
477
640
  if (!map) {
478
641
  return void 0;
479
642
  }
480
- const { onChange: _a, onTool: _b, ...o } = latest.current;
481
- const tool = map.drawTool({ ...o, onChange: (e) => latest.current.onChange?.(e) });
643
+ const { onChange: _a, onTool: _b, onDrawing: _e, ...o } = latest.current;
644
+ const options = { ...liveFunctions(o, () => latest.current), onChange: (e) => latest.current.onChange?.(e) };
645
+ if (latest.current.onDrawing) {
646
+ options.onDrawing = (e) => latest.current.onDrawing?.(e);
647
+ }
648
+ const tool = map.drawTool(options);
482
649
  latest.current.onTool?.(tool);
483
650
  return () => {
484
651
  latest.current.onTool?.(null);
@@ -489,6 +656,92 @@ function DrawTool(props) {
489
656
  }, [map, looks]);
490
657
  return null;
491
658
  }
659
+ function useMapImage(id, image, options) {
660
+ const map = useSMap();
661
+ const [loaded, setLoaded] = useState3(null);
662
+ const onError = useLatest(options?.onError);
663
+ const pixelRatio = options?.pixelRatio;
664
+ const sdf = options?.sdf;
665
+ const last = useRef4(null);
666
+ const lastTarget = useRef4({ map: null, id: "" });
667
+ lastTarget.current = { map, id };
668
+ useEffect4(() => {
669
+ if (!map || image === null || image === void 0) {
670
+ return void 0;
671
+ }
672
+ let alive = true;
673
+ const mine = {};
674
+ last.current = mine;
675
+ const o = {};
676
+ if (pixelRatio !== void 0) {
677
+ o.pixelRatio = pixelRatio;
678
+ }
679
+ if (sdf !== void 0) {
680
+ o.sdf = sdf;
681
+ }
682
+ let added;
683
+ try {
684
+ added = Promise.resolve(map.addImage(id, image, o));
685
+ } catch (e) {
686
+ onError.current?.(e);
687
+ return void 0;
688
+ }
689
+ added.then(() => {
690
+ if (alive) {
691
+ setLoaded({ map, id, image });
692
+ }
693
+ }, (e) => {
694
+ if (alive) {
695
+ onError.current?.(e);
696
+ }
697
+ });
698
+ return () => {
699
+ alive = false;
700
+ setLoaded(null);
701
+ queueMicrotask(() => {
702
+ const replaced = last.current !== mine && lastTarget.current.map === map && lastTarget.current.id === id;
703
+ if (!replaced && !removedMaps.has(map)) {
704
+ map.removeImage(id);
705
+ }
706
+ });
707
+ };
708
+ }, [map, id, image, pixelRatio, sdf]);
709
+ return !!loaded && loaded.map === map && loaded.id === id && loaded.image === image;
710
+ }
711
+ function MapImage(props) {
712
+ const loaded = useMapImage(props.id, props.image, { pixelRatio: props.pixelRatio, sdf: props.sdf, onError: props.onError });
713
+ const onLoad = useLatest(props.onLoad);
714
+ useEffect4(() => {
715
+ if (loaded) {
716
+ onLoad.current?.();
717
+ }
718
+ }, [loaded]);
719
+ return null;
720
+ }
721
+ function ThreeLayer(props) {
722
+ const map = useSMap();
723
+ const latest = useLatest(props);
724
+ const { id, onAdd, render, onRemove, onError: _e, ...placement } = props;
725
+ const looks = optionsKey(placement) + (onAdd ? "a" : "") + (render ? "r" : "") + (onRemove ? "x" : "");
726
+ useEffect4(() => {
727
+ if (!map) {
728
+ return void 0;
729
+ }
730
+ const { id: _i, onError: _o, ...given } = latest.current;
731
+ try {
732
+ map.addThreeLayer(id, liveFunctions(given, () => latest.current));
733
+ } catch (e) {
734
+ latest.current.onError?.(e);
735
+ return void 0;
736
+ }
737
+ return () => {
738
+ if (!removedMaps.has(map)) {
739
+ map.removeLayer(id);
740
+ }
741
+ };
742
+ }, [map, id, looks]);
743
+ return null;
744
+ }
492
745
 
493
746
  // src/plan.tsx
494
747
  import { forwardRef as forwardRef2, useEffect as useEffect5, useImperativeHandle as useImperativeHandle2, useRef as useRef5, useState as useState4 } from "react";
@@ -961,6 +1214,7 @@ import { popupTemplate, planTexts } from "@superrouteca/smap";
961
1214
  export {
962
1215
  ArcGisLayer,
963
1216
  CanvasTiles,
1217
+ Circles,
964
1218
  CustomMarkers,
965
1219
  Data,
966
1220
  DeckLayer,
@@ -968,14 +1222,17 @@ export {
968
1222
  Heatmap,
969
1223
  Layers,
970
1224
  Lines,
1225
+ MapImage,
971
1226
  Models,
972
1227
  PlanEditor,
973
1228
  Points,
974
1229
  Polygons,
975
1230
  RasterLayer,
976
1231
  SMap,
1232
+ SearchTool,
977
1233
  Source,
978
1234
  StyleLayer,
1235
+ ThreeLayer,
979
1236
  Tool,
980
1237
  VectorTiles,
981
1238
  WmsLayer,
@@ -983,6 +1240,7 @@ export {
983
1240
  formats2 as formats,
984
1241
  planTexts,
985
1242
  popupTemplate,
1243
+ useMapImage,
986
1244
  usePlanEditor,
987
1245
  useRasterUrl,
988
1246
  useSMap