@superrouteca/smap-react 1.1.2 → 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/dist/index.js CHANGED
@@ -68,7 +68,8 @@ var SMap = forwardRef(function SMap2(props, ref) {
68
68
  nearest: p.nearest,
69
69
  projection: p.projection,
70
70
  popup: p.popup,
71
- popupLoading: p.popupLoading
71
+ popupLoading: p.popupLoading,
72
+ tools: p.tools
72
73
  };
73
74
  for (const k of Object.keys(options)) {
74
75
  if (options[k] === void 0) {
@@ -92,6 +93,7 @@ var SMap = forwardRef(function SMap2(props, ref) {
92
93
  buildings: false,
93
94
  basemap: key(void 0)
94
95
  };
96
+ created.onToolsChange((e) => latest.current.onToolsChange?.(e));
95
97
  created.ready.then(() => {
96
98
  ready = true;
97
99
  if (!alive) {
@@ -279,10 +281,208 @@ function Polygons(props) {
279
281
  return useLayer("addPolygons", props);
280
282
  }
281
283
 
284
+ // src/tools.tsx
285
+ import { useEffect as useEffect3, useRef as useRef3, useState as useState2 } from "react";
286
+ import { createPortal } from "react-dom";
287
+ import { jsx as jsx2 } from "react/jsx-runtime";
288
+ var optionsKey2 = (v) => JSON.stringify(v ?? null);
289
+ function Tool(props) {
290
+ const map = useSMap();
291
+ const { id, label, kind } = props;
292
+ const layer = props.layer;
293
+ const layerKey = layer === void 0 ? null : JSON.stringify(layer);
294
+ const isLayer = layerKey !== null;
295
+ const position = isLayer ? void 0 : props.position ?? (kind ? void 0 : "top-left");
296
+ const inPlace = !kind && !isLayer && position === "none";
297
+ const [box, setBox] = useState2(null);
298
+ useEffect3(() => {
299
+ if (!kind && !isLayer && !inPlace && !box) {
300
+ setBox(document.createElement("div"));
301
+ }
302
+ }, [kind, isLayer, inPlace, box]);
303
+ const inPlaceRef = useRef3(null);
304
+ const kindOptions = optionsKey2(props.options);
305
+ const latestOptions = useRef3(props.options);
306
+ latestOptions.current = props.options;
307
+ if (box) {
308
+ box.className = props.className ?? "";
309
+ box.removeAttribute("style");
310
+ Object.assign(box.style, props.style || {});
311
+ }
312
+ useEffect3(() => {
313
+ if (!map) {
314
+ return void 0;
315
+ }
316
+ if (layerKey !== null) {
317
+ map.addTool(id, { layer: JSON.parse(layerKey) }, { label });
318
+ return () => {
319
+ if (!removedMaps.has(map)) {
320
+ map.removeTool(id);
321
+ }
322
+ };
323
+ }
324
+ const target = kind ?? (inPlace ? inPlaceRef.current : box);
325
+ if (!target) {
326
+ return void 0;
327
+ }
328
+ map.addTool(id, target, { label, position, options: latestOptions.current });
329
+ return () => {
330
+ if (!removedMaps.has(map)) {
331
+ map.removeTool(id);
332
+ }
333
+ };
334
+ }, [map, id, label, position, kind, inPlace, kindOptions, box, layerKey]);
335
+ if (kind || isLayer) {
336
+ return null;
337
+ }
338
+ if (inPlace) {
339
+ return /* @__PURE__ */ jsx2("div", { ref: inPlaceRef, className: props.className, style: props.style, children: props.children });
340
+ }
341
+ return box ? createPortal(props.children, box) : null;
342
+ }
343
+
344
+ // src/plan.tsx
345
+ import { forwardRef as forwardRef2, useEffect as useEffect4, useImperativeHandle as useImperativeHandle2, useRef as useRef4, useState as useState3 } from "react";
346
+ function destroyOffMap(made) {
347
+ made.layer.remove = () => {
348
+ };
349
+ try {
350
+ made.destroy();
351
+ } catch {
352
+ }
353
+ }
354
+ var key2 = (v) => JSON.stringify(v ?? null);
355
+ function planKey(plan) {
356
+ if (!plan) {
357
+ return key2(null);
358
+ }
359
+ 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);
360
+ const jobs = (plan.jobs || []).map((j) => j.id).sort();
361
+ return key2({ vehicles: plan.vehicles, jobs, routes, region: plan.region ?? null, profile: plan.profile ?? null, departed_at: plan.departed_at ?? null });
362
+ }
363
+ var EMPTY = {
364
+ plan: null,
365
+ result: null,
366
+ busy: false,
367
+ error: null,
368
+ canUndo: false,
369
+ canRedo: false,
370
+ choices: null,
371
+ locked: [],
372
+ last: null,
373
+ preview: null
374
+ };
375
+ function usePlanEditor(props) {
376
+ const map = useSMap();
377
+ const [editor, setEditor] = useState3(null);
378
+ const [state, setState] = useState3(EMPTY);
379
+ const latest = useRef4(props);
380
+ latest.current = props;
381
+ const applied = useRef4(null);
382
+ const credentialKey = "key" in props.credentials ? "key:" + props.credentials.key : "token";
383
+ const madeKey = key2({
384
+ baseUrl: props.baseUrl,
385
+ app: props.app,
386
+ region: props.region,
387
+ profile: props.profile,
388
+ snapshot: props.snapshot,
389
+ geometry: props.geometry,
390
+ insertLimit: props.insertLimit,
391
+ objective: props.objective,
392
+ id: props.id,
393
+ colors: props.colors,
394
+ preview: props.preview,
395
+ panel: props.panel,
396
+ labels: props.labels
397
+ });
398
+ useEffect4(() => {
399
+ if (!map) {
400
+ return void 0;
401
+ }
402
+ const p = latest.current;
403
+ const credentials = "key" in p.credentials ? { key: p.credentials.key } : { token: () => latest.current.credentials.token() };
404
+ const options = {
405
+ credentials,
406
+ app: p.app,
407
+ baseUrl: p.baseUrl,
408
+ fetch: p.fetch,
409
+ region: p.region,
410
+ profile: p.profile,
411
+ snapshot: p.snapshot,
412
+ geometry: p.geometry,
413
+ insertLimit: p.insertLimit,
414
+ objective: p.objective,
415
+ id: p.id,
416
+ colors: p.colors,
417
+ preview: p.preview,
418
+ language: p.language,
419
+ labels: p.labels,
420
+ panel: p.panel
421
+ };
422
+ for (const k of Object.keys(options)) {
423
+ if (options[k] === void 0) {
424
+ delete options[k];
425
+ }
426
+ }
427
+ const made = map.planEditor(options);
428
+ const refresh = () => setState(made.state());
429
+ made.on("change", (e) => {
430
+ refresh();
431
+ latest.current.onChange?.(e);
432
+ });
433
+ made.on("busy", (e) => {
434
+ refresh();
435
+ latest.current.onBusy?.(e.busy);
436
+ });
437
+ made.on("error", (e) => {
438
+ refresh();
439
+ latest.current.onError?.(e.error, e.kind);
440
+ });
441
+ made.on("preview", refresh);
442
+ made.on("snapshot", (e) => latest.current.onSnapshot?.(e));
443
+ applied.current = null;
444
+ setEditor(made);
445
+ latest.current.onReady?.(made);
446
+ return () => {
447
+ setEditor(null);
448
+ setState(EMPTY);
449
+ if (!removedMaps.has(map)) {
450
+ made.destroy();
451
+ } else {
452
+ destroyOffMap(made);
453
+ }
454
+ };
455
+ }, [map, credentialKey, madeKey]);
456
+ const incoming = planKey(props.plan);
457
+ useEffect4(() => {
458
+ if (!editor || !latest.current.plan) {
459
+ return;
460
+ }
461
+ if (incoming === applied.current || incoming === planKey(editor.getPlan())) {
462
+ applied.current = incoming;
463
+ return;
464
+ }
465
+ applied.current = incoming;
466
+ editor.setPlan(latest.current.plan).catch(() => {
467
+ });
468
+ }, [editor, incoming]);
469
+ useEffect4(() => {
470
+ if (editor && editor.panel && props.language) {
471
+ editor.panel.setLanguage(props.language);
472
+ }
473
+ }, [editor, props.language]);
474
+ return { ...state, editor };
475
+ }
476
+ var PlanEditor = forwardRef2(function PlanEditor2(props, ref) {
477
+ const { editor } = usePlanEditor(props);
478
+ useImperativeHandle2(ref, () => editor, [editor]);
479
+ return null;
480
+ });
481
+
282
482
  // src/interop.tsx
283
- import { createContext as createContext2, Fragment, useContext as useContext2, useEffect as useEffect3, useMemo, useRef as useRef3, useState as useState2 } from "react";
483
+ import { createContext as createContext2, Fragment, useContext as useContext2, useEffect as useEffect5, useMemo, useRef as useRef5, useState as useState4 } from "react";
284
484
  import * as sdk from "@superrouteca/smap";
285
- import { jsx as jsx2 } from "react/jsx-runtime";
485
+ import { jsx as jsx3 } from "react/jsx-runtime";
286
486
  function quietly(f) {
287
487
  try {
288
488
  f();
@@ -293,12 +493,12 @@ var SourceContext = createContext2(null);
293
493
  function Source(props) {
294
494
  const map = useSMap();
295
495
  const { id, source, children } = props;
296
- const latest = useRef3(source);
496
+ const latest = useRef5(source);
297
497
  latest.current = source;
298
- const inside = useRef3(/* @__PURE__ */ new Set());
299
- const [shown, setShown] = useState2(null);
498
+ const inside = useRef5(/* @__PURE__ */ new Set());
499
+ const [shown, setShown] = useState4(null);
300
500
  const sourceKey = optionsKey(source);
301
- useEffect3(() => {
501
+ useEffect5(() => {
302
502
  if (!map) {
303
503
  return void 0;
304
504
  }
@@ -323,7 +523,7 @@ function Source(props) {
323
523
  if (!map || !shown || shown.map !== map) {
324
524
  return null;
325
525
  }
326
- return /* @__PURE__ */ jsx2(SourceContext.Provider, { value: scope, children: /* @__PURE__ */ jsx2(Fragment, { children }, shown.generation) });
526
+ return /* @__PURE__ */ jsx3(SourceContext.Provider, { value: scope, children: /* @__PURE__ */ jsx3(Fragment, { children }, shown.generation) });
327
527
  }
328
528
  function effectiveLayer(layer, sourceId, visible) {
329
529
  const out = { ...layer };
@@ -353,11 +553,11 @@ function StyleLayer(props) {
353
553
  const scope = useContext2(SourceContext);
354
554
  const { layer, before, slot, visible = true } = props;
355
555
  const current = effectiveLayer(layer, scope?.id, visible);
356
- const latest = useRef3(current);
556
+ const latest = useRef5(current);
357
557
  latest.current = current;
358
- const shown = useRef3(null);
558
+ const shown = useRef5(null);
359
559
  const structure = structureKey(current, before, slot);
360
- useEffect3(() => {
560
+ useEffect5(() => {
361
561
  if (!map) {
362
562
  return void 0;
363
563
  }
@@ -381,7 +581,7 @@ function StyleLayer(props) {
381
581
  };
382
582
  }, [map, structure, scope]);
383
583
  const looks = optionsKey(current.paint) + optionsKey(current.layout) + optionsKey(current.filter);
384
- useEffect3(() => {
584
+ useEffect5(() => {
385
585
  const s = shown.current;
386
586
  if (!map || !s || s.map !== map) {
387
587
  return;
@@ -402,12 +602,12 @@ function StyleLayer(props) {
402
602
  }
403
603
  function useAddedLayer(method, id, options, remove) {
404
604
  const map = useSMap();
405
- const latest = useRef3(options);
605
+ const latest = useRef5(options);
406
606
  latest.current = options;
407
- const removeLatest = useRef3(remove);
607
+ const removeLatest = useRef5(remove);
408
608
  removeLatest.current = remove;
409
609
  const looks = optionsKey(options);
410
- useEffect3(() => {
610
+ useEffect5(() => {
411
611
  if (!map) {
412
612
  return void 0;
413
613
  }
@@ -454,13 +654,13 @@ var dataLayerIds = (id) => [id, `${id}:lines`, `${id}:areas`];
454
654
  function Data(props) {
455
655
  const map = useSMap();
456
656
  const { id, data, format, lat, lng, options, slot, onTap, visible = true, popupOptions } = props;
457
- const latest = useRef3(props);
657
+ const latest = useRef5(props);
458
658
  latest.current = props;
459
- const shownVisible = useRef3(true);
659
+ const shownVisible = useRef5(true);
460
660
  const hasTap = !!onTap || typeof options?.onClick === "function";
461
661
  const hasPopup = !!props.popup || typeof options?.popup === "function";
462
662
  const looks = optionsKey(options) + optionsKey(popupOptions) + (slot ?? "") + "|" + (format ?? "") + "|" + (lat ?? "") + "|" + (lng ?? "");
463
- useEffect3(() => {
663
+ useEffect5(() => {
464
664
  if (!map) {
465
665
  return void 0;
466
666
  }
@@ -503,7 +703,7 @@ function Data(props) {
503
703
  ids.forEach((l) => quietly(() => map.removeLayer(l)));
504
704
  };
505
705
  }, [map, id, data, looks, hasTap, hasPopup]);
506
- useEffect3(() => {
706
+ useEffect5(() => {
507
707
  if (!map || shownVisible.current === visible) {
508
708
  return;
509
709
  }
@@ -515,10 +715,10 @@ function Data(props) {
515
715
  }
516
716
  function Layers(props) {
517
717
  const map = useSMap();
518
- const latest = useRef3(props.json);
718
+ const latest = useRef5(props.json);
519
719
  latest.current = props.json;
520
720
  const jsonKey = typeof props.json === "string" ? props.json : optionsKey(props.json);
521
- useEffect3(() => {
721
+ useEffect5(() => {
522
722
  if (!map) {
523
723
  return void 0;
524
724
  }
@@ -538,10 +738,10 @@ function Layers(props) {
538
738
  function DeckLayer(props) {
539
739
  const map = useSMap();
540
740
  const { layer } = props;
541
- const latest = useRef3(layer);
741
+ const latest = useRef5(layer);
542
742
  latest.current = layer;
543
- const shown = useRef3(null);
544
- useEffect3(() => {
743
+ const shown = useRef5(null);
744
+ useEffect5(() => {
545
745
  if (!map) {
546
746
  return void 0;
547
747
  }
@@ -554,7 +754,7 @@ function DeckLayer(props) {
554
754
  }
555
755
  };
556
756
  }, [map, layer.id]);
557
- useEffect3(() => {
757
+ useEffect5(() => {
558
758
  const s = shown.current;
559
759
  if (!map || !s || s.map !== map || s.layer === layer) {
560
760
  return;
@@ -593,24 +793,28 @@ var formats2 = {
593
793
  };
594
794
 
595
795
  // src/index.ts
596
- import { popupTemplate } from "@superrouteca/smap";
796
+ import { popupTemplate, planTexts } from "@superrouteca/smap";
597
797
  export {
598
798
  ArcGisLayer,
599
799
  Data,
600
800
  DeckLayer,
601
801
  Layers,
602
802
  Lines,
803
+ PlanEditor,
603
804
  Points,
604
805
  Polygons,
605
806
  RasterLayer,
606
807
  SMap,
607
808
  Source,
608
809
  StyleLayer,
810
+ Tool,
609
811
  VectorTiles,
610
812
  WmsLayer,
611
813
  WmtsLayer,
612
814
  formats2 as formats,
815
+ planTexts,
613
816
  popupTemplate,
817
+ usePlanEditor,
614
818
  useRasterUrl,
615
819
  useSMap
616
820
  };