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

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
@@ -613,17 +613,19 @@ import { defaultMapStyles } from "@biodiv-platform/naksha-commons";
613
613
  import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
614
614
  var Map = (props) => {
615
615
  var _a;
616
- console.info("Rendering Naksha Maplibre Draw Map Component");
617
616
  const mapRef = useRef(null);
618
617
  const [map, setMap] = useState2();
619
618
  const [mode, setMode] = useState2("static");
620
619
  const [draw, setDraw] = useState2();
621
- const { isMultiple = true, features = [], onFeaturesChange } = props;
620
+ const drawRef = useRef();
621
+ const { isMultiple, features = [], onFeaturesChange } = props;
622
622
  const [selectedMapStyleIdx, setSelectedMapStyleIdx] = useState2(
623
623
  (props == null ? void 0 : props.mapStyle) || 0
624
624
  );
625
625
  const mapTile = props.mapStyles ? props.mapStyles : defaultMapStyles;
626
626
  const mapStyle = (_a = mapTile[selectedMapStyleIdx]) == null ? void 0 : _a.style;
627
+ const mapStyleConfig = mapTile[selectedMapStyleIdx];
628
+ const storedFeaturesRef = useRef([]);
627
629
  const autoFocus = useCallback2(
628
630
  (features2) => {
629
631
  if (!(features2 == null ? void 0 : features2.length) || !map)
@@ -637,9 +639,11 @@ var Map = (props) => {
637
639
  },
638
640
  [map]
639
641
  );
640
- const mapStyleConfig = mapTile[selectedMapStyleIdx];
641
642
  useEffect2(() => {
642
643
  var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
644
+ if (features && features.length > 0) {
645
+ storedFeaturesRef.current = ensureModeProperty(features);
646
+ }
643
647
  const maplibreMap = setupMaplibreMap({
644
648
  containerId: "maplibre-map",
645
649
  latitude: (_b = (_a2 = props.defaultViewState) == null ? void 0 : _a2.latitude) != null ? _b : 0,
@@ -660,11 +664,29 @@ var Map = (props) => {
660
664
  const fullscreen = new maplibregl2.FullscreenControl({});
661
665
  maplibreMap.addControl(navControl, "bottom-right");
662
666
  maplibreMap.addControl(fullscreen, "bottom-right");
663
- maplibreMap.once("style.load", () => setMap(maplibreMap));
667
+ maplibreMap.once("style.load", () => {
668
+ setMap(maplibreMap);
669
+ });
664
670
  return () => {
665
- maplibreMap.removeControl(navControl);
671
+ if (drawRef.current) {
672
+ storedFeaturesRef.current = drawRef.current.getSnapshot();
673
+ }
674
+ if (drawRef.current) {
675
+ try {
676
+ drawRef.current.stop();
677
+ } catch (e) {
678
+ console.warn("terraDraw stop failed", e);
679
+ }
680
+ drawRef.current = void 0;
681
+ setDraw(void 0);
682
+ }
683
+ if (maplibreMap) {
684
+ maplibreMap.removeControl(navControl);
685
+ maplibreMap.removeControl(fullscreen);
686
+ maplibreMap.remove();
687
+ }
666
688
  };
667
- }, [mapStyle, props.defaultViewState]);
689
+ }, [mapStyle]);
668
690
  const filterUserFeatures = (features2) => features2.filter(
669
691
  (f) => f.geometry.type !== "Point" || f.properties.mode !== "static" && f.properties.mode !== "select"
670
692
  );
@@ -673,6 +695,7 @@ var Map = (props) => {
673
695
  return;
674
696
  const terraDraw = setupDraw(map);
675
697
  terraDraw.start();
698
+ drawRef.current = terraDraw;
676
699
  setDraw(terraDraw);
677
700
  const handleChange = () => {
678
701
  let snapshot = terraDraw.getSnapshot();
@@ -691,24 +714,29 @@ var Map = (props) => {
691
714
  }
692
715
  onFeaturesChange == null ? void 0 : onFeaturesChange(userFeatures);
693
716
  autoFocus(userFeatures);
694
- setMode("static");
695
717
  };
696
718
  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);
719
+ setTimeout(() => {
720
+ if (terraDraw.getSnapshot().length === 0) {
721
+ let featuresToAdd = [];
722
+ if (storedFeaturesRef.current.length > 0) {
723
+ featuresToAdd = storedFeaturesRef.current;
724
+ } else if (features && features.length > 0) {
725
+ featuresToAdd = ensureModeProperty(features);
704
726
  }
705
- });
706
- }
727
+ if (!isMultiple && featuresToAdd.length > 1) {
728
+ featuresToAdd = [featuresToAdd.at(-1)];
729
+ }
730
+ if (featuresToAdd.length > 0) {
731
+ terraDraw.addFeatures(featuresToAdd);
732
+ autoFocus(featuresToAdd);
733
+ }
734
+ }
735
+ }, 100);
707
736
  return () => {
708
737
  terraDraw.off("finish", handleChange);
709
- terraDraw.stop();
710
738
  };
