@eox/drawtools 0.8.1 → 0.10.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.
Files changed (35) hide show
  1. package/README.md +4 -0
  2. package/dist/eox-drawtools.js +213 -234
  3. package/dist/eox-drawtools.umd.cjs +32 -32
  4. package/package.json +6 -14
  5. package/src/components/controller.js +187 -0
  6. package/src/components/list.js +189 -0
  7. package/src/enums/draw.js +5 -0
  8. package/src/enums/index.js +9 -0
  9. package/src/enums/stories.js +17 -0
  10. package/src/enums/test.js +7 -0
  11. package/src/helpers/copy-text-to-clipboard.js +13 -0
  12. package/src/helpers/generate-upload-events.js +82 -0
  13. package/src/helpers/index.js +3 -0
  14. package/src/main.js +302 -0
  15. package/src/methods/controller/index.js +3 -0
  16. package/src/methods/controller/update-button-states.js +23 -0
  17. package/src/methods/draw/discard-drawing.js +29 -0
  18. package/src/methods/draw/emit-drawn-features.js +23 -0
  19. package/src/methods/draw/index.js +7 -0
  20. package/src/methods/draw/init-layer.js +83 -0
  21. package/src/methods/draw/on-draw-end.js +22 -0
  22. package/src/methods/draw/start-drawing.js +27 -0
  23. package/src/methods/list/delete-feature.js +24 -0
  24. package/src/methods/list/first-update.js +31 -0
  25. package/src/methods/list/hover-feature.js +17 -0
  26. package/src/methods/list/index.js +6 -0
  27. package/src/methods/list/select-deselect-feature.js +31 -0
  28. package/src/style.eox.js +43 -0
  29. package/test/_mockMap.js +0 -54
  30. package/test/cases/click-discard-btn.js +0 -30
  31. package/test/cases/click-draw-btn.js +0 -27
  32. package/test/cases/copy-geo-json-editor.js +0 -37
  33. package/test/cases/index.js +0 -6
  34. package/test/cases/load-drawtool.js +0 -14
  35. package/test/general.cy.js +0 -39
