@superrouteca/smap-react 1.1.7 → 1.2.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/dist/index.js CHANGED
@@ -341,8 +341,157 @@ function Tool(props) {
341
341
  return box ? createPortal(props.children, box) : null;
342
342
  }
343
343
 
344
+ // src/features.tsx
345
+ import { useEffect as useEffect4, useRef as useRef4, useState as useState3 } from "react";
346
+ import { createPortal as createPortal2 } from "react-dom";
347
+ import { Fragment, jsx as jsx3 } from "react/jsx-runtime";
348
+ function CustomMarkers(props) {
349
+ const map = useSMap();
350
+ const { id, data, render: _render, ...rest } = props;
351
+ const latest = useRef4(props);
352
+ latest.current = props;
353
+ const [entries, setEntries] = useState3(/* @__PURE__ */ new Map());
354
+ const held = useRef4(/* @__PURE__ */ new Map());
355
+ const shown = useRef4(null);
356
+ const looks = optionsKey(rest);
357
+ const keyOf = (properties, feature) => {
358
+ const by = latest.current.idProperty;
359
+ const v = by ? properties[by] : feature.id;
360
+ return v === void 0 || v === null ? `@${JSON.stringify(feature.geometry)}` : String(v);
361
+ };
362
+ useEffect4(() => {
363
+ if (!map) {
364
+ return void 0;
365
+ }
366
+ held.current = /* @__PURE__ */ new Map();
367
+ let scheduled = false;
368
+ const publish = () => {
369
+ if (scheduled) {
370
+ return;
371
+ }
372
+ scheduled = true;
373
+ queueMicrotask(() => {
374
+ scheduled = false;
375
+ setEntries(new Map(held.current));
376
+ });
377
+ };
378
+ const options = {
379
+ ...latest.current,
380
+ render: (properties, feature) => {
381
+ const el = document.createElement("div");
382
+ held.current.set(keyOf(properties, feature), { el, properties, feature });
383
+ publish();
384
+ return el;
385
+ }
386
+ };
387
+ delete options.id;
388
+ delete options.data;
389
+ map.addCustomMarkers(id, latest.current.data, options);
390
+ shown.current = latest.current.data;
391
+ return () => {
392
+ shown.current = null;
393
+ held.current = /* @__PURE__ */ new Map();
394
+ setEntries(/* @__PURE__ */ new Map());
395
+ if (!removedMaps.has(map)) {
396
+ map.removeLayer(id);
397
+ }
398
+ };
399
+ }, [map, id, looks]);
400
+ useEffect4(() => {
401
+ if (!map || shown.current === null || shown.current === data) {
402
+ return;
403
+ }
404
+ shown.current = data;
405
+ map.setData(id, data);
406
+ const keep = /* @__PURE__ */ new Set();
407
+ const features = data && !Array.isArray(data) && data.features || [];
408
+ for (const f of features) {
409
+ keep.add(keyOf(f.properties || {}, f));
410
+ }
411
+ const by = latest.current.idProperty;
412
+ if (Array.isArray(data) && by) {
413
+ data.forEach((p) => keep.add(String(p[by])));
414
+ }
415
+ if (!Array.isArray(data) || by) {
416
+ let changed = false;
417
+ for (const k of [...held.current.keys()]) {
418
+ if (!keep.has(k)) {
419
+ held.current.delete(k);
420
+ changed = true;
421
+ }
422
+ }
423
+ if (changed) {
424
+ setEntries(new Map(held.current));
425
+ }
426
+ }
427
+ }, [map, id, data]);
428
+ return /* @__PURE__ */ jsx3(Fragment, { children: [...entries.entries()].map(([k, e]) => createPortal2(latest.current.render(e.properties, e.feature), e.el, `${id}\0${k}`)) });
429
+ }
430
+ function useNativeLayer(add, id, looks, data) {
431
+ const map = useSMap();
432
+ const shown = useRef4(null);
433
+ const addRef = useRef4(add);
434
+ addRef.current = add;
435
+ useEffect4(() => {
436
+ if (!map) {
437
+ return void 0;
438
+ }
439
+ addRef.current(map);
440
+ shown.current = data;
441
+ return () => {
442
+ shown.current = null;
443
+ if (!removedMaps.has(map)) {
444
+ map.removeLayer(id);
445
+ }
446
+ };
447
+ }, [map, id, looks]);
448
+ useEffect4(() => {
449
+ if (!map || data === void 0 || shown.current === null || shown.current === data) {
450
+ return;
451
+ }
452
+ shown.current = data;
453
+ map.setData(id, data);
454
+ }, [map, id, data]);
455
+ }
456
+ function Heatmap(props) {
457
+ useNativeLayer((m) => m.addHeatmap(props.id, props.data, props.options), props.id, optionsKey(props.options), props.data);
458
+ return null;
459
+ }
460
+ function Models(props) {
461
+ useNativeLayer((m) => m.addModels(props.id, props.data, props.options), props.id, optionsKey(props.options), props.data);
462
+ return null;
463
+ }
464
+ function CanvasTiles(props) {
465
+ const draw = useRef4(props.draw);
466
+ draw.current = props.draw;
467
+ useNativeLayer((m) => m.addCanvasTiles(props.id, (...a) => draw.current(...a), props.options), props.id, optionsKey(props.options));
468
+ return null;
469
+ }
470
+ function DrawTool(props) {
471
+ const map = useSMap();
472
+ const latest = useRef4(props);
473
+ latest.current = props;
474
+ const { onChange: _c, onTool: _t, ...rest } = props;
475
+ const looks = optionsKey(rest);
476
+ useEffect4(() => {
477
+ if (!map) {
478
+ return void 0;
479
+ }
480
+ const { onChange: _a, onTool: _b, ...o } = latest.current;
481
+ const tool = map.drawTool({ ...o, onChange: (e) => latest.current.onChange?.(e) });
482
+ latest.current.onTool?.(tool);
483
+ return () => {
484
+ latest.current.onTool?.(null);
485
+ if (!removedMaps.has(map)) {
486
+ tool.destroy();
487
+ }
488
+ };
489
+ }, [map, looks]);
490
+ return null;
491
+ }
492
+
344
493
  // src/plan.tsx
