@superrouteca/smap-react 1.1.5 → 1.1.6

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 CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.6 — 2026-10-28
4
+
5
+ - Plan editor: `<PlanEditor>` edits a plan of routes on the map with the ready-made panel in 14 languages — drag stops
6
+ within and between routes, remove, insert, refine, undo and redo — and reports every new plan to `onChange`.
7
+ `usePlanEditor` gives the editor and its state for an interface of your own.
8
+ - Map tools: `<Tool layer="stops">` (or `layer={['stops', 'stop-labels']}`) makes a data layer a tool, shown and
9
+ hidden by the tools control like every other tool.
10
+ - Map tools: the tools control is the last button of the map's toolbar by default (the corner of the zoom buttons,
11
+ top left without one); `tools={{ control: 'top-right' }}` keeps a corner as before. Its corner is drawn above the
12
+ others while the panel is open.
13
+
3
14
  ## 1.1.5 — 2026-10-28
4
15
 
5
16
  - Map tools: `<Tool>` puts a control or panel on the map that the map's tools control shows and hides, one by one or
package/README.md CHANGED
@@ -82,8 +82,8 @@ replaces the data and keeps the look, new `options` draw the layer again. Higher
82
82
 
83
83
  ## Map tools
84
84
 
85
- Put controls and panels on the map as `<Tool>`s; the map gets a tools control (one button, top right) that shows or
86
- hides each of them, with *Show all* and *Hide all*:
85
+ Put controls, panels and data layers on the map as `<Tool>`s; the map gets a tools control (one button, the last of
86
+ the map's toolbar) that shows or hides each of them, with *Show all* and *Hide all*:
87
87
 
88
88
  ```jsx
89
89
  <SMap apiKey="smk_…" tools={{ hidden: saved }} onToolsChange={(e) => save(e.hidden)}>
@@ -91,17 +91,46 @@ hides each of them, with *Show all* and *Hide all*:
91
91
  <Tool id="fullscreen" kind="fullscreen" />
92
92
  <Tool id="legend" label="Legend" position="bottom-left"><Legend /></Tool>
93
93
  <Tool id="orders" label="Orders list" position="none"><OrdersList /></Tool>
94
+ <Tool id="stops" label="Stops" layer={['stops', 'stop-labels']} />
94
95
  </SMap>
95
96
  ```
96
97
 
97
98
  - `kind` makes a control: `navigation`, `geolocate`, `fullscreen` or `scale`. Otherwise the children are the tool,
98
99
  drawn in a corner (`position`, default `top-left`) or where `<Tool>` is (`position="none"`).
100
+ - `layer` makes a data-layer tool: one or more layer ids (`<Points>`, `<Lines>`, `<Polygons>`, the map's layer calls,
101
+ or the basemap's). Hiding the tool hides them, showing it shows them; it renders nothing.
99
102
  - A hidden tool stays mounted: its state is kept and it comes back as it was.
100
- - `tools` is read when the map is created: `hidden` (ids hidden from the start), `control` (a corner, or `false` for
101
- none) and `labels` (`button`, `showAll`, `hideAll`). The map keeps no state: keep `onToolsChange`'s `hidden`
103
+ - `tools` is read when the map is created: `hidden` (ids hidden from the start), `control` (`'toolbar'` by default: the last
104
+ button of the corner holding the zoom buttons, top left without one; a corner; or `false` for none) and `labels` (`button`, `showAll`, `hideAll`). The map keeps no state: keep `onToolsChange`'s `hidden`
102
105
  yourself. The ref's `setToolVisible`, `setToolsVisible` and `tools()` switch and read them in code.
103
106
  - The map data credit and the sMap badge are never tools and always stay.
104
107
 
108
+ ## Plan editor
109
+
110
+ `<PlanEditor>` edits a plan of routes on the map with the ready-made panel (see the Web SDK's *Plan editor*): drag
111
+ stops within and between routes, remove, insert, refine, undo and redo, each a call of the Open Platform's plan
112
+ editing. The ref is the editor once the map is ready.
113
+
114
+ ```tsx
115
+ const [plan, setPlan] = useState<Plan>(initialPlan);
116
+ const editor = useRef<MapPlanEditor>(null);
117
+
118
+ <SMap apiKey="smk_…">
119
+ <PlanEditor ref={editor} credentials={{ token: getPlanToken }} plan={plan} language="de"
120
+ onChange={(e) => { if (!e.pending) { setPlan(e.plan!); } }} onError={(err) => report(err.code)} />
121
+ </SMap>
122
+ ```
123
+
124
+ - `credentials`: `{ token: () => Promise<string | PlanTokenAnswer> }` (the latest function is called; your own
125
+ server mints the token with `POST /v1/plan/token` and may hand its answer on as it is) or `{ key: 'smk_…' }` (a map
126
+ key with plan editing switched on). Both are sent as `Authorization: Bearer …`.
127
+ - `plan`: a plan that differs from the one the editor holds replaces it; handing back the plan of `onChange` is not a
128
+ change.
129
+ - `usePlanEditor(props)` gives the editor and its state (`plan`, `result`, `busy`, `error`, `canUndo`, `canRedo`,
130
+ `choices`, `preview`) for an interface of your own; with `panel={false}` the map shows only the routes.
131
+ - Every option of `map.planEditor` is a prop (`region`, `profile`, `insertLimit`, `objective`, `colors`, `panel`,
132
+ `labels`, …); `onBusy`, `onSnapshot` and `onReady` report the rest.
133
+
105
134
  ## Basemap and custom styles
106
135
 
107
136
  `basemap` shows, hides and recolours the basemap by class — `water`, `land`, `parks`, `roads` (and `roads.highway`,
package/dist/index.cjs CHANGED
@@ -12,9 +12,9 @@ var __export = (target, all) => {
12
12
  };
13
13
  var __copyProps = (to, from, except, desc) => {
14
14
  if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key2 of __getOwnPropNames(from))
16
- if (!__hasOwnProp.call(to, key2) && key2 !== except)
17
- __defProp(to, key2, { get: () => from[key2], enumerable: !(desc = __getOwnPropDesc(from, key2)) || desc.enumerable });
15
+ for (let key3 of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key3) && key3 !== except)
17
+ __defProp(to, key3, { get: () => from[key3], enumerable: !(desc = __getOwnPropDesc(from, key3)) || desc.enumerable });
18
18
  }
19
19
  return to;
20
20
  };
@@ -36,6 +36,7 @@ __export(index_exports, {
36
36
  DeckLayer: () => DeckLayer,
37
37
  Layers: () => Layers,
38
38
  Lines: () => Lines,
39
+ PlanEditor: () => PlanEditor,
39
40
  Points: () => Points,
40
41
  Polygons: () => Polygons,
41
42
  RasterLayer: () => RasterLayer,
@@ -47,7 +48,9 @@ __export(index_exports, {
47
48
  WmsLayer: () => WmsLayer,
48
49
  WmtsLayer: () => WmtsLayer,
49
50
  formats: () => formats2,
51
+ planTexts: () => import_smap2.planTexts,
50
52
  popupTemplate: () => import_smap2.popupTemplate,
53
+ usePlanEditor: () => usePlanEditor,
51
54
  useRasterUrl: () => useRasterUrl,
52
55
  useSMap: () => useSMap
53
56
  });
@@ -334,14 +337,17 @@ var optionsKey2 = (v) => JSON.stringify(v ?? null);
334
337
  function Tool(props) {
335
338
  const map = useSMap();
336
339
  const { id, label, kind } = props;
337
- const position = props.position ?? (kind ? void 0 : "top-left");
338
- const inPlace = !kind && position === "none";
340
+ const layer = props.layer;
341
+ const layerKey = layer === void 0 ? null : JSON.stringify(layer);
342
+ const isLayer = layerKey !== null;
343
+ const position = isLayer ? void 0 : props.position ?? (kind ? void 0 : "top-left");
344
+ const inPlace = !kind && !isLayer && position === "none";
339
345
  const [box, setBox] = (0, import_react3.useState)(null);
340
346
  (0, import_react3.useEffect)(() => {
341
- if (!kind && !inPlace && !box) {
347
+ if (!kind && !isLayer && !inPlace && !box) {
342
348
  setBox(document.createElement("div"));
343
349
  }
344
- }, [kind, inPlace, box]);
350
+ }, [kind, isLayer, inPlace, box]);
345
351
  const inPlaceRef = (0, import_react3.useRef)(null);
346
352
  const kindOptions = optionsKey2(props.options);
347
353
  const latestOptions = (0, import_react3.useRef)(props.options);
@@ -355,6 +361,14 @@ function Tool(props) {
355
361
  if (!map) {
356
362
  return void 0;
357
363
  }
364
+ if (layerKey !== null) {
365
+ map.addTool(id, { layer: JSON.parse(layerKey) }, { label });
366
+ return () => {
367
+ if (!removedMaps.has(map)) {
368
+ map.removeTool(id);
369
+ }
370
+ };
371
+ }
358
372
  const target = kind ?? (inPlace ? inPlaceRef.current : box);
359
373
  if (!target) {
360
374
  return void 0;
@@ -365,8 +379,8 @@ function Tool(props) {
365
379
  map.removeTool(id);
366
380
  }
367
381
  };
368
- }, [map, id, label, position, kind, inPlace, kindOptions, box]);
369
- if (kind) {
382
+ }, [map, id, label, position, kind, inPlace, kindOptions, box, layerKey]);
383
+ if (kind || isLayer) {
370
384
  return null;
371
385
  }
372
386
  if (inPlace) {
@@ -375,8 +389,146 @@ function Tool(props) {
375
389
  return box ? (0, import_react_dom.createPortal)(props.children, box) : null;
376
390
  }
377
391
 
378
- // src/interop.tsx
392
+ // src/plan.tsx
379
393
  var import_react4 = require("react");
394
+ function destroyOffMap(made) {
395
+ made.layer.remove = () => {
396
+ };
397
+ try {
398
+ made.destroy();
399
+ } catch {
400
+ }
401
+ }
402
+ var key2 = (v) => JSON.stringify(v ?? null);
403
+ function planKey(plan) {
404
+ if (!plan) {
405
+ return key2(null);
406
+ }
407
+ const routes = (plan.routes || []).map((r) => ({ v: String(r.vehicle_id), s: (r.reverse ? r.stops.slice().reverse() : r.stops).map(String) })).filter((r) => r.s.length).sort((a, b) => a.v < b.v ? -1 : a.v > b.v ? 1 : 0);
408
+ const jobs = (plan.jobs || []).map((j) => j.id).sort();
409
+ return key2({ vehicles: plan.vehicles, jobs, routes, region: plan.region ?? null, profile: plan.profile ?? null, departed_at: plan.departed_at ?? null });
410
+ }
411
+ var EMPTY = {
412
+ plan: null,
413
+ result: null,
414
+ busy: false,
415
+ error: null,
416
+ canUndo: false,
417
+ canRedo: false,
418
+ choices: null,
419
+ locked: [],
420
+ last: null,
421
+ preview: null
422
+ };
423
+ function usePlanEditor(props) {
424
+ const map = useSMap();
425
+ const [editor, setEditor] = (0, import_react4.useState)(null);
426
+ const [state, setState] = (0, import_react4.useState)(EMPTY);
427
+ const latest = (0, import_react4.useRef)(props);
428
+ latest.current = props;
429
+ const applied = (0, import_react4.useRef)(null);
430
+ const credentialKey = "key" in props.credentials ? "key:" + props.credentials.key : "token";
431
+ const madeKey = key2({
432
+ baseUrl: props.baseUrl,
433
+ app: props.app,
434
+ region: props.region,
435
+ profile: props.profile,
436
+ snapshot: props.snapshot,
437
+ geometry: props.geometry,
438
+ insertLimit: props.insertLimit,
439
+ objective: props.objective,
440
+ id: props.id,
441
+ colors: props.colors,
442
+ preview: props.preview,
443
+ panel: props.panel,
444
+ labels: props.labels
445
+ });
446
+ (0, import_react4.useEffect)(() => {
447
+ if (!map) {
448
+ return void 0;
449
+ }
450
+ const p = latest.current;
451
+ const credentials = "key" in p.credentials ? { key: p.credentials.key } : { token: () => latest.current.credentials.token() };
452
+ const options = {
453
+ credentials,
454
+ app: p.app,
455
+ baseUrl: p.baseUrl,
456
+ fetch: p.fetch,
457
+ region: p.region,
458
+ profile: p.profile,
459
+ snapshot: p.snapshot,
460
+ geometry: p.geometry,
461
+ insertLimit: p.insertLimit,
462
+ objective: p.objective,
463
+ id: p.id,
464
+ colors: p.colors,
465
+ preview: p.preview,
466
+ language: p.language,
467
+ labels: p.labels,
468
+ panel: p.panel
469
+ };
470
+ for (const k of Object.keys(options)) {
471
+ if (options[k] === void 0) {
472
+ delete options[k];
473
+ }
474
+ }
475
+ const made = map.planEditor(options);
476
+ const refresh = () => setState(made.state());
477
+ made.on("change", (e) => {
478
+ refresh();
479
+ latest.current.onChange?.(e);
480
+ });
481
+ made.on("busy", (e) => {
482
+ refresh();
483
+ latest.current.onBusy?.(e.busy);
484
+ });
485
+ made.on("error", (e) => {
486
+ refresh();
487
+ latest.current.onError?.(e.error, e.kind);
488
+ });
489
+ made.on("preview", refresh);
490
+ made.on("snapshot", (e) => latest.current.onSnapshot?.(e));
491
+ applied.current = null;
492
+ setEditor(made);
493
+ latest.current.onReady?.(made);
494
+ return () => {
495
+ setEditor(null);
496
+ setState(EMPTY);
497
+ if (!removedMaps.has(map)) {
498
+ made.destroy();
499
+ } else {
500
+ destroyOffMap(made);
501
+ }
502
+ };
503
+ }, [map, credentialKey, madeKey]);
504
+ const incoming = planKey(props.plan);
505
+ (0, import_react4.useEffect)(() => {
506
+ if (!editor || !latest.current.plan) {
507
+ return;
508
+ }
509
+ if (incoming === applied.current || incoming === planKey(editor.getPlan())) {
510
+ applied.current = incoming;
511
+ return;
512
+ }
513
+ applied.current = incoming;
514
+ editor.setPlan(latest.current.plan).catch(() => {
515
+ });
516
+ }, [editor, incoming]);
517
+ (0, import_react4.useEffect)(() => {
518
+ if (editor && editor.panel && props.language) {
519
+ editor.panel.setLanguage(props.language);
520
+ }
521
+ }, [editor, props.language]);
522
+ return { ...state, editor };
523
+ }
524
+ var PlanEditor = (0, import_react4.forwardRef)(function PlanEditor2(props, ref) {
525
+ const { editor } = usePlanEditor(props);
526
+ (0, import_react4.useImperativeHandle)(ref, () => editor, [editor]);
527
+ return null;
528
+ });
529
+
530
+ // src/interop.tsx
531
+ var import_react5 = require("react");
380
532
  var sdk = __toESM(require("@superrouteca/smap"), 1);
381
533
  var import_jsx_runtime3 = require("react/jsx-runtime");
382
534
  function quietly(f) {
@@ -385,16 +537,16 @@ function quietly(f) {
385
537
  } catch {
386
538
  }
387
539
  }
388
- var SourceContext = (0, import_react4.createContext)(null);
540
+ var SourceContext = (0, import_react5.createContext)(null);
389
541
  function Source(props) {
390
542
  const map = useSMap();
391
543
  const { id, source, children } = props;
392
- const latest = (0, import_react4.useRef)(source);
544
+ const latest = (0, import_react5.useRef)(source);
393
545
  latest.current = source;
394
- const inside = (0, import_react4.useRef)(/* @__PURE__ */ new Set());
395
- const [shown, setShown] = (0, import_react4.useState)(null);
546
+ const inside = (0, import_react5.useRef)(/* @__PURE__ */ new Set());
547
+ const [shown, setShown] = (0, import_react5.useState)(null);
396
548
  const sourceKey = optionsKey(source);
397
- (0, import_react4.useEffect)(() => {
549
+ (0, import_react5.useEffect)(() => {
398
550
  if (!map) {
399
551
  return void 0;
400
552
  }
@@ -409,7 +561,7 @@ function Source(props) {
409
561
  quietly(() => map.removeSource(id));
410
562
  };
411
563
  }, [map, id, sourceKey]);
412
- const scope = (0, import_react4.useMemo)(() => ({
564
+ const scope = (0, import_react5.useMemo)(() => ({
413
565
  id,
414
566
  register: (layerId) => {
415
567
  inside.current.add(layerId);
@@ -419,7 +571,7 @@ function Source(props) {
419
571
  if (!map || !shown || shown.map !== map) {
420
572
  return null;
421
573
  }
422
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(SourceContext.Provider, { value: scope, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react4.Fragment, { children }, shown.generation) });
574
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(SourceContext.Provider, { value: scope, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react5.Fragment, { children }, shown.generation) });
423
575
  }
424
576
  function effectiveLayer(layer, sourceId, visible) {
425
577
  const out = { ...layer };
@@ -446,14 +598,14 @@ function applyDiff(prev, next, set) {
446
598
  }
447
599
  function StyleLayer(props) {
448
600
  const map = useSMap();
449
- const scope = (0, import_react4.useContext)(SourceContext);
601
+ const scope = (0, import_react5.useContext)(SourceContext);
450
602
  const { layer, before, slot, visible = true } = props;
451
603
  const current = effectiveLayer(layer, scope?.id, visible);
452
- const latest = (0, import_react4.useRef)(current);
604
+ const latest = (0, import_react5.useRef)(current);
453
605
  latest.current = current;
454
- const shown = (0, import_react4.useRef)(null);
606
+ const shown = (0, import_react5.useRef)(null);
455
607
  const structure = structureKey(current, before, slot);
456
- (0, import_react4.useEffect)(() => {
608
+ (0, import_react5.useEffect)(() => {
457
609
  if (!map) {
458
610
  return void 0;
459
611
  }
@@ -477,7 +629,7 @@ function StyleLayer(props) {
477
629
  };
478
630
  }, [map, structure, scope]);
479
631
  const looks = optionsKey(current.paint) + optionsKey(current.layout) + optionsKey(current.filter);
480
- (0, import_react4.useEffect)(() => {
632
+ (0, import_react5.useEffect)(() => {
481
633
  const s = shown.current;
482
634
  if (!map || !s || s.map !== map) {
483
635
  return;
@@ -498,12 +650,12 @@ function StyleLayer(props) {
498
650
  }
499
651
  function useAddedLayer(method, id, options, remove) {
500
652
  const map = useSMap();
501
- const latest = (0, import_react4.useRef)(options);
653
+ const latest = (0, import_react5.useRef)(options);
502
654
  latest.current = options;
503
- const removeLatest = (0, import_react4.useRef)(remove);
655
+ const removeLatest = (0, import_react5.useRef)(remove);
504
656
  removeLatest.current = remove;
505
657
  const looks = optionsKey(options);
506
- (0, import_react4.useEffect)(() => {
658
+ (0, import_react5.useEffect)(() => {
507
659
  if (!map) {
508
660
  return void 0;
509
661
  }
@@ -550,13 +702,13 @@ var dataLayerIds = (id) => [id, `${id}:lines`, `${id}:areas`];
550
702
  function Data(props) {
551
703
  const map = useSMap();
552
704
  const { id, data, format, lat, lng, options, slot, onTap, visible = true, popupOptions } = props;
553
- const latest = (0, import_react4.useRef)(props);
705
+ const latest = (0, import_react5.useRef)(props);
554
706
  latest.current = props;
555
- const shownVisible = (0, import_react4.useRef)(true);
707
+ const shownVisible = (0, import_react5.useRef)(true);
556
708
  const hasTap = !!onTap || typeof options?.onClick === "function";
557
709
  const hasPopup = !!props.popup || typeof options?.popup === "function";
558
710
  const looks = optionsKey(options) + optionsKey(popupOptions) + (slot ?? "") + "|" + (format ?? "") + "|" + (lat ?? "") + "|" + (lng ?? "");
559
- (0, import_react4.useEffect)(() => {
711
+ (0, import_react5.useEffect)(() => {
560
712
  if (!map) {
561
713
  return void 0;
562
714
  }
@@ -599,7 +751,7 @@ function Data(props) {
599
751
  ids.forEach((l) => quietly(() => map.removeLayer(l)));
600
752
  };
601
753
  }, [map, id, data, looks, hasTap, hasPopup]);
602
- (0, import_react4.useEffect)(() => {
754
+ (0, import_react5.useEffect)(() => {
603
755
  if (!map || shownVisible.current === visible) {
604
756
  return;
605
757
  }
@@ -611,10 +763,10 @@ function Data(props) {
611
763
  }
612
764
  function Layers(props) {
613
765
  const map = useSMap();
614
- const latest = (0, import_react4.useRef)(props.json);
766
+ const latest = (0, import_react5.useRef)(props.json);
615
767
  latest.current = props.json;
616
768
  const jsonKey = typeof props.json === "string" ? props.json : optionsKey(props.json);
617
- (0, import_react4.useEffect)(() => {
769
+ (0, import_react5.useEffect)(() => {
618
770
  if (!map) {
619
771
  return void 0;
620
772
  }
@@ -634,10 +786,10 @@ function Layers(props) {
634
786
  function DeckLayer(props) {
635
787
  const map = useSMap();
636
788
  const { layer } = props;
637
- const latest = (0, import_react4.useRef)(layer);
789
+ const latest = (0, import_react5.useRef)(layer);
638
790
  latest.current = layer;
639
- const shown = (0, import_react4.useRef)(null);
640
- (0, import_react4.useEffect)(() => {
791
+ const shown = (0, import_react5.useRef)(null);
792
+ (0, import_react5.useEffect)(() => {
641
793
  if (!map) {
642
794
  return void 0;
643
795
  }
@@ -650,7 +802,7 @@ function DeckLayer(props) {
650
802
  }
651
803
  };
652
804
  }, [map, layer.id]);
653
- (0, import_react4.useEffect)(() => {
805
+ (0, import_react5.useEffect)(() => {
654
806
  const s = shown.current;
655
807
  if (!map || !s || s.map !== map || s.layer === layer) {
656
808
  return;
@@ -664,7 +816,7 @@ function useRasterUrl(style, options) {
664
816
  const map = useSMap();
665
817
  const lang = options?.lang;
666
818
  const retina = options?.retina;
667
- return (0, import_react4.useMemo)(() => {
819
+ return (0, import_react5.useMemo)(() => {
668
820
  if (!map) {
669
821
  return null;
670
822
  }
@@ -697,6 +849,7 @@ var import_smap2 = require("@superrouteca/smap");
697
849
  DeckLayer,
698
850
  Layers,
699
851
  Lines,
852
+ PlanEditor,
700
853
  Points,
701
854
  Polygons,
702
855
  RasterLayer,
@@ -708,7 +861,9 @@ var import_smap2 = require("@superrouteca/smap");
708
861
  WmsLayer,
709
862
  WmtsLayer,
710
863
  formats,
864
+ planTexts,
711
865
  popupTemplate,
866
+ usePlanEditor,
712
867
  useRasterUrl,
713
868
  useSMap
714
869
  });