leaflet-anvil 0.1.1 → 0.2.0

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.mjs CHANGED
@@ -5,6 +5,29 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
5
5
  // src/index.ts
6
6
  import * as L22 from "leaflet";
7
7
 
8
+ // src/types.ts
9
+ var AnvilMode = /* @__PURE__ */ ((AnvilMode2) => {
10
+ AnvilMode2["Polygon"] = "polygon";
11
+ AnvilMode2["Polyline"] = "polyline";
12
+ AnvilMode2["Marker"] = "marker";
13
+ AnvilMode2["Rectangle"] = "rectangle";
14
+ AnvilMode2["Square"] = "square";
15
+ AnvilMode2["Triangle"] = "triangle";
16
+ AnvilMode2["Circle"] = "circle";
17
+ AnvilMode2["Freehand"] = "freehand";
18
+ AnvilMode2["Cut"] = "cut";
19
+ AnvilMode2["Split"] = "split";
20
+ AnvilMode2["Drag"] = "drag";
21
+ AnvilMode2["Scale"] = "scale";
22
+ AnvilMode2["Rotate"] = "rotate";
23
+ AnvilMode2["Union"] = "union";
24
+ AnvilMode2["Subtract"] = "subtract";
25
+ AnvilMode2["Edit"] = "edit";
26
+ AnvilMode2["Delete"] = "delete";
27
+ AnvilMode2["Off"] = "off";
28
+ return AnvilMode2;
29
+ })(AnvilMode || {});
30
+
8
31
  // src/events.ts
9
32
  var ANVIL_EVENTS = {
10
33
  CREATED: "anvil:created",
@@ -54,6 +77,7 @@ import * as L3 from "leaflet";
54
77
  import * as L from "leaflet";
55
78
  function getSnapLatLng(map, latlng, store, options, additionalPoints = [], skipLayer) {
56
79
  if (!options.snapping) return latlng;
80
+ const verifiedAdditionalPoints = Array.isArray(additionalPoints) ? additionalPoints : [];
57
81
  const snapDistance = options.snapDistance || 10;
58
82
  const basePoint = map.latLngToContainerPoint(latlng);
59
83
  let closestPoint = null;
@@ -90,7 +114,7 @@ function getSnapLatLng(map, latlng, store, options, additionalPoints = [], skipL
90
114
  }
91
115
  });
92
116
  });