345
- import { forwardRef as forwardRef2, useEffect as useEffect4, useImperativeHandle as useImperativeHandle2, useRef as useRef4, useState as useState3 } from "react";
494
+ import { forwardRef as forwardRef2, useEffect as useEffect5, useImperativeHandle as useImperativeHandle2, useRef as useRef5, useState as useState4 } from "react";
346
495
  function destroyOffMap(made) {
347
496
  made.layer.remove = () => {
348
497
  };
@@ -375,11 +524,11 @@ var EMPTY = {
375
524
  };
376
525
  function usePlanEditor(props) {
377
526
  const map = useSMap();
378
- const [editor, setEditor] = useState3(null);
379
- const [state, setState] = useState3(EMPTY);
380
- const latest = useRef4(props);
527
+ const [editor, setEditor] = useState4(null);
528
+ const [state, setState] = useState4(EMPTY);
529
+ const latest = useRef5(props);
381
530
  latest.current = props;
382
- const applied = useRef4(null);
531
+ const applied = useRef5(null);
383
532
  const credentialKey = "key" in props.credentials ? "key:" + props.credentials.key : "token";
384
533
  const madeKey = key2({
385
534
  baseUrl: props.baseUrl,
@@ -400,7 +549,7 @@ function usePlanEditor(props) {
400
549
  fastPathTimeout: props.fastPathTimeout,
401
550
  fastPathRefineTimeout: props.fastPathRefineTimeout
402
551
  });
403
- useEffect4(() => {
552
+ useEffect5(() => {
404
553
  if (!map) {
405
554
  return void 0;
406
555
  }
@@ -463,7 +612,7 @@ function usePlanEditor(props) {
463
612
  };
464
613
  }, [map, credentialKey, madeKey]);
465
614
  const incoming = planKey(props.plan);
466
- useEffect4(() => {
615
+ useEffect5(() => {
467
616
  if (!editor || !latest.current.plan) {
468
617
  return;
469
618
  }
@@ -475,7 +624,7 @@ function usePlanEditor(props) {
475
624
  editor.setPlan(latest.current.plan).catch(() => {
476
625
  });
477
626
  }, [editor, incoming]);
478
- useEffect4(() => {
627
+ useEffect5(() => {
479
628
  if (editor && editor.panel && props.language) {
480
629
  editor.panel.setLanguage(props.language);
481
630
  }
@@ -489,9 +638,9 @@ var PlanEditor = forwardRef2(function PlanEditor2(props, ref) {
489
638
  });
490
639
 
491
640
  // src/interop.tsx
492
- import { createContext as createContext2, Fragment, useContext as useContext2, useEffect as useEffect5, useMemo, useRef as useRef5, useState as useState4 } from "react";
641
+ import { createContext as createContext2, Fragment as Fragment2, useContext as useContext2, useEffect as useEffect6, useMemo, useRef as useRef6, useState as useState5 } from "react";
493
642
  import * as sdk from "@superrouteca/smap";
494
- import { jsx as jsx3 } from "react/jsx-runtime";
643
+ import { jsx as jsx4 } from "react/jsx-runtime";
495
644
  function quietly(f) {
496
645
  try {
497
646
  f();
@@ -502,12 +651,12 @@ var SourceContext = createContext2(null);
502
651
  function Source(props) {
503
652
  const map = useSMap();
504
653
  const { id, source, children } = props;
505
- const latest = useRef5(source);
654
+ const latest = useRef6(source);
506
655
  latest.current = source;
507
- const inside = useRef5(/* @__PURE__ */ new Set());
508
- const [shown, setShown] = useState4(null);
656
+ const inside = useRef6(/* @__PURE__ */ new Set());
657
+ const [shown, setShown] = useState5(null);
509
658
  const sourceKey = optionsKey(source);
510
- useEffect5(() => {
659
+ useEffect6(() => {
511
660
  if (!map) {
512
661
  return void 0;
513
662
  }
@@ -532,7 +681,7 @@ function Source(props) {
532
681
  if (!map || !shown || shown.map !== map) {
533
682
  return null;
534
683
  }
535
- return /* @__PURE__ */ jsx3(SourceContext.Provider, { value: scope, children: /* @__PURE__ */ jsx3(Fragment, { children }, shown.generation) });
684
+ return /* @__PURE__ */ jsx4(SourceContext.Provider, { value: scope, children: /* @__PURE__ */ jsx4(Fragment2, { children }, shown.generation) });
536
685
  }
537
686
  function effectiveLayer(layer, sourceId, visible) {
538
687
  const out = { ...layer };
@@ -562,11 +711,11 @@ function StyleLayer(props) {
562
711
  const scope = useContext2(SourceContext);
563
712
  const { layer, before, slot, visible = true } = props;
564
713
  const current = effectiveLayer(layer, scope?.id, visible);
565
- const latest = useRef5(current);
714
+ const latest = useRef6(current);
566
715
  latest.current = current;
567
- const shown = useRef5(null);
716
+ const shown = useRef6(null);
568
717
  const structure = structureKey(current, before, slot);
569
- useEffect5(() => {
718
+ useEffect6(() => {
570
719
  if (!map) {
571
720
  return void 0;
572
721
  }
@@ -590,7 +739,7 @@ function StyleLayer(props) {
590
739
  };
591
740
  }, [map, structure, scope]);
592
741
  const looks = optionsKey(current.paint) + optionsKey(current.layout) + optionsKey(current.filter);
593
- useEffect5(() => {
742
+ useEffect6(() => {
594
743
  const s = shown.current;
595
744
  if (!map || !s || s.map !== map) {
596
745
  return;
@@ -611,12 +760,12 @@ function StyleLayer(props) {
611
760
  }
612
761
  function useAddedLayer(method, id, options, remove) {
613
762
  const map = useSMap();
614
- const latest = useRef5(options);
763
+ const latest = useRef6(options);
615
764
  latest.current = options;
616
- const removeLatest = useRef5(remove);
765
+ const removeLatest = useRef6(remove);
617
766
  removeLatest.current = remove;
618
767
  const looks = optionsKey(options);
619
- useEffect5(() => {
768
+ useEffect6(() => {
620
769
  if (!map) {
621
770
  return void 0;
622
771
  }
@@ -663,13 +812,13 @@ var dataLayerIds = (id) => [id, `${id}:lines`, `${id}:areas`];
663
812
  function Data(props) {
664
813
  const map = useSMap();
665
814
  const { id, data, format, lat, lng, options, slot, onTap, visible = true, popupOptions } = props;
666
- const latest = useRef5(props);
815
+ const latest = useRef6(props);
667
816
  latest.current = props;
668
- const shownVisible = useRef5(true);
817
+ const shownVisible = useRef6(true);
669
818
  const hasTap = !!onTap || typeof options?.onClick === "function";
670
819
  const hasPopup = !!props.popup || typeof options?.popup === "function";
671
820
  const looks = optionsKey(options) + optionsKey(popupOptions) + (slot ?? "") + "|" + (format ?? "") + "|" + (lat ?? "") + "|" + (lng ?? "");
672
- useEffect5(() => {
821
+ useEffect6(() => {
673
822
  if (!map) {
674
823
  return void 0;
675
824
  }
@@ -712,7 +861,7 @@ function Data(props) {
712
861
  ids.forEach((l) => quietly(() => map.removeLayer(l)));
713
862
  };
714
863
  }, [map, id, data, looks, hasTap, hasPopup]);
715
- useEffect5(() => {
864
+ useEffect6(() => {
716
865
  if (!map || shownVisible.current === visible) {
717
866
  return;
718
867
  }
@@ -724,10 +873,10 @@ function Data(props) {
724
873
  }
725
874
  function Layers(props) {
726
875
  const map = useSMap();
727
- const latest = useRef5(props.json);
876
+ const latest = useRef6(props.json);
728
877
  latest.current = props.json;
729
878
  const jsonKey = typeof props.json === "string" ? props.json : optionsKey(props.json);
730
- useEffect5(() => {
879
+ useEffect6(() => {
731
880
  if (!map) {
732
881
  return void 0;
733
882
  }
@@ -747,10 +896,10 @@ function Layers(props) {
747
896
  function DeckLayer(props) {
748
897
  const map = useSMap();
749
898
  const { layer } = props;
750
- const latest = useRef5(layer);
899
+ const latest = useRef6(layer);
751
900
  latest.current = layer;
752
- const shown = useRef5(null);
753
- useEffect5(() => {
901
+ const shown = useRef6(null);
902
+ useEffect6(() => {
754
903
  if (!map) {
755
904
  return void 0;
756
905
  }
@@ -763,7 +912,7 @@ function DeckLayer(props) {
763
912
  }
764
913
  };
765
914
  }, [map, layer.id]);
766
- useEffect5(() => {
915
+ useEffect6(() => {
767
916
  const s = shown.current;
768
917
  if (!map || !s || s.map !== map || s.layer === layer) {
769
918
  return;
@@ -805,10 +954,15 @@ var formats2 = {
805
954
  import { popupTemplate, planTexts } from "@superrouteca/smap";
806
955
  export {
807
956
  ArcGisLayer,
957
+ CanvasTiles,
958
+ CustomMarkers,
808
959
  Data,
809
960
  DeckLayer,
961
+ DrawTool,
962
+ Heatmap,
810
963
  Layers,
811
964
  Lines,
965
+ Models,
812
966
  PlanEditor,
813
967
  Points,
814
968
  Polygons,