711
- }, [map, isMultiple, autoFocus, features, onFeaturesChange]);
739
+ }, [map]);
712
740
  const changeMode = useCallback2(
713
741
  (newMode) => {
714
742
  draw == null ? void 0 : draw.setMode(newMode);
@@ -717,7 +745,11 @@ var Map = (props) => {
717
745
  [draw]
718
746
  );
719
747
  const handleStyleChange = (idx) => {
748
+ if (drawRef.current) {
749
+ storedFeaturesRef.current = drawRef.current.getSnapshot();
750
+ }
720
751
  setSelectedMapStyleIdx(idx);
752
+ setMode("static");
721
753
  };
722
754
  return /* @__PURE__ */ jsxs3("div", {
723
755
  ref: mapRef,
package/dist/index.js CHANGED
@@ -632,17 +632,19 @@ var import_naksha_commons = require("@biodiv-platform/naksha-commons");
632
632
  var import_jsx_runtime5 = require("react/jsx-runtime");
633
633
  var Map = (props) => {
634
634
  var _a;
635
- console.info("Rendering Naksha Maplibre Draw Map Component");
636
635
  const mapRef = (0, import_react3.useRef)(null);
637
636
  const [map, setMap] = (0, import_react3.useState)();
638
637
  const [mode, setMode] = (0, import_react3.useState)("static");
639
638
  const [draw, setDraw] = (0, import_react3.useState)();
640
- const { isMultiple = true, features = [], onFeaturesChange } = props;
639
+ const drawRef = (0, import_react3.useRef)();
640
+ const { isMultiple, features = [], onFeaturesChange } = props;
641
641
  const [selectedMapStyleIdx, setSelectedMapStyleIdx] = (0, import_react3.useState)(
642
642
  (props == null ? void 0 : props.mapStyle) || 0
643
643
  );
644
644
  const mapTile = props.mapStyles ? props.mapStyles : import_naksha_commons.defaultMapStyles;
645
645
  const mapStyle = (_a = mapTile[selectedMapStyleIdx]) == null ? void 0 : _a.style;
646
+ const mapStyleConfig = mapTile[selectedMapStyleIdx];
647
+ const storedFeaturesRef = (0, import_react3.useRef)([]);
646
648
  const autoFocus = (0, import_react3.useCallback)(
647
649
  (features2) => {
648
650
  if (!(features2 == null ? void 0 : features2.length) || !map)
@@ -656,9 +658,11 @@ var Map = (props) => {
656
658
  },
657
659
  [map]
658
660
  );
659
- const mapStyleConfig = mapTile[selectedMapStyleIdx];
660
661
  (0, import_react3.useEffect)(() => {
661
662
  var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
663
+ if (features && features.length > 0) {
664
+ storedFeaturesRef.current = ensureModeProperty(features);
665
+ }
662
666
  const maplibreMap = setupMaplibreMap({
663
667
  containerId: "maplibre-map",
664
668
  latitude: (_b = (_a2 = props.defaultViewState) == null ? void 0 : _a2.latitude) != null ? _b : 0,
@@ -679,11 +683,29 @@ var Map = (props) => {
679
683
  const fullscreen = new import_maplibre_gl2.default.FullscreenControl({});
680
684
  maplibreMap.addControl(navControl, "bottom-right");
681
685
  maplibreMap.addControl(fullscreen, "bottom-right");
682
- maplibreMap.once("style.load", () => setMap(maplibreMap));
686
+ maplibreMap.once("style.load", () => {
687
+ setMap(maplibreMap);
688
+ });
683
689
  return () => {
684
- maplibreMap.removeControl(navControl);
690
+ if (drawRef.current) {
691
+ storedFeaturesRef.current = drawRef.current.getSnapshot();
692
+ }
693
+ if (drawRef.current) {
694
+ try {
695
+ drawRef.current.stop();
696
+ } catch (e) {
697
+ console.warn("terraDraw stop failed", e);
698
+ }
699
+ drawRef.current = void 0;
700
+ setDraw(void 0);
701
+ }
702
+ if (maplibreMap) {
703
+ maplibreMap.removeControl(navControl);
704
+ maplibreMap.removeControl(fullscreen);
705
+ maplibreMap.remove();
706
+ }
685
707
  };
686
- }, [mapStyle, props.defaultViewState]);
708
+ }, [mapStyle]);
687
709
  const filterUserFeatures = (features2) => features2.filter(
688
710
  (f) => f.geometry.type !== "Point" || f.properties.mode !== "static" && f.properties.mode !== "select"
689
711
  );
@@ -692,6 +714,7 @@ var Map = (props) => {
692
714
  return;
693
715
  const terraDraw = setupDraw(map);
694
716
  terraDraw.start();
717
+ drawRef.current = terraDraw;
695
718
  setDraw(terraDraw);
696
719
  const handleChange = () => {
697
720
  let snapshot = terraDraw.getSnapshot();
@@ -710,24 +733,29 @@ var Map = (props) => {
710
733
  }
711
734
  onFeaturesChange == null ? void 0 : onFeaturesChange(userFeatures);
712
735
  autoFocus(userFeatures);
713
- setMode("static");
714
736
  };
715
737
  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);
738
+ setTimeout(() => {
739
+ if (terraDraw.getSnapshot().length === 0) {
740
+ let featuresToAdd = [];
741
+ if (storedFeaturesRef.current.length > 0) {
742
+ featuresToAdd = storedFeaturesRef.current;
743
+ } else if (features && features.length > 0) {
744
+ featuresToAdd = ensureModeProperty(features);
723
745
  }
724
- });
725
- }
746
+ if (!isMultiple && featuresToAdd.length > 1) {
747
+ featuresToAdd = [featuresToAdd.at(-1)];
748
+ }
749
+ if (featuresToAdd.length > 0) {
750
+ terraDraw.addFeatures(featuresToAdd);
751
+ autoFocus(featuresToAdd);
752
+ }
753
+ }
754
+ }, 100);
726
755
  return () => {
727
756
  terraDraw.off("finish", handleChange);
728
- terraDraw.stop();
729
757
  };
730
- }, [map, isMultiple, autoFocus, features, onFeaturesChange]);
758
+ }, [map]);
731
759
  const changeMode = (0, import_react3.useCallback)(
732
760
  (newMode) => {
733
761
  draw == null ? void 0 : draw.setMode(newMode);
@@ -736,7 +764,11 @@ var Map = (props) => {
736
764
  [draw]
737
765
  );
738
766
  const handleStyleChange = (idx) => {
767
+ if (drawRef.current) {
768
+ storedFeaturesRef.current = drawRef.current.getSnapshot();
769
+ }
739
770
  setSelectedMapStyleIdx(idx);
771
+ setMode("static");
740
772
  };
741
773
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", {
742
774
  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.9",
4
4
  "author": "arun",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/esm/index.js",