93
- additionalPoints.forEach((p) => {
117
+ verifiedAdditionalPoints.forEach((p) => {
94
118
  const containerPoint = map.latLngToContainerPoint(p);
95
119
  const dist = basePoint.distanceTo(containerPoint);
96
120
  if (dist < minDistance) {
@@ -362,7 +386,7 @@ var DrawMarkerMode = class {
362
386
  }
363
387
  onClick(e) {
364
388
  const latlng = this.store ? getSnapLatLng(this.map, e.latlng, this.store, this.options) : e.latlng;
365
- const marker4 = L5.marker(latlng).addTo(this.map);
389
+ const marker4 = L5.marker(latlng, this.options.vertexOptions).addTo(this.map);
366
390
  this.map.fire(ANVIL_EVENTS.CREATED, { layer: marker4 });
367
391
  }
368
392
  };
@@ -1029,27 +1053,41 @@ var DragMode = class {
1029
1053
  this.map = map;
1030
1054
  this.store = store;
1031
1055
  this.options = options;
1056
+ __publicField(this, "isEnabled", false);
1032
1057
  __publicField(this, "draggingLayer", null);
1033
1058
  __publicField(this, "startLatLng", null);
1034
1059
  __publicField(this, "initialLatLngs", null);
1060
+ this.store.getGroup().on("layeradd", (e) => {
1061
+ if (this.isEnabled) {
1062
+ this.addLayerListener(e.layer);
1063
+ }
1064
+ });
1035
1065
  }
1036
1066
  enable() {
1067
+ this.isEnabled = true;
1037
1068
  this.store.getGroup().eachLayer((layer) => {
1038
- if (layer instanceof L13.Path || layer instanceof L13.Marker) {
1039
- layer.on("mousedown", this.onMouseDown, this);
1040
- layer.getElement()?.style.setProperty("cursor", "move");
1041
- }
1069
+ this.addLayerListener(layer);
1042
1070
  });
1043
1071
  }
1044
1072
  disable() {
1073
+ this.isEnabled = false;
1045
1074
  this.store.getGroup().eachLayer((layer) => {
1046
- if (layer instanceof L13.Path || layer instanceof L13.Marker) {
1047
- layer.off("mousedown", this.onMouseDown, this);
1048
- layer.getElement()?.style.setProperty("cursor", "");
1049
- }
1075
+ this.removeLayerListener(layer);
1050
1076
  });
1051
1077
  this.stopDragging();
1052
1078
  }
1079
+ addLayerListener(layer) {
1080
+ if (layer instanceof L13.Path || layer instanceof L13.Marker) {
1081
+ layer.on("mousedown", this.onMouseDown, this);
1082
+ layer.getElement()?.style.setProperty("cursor", "move");
1083
+ }
1084
+ }
1085
+ removeLayerListener(layer) {
1086
+ if (layer instanceof L13.Path || layer instanceof L13.Marker) {
1087
+ layer.off("mousedown", this.onMouseDown, this);
1088
+ layer.getElement()?.style.setProperty("cursor", "");
1089
+ }
1090
+ }
1053
1091
  onMouseDown(e) {
1054
1092
  L13.DomEvent.stopPropagation(e);
1055
1093
  const layer = e.target;
@@ -1834,23 +1872,37 @@ var DeleteMode = class {
1834
1872
  constructor(map, store) {
1835
1873
  this.map = map;
1836
1874
  this.store = store;
1875
+ __publicField(this, "isEnabled", false);
1876
+ this.store.getGroup().on("layeradd", (e) => {
1877
+ if (this.isEnabled) {
1878
+ this.addLayerListener(e.layer);
1879
+ }
1880
+ });
1837
1881
  }
1838
1882
  enable() {
1883
+ this.isEnabled = true;
1839
1884
  this.store.getGroup().eachLayer((layer) => {
1840
- layer.on("click", this.onClick, this);
1841
- if (layer instanceof L19.Path || layer instanceof L19.Marker) {
1842
- layer.getElement()?.style.setProperty("cursor", "pointer");
1843
- }
1885
+ this.addLayerListener(layer);
1844
1886
  });
1845
1887
  }
1846
1888
  disable() {
1889
+ this.isEnabled = false;
1847
1890
  this.store.getGroup().eachLayer((layer) => {
1848
- layer.off("click", this.onClick, this);
1849
- if (layer instanceof L19.Path || layer instanceof L19.Marker) {
1850
- layer.getElement()?.style.setProperty("cursor", "");
1851
- }
1891
+ this.removeLayerListener(layer);
1852
1892
  });
1853
1893
  }
1894
+ addLayerListener(layer) {
1895
+ layer.on("click", this.onClick, this);
1896
+ if (layer instanceof L19.Path || layer instanceof L19.Marker) {
1897
+ layer.getElement()?.style.setProperty("cursor", "pointer");
1898
+ }
1899
+ }
1900
+ removeLayerListener(layer) {
1901
+ layer.off("click", this.onClick, this);
1902
+ if (layer instanceof L19.Path || layer instanceof L19.Marker) {
1903
+ layer.getElement()?.style.setProperty("cursor", "");
1904
+ }
1905
+ }
1854
1906
  onClick(e) {
1855
1907
  L19.DomEvent.stopPropagation(e);
1856
1908
  const layer = e.target;
@@ -1879,50 +1931,8 @@ var LayerStore = class {
1879
1931
  getGroup() {
1880
1932
  return this.group;
1881
1933
  }
1882
- };
1883
-
1884
- // src/anvil.ts
1885
- var Anvil = class {
1886
- constructor(map, options) {
1887
- this.map = map;
1888
- __publicField(this, "modeManager");
1889
- __publicField(this, "store");
1890
- __publicField(this, "options");
1891
- this.options = { snapping: false, snapDistance: 10, preventSelfIntersection: false, ...options };
1892
- this.store = new LayerStore(map, this.options.layerGroup);
1893
- this.modeManager = new ModeManager(map);
1894
- this.modeManager.addMode("draw:polygon", new DrawPolygonMode(this.map, this.options, this.store));
1895
- this.modeManager.addMode("draw:polyline", new DrawPolylineMode(this.map, this.options, this.store));
1896
- this.modeManager.addMode("draw:marker", new DrawMarkerMode(this.map, this.options, this.store));
1897
- this.modeManager.addMode("draw:rectangle", new DrawRectangleMode(this.map, this.options, this.store));
1898
- this.modeManager.addMode("draw:square", new DrawSquareMode(this.map, this.options, this.store));
1899
- this.modeManager.addMode("draw:triangle", new DrawTriangleMode(this.map, this.options, this.store));
1900
- this.modeManager.addMode("draw:circle", new DrawCircleMode(this.map, this.options, this.store));
1901
- this.modeManager.addMode("draw:freehand", new FreehandMode(this.map, this.store, this.options));
1902
- this.modeManager.addMode("cut", new CutMode(map, this.store, this.options));
1903
- this.modeManager.addMode("split", new SplitMode(map, this.store, this.options));
1904
- this.modeManager.addMode("union", new UnionMode(map, this.store, this.options));
1905
- this.modeManager.addMode("subtract", new SubtractMode(map, this.store, this.options));
1906
- this.modeManager.addMode("drag", new DragMode(map, this.store, this.options));
1907
- this.modeManager.addMode("scale", new ScaleMode(map, this.store, this.options));
1908
- this.modeManager.addMode("rotate", new RotateMode(map, this.store, this.options));
1909
- this.modeManager.addMode("edit", new EditMode(this.map, this.store, this.options));
1910
- this.modeManager.addMode("delete", new DeleteMode(map, this.store));
1911
- this.map.on(ANVIL_EVENTS.CREATED, (e) => {
1912
- this.store.addLayer(e.layer);
1913
- });
1914
- this.map.on(ANVIL_EVENTS.DELETED, (e) => {
1915
- this.store.removeLayer(e.layer);
1916
- });
1917
- }
1918
- enable(mode) {
1919
- this.modeManager.enable(mode);
1920
- }
1921
- disable() {
1922
- this.modeManager.disable();
1923
- }
1924
- getLayerGroup() {
1925
- return this.store.getGroup();
1934
+ getLayers() {
1935
+ return this.group.getLayers();
1926
1936
  }
1927
1937
  };
1928
1938
 
@@ -1931,101 +1941,133 @@ import * as L21 from "leaflet";
1931
1941
  var SVG_ATTRS = 'xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"';
1932
1942
  var ICONS = {
1933
1943
  // Marker: Map-Pin mit Punkt in der Mitte
1934
- "draw:marker": `<svg ${SVG_ATTRS}><path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z"/><circle cx="12" cy="10" r="3"/></svg>`,
1944
+ ["marker" /* Marker */]: `<svg ${SVG_ATTRS}><path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z"/><circle cx="12" cy="10" r="3"/></svg>`,
1935
1945
  // Polyline: Kurvige Route mit Endpunkten
1936
- "draw:polyline": `<svg ${SVG_ATTRS}><path d="M9 19h8.5a3.5 3.5 0 0 0 0-7h-11a3.5 3.5 0 0 1 0-7H15"/><circle cx="18" cy="5" r="3"/><circle cx="6" cy="19" r="3"/></svg>`,
1946
+ ["polyline" /* Polyline */]: `<svg ${SVG_ATTRS}><path d="M9 19h8.5a3.5 3.5 0 0 0 0-7h-11a3.5 3.5 0 0 1 0-7H15"/><circle cx="18" cy="5" r="3"/><circle cx="6" cy="19" r="3"/></svg>`,
1937
1947
  // Polygon: Klassische unregelmäßige 5-Eck-Form
1938
- "draw:polygon": `<svg ${SVG_ATTRS}><path d="m12 2 10 7-3 12H5l-3-12Z"/></svg>`,
1948
+ ["polygon" /* Polygon */]: `<svg ${SVG_ATTRS}><path d="m12 2 10 7-3 12H5l-3-12Z"/></svg>`,
1939
1949
  // Rectangle: Horizontales Rechteck
1940
- "draw:rectangle": `<svg ${SVG_ATTRS}><rect width="20" height="12" x="2" y="6" rx="2"/></svg>`,
1950
+ ["rectangle" /* Rectangle */]: `<svg ${SVG_ATTRS}><rect width="20" height="12" x="2" y="6" rx="2"/></svg>`,
1941
1951
  // Square: Quadratisches Rechteck (1:1)
1942
- "draw:square": `<svg ${SVG_ATTRS}><rect width="18" height="18" x="3" y="3" rx="2"/></svg>`,
1952
+ ["square" /* Square */]: `<svg ${SVG_ATTRS}><rect width="18" height="18" x="3" y="3" rx="2"/></svg>`,
1943
1953
  // Triangle: Gleichschenkliges Dreieck
1944
- "draw:triangle": `<svg ${SVG_ATTRS}><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"/></svg>`,
1954
+ ["triangle" /* Triangle */]: `<svg ${SVG_ATTRS}><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"/></svg>`,
1945
1955
  // Circle: Klassischer Kreis
1946
- "draw:circle": `<svg ${SVG_ATTRS}><circle cx="12" cy="12" r="10"/></svg>`,
1956
+ ["circle" /* Circle */]: `<svg ${SVG_ATTRS}><circle cx="12" cy="12" r="10"/></svg>`,
1947
1957
  // Freehand: Geschwungene Signatur-Linie
1948
- "draw:freehand": `<svg ${SVG_ATTRS}><path d="m3 16 2 2 16-16"/><path d="M7 21h14"/><path d="M3 11c0 2 2 2 2 2z"/></svg>`,
1958
+ ["freehand" /* Freehand */]: `<svg ${SVG_ATTRS}><path d="m3 16 2 2 16-16"/><path d="M7 21h14"/><path d="M3 11c0 2 2 2 2 2z"/></svg>`,
1949
1959
  // Cut: Offene Schere
1950
- "cut": `<svg ${SVG_ATTRS}><circle cx="6" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M20 4 8.12 15.88"/><path d="M14.47 14.48 20 20"/><path d="M8.12 8.12 12 12"/></svg>`,
1960
+ ["cut" /* Cut */]: `<svg ${SVG_ATTRS}><circle cx="6" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M20 4 8.12 15.88"/><path d="M14.47 14.48 20 20"/><path d="M8.12 8.12 12 12"/></svg>`,
1951
1961
  // Split: Linie, die eine Form teilt
1952
- "split": `<svg ${SVG_ATTRS}><path d="M3 12h18"/><path d="M8 3v18"/><path d="M16 3v18"/><rect width="18" height="18" x="3" y="3" rx="2" stroke-dasharray="4 4" opacity="0.5"/></svg>`,
1962
+ ["split" /* Split */]: `<svg ${SVG_ATTRS}><path d="M3 12h18"/><path d="M8 3v18"/><path d="M16 3v18"/><rect width="18" height="18" x="3" y="3" rx="2" stroke-dasharray="4 4" opacity="0.5"/></svg>`,
1953
1963
  // Union: Zwei verschmolzene Rechtecke
1954
- "union": `<svg ${SVG_ATTRS}><path d="M8 4H4v4"/><path d="M4 12v4a2 2 0 0 0 2 2h4"/><path d="M14 18h4v-4"/><path d="M20 10V6a2 2 0 0 0-2-2h-4"/><path d="M14 10h-4v4" stroke-dasharray="2 2"/></svg>`,
1964
+ ["union" /* Union */]: `<svg ${SVG_ATTRS}><path d="M8 4H4v4"/><path d="M4 12v4a2 2 0 0 0 2 2h4"/><path d="M14 18h4v-4"/><path d="M20 10V6a2 2 0 0 0-2-2h-4"/><path d="M14 10h-4v4" stroke-dasharray="2 2"/></svg>`,
1955
1965
  // Subtract: Hauptform mit "ausgeschnittenem" Bereich (Minus-Metapher)
1956
- "subtract": `<svg ${SVG_ATTRS}><path d="M4 4h16v16H4z"/><path d="M10 10h10v10H10z" stroke-dasharray="2 2" opacity="0.7"/><path d="M15 6h-6"/></svg>`,
1966
+ ["subtract" /* Subtract */]: `<svg ${SVG_ATTRS}><path d="M4 4h16v16H4z"/><path d="M10 10h10v10H10z" stroke-dasharray="2 2" opacity="0.7"/><path d="M15 6h-6"/></svg>`,
1957
1967
  // Drag: Vier-Wege-Pfeil (Move-Metapher)
1958
- "drag": `<svg ${SVG_ATTRS}><path d="m5 9-3 3 3 3"/><path d="m9 5 3-3 3 3"/><path d="m15 19 3 3 3-3"/><path d="m19 9 3 3-3 3"/><path d="M2 12h20"/><path d="M12 2v20"/></svg>`,
1968
+ ["drag" /* Drag */]: `<svg ${SVG_ATTRS}><path d="m5 9-3 3 3 3"/><path d="m9 5 3-3 3 3"/><path d="m15 19 3 3 3-3"/><path d="m19 9 3 3-3 3"/><path d="M2 12h20"/><path d="M12 2v20"/></svg>`,
1959
1969
  // Scale: Diagonal-Pfeile (Maximize-Metapher)
1960
- "scale": `<svg ${SVG_ATTRS}><path d="M15 3h6v6"/><path d="M9 21H3v-6"/><path d="M21 3 14 10"/><path d="M3 21l7-7"/></svg>`,
1970
+ ["scale" /* Scale */]: `<svg ${SVG_ATTRS}><path d="M15 3h6v6"/><path d="M9 21H3v-6"/><path d="M21 3 14 10"/><path d="M3 21l7-7"/></svg>`,
1961
1971
  // Rotate: Kreispfeil mit Ziel-Icon
1962
- "rotate": `<svg ${SVG_ATTRS}><path d="M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/></svg>`,
1972
+ ["rotate" /* Rotate */]: `<svg ${SVG_ATTRS}><path d="M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/></svg>`,
1963
1973
  // Edit: Stift, der über einen Pfad zeichnet
1964
- "edit": `<svg ${SVG_ATTRS}><path d="M12 20h9"/><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z"/></svg>`,
1974
+ ["edit" /* Edit */]: `<svg ${SVG_ATTRS}><path d="M12 20h9"/><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z"/></svg>`,
1965
1975
  // Delete: Mülleimer mit Deckel und Schlitzen
1966
- "delete": `<svg ${SVG_ATTRS}><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/><line x1="10" x2="10" y1="11" y2="17"/><line x1="14" x2="14" y1="11" y2="17"/></svg>`,
1976
+ ["delete" /* Delete */]: `<svg ${SVG_ATTRS}><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/><line x1="10" x2="10" y1="11" y2="17"/><line x1="14" x2="14" y1="11" y2="17"/></svg>`,
1967
1977
  // Off: Durchgestrichener Kreis (Power-Off/Disable Metapher)
1968
- "off": `<svg ${SVG_ATTRS}><circle cx="12" cy="12" r="10"/><path d="m15 9-6 6"/><path d="m9 9 6 6"/></svg>`
1978
+ ["off" /* Off */]: `<svg ${SVG_ATTRS}><circle cx="12" cy="12" r="10"/><path d="m15 9-6 6"/><path d="m9 9 6 6"/></svg>`
1969
1979
  };
1970
1980
  var AnvilControl = class extends L21.Control {
1971
1981
  constructor(anvil, options) {
1972
1982
  super(L21.Util.extend({ position: "topleft" }, options));
1973
1983
  __publicField(this, "_btns", {});
1974
1984
  __publicField(this, "_anvil");
1985
+ __publicField(this, "_options");
1975
1986
  this._anvil = anvil;
1987
+ this._options = { ...options };
1976
1988
  }
1977
1989
  onAdd(map) {
1978
1990
  console.log("AnvilControl: triggering onAdd");
1979
- const container = L21.DomUtil.create("div", "leaflet-bar anvil-toolbar");
1980
- container.style.backgroundColor = "white";
1991
+ const container = L21.DomUtil.create("div", "anvil-toolbar-container");
1981
1992
  container.style.display = "flex";
1982
1993
  container.style.flexDirection = "column";
1983
- const modes = [
1984
- { id: "draw:marker", title: "Marker" },
1985
- { id: "draw:polyline", title: "Line" },
1986
- { id: "draw:polygon", title: "Polygon" },
1987
- { id: "draw:rectangle", title: "Rectangle" },
1988
- { id: "draw:square", title: "Square" },
1989
- { id: "draw:triangle", title: "Triangle" },
1990
- { id: "draw:circle", title: "Circle" },
1991
- { id: "draw:freehand", title: "Freehand" },
1992
- { id: "cut", title: "Cut" },
1993
- { id: "split", title: "Split" },
1994
- { id: "union", title: "Union" },
1995
- { id: "subtract", title: "Subtract" },
1996
- { id: "drag", title: "Drag" },
1997
- { id: "scale", title: "Scale" },
1998
- { id: "rotate", title: "Rotate" },
1999
- { id: "edit", title: "Edit" },
2000
- { id: "delete", title: "Delete" },
2001
- { id: "off", title: "Turn Off" }
1994
+ container.style.gap = "10px";
1995
+ const allModeConfigs = [
1996
+ { id: "marker" /* Marker */, title: "Marker" },
1997
+ { id: "polyline" /* Polyline */, title: "Line" },
1998
+ { id: "polygon" /* Polygon */, title: "Polygon" },
1999
+ { id: "rectangle" /* Rectangle */, title: "Rectangle" },
2000
+ { id: "square" /* Square */, title: "Square" },
2001
+ { id: "triangle" /* Triangle */, title: "Triangle" },
2002
+ { id: "circle" /* Circle */, title: "Circle" },
2003
+ { id: "freehand" /* Freehand */, title: "Freehand" },
2004
+ { id: "cut" /* Cut */, title: "Cut" },
2005
+ { id: "split" /* Split */, title: "Split" },
2006
+ { id: "union" /* Union */, title: "Union" },
2007
+ { id: "subtract" /* Subtract */, title: "Subtract" },
2008
+ { id: "drag" /* Drag */, title: "Drag" },
2009
+ { id: "scale" /* Scale */, title: "Scale" },
2010
+ { id: "rotate" /* Rotate */, title: "Rotate" },
2011
+ { id: "edit" /* Edit */, title: "Edit" },
2012
+ { id: "delete" /* Delete */, title: "Delete" }
2002
2013
  ];
2003
- modes.forEach((mode) => {
2004
- const btn = L21.DomUtil.create("a", "anvil-control-btn", container);
2005
- btn.innerHTML = ICONS[mode.id] || mode.title;
2006
- btn.href = "#";
2007
- btn.title = mode.title;
2008
- btn.style.display = "flex";
2009
- btn.style.alignItems = "center";
2010
- btn.style.justifyContent = "center";
2011
- btn.style.width = "30px";
2012
- btn.style.height = "30px";
2013
- btn.style.color = "#333";
2014
- btn.style.cursor = "pointer";
2015
- L21.DomEvent.disableClickPropagation(btn);
2016
- L21.DomEvent.on(btn, "click", (e) => {
2017
- L21.DomEvent.preventDefault(e);
2018
- if (mode.id === "off") {
2019
- this._anvil.disable();
2020
- } else {
2021
- this._anvil.enable(mode.id);
2022
- }
2014
+ const modesInput = this._options.modes || allModeConfigs.map((m) => m.id);
2015
+ const blocks = Array.isArray(modesInput[0]) ? modesInput : [modesInput];
2016
+ blocks.forEach((block, index) => {
2017
+ const group = L21.DomUtil.create("div", "leaflet-bar anvil-toolbar-group", container);
2018
+ group.style.display = "flex";
2019
+ group.style.flexDirection = "column";
2020
+ group.style.backgroundColor = "white";
2021
+ block.forEach((modeId) => {
2022
+ const config = allModeConfigs.find((c) => c.id === modeId);
2023
+ if (!config && modeId !== "off" /* Off */) return;
2024
+ const id = modeId;
2025
+ const title = config ? config.title : "Turn Off";
2026
+ const btn = L21.DomUtil.create("a", "anvil-control-btn", group);
2027
+ btn.innerHTML = ICONS[id] || title;
2028
+ btn.href = "#";
2029
+ btn.title = title;
2030
+ btn.style.display = "flex";
2031
+ btn.style.alignItems = "center";
2032
+ btn.style.justifyContent = "center";
2033
+ btn.style.width = "30px";
2034
+ btn.style.height = "30px";
2035
+ btn.style.color = "#333";
2036
+ btn.style.cursor = "pointer";
2037
+ L21.DomEvent.disableClickPropagation(btn);
2038
+ L21.DomEvent.on(btn, "click", (e) => {
2039
+ L21.DomEvent.preventDefault(e);
2040
+ if (id === "off" /* Off */) {
2041
+ this._anvil.disable();
2042
+ } else {
2043
+ this._anvil.enable(id);
2044
+ }
2045
+ });
2046
+ this._btns[id] = btn;
2023
2047
  });
2024
- this._btns[mode.id] = btn;
2048
+ if (index === blocks.length - 1 && !this._btns["off" /* Off */]) {
2049
+ const offBtn = L21.DomUtil.create("a", "anvil-control-btn", group);
2050
+ offBtn.innerHTML = ICONS["off" /* Off */];
2051
+ offBtn.href = "#";
2052
+ offBtn.title = "Turn Off";
2053
+ offBtn.style.display = "flex";
2054
+ offBtn.style.alignItems = "center";
2055
+ offBtn.style.justifyContent = "center";
2056
+ offBtn.style.width = "30px";
2057
+ offBtn.style.height = "30px";
2058
+ offBtn.style.color = "#333";
2059
+ offBtn.style.cursor = "pointer";
2060
+ L21.DomEvent.disableClickPropagation(offBtn);
2061
+ L21.DomEvent.on(offBtn, "click", (e) => {
2062
+ L21.DomEvent.preventDefault(e);
2063
+ this._anvil.disable();
2064
+ });
2065
+ this._btns["off" /* Off */] = offBtn;
2066
+ }
2025
2067
  });
2026
2068
  const updateFn = (m) => {
2027
2069
  for (const id in this._btns) {
2028
- const active = id === m || id === "off" && !m;
2070
+ const active = id === m || id === "off" /* Off */ && !m;
2029
2071
  this._btns[id].style.backgroundColor = active ? "#eee" : "#fff";
2030
2072
  }
2031
2073
  };
@@ -2038,6 +2080,99 @@ function anvilControl(anvil, options) {
2038
2080
  return new AnvilControl(anvil, options);
2039
2081
  }
2040
2082
 
2083
+ // src/anvil.ts
2084
+ var Anvil = class {
2085
+ constructor(map, options) {
2086
+ this.map = map;
2087
+ __publicField(this, "modeManager");
2088
+ __publicField(this, "store");
2089
+ __publicField(this, "options");
2090
+ __publicField(this, "control");
2091
+ this.options = {
2092
+ snapping: false,
2093
+ snapDistance: 10,
2094
+ preventSelfIntersection: false,
2095
+ controlPosition: "topleft",
2096
+ ...options
2097
+ };
2098
+ this.store = new LayerStore(map, this.options.layerGroup);
2099
+ this.modeManager = new ModeManager(map);
2100
+ const modesFromOptions = this.options.modes || Object.values(AnvilMode);
2101
+ const flattenedModes = modesFromOptions.flat();
2102
+ if (flattenedModes.includes("polygon" /* Polygon */)) {
2103
+ this.modeManager.addMode("polygon" /* Polygon */, new DrawPolygonMode(this.map, this.options, this.store));
2104
+ }
2105
+ if (flattenedModes.includes("polyline" /* Polyline */)) {
2106
+ this.modeManager.addMode("polyline" /* Polyline */, new DrawPolylineMode(this.map, this.options, this.store));
2107
+ }
2108
+ if (flattenedModes.includes("marker" /* Marker */)) {
2109
+ this.modeManager.addMode("marker" /* Marker */, new DrawMarkerMode(this.map, this.options, this.store));
2110
+ }
2111
+ if (flattenedModes.includes("rectangle" /* Rectangle */)) {
2112
+ this.modeManager.addMode("rectangle" /* Rectangle */, new DrawRectangleMode(this.map, this.options, this.store));
2113
+ }
2114
+ if (flattenedModes.includes("square" /* Square */)) {
2115
+ this.modeManager.addMode("square" /* Square */, new DrawSquareMode(this.map, this.options, this.store));
2116
+ }
2117
+ if (flattenedModes.includes("triangle" /* Triangle */)) {
2118
+ this.modeManager.addMode("triangle" /* Triangle */, new DrawTriangleMode(this.map, this.options, this.store));
2119
+ }
2120
+ if (flattenedModes.includes("circle" /* Circle */)) {
2121
+ this.modeManager.addMode("circle" /* Circle */, new DrawCircleMode(this.map, this.options, this.store));
2122
+ }
2123
+ if (flattenedModes.includes("freehand" /* Freehand */)) {
2124
+ this.modeManager.addMode("freehand" /* Freehand */, new FreehandMode(this.map, this.store, this.options));
2125
+ }
2126
+ if (flattenedModes.includes("cut" /* Cut */)) {
2127
+ this.modeManager.addMode("cut" /* Cut */, new CutMode(map, this.store, this.options));
2128
+ }
2129
+ if (flattenedModes.includes("split" /* Split */)) {
2130
+ this.modeManager.addMode("split" /* Split */, new SplitMode(map, this.store, this.options));
2131
+ }
2132
+ if (flattenedModes.includes("union" /* Union */)) {
2133
+ this.modeManager.addMode("union" /* Union */, new UnionMode(map, this.store, this.options));
2134
+ }
2135
+ if (flattenedModes.includes("subtract" /* Subtract */)) {
2136
+ this.modeManager.addMode("subtract" /* Subtract */, new SubtractMode(map, this.store, this.options));
2137
+ }
2138
+ if (flattenedModes.includes("drag" /* Drag */)) {
2139
+ this.modeManager.addMode("drag" /* Drag */, new DragMode(map, this.store, this.options));
2140
+ }
2141
+ if (flattenedModes.includes("scale" /* Scale */)) {
2142
+ this.modeManager.addMode("scale" /* Scale */, new ScaleMode(map, this.store, this.options));
2143
+ }
2144
+ if (flattenedModes.includes("rotate" /* Rotate */)) {
2145
+ this.modeManager.addMode("rotate" /* Rotate */, new RotateMode(map, this.store, this.options));
2146
+ }
2147
+ if (flattenedModes.includes("edit" /* Edit */)) {
2148
+ this.modeManager.addMode("edit" /* Edit */, new EditMode(this.map, this.store, this.options));
2149
+ }
2150
+ if (flattenedModes.includes("delete" /* Delete */)) {
2151
+ this.modeManager.addMode("delete" /* Delete */, new DeleteMode(map, this.store));
2152
+ }
2153
+ this.map.on(ANVIL_EVENTS.CREATED, (e) => {
2154
+ this.store.addLayer(e.layer);
2155
+ });
2156
+ this.map.on(ANVIL_EVENTS.DELETED, (e) => {
2157
+ this.store.removeLayer(e.layer);
2158
+ });
2159
+ this.control = anvilControl(this, {
2160
+ position: this.options.controlPosition,
2161
+ modes: modesFromOptions
2162
+ });
2163
+ this.control?.addTo(this.map);
2164
+ }
2165
+ enable(mode) {
2166
+ this.modeManager.enable(mode);
2167
+ }
2168
+ disable() {
2169
+ this.modeManager.disable();
2170
+ }
2171
+ getLayerGroup() {
2172
+ return this.store.getGroup();
2173
+ }
2174
+ };
2175
+
2041
2176
  // src/index.ts
2042
2177
  var leaflet = L22;
2043
2178
  leaflet.Anvil = Anvil;
@@ -2050,5 +2185,6 @@ export {
2050
2185
  ANVIL_EVENTS,
2051
2186
  Anvil,
2052
2187
  AnvilControl,
2188
+ AnvilMode,
2053
2189
  anvilControl
2054
2190
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leaflet-anvil",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "A minimal drawing and editing toolkit for Leaflet.",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",