@biodiv-platform/naksha-maplibre-draw 5.0.4 → 5.0.7

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.
Files changed (3) hide show
  1. package/dist/esm/index.js +118 -132
  2. package/dist/index.js +120 -134
  3. package/package.json +1 -1
package/dist/esm/index.js CHANGED
@@ -126,7 +126,7 @@ function setupDraw(map) {
126
126
  }
127
127
 
128
128
  // src/components/map.tsx
129
- import { useRef, useState, useEffect, useCallback } from "react";
129
+ import { useRef, useState as useState2, useEffect as useEffect2, useCallback } from "react";
130
130
 
131
131
  // src/components/setup-maplibre.tsx
132
132
  import maplibregl from "maplibre-gl";
@@ -316,26 +316,41 @@ var MapButtons = ({
316
316
  };
317
317
  var MapButtons_default = MapButtons;
318
318
 
319
- // src/components/map.tsx
320
- import { defaultMapStyles } from "@biodiv-platform/naksha-commons";
321
-
322
319
  // src/components/icon-buttons/ClearButton.tsx
320
+ import { useState, useEffect } from "react";
323
321
  import { jsx as jsx4 } from "react/jsx-runtime";
324
322
  var ClearButton = ({
325
323
  draw,
326
324
  onFeaturesChange
327
325
  }) => {
328
- if (!draw)
329
- return null;
326
+ const [hasFeatures, setHasFeatures] = useState(false);
327
+ useEffect(() => {
328
+ if (!draw)
329
+ return;
330
+ const updateHasFeatures = () => {
331
+ const snapshot = draw.getSnapshot();
332
+ setHasFeatures(snapshot.length > 0);
333
+ };
334
+ draw.on("finish", updateHasFeatures);
335
+ draw.on("change", updateHasFeatures);
336
+ updateHasFeatures();
337
+ return () => {
338
+ draw.off("finish", updateHasFeatures);
339
+ draw.off("change", updateHasFeatures);
340
+ };
341
+ }, [draw]);
330
342
  const handleClear = () => {
343
+ if (!draw)
344
+ return;
331
345
  draw.clear();
332
- if (onFeaturesChange) {
346
+ setHasFeatures(false);
347
+ if (onFeaturesChange)
333
348
  onFeaturesChange([]);
334
- }
335
349
  };
336
350
  return /* @__PURE__ */ jsx4("button", {
337
351
  onClick: handleClear,
338
- title: "Delete",
352
+ title: "Clear all features",
353
+ disabled: !hasFeatures,
339
354
  style: {
340
355
  display: "flex",
341
356
  justifyContent: "center",
@@ -344,8 +359,8 @@ var ClearButton = ({
344
359
  height: "30px",
345
360
  padding: "0",
346
361
  borderRadius: "4px",
347
- backgroundColor: "#f0f0f0",
348
- cursor: "pointer"
362
+ backgroundColor: hasFeatures ? "#f0f0f0" : "#e0e0e0",
363
+ cursor: hasFeatures ? "pointer" : "not-allowed"
349
364
  },
350
365
  children: /* @__PURE__ */ jsx4(DeleteIcon, {})
351
366
  });
@@ -353,67 +368,38 @@ var ClearButton = ({
353
368
  var ClearButton_default = ClearButton;
354
369
 
355
370
  // src/components/map.tsx
371
+ import { defaultMapStyles } from "@biodiv-platform/naksha-commons";
356
372
  import bbox from "@turf/bbox";
357
373
  import { Fragment, jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
358
- var ensureModeProperty = (features) => features.map((feature) => {
359
- var _a, _b;
360
- if ((_a = feature.properties) == null ? void 0 : _a.mode)
361
- return feature;
362
- let mode;
363
- switch ((_b = feature.geometry) == null ? void 0 : _b.type) {
364
- case "Point":
365
- mode = "point";
366
- break;
367
- case "Polygon":
368
- mode = "polygon";
369
- break;
370
- case "LineString":
371
- mode = "linestring";
372
- break;
373
- default:
374
- mode = "unknown";
375
- }
376
- return __spreadProps(__spreadValues({}, feature), {
377
- properties: __spreadProps(__spreadValues({}, feature.properties), { mode })
374
+ var ensureModeProperty = (features) => {
375
+ return features.map((feature) => {
376
+ var _a, _b;
377
+ if ((_a = feature.properties) == null ? void 0 : _a.mode)
378
+ return feature;
379
+ const geometryType = (_b = feature.geometry) == null ? void 0 : _b.type;
380
+ const mode = geometryType === "Point" ? "point" : geometryType === "Polygon" ? "polygon" : geometryType === "LineString" ? "linestring" : "unknown";
381
+ return __spreadProps(__spreadValues({}, feature), {
382
+ properties: __spreadProps(__spreadValues({}, feature.properties), { mode })
383
+ });
378
384
  });
379
- });
385
+ };
380
386
  var Map = (props) => {
381
- const {
382
- mapStyle: mapStyleIdx = 0,
383
- defaultViewState,
384
- isMultiple = true,
385
- isControlled = false,
386
- onFeaturesChange,
387
- features = []
388
- } = props;
389
387
  const mapRef = useRef(null);
390
- const [map, setMap] = useState(null);
391
- const [mode, setMode] = useState("static");
392
- const [draw, setDraw] = useState(null);
393
- const handleFeaturesChange = useCallback(
394
- (newFeatures) => {
395
- if (onFeaturesChange) {
396
- onFeaturesChange(newFeatures);
397
- }
398
- },
399
- [onFeaturesChange]
400
- );
401
- const autoFocus = useCallback(() => {
402
- if (!(features == null ? void 0 : features.length) || !map)
403
- return;
404
- const _bbox = bbox({ type: "FeatureCollection", features });
405
- map.fitBounds(_bbox, { padding: 40, duration: 1e3 });
406
- }, [map, features]);
407
- useEffect(() => {
408
- var _a, _b, _c, _d, _e;
388
+ const [map, setMap] = useState2();
389
+ const [mode, setMode] = useState2("static");
390
+ const [draw, setDraw] = useState2();
391
+ const mapStyle = defaultMapStyles[(props == null ? void 0 : props.mapStyle) || 0].style;
392
+ const { isMultiple = true } = props;
393
+ useEffect2(() => {
394
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
409
395
  const maplibreMap = setupMaplibreMap({
410
396
  containerId: "maplibre-map",
411
- latitude: (_a = defaultViewState == null ? void 0 : defaultViewState.latitude) != null ? _a : 0,
412
- longitude: (_b = defaultViewState == null ? void 0 : defaultViewState.longitude) != null ? _b : 0,
413
- zoom: (_c = defaultViewState == null ? void 0 : defaultViewState.zoom) != null ? _c : 0,
414
- pitch: (_d = defaultViewState == null ? void 0 : defaultViewState.pitch) != null ? _d : 0,
415
- bearing: (_e = defaultViewState == null ? void 0 : defaultViewState.bearing) != null ? _e : 0,
416
- mapstyle: defaultMapStyles[mapStyleIdx].style
397
+ latitude: (_b = (_a = props.defaultViewState) == null ? void 0 : _a.latitude) != null ? _b : 0,
398
+ longitude: (_d = (_c = props.defaultViewState) == null ? void 0 : _c.longitude) != null ? _d : 0,
399
+ zoom: (_f = (_e = props.defaultViewState) == null ? void 0 : _e.zoom) != null ? _f : 0,
400
+ pitch: (_h = (_g = props.defaultViewState) == null ? void 0 : _g.pitch) != null ? _h : 0,
401
+ bearing: (_j = (_i = props.defaultViewState) == null ? void 0 : _i.bearing) != null ? _j : 0,
402
+ mapstyle: mapStyle
417
403
  });
418
404
  const navControl = new maplibregl2.NavigationControl({
419
405
  showCompass: true,
@@ -423,61 +409,65 @@ var Map = (props) => {
423
409
  maplibreMap.addControl(navControl, "bottom-right");
424
410
  maplibreMap.once("style.load", () => setMap(maplibreMap));
425
411
  return () => {
426
- maplibreMap.removeControl(navControl);
427
- setMap(null);
412
+ if (navControl && maplibreMap) {
413
+ maplibreMap.removeControl(navControl);
414
+ }
428
415
  };
429
416
  }, []);
430
- useEffect(() => {
417
+ const autoFocus = useCallback(
418
+ (features) => {
419
+ if (!(features == null ? void 0 : features.length) || !map)
420
+ return;
421
+ const _bbox = bbox({
422
+ type: "FeatureCollection",
423
+ features
424
+ });
425
+ map.fitBounds(_bbox, { padding: 40, duration: 1e3 });
426
+ },
427
+ [map]
428
+ );
429
+ useEffect2(() => {
430
+ var _a;
431
431
  if (!map)
432
432
  return;
433
433
  const terraDraw = setupDraw(map);
434
434
  terraDraw.start();
435
435
  setDraw(terraDraw);
436
- const onChange = () => {
437
- let snapshot = terraDraw.getSnapshot();
436
+ const handleChange = () => {
437
+ var _a2;
438
+ const snapshot = terraDraw.getSnapshot();
439
+ let filteredFeatures = snapshot;
438
440
  if (!isMultiple && snapshot.length > 1) {
439
441
  const latestFeature = snapshot[snapshot.length - 1];
440
- const toRemove = snapshot.slice(0, -1);
441
- const featureIds = toRemove.map((f) => f.id).filter(Boolean);
442
+ filteredFeatures = [latestFeature];
443
+ const featuresToRemove = snapshot.slice(0, -1);
444
+ const featureIds = featuresToRemove.map((f) => f.id).filter((id) => id !== void 0);
442
445
  if (featureIds.length > 0) {
443
446
  terraDraw.removeFeatures(featureIds);
444
447
  }
445
- snapshot = [latestFeature];
446
448
  }
447
- handleFeaturesChange(snapshot);
449
+ (_a2 = props.onFeaturesChange) == null ? void 0 : _a2.call(props, snapshot);
450
+ autoFocus(filteredFeatures);
448
451
  };
449
- terraDraw.on("change", onChange);
450
- if (features.length) {
451
- const timeoutId = setTimeout(() => {
452
+ terraDraw.on("finish", handleChange);
453
+ if ((_a = props.features) == null ? void 0 : _a.length) {
454
+ setTimeout(() => {
452
455
  if (terraDraw.getSnapshot().length === 0) {
453
- let featuresWithMode = ensureModeProperty(features);
456
+ const featuresWithMode = ensureModeProperty(props.features);
457
+ let initialFeatures = featuresWithMode;
454
458
  if (!isMultiple && featuresWithMode.length > 1) {
455
- featuresWithMode = [featuresWithMode[featuresWithMode.length - 1]];
459
+ initialFeatures = [featuresWithMode[featuresWithMode.length - 1]];
456
460
  }
457
- terraDraw.addFeatures(featuresWithMode);
458
- handleFeaturesChange(featuresWithMode);
459
- autoFocus();
461
+ terraDraw.addFeatures(initialFeatures);
462
+ autoFocus(initialFeatures);
460
463
  }
461
- }, 300);
462
- return () => {
463
- clearTimeout(timeoutId);
464
- terraDraw.off("change", onChange);
465
- terraDraw.stop();
466
- setDraw(null);
467
- };
464
+ });
468
465
  }
469
466
  return () => {
470
- terraDraw.off("change", onChange);
467
+ terraDraw.off("finish", handleChange);
471
468
  terraDraw.stop();
472
- setDraw(null);
473
469
  };
474
- }, [map, isMultiple, features, handleFeaturesChange, autoFocus]);
475
- useEffect(() => {
476
- if (features && features.length > 0) {
477
- onFeaturesChange == null ? void 0 : onFeaturesChange(features);
478
- autoFocus();
479
- }
480
- }, [features, autoFocus, onFeaturesChange]);
470
+ }, [map, isMultiple, autoFocus]);
481
471
  const changeMode = useCallback(
482
472
  (newMode) => {
483
473
  draw == null ? void 0 : draw.setMode(newMode);
@@ -485,41 +475,37 @@ var Map = (props) => {
485
475
  },
486
476
  [draw]
487
477
  );
488
- return /* @__PURE__ */ jsxs3("div", {
489
- children: [
490
- /* @__PURE__ */ jsx5("div", {
491
- ref: mapRef,
492
- id: "maplibre-map",
493
- style: { width: "100%", height: "100vh" }
494
- }),
495
- draw && /* @__PURE__ */ jsxs3(Fragment, {
496
- children: [
497
- /* @__PURE__ */ jsx5(MapButtons_default, {
498
- mode,
499
- changeMode
500
- }),
501
- /* @__PURE__ */ jsx5("div", {
502
- style: {
503
- position: "absolute",
504
- top: "180px",
505
- left: "10px",
506
- display: "flex",
507
- flexDirection: "column",
508
- gap: "6px",
509
- zIndex: 1e3,
510
- backgroundColor: "rgba(255, 255, 255, 0.9)",
511
- padding: "8px",
512
- borderRadius: "6px",
513
- boxShadow: "0 2px 6px rgba(0,0,0,0.2)"
514
- },
515
- children: /* @__PURE__ */ jsx5(ClearButton_default, {
516
- draw,
517
- onFeaturesChange
518
- })
478
+ return /* @__PURE__ */ jsx5("div", {
479
+ ref: mapRef,
480
+ id: "maplibre-map",
481
+ style: { width: "100%", height: "100%" },
482
+ children: draw && /* @__PURE__ */ jsxs3(Fragment, {
483
+ children: [
484
+ /* @__PURE__ */ jsx5(MapButtons_default, {
485
+ mode,
486
+ changeMode
487
+ }),
488
+ /* @__PURE__ */ jsx5("div", {
489
+ style: {
490
+ position: "absolute",
491
+ top: "180px",
492
+ left: "10px",
493
+ display: "flex",
494
+ flexDirection: "column",
495
+ gap: "6px",
496
+ zIndex: 1e3,
497
+ backgroundColor: "rgba(255, 255, 255, 0.9)",
498
+ padding: "8px",
499
+ borderRadius: "6px",
500
+ boxShadow: "0 2px 6px rgba(0,0,0,0.2)"
501
+ },
502
+ children: /* @__PURE__ */ jsx5(ClearButton_default, {
503
+ draw,
504
+ onFeaturesChange: props.onFeaturesChange
519
505
  })
520
- ]
521
- })
522
- ]
506
+ })
507
+ ]
508
+ })
523
509
  });
524
510
  };
525
511
  var map_default = Map;
package/dist/index.js CHANGED
@@ -145,7 +145,7 @@ function setupDraw(map) {
145
145
  }
146
146
 
147
147
  // src/components/map.tsx
148
- var import_react = require("react");
148
+ var import_react2 = require("react");
149
149
 
150
150
  // src/components/setup-maplibre.tsx
151
151
  var import_maplibre_gl = __toESM(require("maplibre-gl"));
@@ -335,26 +335,41 @@ var MapButtons = ({
335
335
  };
336
336
  var MapButtons_default = MapButtons;
337
337
 
338
- // src/components/map.tsx
339
- var import_naksha_commons = require("@biodiv-platform/naksha-commons");
340
-
341
338
  // src/components/icon-buttons/ClearButton.tsx
339
+ var import_react = require("react");
342
340
  var import_jsx_runtime4 = require("react/jsx-runtime");
343
341
  var ClearButton = ({
344
342
  draw,
345
343
  onFeaturesChange
346
344
  }) => {
347
- if (!draw)
348
- return null;
345
+ const [hasFeatures, setHasFeatures] = (0, import_react.useState)(false);
346
+ (0, import_react.useEffect)(() => {
347
+ if (!draw)
348
+ return;
349
+ const updateHasFeatures = () => {
350
+ const snapshot = draw.getSnapshot();
351
+ setHasFeatures(snapshot.length > 0);
352
+ };
353
+ draw.on("finish", updateHasFeatures);
354
+ draw.on("change", updateHasFeatures);
355
+ updateHasFeatures();
356
+ return () => {
357
+ draw.off("finish", updateHasFeatures);
358
+ draw.off("change", updateHasFeatures);
359
+ };
360
+ }, [draw]);
349
361
  const handleClear = () => {
362
+ if (!draw)
363
+ return;
350
364
  draw.clear();
351
- if (onFeaturesChange) {
365
+ setHasFeatures(false);
366
+ if (onFeaturesChange)
352
367
  onFeaturesChange([]);
353
- }
354
368
  };
355
369
  return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", {
356
370
  onClick: handleClear,
357
- title: "Delete",
371
+ title: "Clear all features",
372
+ disabled: !hasFeatures,
358
373
  style: {
359
374
  display: "flex",
360
375
  justifyContent: "center",
@@ -363,8 +378,8 @@ var ClearButton = ({
363
378
  height: "30px",
364
379
  padding: "0",
365
380
  borderRadius: "4px",
366
- backgroundColor: "#f0f0f0",
367
- cursor: "pointer"
381
+ backgroundColor: hasFeatures ? "#f0f0f0" : "#e0e0e0",
382
+ cursor: hasFeatures ? "pointer" : "not-allowed"
368
383
  },
369
384
  children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(DeleteIcon, {})
370
385
  });
@@ -372,67 +387,38 @@ var ClearButton = ({
372
387
  var ClearButton_default = ClearButton;
373
388
 
374
389
  // src/components/map.tsx
390
+ var import_naksha_commons = require("@biodiv-platform/naksha-commons");
375
391
  var import_bbox = __toESM(require("@turf/bbox"));
376
392
  var import_jsx_runtime5 = require("react/jsx-runtime");
377
- var ensureModeProperty = (features) => features.map((feature) => {
378
- var _a, _b;
379
- if ((_a = feature.properties) == null ? void 0 : _a.mode)
380
- return feature;
381
- let mode;
382
- switch ((_b = feature.geometry) == null ? void 0 : _b.type) {
383
- case "Point":
384
- mode = "point";
385
- break;
386
- case "Polygon":
387
- mode = "polygon";
388
- break;
389
- case "LineString":
390
- mode = "linestring";
391
- break;
392
- default:
393
- mode = "unknown";
394
- }
395
- return __spreadProps(__spreadValues({}, feature), {
396
- properties: __spreadProps(__spreadValues({}, feature.properties), { mode })
393
+ var ensureModeProperty = (features) => {
394
+ return features.map((feature) => {
395
+ var _a, _b;
396
+ if ((_a = feature.properties) == null ? void 0 : _a.mode)
397
+ return feature;
398
+ const geometryType = (_b = feature.geometry) == null ? void 0 : _b.type;
399
+ const mode = geometryType === "Point" ? "point" : geometryType === "Polygon" ? "polygon" : geometryType === "LineString" ? "linestring" : "unknown";
400
+ return __spreadProps(__spreadValues({}, feature), {
401
+ properties: __spreadProps(__spreadValues({}, feature.properties), { mode })
402
+ });
397
403
  });
398
- });
404
+ };
399
405
  var Map = (props) => {
400
- const {
401
- mapStyle: mapStyleIdx = 0,
402
- defaultViewState,
403
- isMultiple = true,
404
- isControlled = false,
405
- onFeaturesChange,
406
- features = []
407
- } = props;
408
- const mapRef = (0, import_react.useRef)(null);
409
- const [map, setMap] = (0, import_react.useState)(null);
410
- const [mode, setMode] = (0, import_react.useState)("static");
411
- const [draw, setDraw] = (0, import_react.useState)(null);
412
- const handleFeaturesChange = (0, import_react.useCallback)(
413
- (newFeatures) => {
414
- if (onFeaturesChange) {
415
- onFeaturesChange(newFeatures);
416
- }
417
- },
418
- [onFeaturesChange]
419
- );
420
- const autoFocus = (0, import_react.useCallback)(() => {
421
- if (!(features == null ? void 0 : features.length) || !map)
422
- return;
423
- const _bbox = (0, import_bbox.default)({ type: "FeatureCollection", features });
424
- map.fitBounds(_bbox, { padding: 40, duration: 1e3 });
425
- }, [map, features]);
426
- (0, import_react.useEffect)(() => {
427
- var _a, _b, _c, _d, _e;
406
+ const mapRef = (0, import_react2.useRef)(null);
407
+ const [map, setMap] = (0, import_react2.useState)();
408
+ const [mode, setMode] = (0, import_react2.useState)("static");
409
+ const [draw, setDraw] = (0, import_react2.useState)();
410
+ const mapStyle = import_naksha_commons.defaultMapStyles[(props == null ? void 0 : props.mapStyle) || 0].style;
411
+ const { isMultiple = true } = props;
412
+ (0, import_react2.useEffect)(() => {
413
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
428
414
  const maplibreMap = setupMaplibreMap({
429
415
  containerId: "maplibre-map",
430
- latitude: (_a = defaultViewState == null ? void 0 : defaultViewState.latitude) != null ? _a : 0,
431
- longitude: (_b = defaultViewState == null ? void 0 : defaultViewState.longitude) != null ? _b : 0,
432
- zoom: (_c = defaultViewState == null ? void 0 : defaultViewState.zoom) != null ? _c : 0,
433
- pitch: (_d = defaultViewState == null ? void 0 : defaultViewState.pitch) != null ? _d : 0,
434
- bearing: (_e = defaultViewState == null ? void 0 : defaultViewState.bearing) != null ? _e : 0,
435
- mapstyle: import_naksha_commons.defaultMapStyles[mapStyleIdx].style
416
+ latitude: (_b = (_a = props.defaultViewState) == null ? void 0 : _a.latitude) != null ? _b : 0,
417
+ longitude: (_d = (_c = props.defaultViewState) == null ? void 0 : _c.longitude) != null ? _d : 0,
418
+ zoom: (_f = (_e = props.defaultViewState) == null ? void 0 : _e.zoom) != null ? _f : 0,
419
+ pitch: (_h = (_g = props.defaultViewState) == null ? void 0 : _g.pitch) != null ? _h : 0,
420
+ bearing: (_j = (_i = props.defaultViewState) == null ? void 0 : _i.bearing) != null ? _j : 0,
421
+ mapstyle: mapStyle
436
422
  });
437
423
  const navControl = new import_maplibre_gl2.default.NavigationControl({
438
424
  showCompass: true,
@@ -442,103 +428,103 @@ var Map = (props) => {
442
428
  maplibreMap.addControl(navControl, "bottom-right");
443
429
  maplibreMap.once("style.load", () => setMap(maplibreMap));
444
430
  return () => {
445
- maplibreMap.removeControl(navControl);
446
- setMap(null);
431
+ if (navControl && maplibreMap) {
432
+ maplibreMap.removeControl(navControl);
433
+ }
447
434
  };
448
435
  }, []);
449
- (0, import_react.useEffect)(() => {
436
+ const autoFocus = (0, import_react2.useCallback)(
437
+ (features) => {
438
+ if (!(features == null ? void 0 : features.length) || !map)
439
+ return;
440
+ const _bbox = (0, import_bbox.default)({
441
+ type: "FeatureCollection",
442
+ features
443
+ });
444
+ map.fitBounds(_bbox, { padding: 40, duration: 1e3 });
445
+ },
446
+ [map]
447
+ );
448
+ (0, import_react2.useEffect)(() => {
449
+ var _a;
450
450
  if (!map)
451
451
  return;
452
452
  const terraDraw = setupDraw(map);
453
453
  terraDraw.start();
454
454
  setDraw(terraDraw);
455
- const onChange = () => {
456
- let snapshot = terraDraw.getSnapshot();
455
+ const handleChange = () => {
456
+ var _a2;
457
+ const snapshot = terraDraw.getSnapshot();
458
+ let filteredFeatures = snapshot;
457
459
  if (!isMultiple && snapshot.length > 1) {
458
460
  const latestFeature = snapshot[snapshot.length - 1];
459
- const toRemove = snapshot.slice(0, -1);
460
- const featureIds = toRemove.map((f) => f.id).filter(Boolean);
461
+ filteredFeatures = [latestFeature];
462
+ const featuresToRemove = snapshot.slice(0, -1);
463
+ const featureIds = featuresToRemove.map((f) => f.id).filter((id) => id !== void 0);
461
464
  if (featureIds.length > 0) {
462
465
  terraDraw.removeFeatures(featureIds);
463
466
  }
464
- snapshot = [latestFeature];
465
467
  }
466
- handleFeaturesChange(snapshot);
468
+ (_a2 = props.onFeaturesChange) == null ? void 0 : _a2.call(props, snapshot);
469
+ autoFocus(filteredFeatures);
467
470
  };
468
- terraDraw.on("change", onChange);
469
- if (features.length) {
470
- const timeoutId = setTimeout(() => {
471
+ terraDraw.on("finish", handleChange);
472
+ if ((_a = props.features) == null ? void 0 : _a.length) {
473
+ setTimeout(() => {
471
474
  if (terraDraw.getSnapshot().length === 0) {
472
- let featuresWithMode = ensureModeProperty(features);
475
+ const featuresWithMode = ensureModeProperty(props.features);
476
+ let initialFeatures = featuresWithMode;
473
477
  if (!isMultiple && featuresWithMode.length > 1) {
474
- featuresWithMode = [featuresWithMode[featuresWithMode.length - 1]];
478
+ initialFeatures = [featuresWithMode[featuresWithMode.length - 1]];
475
479
  }
476
- terraDraw.addFeatures(featuresWithMode);
477
- handleFeaturesChange(featuresWithMode);
478
- autoFocus();
480
+ terraDraw.addFeatures(initialFeatures);
481
+ autoFocus(initialFeatures);
479
482
  }
480
- }, 300);
481
- return () => {
482
- clearTimeout(timeoutId);
483
- terraDraw.off("change", onChange);
484
- terraDraw.stop();
485
- setDraw(null);
486
- };
483
+ });
487
484
  }
488
485
  return () => {
489
- terraDraw.off("change", onChange);
486
+ terraDraw.off("finish", handleChange);
490
487
  terraDraw.stop();
491
- setDraw(null);
492
488
  };
493
- }, [map, isMultiple, features, handleFeaturesChange, autoFocus]);
494
- (0, import_react.useEffect)(() => {
495
- if (features && features.length > 0) {
496
- onFeaturesChange == null ? void 0 : onFeaturesChange(features);
497
- autoFocus();
498
- }
499
- }, [features, autoFocus, onFeaturesChange]);
500
- const changeMode = (0, import_react.useCallback)(
489
+ }, [map, isMultiple, autoFocus]);
490
+ const changeMode = (0, import_react2.useCallback)(
501
491
  (newMode) => {
502
492
  draw == null ? void 0 : draw.setMode(newMode);
503
493
  setMode(newMode);
504
494
  },
505
495
  [draw]
506
496
  );
507
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", {
508
- children: [
509
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", {
510
- ref: mapRef,
511
- id: "maplibre-map",
512
- style: { width: "100%", height: "100vh" }
513
- }),
514
- draw && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, {
515
- children: [
516
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(MapButtons_default, {
517
- mode,
518
- changeMode
519
- }),
520
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", {
521
- style: {
522
- position: "absolute",
523
- top: "180px",
524
- left: "10px",
525
- display: "flex",
526
- flexDirection: "column",
527
- gap: "6px",
528
- zIndex: 1e3,
529
- backgroundColor: "rgba(255, 255, 255, 0.9)",
530
- padding: "8px",
531
- borderRadius: "6px",
532
- boxShadow: "0 2px 6px rgba(0,0,0,0.2)"
533
- },
534
- children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ClearButton_default, {
535
- draw,
536
- onFeaturesChange
537
- })
497
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", {
498
+ ref: mapRef,
499
+ id: "maplibre-map",
500
+ style: { width: "100%", height: "100%" },
501
+ children: draw && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, {
502
+ children: [
503
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(MapButtons_default, {
504
+ mode,
505
+ changeMode
506
+ }),
507
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", {
508
+ style: {
509
+ position: "absolute",
510
+ top: "180px",
511
+ left: "10px",
512
+ display: "flex",
513
+ flexDirection: "column",
514
+ gap: "6px",
515
+ zIndex: 1e3,
516
+ backgroundColor: "rgba(255, 255, 255, 0.9)",
517
+ padding: "8px",
518
+ borderRadius: "6px",
519
+ boxShadow: "0 2px 6px rgba(0,0,0,0.2)"
520
+ },
521
+ children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ClearButton_default, {
522
+ draw,
523
+ onFeaturesChange: props.onFeaturesChange
538
524
  })
539
- ]
540
- })
541
- ]
525
+ })
526
+ ]
527
+ })
542
528
  });
543
529
  };
544
530
  var map_default = Map;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biodiv-platform/naksha-maplibre-draw",
3
- "version": "5.0.4",
3
+ "version": "5.0.7",
4
4
  "author": "harshzalavadiya",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/esm/index.js",