@watergis/maplibre-gl-terradraw 0.0.3 → 0.0.5

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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![CI](https://github.com/watergis/maplibre-gl-terradraw/actions/workflows/ci.yml/badge.svg)](https://github.com/watergis/maplibre-gl-terradraw/actions/workflows/ci.yml)
6
6
  [![Netlify Status](https://api.netlify.com/api/v1/badges/3486d35f-7a74-4c1c-a94a-6db0f7250583/deploy-status)](https://app.netlify.com/sites/maplibre-gl-terradraw/deploys)
7
7
 
8
- This plugin is to add controls to your maplibre for sketching powered by [terradraw](https://github.com/JamesLMilner/terra-draw) library.
8
+ This plugin is to add controls to your MapLibre for sketching powered by [Terra Draw](https://github.com/JamesLMilner/terra-draw) library.
9
9
 
10
10
  ## Install
11
11
 
@@ -15,43 +15,7 @@ npm i -D @watergis/maplibre-gl-terradraw
15
15
 
16
16
  ## Usage
17
17
 
18
- ```ts
19
- import { MaplibreTerradrawControl } from '@watergis/maplibre-gl-terradraw';
20
- import '@watergis/maplibre-gl-terradraw/dist/maplibre-gl-terradraw.css';
21
-
22
- map.addControl(
23
- new MaplibreTerradrawControl({
24
- point: true,
25
- line: true,
26
- polygon: true,
27
- rectangle: true,
28
- circle: true,
29
- freehand: true,
30
- angledRectangle: true,
31
- select: true
32
- }),
33
- 'top-left'
34
- );
35
- ```
36
-
37
- As default, all terradraw modes are enabled, you can disable options if you don't want to use them.
38
-
39
- You can get terradraw instance by the following function.
40
-
41
- ```ts
42
- const drawControl = new MaplibreTerradrawControl();
43
- const drawInstance = drawControl.getTerraDrawInstance();
44
- ```
45
-
46
- You can add event listener to subscribe terradraw event as you wish. The below example is to subscribe `select` event of terradraw.
47
-
48
- ```ts
49
- drawInstance.on('select', (id: string) => {
50
- const snapshot = drawInstance.getSnapshot();
51
- const polygon = snapshot?.find((feature) => feature.id === id);
52
- console.log(polygon);
53
- });
54
- ```
18
+ See the [documentation](https://terradraw.water-gis.com/).
55
19
 
56
20
  ## Contribution
57
21
 
@@ -2747,7 +2747,8 @@ const si = {
2747
2747
  circle: !0,
2748
2748
  freehand: !0,
2749
2749
  angledRectangle: !0,
2750
- select: !0
2750
+ select: !0,
2751
+ open: !1
2751
2752
  }, ni = (s) => {
2752
2753
  const t = [];
2753
2754
  return t.push(
@@ -2843,6 +2844,10 @@ const si = {
2843
2844
  ), t;
2844
2845
  };
2845
2846
  class ai {
2847
+ /**
2848
+ * Constructor
2849
+ * @param options Plugin control options
2850
+ */
2846
2851
  constructor(t) {
2847
2852
  k(this, "controlContainer");
2848
2853
  k(this, "map");
@@ -2863,21 +2868,12 @@ class ai {
2863
2868
  const e = ni(this.options);
2864
2869
  if (e.length === 0)
2865
2870
  throw new Error("At least a mode must be enabled.");
2866
- return this.terradraw = new oi({
2871
+ return this.isExpanded = this.options.open === !0, this.terradraw = new oi({
2867
2872
  adapter: new pe({ map: t }),
2868
2873
  modes: e
2869
- }), this.terradraw.start(), this.controlContainer = document.createElement("div"), this.controlContainer.classList.add("maplibregl-ctrl"), this.controlContainer.classList.add("maplibregl-ctrl-group"), this.addButton = document.createElement("button"), this.addButton.classList.add("maplibregl-terradraw-add-button"), this.addButton.type = "button", this.addButton.addEventListener("click", () => {
2870
- var o, n;
2871
- if (!this.terradraw) return;
2872
- const i = document.getElementsByClassName("maplibregl-terradraw-add-control");
2873
- for (let r = 0; r < i.length; r++) {
2874
- const a = i.item(r);
2875
- a && (this.isExpanded ? (a.classList.add("hidden"), (o = this.addButton) == null || o.classList.remove("enabled"), this.resetActiveMode()) : (a.classList.remove("hidden"), (n = this.addButton) == null || n.classList.add("enabled")));
2876
- }
2877
- this.isExpanded = !this.isExpanded;
2878
- }), e.forEach((i) => {
2874
+ }), this.terradraw.start(), this.controlContainer = document.createElement("div"), this.controlContainer.classList.add("maplibregl-ctrl"), this.controlContainer.classList.add("maplibregl-ctrl-group"), this.addButton = document.createElement("button"), this.addButton.classList.add("maplibregl-terradraw-add-button"), this.isExpanded && this.addButton.classList.add("enabled"), this.addButton.type = "button", this.addButton.addEventListener("click", this.toggleEditor.bind(this)), e.forEach((i) => {
2879
2875
  i.mode !== "render" && this.addTerradrawButton(i.mode);
2880
- }), this.deleteButton = document.createElement("button"), this.deleteButton.classList.add("maplibregl-terradraw-add-control"), this.deleteButton.classList.add("maplibregl-terradraw-delete-button"), this.deleteButton.classList.add("hidden"), this.deleteButton.type = "button", this.deleteButton.addEventListener("click", () => {
2876
+ }), this.deleteButton = document.createElement("button"), this.deleteButton.classList.add("maplibregl-terradraw-add-control"), this.deleteButton.classList.add("maplibregl-terradraw-delete-button"), this.isExpanded || this.deleteButton.classList.add("hidden"), this.deleteButton.type = "button", this.deleteButton.addEventListener("click", () => {
2881
2877
  this.terradraw && this.terradraw.enabled && (this.terradraw.clear(), this.deactivate());
2882
2878
  }), this.controlContainer.appendChild(this.addButton), Object.values(this.modeButtons).forEach((i) => {
2883
2879
  var o;
@@ -2887,15 +2883,42 @@ class ai {
2887
2883
  onRemove() {
2888
2884
  !this.controlContainer || !this.controlContainer.parentNode || !this.map || !this.addButton || (this.modeButtons = {}, this.terradraw = void 0, this.map = void 0);
2889
2885
  }
2886
+ /**
2887
+ * Activate Terra Draw to start drawing
2888
+ */
2890
2889
  activate() {
2891
2890
  this.terradraw && (this.terradraw.enabled || this.terradraw.start());
2892
2891
  }
2892
+ /**
2893
+ * Deactivate Terra Draw to stop drawing
2894
+ */
2893
2895
  deactivate() {
2894
2896
  this.terradraw && this.terradraw.enabled && (this.resetActiveMode(), this.terradraw.stop());
2895
2897
  }
2898
+ /**
2899
+ * Get the Terra Draw instance.
2900
+ * For the Terra Draw API, please refer to https://terradraw.io/#/api
2901
+ * @returns Terra Draw instance
2902
+ */
2896
2903
  getTerraDrawInstance() {
2897
2904
  return this.terradraw;
2898
2905
  }
2906
+ /**
2907
+ * Toggle editor control
2908
+ */
2909
+ toggleEditor() {
2910
+ var e, i;
2911
+ if (!this.terradraw) return;
2912
+ const t = document.getElementsByClassName("maplibregl-terradraw-add-control");
2913
+ for (let o = 0; o < t.length; o++) {
2914
+ const n = t.item(o);
2915
+ n && (this.isExpanded ? n.classList.add("hidden") : n.classList.remove("hidden"));
2916
+ }
2917
+ this.isExpanded ? ((e = this.addButton) == null || e.classList.remove("enabled"), this.resetActiveMode()) : (i = this.addButton) == null || i.classList.add("enabled"), this.isExpanded = !this.isExpanded;
2918
+ }
2919
+ /**
2920
+ * Reset active mode to back to render mode
2921
+ */
2899
2922
  resetActiveMode() {
2900
2923
  var e;
2901
2924
  if (!this.terradraw) return;
@@ -2907,9 +2930,13 @@ class ai {
2907
2930
  }
2908
2931
  this.activeMode = "render", (e = this.terradraw) == null || e.setMode("render");
2909
2932
  }
2933
+ /**
2934
+ * Add Terra Draw drawing mode button
2935
+ * @param mode Terra Draw mode name
2936
+ */
2910
2937
  addTerradrawButton(t) {
2911
2938
  const e = document.createElement("button");
2912
- e.classList.add("maplibregl-terradraw-add-control"), e.classList.add(`maplibregl-terradraw-add-${t}-button`), e.classList.add("hidden"), e.type = "button", e.addEventListener("click", () => {
2939
+ e.classList.add("maplibregl-terradraw-add-control"), e.classList.add(`maplibregl-terradraw-add-${t}-button`), this.isExpanded || e.classList.add("hidden"), e.type = "button", e.addEventListener("click", () => {
2913
2940
  if (!this.terradraw) return;
2914
2941
  const i = e.classList.contains("active");
2915
2942
  this.activate(), this.resetActiveMode(), i || (this.terradraw.setMode(t), this.activeMode = t, e.classList.add("active"));