@@ -0,0 +1,189 @@
1
+ import { LitElement, html, nothing } from "lit";
2
+ import { keyed } from "lit/directives/keyed.js";
3
+ import {
4
+ deleteFeatureMethod,
5
+ firstUpdatedMethod,
6
+ hoverFeatureMethod,
7
+ selectAndDeselectFeatureMethod,
8
+ } from "../methods/list";
9
+ import listStyle from "@eox/elements-utils/styles/dist/list.style";
10
+
11
+ /**
12
+ * Display list of features
13
+ *
14
+ * @element eox-drawtools-list
15
+ */
16
+ export class EOxDrawToolsList extends LitElement {
17
+ static properties = {
18
+ eoxMap: { attribute: false, state: true },
19
+ olMap: { attribute: false, state: true },
20
+ draw: { attribute: false, state: true },
21
+ drawLayer: { attribute: false, state: true },
22
+ drawnFeatures: { attribute: false, state: true, type: Array },
23
+ modify: { attribute: false, state: true },
24
+ unstyled: { type: Boolean },
25
+ };
26
+
27
+ /**
28
+ * @type import("../../../map/src/select").EOxSelectInteraction
29
+ */
30
+ hoverInteraction;
31
+
32
+ /**
33
+ * @type import("../../../map/src/select").EOxSelectInteraction
34
+ */
35
+ clickInteraction;
36
+
37
+ /**
38
+ * @type string | number
39
+ */
40
+ hoverId;
41
+
42
+ /**
43
+ * @type string | number
44
+ */
45
+ clickId;
46
+
47
+ constructor() {
48
+ super();
49
+
50
+ /**
51
+ * @type import("../../../map/main").EOxMap
52
+ */
53
+ this.eoxMap = null;
54
+
55
+ /**
56
+ * @type import("ol").Map
57
+ */
58
+ this.olMap = null;
59
+
60
+ /**
61
+ * The current native OpenLayers `draw` interaction
62
+ * @type import("ol/interaction").Draw
63
+ */
64
+
65
+ this.draw = null;
66
+
67
+ /**
68
+ * The current native OpenLayers draw `layer`
69
+ * @type import("ol/layer").Vector<import("ol/source").Vector>
70
+ */
71
+
72
+ this.drawLayer = null;
73
+
74
+ /**
75
+ * The array of drawn native OpenLayers features. Normally includes only one feature,
76
+ * until multiple feature drawing is enabled.
77
+ *
78
+ * @type Array<import("ol").Feature>
79
+ */
80
+ this.drawnFeatures = [];
81
+
82
+ /**
83
+ * The current native OpenLayers `modify` interaction
84
+ * @type import("ol/interaction").Modify
85
+ */
86
+
87
+ this.modify = null;
88
+
89
+ /**
90
+ * Render the element without additional styles
91
+ */
92
+ this.unstyled = false;
93
+ }
94
+
95
+ /**
96
+ * Handles the deletion of an individual feature.
97
+ *
98
+ * @param {Event & { target: HTMLButtonElement }} evt - Event object containing button target.
99
+ */
100
+ _handleDelete(evt) {
101
+ deleteFeatureMethod(evt, this);
102
+ this.dispatchEvent(new CustomEvent("changed", { bubbles: true }));
103
+ }
104
+
105
+ /**
106
+ * Handles the selection and deselection of a feature from the list.
107
+ *
108
+ * @param {import("ol").Feature} feature - The selected feature.
109
+ */
110
+ _handleFeatureSelectAndDeselect(feature) {
111
+ selectAndDeselectFeatureMethod(feature, this);
112
+ }
113
+
114
+ /**
115
+ *
116
+ * @param {Number} featureId - The ID of the feature to hover.
117
+ * @param {Boolean} mouseOut - Flag indicating mouse out event.
118
+ */
119
+ _handleHoverFeature(featureId, mouseOut = false) {
120
+ hoverFeatureMethod(this, featureId, mouseOut);
121
+ }
122
+
123
+ /**
124
+ * Initiates initial settings and event triggers upon the component's first update.
125
+ */
126
+ firstUpdated() {
127
+ firstUpdatedMethod(this);
128
+ }
129
+
130
+ /**
131
+ * Overrides createRenderRoot to handle shadow DOM.
132
+ */
133
+ createRenderRoot() {
134
+ return this;
135
+ }
136
+
137
+ render() {
138
+ // Update hover and click IDs
139
+ this.hoverId = this.hoverInteraction?.selectedFids[0];
140
+ this.clickId = this.clickInteraction?.selectedFids[0];
141
+
142
+ return html`
143
+ <style>
144
+ ${!this.unstyled && listStyle}
145
+ </style>
146
+ <ul class="list-wrap">
147
+ ${this.drawnFeatures.map((feature, i) => {
148
+ // Determine feature number and ID
149
+ const featureNumber = i + 1;
150
+ const featureId = feature.get("id");
151
+
152
+ // Check if the feature is hovered or clicked
153
+ const isFeatureHovered = this.hoverId === featureId;
154
+ const isFeatureClicked = this.clickId === featureId;
155
+ const isSelected = isFeatureHovered || isFeatureClicked;
156
+ const selectionClass = isSelected ? "selected" : nothing;
157
+
158
+ return keyed(
159
+ featureNumber,
160
+ html`
161
+ <li
162
+ class="${selectionClass}"
163
+ @mouseover=${() => this._handleHoverFeature(featureId)}
164
+ @mouseout=${() => this._handleHoverFeature(featureId, true)}
165
+ >
166
+ <div
167
+ class="list"
168
+ @click="${() =>
169
+ this._handleFeatureSelectAndDeselect(feature)}"
170
+ >
171
+ <span class="title">Feature #${featureNumber}</span>
172
+ <button
173
+ index=${i}
174
+ class="icon smallest discard"
175
+ @click="${this._handleDelete}"
176
+ >
177
+ x
178
+ </button>
179
+ </div>
180
+ </li>
181
+ `,
182
+ );
183
+ })}
184
+ </ul>
185
+ `;
186
+ }
187
+ }
188
+
189
+ customElements.define("eox-drawtools-list", EOxDrawToolsList);
@@ -0,0 +1,5 @@
1
+ export const FIT_OPTIONS = { duration: 750 };
2
+ export const DUMMY_GEO_JSON = {
3
+ type: "FeatureCollection",
4
+ features: [],
5
+ };
@@ -0,0 +1,9 @@
1
+ export {
2
+ STORIES_MAP_STYLE,
3
+ STORIES_DRAW_TYPES,
4
+ STORIES_LAYERS_ARRAY,
5
+ } from "./stories";
6
+
7
+ export { FIT_OPTIONS, DUMMY_GEO_JSON } from "./draw";
8
+
9
+ export { TEST_SELECTORS } from "./test";
@@ -0,0 +1,17 @@
1
+ export const STORIES_LAYERS_ARRAY = [
2
+ {
3
+ type: "Tile",
4
+ source: {
5
+ type: "OSM",
6
+ },
7
+ },
8
+ ];
9
+
10
+ export const STORIES_MAP_STYLE = "width: 400px; height: 300px;";
11
+
12
+ export const STORIES_DRAW_TYPES = [
13
+ { id: "box", type: "Box" },
14
+ { id: "point", type: "Point" },
15
+ { id: "circle", type: "Circle" },
16
+ { id: "linestring", type: "LineString" },
17
+ ];
@@ -0,0 +1,7 @@
1
+ export const TEST_SELECTORS = {
2
+ drawTools: "eox-drawtools",
3
+ controller: "eox-drawtools-controller",
4
+ drawBtn: "[data-cy='drawBtn']",
5
+ copyBtn: "[data-cy='copyBtn']",
6
+ discardBtn: "[data-cy='discardBtn']",
7
+ };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copy a given text string to the clipboard
3
+ *
4
+ * @param {string} text - The text to be copied to the clipboard.
5
+ */
6
+ export default function copyTextToClipboard(text) {
7
+ navigator.clipboard.writeText(text).then(
8
+ function () {},
9
+ function (err) {
10
+ console.error("Could not copy text: ", err);
11
+ },
12
+ );
13
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Initializes upload-related event listeners for drag-and-drop interactions on a map element.
3
+ * It configures the map to react visually to drag-and-drop actions and processes dropped files.
4
+ *
5
+ * @param {import("../main.js").EOxDrawTools} EOxDrawTool - The drawing tool instance.
6
+ * @param {import("@eox/map/main").EOxMap} EOxMap - The EOx Map instance.
7
+ */
8
+ export function initMapDragDropImport(EOxDrawTool, EOxMap) {
9
+ // Helper function to prevent default behavior for drag and drop events
10
+ function preventDefaults(e) {
11
+ e.preventDefault();
12
+ e.stopPropagation();
13
+ }
14
+
15
+ // Changes the opacity of the map element to indicate a drag-and-drop action is in progress
16
+ function highlight(e) {
17
+ e.srcElement.style.opacity = "0.4";
18
+ }
19
+
20
+ // Reverts the opacity of the map element back to normal
21
+ function unHighlight(e) {
22
+ e.srcElement.style.opacity = "1";
23
+ }
24
+
25
+ // Attaches event listeners for drag-and-drop actions
26
+ ["dragenter", "dragover", "dragleave", "drop"].forEach((eventName) => {
27
+ EOxMap.addEventListener(eventName, preventDefaults, false);
28
+ if (["dragenter", "dragover"].includes(eventName))
29
+ EOxMap.addEventListener(eventName, highlight, false);
30
+ else EOxMap.addEventListener(eventName, unHighlight, false);
31
+ });
32
+
33
+ // Handle dropped files specifically
34
+ EOxMap.addEventListener("drop", (e) => handleFiles(e, EOxDrawTool), false);
35
+ }
36
+
37
+ /**
38
+ * Prevents the default action of an event from occurring
39
+ * and stops it from propagating further.
40
+ *
41
+ * @param {Event} e - The event object to be handled.
42
+ */
43
+ function preventDefaults(e) {
44
+ e.preventDefault();
45
+ e.stopPropagation();
46
+ }
47
+
48
+ /**
49
+ * Handles file input from drag-and-drop or file selection events,
50
+ * forwarding each file to a specified processing function.
51
+ *
52
+ * @param {DragEvent | Event} e - The event object from the file input interaction.
53
+ * @param {import("../main.js").EOxDrawTools} EOxDrawTool - The drawing tool instance.
54
+ */
55
+ export function handleFiles(e, EOxDrawTool) {
56
+ preventDefaults(e);
57
+
58
+ // Extract files from the event, supporting both drag-and-drop and file input sources
59
+ const files = e.dataTransfer ? e.dataTransfer.files : e.target.files;
60
+
61
+ Array.from(files).forEach((file) => importFile(file, EOxDrawTool));
62
+ }
63
+
64
+ /**
65
+ * Reads the content of a provided file as text and
66
+ * then triggers a specified handling function
67
+ *
68
+ * @param {File} file - The file object to be read.
69
+ * @param {import("../main.js").EOxDrawTools} EOxDrawTool - The drawing tool instance.
70
+ */
71
+ function importFile(file, EOxDrawTool) {
72
+ const reader = new FileReader();
73
+
74
+ // Initiate reading the file's content as text
75
+ reader.readAsText(file);
76
+
77
+ // Define the onloadend event handler
78
+ reader.onloadend = function () {
79
+ if (typeof reader.result === "string")
80
+ EOxDrawTool.handleFeatureChange(reader.result);
81
+ };
82
+ }
@@ -0,0 +1,3 @@
1
+ // Import and export helper functions
2
+
3
+ export { default as copyTextToClipboard } from "./copy-text-to-clipboard"; // Copy a given text string to the clipboard
package/src/main.js ADDED
@@ -0,0 +1,302 @@
1
+ import { LitElement, html, nothing } from "lit";
2
+ import "./components/list";
3
+ import "./components/controller";
4
+ import { styleEOX } from "./style.eox";
5
+ import {
6
+ startDrawingMethod,
7
+ initLayerMethod,
8
+ discardDrawingMethod,
9
+ emitDrawnFeaturesMethod,
10
+ } from "./methods/draw";
11
+ import mainStyle from "@eox/elements-utils/styles/dist/main.style";
12
+ import { DUMMY_GEO_JSON } from "./enums/index.js";
13
+ import {
14
+ initMapDragDropImport,
15
+ handleFiles,
16
+ } from "./helpers/generate-upload-events.js";
17
+
18
+ /**
19
+ * Manage drawn features on a map
20
+ *
21
+ * @element eox-drawtools
22
+ */
23
+ export class EOxDrawTools extends LitElement {
24
+ // Define properties with defaults and types
25
+ static get properties() {
26
+ return {
27
+ allowModify: { attribute: "allow-modify", type: Boolean },
28
+ for: { type: String },
29
+ currentlyDrawing: { attribute: false, state: true, type: Boolean },
30
+ draw: { attribute: false, state: true },
31
+ drawLayer: { attribute: false, state: true },
32
+ drawnFeatures: { attribute: false, state: true, type: Array },
33
+ modify: { attribute: false, state: true },
34
+ multipleFeatures: { attribute: "multiple-features", type: Boolean },
35
+ importFeatures: { attribute: "import-features", type: Boolean },
36
+ showEditor: { attribute: "show-editor", type: Boolean },
37
+ showList: { attribute: "show-list", type: Boolean },
38
+ noShadow: { type: Boolean },
39
+ type: { type: String },
40
+ unstyled: { type: Boolean },
41
+ };
42
+ }
43
+
44
+ /**
45
+ * @type import("../../map/main").EOxMap
46
+ */
47
+ #eoxMap;
48
+
49
+ /**
50
+ * @type import("ol").Map
51
+ */
52
+ #olMap;
53
+
54
+ /**
55
+ * @type string
56
+ */
57
+ #geoJSON;
58
+
59
+ constructor() {
60
+ super();
61
+
62
+ /**
63
+ * Allow modifying the drawn feature(s)
64
+ */
65
+ this.allowModify = false;
66
+
67
+ /**
68
+ * Query selector of an `eox-map` (`String`, passed as an attribute or property)
69
+ * or an `eox-map` DOM element (`HTMLElement`, passed as property)
70
+ *
71
+ * @type {String|HTMLElement}
72
+ */
73
+ this.for = "eox-map";
74
+
75
+ /**
76
+ * Whether the user is currently in the process of drawing or not
77
+ */
78
+ this.currentlyDrawing = false;
79
+
80
+ /**
81
+ * The current native OpenLayers `draw` interaction
82
+ * @type import("ol/interaction").Draw
83
+ */
84
+ this.draw = null;
85
+
86
+ /**
87
+ * The current native OpenLayers draw `layer`
88
+ * @type import("ol/layer/Vector").default<import("ol/Feature").default>
89
+ */
90
+
91
+ this.drawLayer = null;
92
+
93
+ /**
94
+ * The array of drawn native OpenLayers features. Normally includes only one feature, until multiple feature drawing is enabled.
95
+ * @type Array<import("ol").Feature>
96
+ */
97
+ this.drawnFeatures = [];
98
+
99
+ /**
100
+ * The current native OpenLayers `modify` interaction
101
+ * @type import("ol/interaction").Modify
102
+ */
103
+ this.modify = null;
104
+
105
+ /**
106
+ * Allow adding more than one feature at a time
107
+ */
108
+ this.multipleFeatures = false;
109
+
110
+ /**
111
+ * Allow import features using drag-drop and upload button
112
+ */
113
+ this.importFeatures = false;
114
+
115
+ /**
116
+ * Show geo-json editor for draw tool
117
+ */
118
+ this.showEditor = false;
119
+
120
+ /**
121
+ * Show list of features
122
+ */
123
+ this.showList = false;
124
+
125
+ /**
126
+ * Type of the drawn feature
127
+ * @type {"Polygon" | "Point" | "LineString" | "Circle" | "Box"}
128
+ */
129
+ this.type = "Polygon";
130
+
131
+ /**
132
+ * Render the element without additional styles
133
+ */
134
+ this.unstyled = false;
135
+
136
+ /**
137
+ * Renders the element without a shadow root
138
+ *
139
+ * @type {Boolean}
140
+ */
141
+ this.noShadow = false;
142
+ }
143
+
144
+ /**
145
+ * @onClick Event handler triggered to start drawing on the map.
146
+ */
147
+ handleStartDrawing() {
148
+ startDrawingMethod(this);
149
+ }
150
+
151
+ /**
152
+ * @onClick Event handler triggered to discard/stop drawing
153
+ * on the map and delete the drawn shapes.
154
+ */
155
+ handleDiscardDrawing() {
156
+ discardDrawingMethod(this);
157
+ }
158
+
159
+ /**
160
+ * @param {string} text - The string representation of the features to be parsed.
161
+ * @param {boolean} replaceFeatures - A boolean flag indicating whether to replace the existing features.
162
+ */
163
+ handleFeatureChange(text, replaceFeatures = false) {
164
+ this.eoxMap.parseTextToFeature(
165
+ text || JSON.stringify(DUMMY_GEO_JSON),
166
+ this.drawLayer,
167
+ replaceFeatures,
168
+ );
169
+ }
170
+
171
+ /**
172
+ * @param {DragEvent | Event} evt - The event object from the file input interaction.
173
+ */
174
+ handleFilesChange(evt) {
175
+ handleFiles(evt, this);
176
+ }
177
+
178
+ /**
179
+ * @event onModifyEnd triggered when the modification of a shape is completed.
180
+ */
181
+ onModifyEnd() {
182
+ this.emitDrawnFeatures();
183
+ }
184
+
185
+ /**
186
+ * Update #geoJSON with stringify feature.
187
+ */
188
+ updateGeoJSON() {
189
+ this.#geoJSON = JSON.stringify(
190
+ this.eoxMap.parseFeature(this.drawnFeatures) || DUMMY_GEO_JSON,
191
+ undefined,
192
+ 2,
193
+ );
194
+ }
195
+
196
+ /**
197
+ * Triggers different events when the drawing of a shape is completed.
198
+ */
199
+ emitDrawnFeatures() {
200
+ const drawUpdateEvent = () => {
201
+ /**
202
+ * Fires whenever features are added, modified or discarded, where the event detail
203
+ * is the `drawnFeatures` array
204
+ * @type Array<import("ol").Feature>
205
+ */
206
+ this.dispatchEvent(
207
+ new CustomEvent("drawupdate", { detail: this.drawnFeatures }),
208
+ );
209
+ };
210
+ emitDrawnFeaturesMethod(this, drawUpdateEvent);
211
+ }
212
+
213
+ /**
214
+ * Overrides createRenderRoot to handle shadow DOM creation based on the noShadow property.
215
+ */
216
+ createRenderRoot() {
217
+ return this.noShadow ? this : super.createRenderRoot();
218
+ }
219
+
220
+ /**
221
+ * initializes the EOxMap and OlMap instances
222
+ * And stores them in the private properties #eoxMap and #olMap, respectively.
223
+ * It then calls requestUpdate to trigger a re-render.
224
+ */
225
+ firstUpdated() {
226
+ const { EoxMap, OlMap } = initLayerMethod(this, this.multipleFeatures);
227
+ this.eoxMap = EoxMap;
228
+ this.#olMap = OlMap;
229
+
230
+ if (this.importFeatures) initMapDragDropImport(this, this.eoxMap);
231
+
232
+ this.updateGeoJSON();
233
+ this.requestUpdate();
234
+ }
235
+
236
+ updated(changedProperties) {
237
+ if (changedProperties.has("for")) {
238
+ const { EoxMap, OlMap } = initLayerMethod(this, this.multipleFeatures);
239
+ this.eoxMap = EoxMap;
240
+ this.#olMap = OlMap;
241
+ }
242
+ }
243
+
244
+ get eoxMap() {
245
+ return this.#eoxMap;
246
+ }
247
+
248
+ set eoxMap(value) {
249
+ const oldValue = this.#eoxMap;
250
+ this.#eoxMap = value;
251
+ this.requestUpdate("eoxMap", oldValue);
252
+ }
253
+
254
+ // Render method for UI display
255
+ render() {
256
+ return html`
257
+ <style>
258
+ :host { display: block; }
259
+ ${!this.unstyled && mainStyle}
260
+ ${!this.unstyled && styleEOX}
261
+ </style>
262
+
263
+ <!-- Controller Component -->
264
+ <eox-drawtools-controller
265
+ .drawFunc=${{
266
+ start: () => this.handleStartDrawing(),
267
+ discard: () => this.handleDiscardDrawing(),
268
+ editor: (/** @type {{ target: { value: string; }; }} */ evt) =>
269
+ this.handleFeatureChange(evt.target.value, true),
270
+ import: (/** @type {DragEvent | Event} */ evt) =>
271
+ this.handleFilesChange(evt),
272
+ }}
273
+ .unstyled=${this.unstyled}
274
+ .drawnFeatures=${this.drawnFeatures}
275
+ .currentlyDrawing=${this.currentlyDrawing}
276
+ .multipleFeatures=${this.multipleFeatures}
277
+ .importFeatures=${this.importFeatures}
278
+ .showEditor=${this.showEditor}
279
+ .geoJSON=${this.#geoJSON}
280
+ ></eox-drawtools-controller>
281
+
282
+ <!-- List Component -->
283
+ ${this.showList && this.drawnFeatures?.length
284
+ ? html`<eox-drawtools-list
285
+ .eoxMap=${this.eoxMap}
286
+ .olMap=${this.#olMap}
287
+ .draw=${this.draw}
288
+ .drawLayer=${this.drawLayer}
289
+ .drawnFeatures=${this.drawnFeatures}
290
+ .modify=${this.modify}
291
+ .unstyled=${this.unstyled}
292
+ @changed=${() => {
293
+ this.updateGeoJSON();
294
+ this.requestUpdate();
295
+ }}
296
+ ></eox-drawtools-list>`
297
+ : nothing}
298
+ `;
299
+ }
300
+ }
301
+
302
+ customElements.define("eox-drawtools", EOxDrawTools);
@@ -0,0 +1,3 @@
1
+ // Import and export methods
2
+
3
+ export { default as updateButtonStatesMethod } from "./update-button-states"; // Update disabled state of draw/discard buttons
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Update button state of draw and discard buttons based on specific conditions.
3
+ *
4
+ * @param {import("../../components/controller").EOxDrawToolsController} EoxDrawToolController - The controller instance.
5
+ * @returns {{ drawDisabled: boolean | import("lit").nothing, discardDisabled: boolean | import("lit").nothing }} - The updated button states.
6
+ */
7
+ const updateButtonStatesMethod = (EoxDrawToolController) => {
8
+ // Destructure relevant properties from the controller
9
+ const { multipleFeatures, drawnFeatures, currentlyDrawing } =
10
+ EoxDrawToolController;
11
+
12
+ // Determine draw button's disabled state based on conditions
13
+ const drawDisabled =
14
+ (!multipleFeatures && drawnFeatures?.length > 0) || currentlyDrawing;
15
+
16
+ // Determine discard button's disabled state based on conditions
17
+ const discardDisabled = !drawnFeatures?.length && !currentlyDrawing;
18
+
19
+ // Return the updated button states
20
+ return { drawDisabled, discardDisabled };
21
+ };
22
+
23
+ export default updateButtonStatesMethod;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Discards drawing on the map, resets drawing interactions and features,
3
+ * clears the source, and triggers updates.
4
+ *
5
+ * @param {import("../../main").EOxDrawTools} EoxDrawTool - The drawing tool instance.
6
+ */
7
+ const discardDrawingMethod = (EoxDrawTool) => {
8
+ // Function to discard drawing interactions and features
9
+ const discardDrawingActions = () => {
10
+ // Reset drawnFeatures, deactivate drawing, and clear drawLayer's source
11
+ EoxDrawTool.drawnFeatures = [];
12
+ EoxDrawTool.draw.setActive(false);
13
+ EoxDrawTool.drawLayer.getSource().clear();
14
+ EoxDrawTool.geoJSON = null;
15
+ };
16
+
17
+ // Function to trigger updates after discarding drawing
18
+ const triggerUpdates = () => {
19
+ EoxDrawTool.emitDrawnFeatures();
20
+ EoxDrawTool.currentlyDrawing = false;
21
+ EoxDrawTool.requestUpdate();
22
+ };
23
+
24
+ // Execute the discard actions and trigger updates
25
+ discardDrawingActions();
26
+ triggerUpdates();
27
+ };
28
+
29
+ export default discardDrawingMethod;