@eox/drawtools 0.9.0 → 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 +81 -81
  3. package/dist/eox-drawtools.umd.cjs +9 -9
  4. package/package.json +5 -13
  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
package/package.json CHANGED
@@ -1,14 +1,9 @@
1
1
  {
2
2
  "name": "@eox/drawtools",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "devDependencies": {
6
- "@eox/eslint-config": "^1.0.0",
7
- "@typescript-eslint/eslint-plugin": "^7.0.1",
8
- "@typescript-eslint/parser": "^7.0.1",
9
- "eslint-plugin-typescript": "^0.14.0",
10
6
  "ol": "^10.0.0",
11
- "typescript": "^5.2.2",
12
7
  "vite": "^5.0.2"
13
8
  },
14
9
  "engines": {
@@ -17,18 +12,15 @@
17
12
  },
18
13
  "files": [
19
14
  "dist",
20
- "test"
15
+ "src"
21
16
  ],
22
- "main": "./dist/eox-drawtools.js",
17
+ "main": "./src/main.js",
23
18
  "scripts": {
24
19
  "build": "vite build",
25
- "watch": "vite build --watch",
26
- "format": "prettier --write .",
27
- "lint": "eslint --ext .js,.ts .",
28
- "lint:fix": "eslint --ext .js,.ts . --fix",
29
- "typecheck": "tsc"
20
+ "watch": "vite build --watch"
30
21
  },
31
22
  "dependencies": {
23
+ "@eox/elements-utils": "^0.0.1",
32
24
  "lit": "^3.0.2"
33
25
  }
34
26
  }
@@ -0,0 +1,187 @@
1
+ import { LitElement, html, nothing } from "lit";
2
+ import { updateButtonStatesMethod } from "../methods/controller";
3
+ import buttonStyle from "@eox/elements-utils/styles/dist/button.style";
4
+ import inputStyle from "@eox/elements-utils/styles/dist/input.style";
5
+ import { copyTextToClipboard } from "../helpers/index.js";
6
+ import { when } from "lit/directives/when.js";
7
+
8
+ /**
9
+ * Controller component for drawing features
10
+ *
11
+ * @element eox-drawtools-controller
12
+ */
13
+ export class EOxDrawToolsController extends LitElement {
14
+ // Define properties with default values and types
15
+ static properties = {
16
+ multipleFeatures: { attribute: false, type: Boolean },
17
+ drawnFeatures: { attribute: false, state: true, type: Array },
18
+ currentlyDrawing: { attribute: false, state: true, type: Boolean },
19
+ drawFunc: { attribute: false, type: Object },
20
+ importFeatures: { attribute: "import-features", type: Boolean },
21
+ showEditor: { attribute: "show-editor", type: Boolean },
22
+ geoJSON: { attribute: "geo-json", type: String },
23
+ unstyled: { type: Boolean },
24
+ };
25
+
26
+ /**
27
+ * @type boolean | import("lit").nothing
28
+ */
29
+ #drawDisabled = true;
30
+
31
+ /**
32
+ * @type boolean | import("lit").nothing
33
+ */
34
+ #discardDisabled = true;
35
+
36
+ constructor() {
37
+ super();
38
+
39
+ /**
40
+ * Allow adding more than one feature at a time
41
+ */
42
+ this.multipleFeatures = false;
43
+
44
+ /**
45
+ * The array of drawn native OpenLayers features. Normally includes only one feature,
46
+ * until multiple feature drawing is enabled.
47
+ *
48
+ * @type Array<import("ol").Feature>
49
+ */
50
+ this.drawnFeatures = [];
51
+
52
+ /**
53
+ * Allow import features using drag-drop and upload button
54
+ * @type Boolean
55
+ */
56
+ this.importFeatures = false;
57
+
58
+ /**
59
+ * Show geo-json editor for draw tool
60
+ * @type Boolean
61
+ */
62
+ this.showEditor = false;
63
+
64
+ /**
65
+ * Whether the user is currently in the process of drawing or not
66
+ */
67
+ this.currentlyDrawing = false;
68
+
69
+ /**
70
+ * List of draw function object like start, discard etc.
71
+ */
72
+ this.drawFunc = null;
73
+
74
+ /**
75
+ * Parsed and Stringify Geo JSON
76
+ * @type String
77
+ */
78
+ this.geoJSON = "";
79
+
80
+ /**
81
+ * Render the element without additional styles
82
+ */
83
+ this.unstyled = false;
84
+ }
85
+
86
+ /**
87
+ * Updates button states based on conditions
88
+ */
89
+ updateButtonStates() {
90
+ const { drawDisabled, discardDisabled } = updateButtonStatesMethod(this);
91
+
92
+ this.#drawDisabled = drawDisabled;
93
+ this.#discardDisabled = discardDisabled;
94
+ }
95
+
96
+ /**
97
+ * Overrides createRenderRoot to handle shadow DOM
98
+ */
99
+ createRenderRoot() {
100
+ return this;
101
+ }
102
+
103
+ render() {
104
+ this.updateButtonStates();
105
+ const drawLabel = this.currentlyDrawing ? "drawing" : "draw";
106
+
107
+ return html`
108
+ <style>
109
+ ${!this.unstyled && buttonStyle}
110
+ ${!this.unstyled && inputStyle}
111
+ </style>
112
+ <div class="wrap-btn">
113
+ <slot></slot>
114
+ <div>
115
+ <!-- Draw Button -->
116
+ <button
117
+ data-cy="drawBtn"
118
+ class="polygon icon"
119
+ ?disabled="${this.#drawDisabled || nothing}"
120
+ @click="${() => this.drawFunc.start()}"
121
+ >
122
+ ${drawLabel}
123
+ </button>
124
+
125
+ <!-- Discard Button -->
126
+ <button
127
+ data-cy="discardBtn"
128
+ class="discard icon"
129
+ ?disabled="${this.#discardDisabled || nothing}"
130
+ @click="${() => this.drawFunc.discard()}"
131
+ >
132
+ discard
133
+ </button>
134
+ </div>
135
+
136
+ <!-- Import Button -->
137
+ ${when(
138
+ this.importFeatures,
139
+ () => html`
140
+ <!-- Import Input Field : Hidden -->
141
+ <input
142
+ type="file"
143
+ id="import-file"
144
+ style="display: none;"
145
+ @change=${this.drawFunc.import}
146
+ />
147
+
148
+ <!-- Main Import Button -->
149
+ <button
150
+ data-cy="importBtn"
151
+ class="import icon"
152
+ @click=${() => this.querySelector("#import-file").click()}
153
+ >
154
+ import
155
+ </button>
156
+ `,
157
+ )}
158
+ </div>
159
+
160
+ <!-- Geo JSON Wrapper -->
161
+ ${when(
162
+ this.showEditor,
163
+ () => html`
164
+ <div class="json-wrapper">
165
+ <!-- Geo JSON Editor -->
166
+ <textarea
167
+ @drop=${this.drawFunc.import}
168
+ @input=${this.drawFunc.editor}
169
+ .value=${this.geoJSON}
170
+ ></textarea>
171
+
172
+ <!-- Geo JSON Copy Button -->
173
+ <button
174
+ data-cy="copyBtn"
175
+ class="icon-copy"
176
+ @click=${() => copyTextToClipboard(this.geoJSON)}
177
+ >
178
+ ${this.unstyled ? "copy" : nothing}
179
+ </button>
180
+ </div>
181
+ `,
182
+ )}
183
+ `;
184
+ }
185
+ }
186
+
187
+ customElements.define("eox-drawtools-controller", EOxDrawToolsController);
@@ -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