@biodiv-platform/naksha-maplibre-draw 5.0.8 → 5.0.10

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/esm/index.js CHANGED
@@ -432,7 +432,9 @@ var MapButton = ({
432
432
  style: __spreadProps(__spreadValues({}, BUTTON_STYLE), {
433
433
  backgroundColor: disabled ? "#d3d3d3" : currentMode === mode ? "#02cf87" : "#f0f0f0",
434
434
  cursor: disabled ? "not-allowed" : "pointer",
435
- opacity: disabled ? 0.6 : 1
435
+ opacity: disabled ? 0.6 : 1,
436
+ width: 40,
437
+ height: 40
436
438
  }),
437
439
  children: icon
438
440
  });
@@ -573,7 +575,8 @@ function MapButtons({
573
575
  backgroundColor: "rgba(255,255,255,0.9)",
574
576
  padding: 8,
575
577
  borderRadius: 6,
576
- boxShadow: "0 2px 6px rgba(0,0,0,0.2)"
578
+ boxShadow: "0 2px 6px rgba(0,0,0,0.2)",
579
+ maxWidth: "90vw"
577
580
  },
578
581
  children: [
579
582
  /* @__PURE__ */ jsx4(IconButton, {
@@ -582,8 +585,8 @@ function MapButtons({
582
585
  display: "flex",
583
586
  justifyContent: "center",
584
587
  alignItems: "center",
585
- width: 30,
586
- height: 30,
588
+ width: 40,
589
+ height: 40,
587
590
  padding: 0,
588
591
  borderRadius: 4,
589
592
  backgroundColor: "#d3d3d3"
@@ -592,7 +595,15 @@ function MapButtons({
592
595
  children: expanded ? /* @__PURE__ */ jsx4(PencilCloseIcon, {}) : /* @__PURE__ */ jsx4(PencilOpenIcon, {})
593
596
  }),
594
597
  expanded && /* @__PURE__ */ jsx4("div", {
595
- style: { display: "flex", flexDirection: "column", gap: 6 },
598
+ style: {
599
+ display: "grid",
600
+ gridTemplateColumns: "repeat(auto-fit, minmax(40px, 1fr))",
601
+ gap: 6,
602
+ maxHeight: "200px",
603
+ overflowY: "auto",
604
+ paddingRight: 1
605
+ },
606
+ className: "custom-scrollbar",
596
607
  children: buttons.map((btn, i) => /* @__PURE__ */ jsx4(map_button_default, {
597
608
  icon: btn.icon,
598
609
  mode: btn.mode,
@@ -612,18 +623,21 @@ import bbox from "@turf/bbox";
612
623
  import { defaultMapStyles } from "@biodiv-platform/naksha-commons";
613
624
  import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
614
625
  var Map = (props) => {
615
- var _a;
616
- console.info("Rendering Naksha Maplibre Draw Map Component");
626
+ var _a, _b;
627
+ console.info("draw map");
617
628
  const mapRef = useRef(null);
618
629
  const [map, setMap] = useState2();
619
630
  const [mode, setMode] = useState2("static");
620
631
  const [draw, setDraw] = useState2();
621
- const { isMultiple = true, features = [], onFeaturesChange } = props;
632
+ const drawRef = useRef();
633
+ const { isMultiple, features = [], onFeaturesChange } = props;
622
634
  const [selectedMapStyleIdx, setSelectedMapStyleIdx] = useState2(
623
635
  (props == null ? void 0 : props.mapStyle) || 0
624
636
  );
625
- const mapTile = props.mapStyles ? props.mapStyles : defaultMapStyles;
626
- const mapStyle = (_a = mapTile[selectedMapStyleIdx]) == null ? void 0 : _a.style;
637
+ const mapTile = (_a = props.mapStyles) != null ? _a : defaultMapStyles;
638
+ const mapStyle = (_b = mapTile[selectedMapStyleIdx]) == null ? void 0 : _b.style;
639
+ const mapStyleConfig = mapTile[selectedMapStyleIdx];
640
+ const storedFeaturesRef = useRef([]);
627
641
  const autoFocus = useCallback2(
628
642
  (features2) => {
629
643
  if (!(features2 == null ? void 0 : features2.length) || !map)
@@ -637,12 +651,14 @@ var Map = (props) => {
637
651
  },
638
652
  [map]
639
653
  );
640
- const mapStyleConfig = mapTile[selectedMapStyleIdx];
641
654
  useEffect2(() => {
642
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
655
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j;
656
+ if (features.length > 0) {
657
+ storedFeaturesRef.current = ensureModeProperty(features);
658
+ }
643
659
  const maplibreMap = setupMaplibreMap({
644
660
  containerId: "maplibre-map",
645
- latitude: (_b = (_a2 = props.defaultViewState) == null ? void 0 : _a2.latitude) != null ? _b : 0,
661
+ latitude: (_b2 = (_a2 = props.defaultViewState) == null ? void 0 : _a2.latitude) != null ? _b2 : 0,
646
662
  longitude: (_d = (_c = props.defaultViewState) == null ? void 0 : _c.longitude) != null ? _d : 0,
647
663
  zoom: (_f = (_e = props.defaultViewState) == null ? void 0 : _e.zoom) != null ? _f : 0,
648
664
  pitch: (_h = (_g = props.defaultViewState) == null ? void 0 : _g.pitch) != null ? _h : 0,
@@ -660,11 +676,28 @@ var Map = (props) => {
660
676
  const fullscreen = new maplibregl2.FullscreenControl({});
661
677
  maplibreMap.addControl(navControl, "bottom-right");
662
678
  maplibreMap.addControl(fullscreen, "bottom-right");
663
- maplibreMap.once("style.load", () => setMap(maplibreMap));
679
+ maplibreMap.once("style.load", () => {
680
+ setMap(maplibreMap);
681
+ });
664
682
  return () => {
665
- maplibreMap.removeControl(navControl);
683
+ if (drawRef.current) {
684
+ storedFeaturesRef.current = drawRef.current.getSnapshot();
685
+ }
686
+ if (drawRef.current) {
687
+ try {
688
+ drawRef.current.stop();
689
+ } catch (e) {
690
+ }
691
+ drawRef.current = void 0;
692
+ setDraw(void 0);
693
+ }
694
+ if (maplibreMap) {
695
+ maplibreMap.removeControl(navControl);
696
+ maplibreMap.removeControl(fullscreen);
697
+ maplibreMap.remove();
698
+ }
666
699
  };
667
- }, [mapStyle, props.defaultViewState]);
700
+ }, [mapStyle]);
668
701
  const filterUserFeatures = (features2) => features2.filter(
669
702
  (f) => f.geometry.type !== "Point" || f.properties.mode !== "static" && f.properties.mode !== "select"
670
703
  );
@@ -673,6 +706,7 @@ var Map = (props) => {
673
706
  return;
674
707
  const terraDraw = setupDraw(map);
675
708
  terraDraw.start();
709
+ drawRef.current = terraDraw;
676
710
  setDraw(terraDraw);
677
711
  const handleChange = () => {
678
712
  let snapshot = terraDraw.getSnapshot();
@@ -684,31 +718,35 @@ var Map = (props) => {
684
718
  try {
685
719
  terraDraw.removeFeatures(toRemove);
686
720
  } catch (e) {
687
- console.warn("removeFeatures failed", e);
688
721
  }
689
722
  }
690
723
  userFeatures = [lastFeature];
691
724
  }
692
725
  onFeaturesChange == null ? void 0 : onFeaturesChange(userFeatures);
693
726
  autoFocus(userFeatures);
694
- setMode("static");
695
727
  };
696
728
  terraDraw.on("finish", handleChange);
697
- if (features.length) {
698
- setTimeout(() => {
699
- if (terraDraw.getSnapshot().length === 0) {
700
- let withMode = ensureModeProperty(features);
701
- let initial = !isMultiple && withMode.length > 1 ? [withMode.at(-1)] : withMode;
702
- terraDraw.addFeatures(initial);
703
- autoFocus(initial);
729
+ setTimeout(() => {
730
+ if (terraDraw.getSnapshot().length === 0) {
731
+ let featuresToAdd = [];
732
+ if (storedFeaturesRef.current.length > 0) {
733
+ featuresToAdd = storedFeaturesRef.current;
734
+ } else if (features.length > 0) {
735
+ featuresToAdd = ensureModeProperty(features);
704
736
  }
705
- });
706
- }
737
+ if (!isMultiple && featuresToAdd.length > 1) {
738
+ featuresToAdd = [featuresToAdd.at(-1)];
739
+ }
740
+ if (featuresToAdd.length > 0) {
741
+ terraDraw.addFeatures(featuresToAdd);
742
+ autoFocus(featuresToAdd);
743
+ }
744
+ }
745
+ }, 100);
707
746
  return () => {
708
747
  terraDraw.off("finish", handleChange);
709
- terraDraw.stop();
710
748
  };
711
- }, [map, isMultiple, autoFocus, features, onFeaturesChange]);
749
+ }, [map]);
712
750
  const changeMode = useCallback2(
713
751
  (newMode) => {
714
752
  draw == null ? void 0 : draw.setMode(newMode);
@@ -717,7 +755,11 @@ var Map = (props) => {
717
755
  [draw]
718
756
  );
719
757
  const handleStyleChange = (idx) => {
758
+ if (drawRef.current) {
759
+ storedFeaturesRef.current = drawRef.current.getSnapshot();
760
+ }
720
761
  setSelectedMapStyleIdx(idx);
762
+ setMode("static");
721
763
  };
722
764
  return /* @__PURE__ */ jsxs3("div", {
723
765
  ref: mapRef,
package/dist/index.js CHANGED
@@ -451,7 +451,9 @@ var MapButton = ({
451
451
  style: __spreadProps(__spreadValues({}, BUTTON_STYLE), {
452
452
  backgroundColor: disabled ? "#d3d3d3" : currentMode === mode ? "#02cf87" : "#f0f0f0",
453
453
  cursor: disabled ? "not-allowed" : "pointer",
454
- opacity: disabled ? 0.6 : 1
454
+ opacity: disabled ? 0.6 : 1,
455
+ width: 40,
456
+ height: 40
455
457
  }),
456
458
  children: icon
457
459
  });
@@ -592,7 +594,8 @@ function MapButtons({
592
594
  backgroundColor: "rgba(255,255,255,0.9)",
593
595
  padding: 8,
594
596
  borderRadius: 6,
595
- boxShadow: "0 2px 6px rgba(0,0,0,0.2)"
597
+ boxShadow: "0 2px 6px rgba(0,0,0,0.2)",
598
+ maxWidth: "90vw"
596
599
  },
597
600
  children: [
598
601
  /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(IconButton, {
@@ -601,8 +604,8 @@ function MapButtons({
601
604
  display: "flex",
602
605
  justifyContent: "center",
603
606
  alignItems: "center",
604
- width: 30,
605
- height: 30,
607
+ width: 40,
608
+ height: 40,
606
609
  padding: 0,
607
610
  borderRadius: 4,
608
611
  backgroundColor: "#d3d3d3"
@@ -611,7 +614,15 @@ function MapButtons({
611
614
  children: expanded ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(PencilCloseIcon, {}) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(PencilOpenIcon, {})
612
615
  }),
613
616
  expanded && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", {
614
- style: { display: "flex", flexDirection: "column", gap: 6 },
617
+ style: {
618
+ display: "grid",
619
+ gridTemplateColumns: "repeat(auto-fit, minmax(40px, 1fr))",
620
+ gap: 6,
621
+ maxHeight: "200px",
622
+ overflowY: "auto",
623
+ paddingRight: 1
624
+ },
625
+ className: "custom-scrollbar",
615
626
  children: buttons.map((btn, i) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(map_button_default, {
616
627
  icon: btn.icon,
617
628
  mode: btn.mode,
@@ -631,18 +642,21 @@ var import_bbox = __toESM(require("@turf/bbox"));
631
642
  var import_naksha_commons = require("@biodiv-platform/naksha-commons");
632
643
  var import_jsx_runtime5 = require("react/jsx-runtime");
633
644
  var Map = (props) => {
634
- var _a;
635
- console.info("Rendering Naksha Maplibre Draw Map Component");
645
+ var _a, _b;
646
+ console.info("draw map");
636
647
  const mapRef = (0, import_react3.useRef)(null);
637
648
  const [map, setMap] = (0, import_react3.useState)();
638
649
  const [mode, setMode] = (0, import_react3.useState)("static");
639
650
  const [draw, setDraw] = (0, import_react3.useState)();
640
- const { isMultiple = true, features = [], onFeaturesChange } = props;
651
+ const drawRef = (0, import_react3.useRef)();
652
+ const { isMultiple, features = [], onFeaturesChange } = props;
641
653
  const [selectedMapStyleIdx, setSelectedMapStyleIdx] = (0, import_react3.useState)(
642
654
  (props == null ? void 0 : props.mapStyle) || 0
643
655
  );
644
- const mapTile = props.mapStyles ? props.mapStyles : import_naksha_commons.defaultMapStyles;
645
- const mapStyle = (_a = mapTile[selectedMapStyleIdx]) == null ? void 0 : _a.style;
656
+ const mapTile = (_a = props.mapStyles) != null ? _a : import_naksha_commons.defaultMapStyles;
657
+ const mapStyle = (_b = mapTile[selectedMapStyleIdx]) == null ? void 0 : _b.style;
658
+ const mapStyleConfig = mapTile[selectedMapStyleIdx];
659
+ const storedFeaturesRef = (0, import_react3.useRef)([]);
646
660
  const autoFocus = (0, import_react3.useCallback)(
647
661
  (features2) => {
648
662
  if (!(features2 == null ? void 0 : features2.length) || !map)
@@ -656,12 +670,14 @@ var Map = (props) => {
656
670
  },
657
671
  [map]
658
672
  );
659
- const mapStyleConfig = mapTile[selectedMapStyleIdx];
660
673
  (0, import_react3.useEffect)(() => {
661
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
674
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j;
675
+ if (features.length > 0) {
676
+ storedFeaturesRef.current = ensureModeProperty(features);
677
+ }
662
678
  const maplibreMap = setupMaplibreMap({
663
679
  containerId: "maplibre-map",
664
- latitude: (_b = (_a2 = props.defaultViewState) == null ? void 0 : _a2.latitude) != null ? _b : 0,
680
+ latitude: (_b2 = (_a2 = props.defaultViewState) == null ? void 0 : _a2.latitude) != null ? _b2 : 0,
665
681
  longitude: (_d = (_c = props.defaultViewState) == null ? void 0 : _c.longitude) != null ? _d : 0,
666
682
  zoom: (_f = (_e = props.defaultViewState) == null ? void 0 : _e.zoom) != null ? _f : 0,
667
683
  pitch: (_h = (_g = props.defaultViewState) == null ? void 0 : _g.pitch) != null ? _h : 0,
@@ -679,11 +695,28 @@ var Map = (props) => {
679
695
  const fullscreen = new import_maplibre_gl2.default.FullscreenControl({});
680
696
  maplibreMap.addControl(navControl, "bottom-right");
681
697
  maplibreMap.addControl(fullscreen, "bottom-right");
682
- maplibreMap.once("style.load", () => setMap(maplibreMap));
698
+ maplibreMap.once("style.load", () => {
699
+ setMap(maplibreMap);
700
+ });
683
701
  return () => {
684
- maplibreMap.removeControl(navControl);
702
+ if (drawRef.current) {
703
+ storedFeaturesRef.current = drawRef.current.getSnapshot();
704
+ }
705
+ if (drawRef.current) {
706
+ try {
707
+ drawRef.current.stop();
708
+ } catch (e) {
709
+ }
710
+ drawRef.current = void 0;
711
+ setDraw(void 0);
712
+ }
713
+ if (maplibreMap) {
714
+ maplibreMap.removeControl(navControl);
715
+ maplibreMap.removeControl(fullscreen);
716
+ maplibreMap.remove();
717
+ }
685
718
  };
686
- }, [mapStyle, props.defaultViewState]);
719
+ }, [mapStyle]);
687
720
  const filterUserFeatures = (features2) => features2.filter(
688
721
  (f) => f.geometry.type !== "Point" || f.properties.mode !== "static" && f.properties.mode !== "select"
689
722
  );
@@ -692,6 +725,7 @@ var Map = (props) => {
692
725
  return;
693
726
  const terraDraw = setupDraw(map);
694
727
  terraDraw.start();
728
+ drawRef.current = terraDraw;
695
729
  setDraw(terraDraw);
696
730
  const handleChange = () => {
697
731
  let snapshot = terraDraw.getSnapshot();
@@ -703,31 +737,35 @@ var Map = (props) => {
703
737
  try {
704
738
  terraDraw.removeFeatures(toRemove);
705
739
  } catch (e) {
706
- console.warn("removeFeatures failed", e);
707
740
  }
708
741
  }
709
742
  userFeatures = [lastFeature];
710
743
  }
711
744
  onFeaturesChange == null ? void 0 : onFeaturesChange(userFeatures);
712
745
  autoFocus(userFeatures);
713
- setMode("static");
714
746
  };
715
747
  terraDraw.on("finish", handleChange);
716
- if (features.length) {
717
- setTimeout(() => {
718
- if (terraDraw.getSnapshot().length === 0) {
719
- let withMode = ensureModeProperty(features);
720
- let initial = !isMultiple && withMode.length > 1 ? [withMode.at(-1)] : withMode;
721
- terraDraw.addFeatures(initial);
722
- autoFocus(initial);
748
+ setTimeout(() => {
749
+ if (terraDraw.getSnapshot().length === 0) {
750
+ let featuresToAdd = [];
751
+ if (storedFeaturesRef.current.length > 0) {
752
+ featuresToAdd = storedFeaturesRef.current;
753
+ } else if (features.length > 0) {
754
+ featuresToAdd = ensureModeProperty(features);
723
755
  }
724
- });
725
- }
756
+ if (!isMultiple && featuresToAdd.length > 1) {
757
+ featuresToAdd = [featuresToAdd.at(-1)];
758
+ }
759
+ if (featuresToAdd.length > 0) {
760
+ terraDraw.addFeatures(featuresToAdd);
761
+ autoFocus(featuresToAdd);
762
+ }
763
+ }
764
+ }, 100);
726
765
  return () => {
727
766
  terraDraw.off("finish", handleChange);
728
- terraDraw.stop();
729
767
  };
730
- }, [map, isMultiple, autoFocus, features, onFeaturesChange]);
768
+ }, [map]);
731
769
  const changeMode = (0, import_react3.useCallback)(
732
770
  (newMode) => {
733
771
  draw == null ? void 0 : draw.setMode(newMode);
@@ -736,7 +774,11 @@ var Map = (props) => {
736
774
  [draw]
737
775
  );
738
776
  const handleStyleChange = (idx) => {
777
+ if (drawRef.current) {
778
+ storedFeaturesRef.current = drawRef.current.getSnapshot();
779
+ }
739
780
  setSelectedMapStyleIdx(idx);
781
+ setMode("static");
740
782
  };
741
783
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", {
742
784
  ref: mapRef,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biodiv-platform/naksha-maplibre-draw",
3
- "version": "5.0.8",
3
+ "version": "5.0.10",
4
4
  "author": "arun",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/esm/index.js",