Skip to content

Commit b7640d2

Browse files
committed
feat(hotspot): refactor code where needed pd-597
1 parent 772762e commit b7640d2

3 files changed

Lines changed: 67 additions & 58 deletions

File tree

packages/hotspot/configure/src/DeleteWidget.jsx

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,11 @@ import PropTypes from 'prop-types';
33
import { Group } from 'react-konva';
44
import { faDelete } from './icons';
55
import Image from './image';
6-
7-
function calculate(polygonPoints) {
8-
let minX = polygonPoints[0].x;
9-
let minY = polygonPoints[0].y;
10-
let maxX = polygonPoints[0].x;
11-
let maxY = polygonPoints[0].y;
12-
13-
polygonPoints.forEach((point) => {
14-
if (point.x < minX) minX = point.x;
15-
if (point.x > maxX) maxX = point.x;
16-
if (point.y < minY) minY = point.y;
17-
if (point.y > maxY) maxY = point.y;
18-
});
19-
20-
// Find a suitable position for the text element within the polygon
21-
let textX, textY;
22-
23-
for (let x = minX; x <= maxX - 20; x++) {
24-
for (let y = maxY - 20; y > minY; y--) {
25-
// Check if the text element's position (x, y) is within the polygon
26-
if (isPointInsidePolygon(polygonPoints, x, y)) {
27-
textX = x - 10;
28-
textY = y;
29-
break;
30-
}
31-
}
32-
}
33-
34-
return { x: textX, y: textY };
35-
}
36-
37-
function isPointInsidePolygon(polygon, x, y) {
38-
let inside = false;
39-
40-
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
41-
const xi = polygon[i].x;
42-
const yi = polygon[i].y;
43-
const xj = polygon[j].x;
44-
const yj = polygon[j].y;
45-
46-
const intersect = yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
47-
48-
if (intersect) {
49-
inside = !inside;
50-
}
51-
}
52-
53-
return inside;
54-
}
6+
import { calculate } from './utils';
557

568
const DeleteWidget = ({ height, id, width, x, y, points, outlineColor, handleWidgetClick }) => {
579
let positionX, positionY;
10+
// if points exist we have an irregular form (polygon) and position should be computed
5811
if (points) {
5912
const { x, y } = calculate(points);
6013
positionX = x;

packages/hotspot/configure/src/hotspot-polygon.jsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,11 @@ class PolComponent extends React.Component {
132132
return acc;
133133
}, []);
134134

135-
if (updateModel) {
136-
this.setState({ points: newPoints, ...this.getOffset(newPoints), isDragging: false });
137-
onDragEnd(id, { points: newPoints });
138-
} else {
139-
this.setState({ points: newPoints, ...this.getOffset(newPoints) });
140-
}
135+
this.setState({
136+
points: newPoints,
137+
...this.getOffset(newPoints),
138+
isDragging: updateModel ? false : this.state.isDragging,
139+
});
141140
};
142141

143142
handleOnDragVertex = (e, changedIndex, updateModel) => {
@@ -148,11 +147,14 @@ class PolComponent extends React.Component {
148147
index === changedIndex ? { x: e.target.x(), y: e.target.y() } : point,
149148
);
150149

150+
this.setState({
151+
points: newPoints,
152+
...this.getOffset(newPoints),
153+
isDragging: updateModel ? false : this.state.isDragging,
154+
});
155+
151156
if (updateModel) {
152157
onDragEnd(id, { points: newPoints });
153-
this.setState({ points: newPoints, ...this.getOffset(newPoints), isDragging: false });
154-
} else {
155-
this.setState({ points: newPoints, ...this.getOffset(newPoints) });
156158
}
157159
};
158160

packages/hotspot/configure/src/utils.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,58 @@ const groupShapes = (shapesArray) => {
114114
return cloneDeep(shapesMap);
115115
};
116116

117+
const isPointInsidePolygon = (polygon, x, y) => {
118+
let inside = false;
119+
120+
if (!polygon || polygon.length <= 0) {
121+
return inside;
122+
}
123+
124+
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
125+
const xi = polygon[i].x;
126+
const yi = polygon[i].y;
127+
const xj = polygon[j].x;
128+
const yj = polygon[j].y;
129+
130+
const intersect = yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
131+
132+
if (intersect) {
133+
inside = !inside;
134+
}
135+
}
136+
137+
return inside;
138+
};
139+
140+
const calculate = (polygonPoints) => {
141+
if (!polygonPoints || polygonPoints.length <= 0) {
142+
return { x: 0, y: 0 };
143+
}
144+
145+
const xPoints = polygonPoints.map((point) => point.x);
146+
const yPoints = polygonPoints.map((point) => point.y);
147+
const minX = Math.min(...xPoints);
148+
const minY = Math.min(...yPoints);
149+
const maxX = Math.max(...xPoints);
150+
const maxY = Math.max(...yPoints);
151+
152+
// Find a suitable position for the text element within the polygon
153+
let textX, textY;
154+
155+
for (let x = minX; x <= maxX - 20; x++) {
156+
for (let y = maxY - 20; y > minY; y--) {
157+
// Check if the text element's position (x, y) is within the polygon
158+
if (isPointInsidePolygon(polygonPoints, x, y)) {
159+
textX = x - 10;
160+
textY = y;
161+
break;
162+
}
163+
}
164+
}
165+
166+
return { x: textX, y: textY };
167+
};
168+
117169
const generateValidationMessage = (config) => {
118170
const { minShapes, maxShapes, maxSelections } = config;
119171

@@ -133,6 +185,8 @@ const generateValidationMessage = (config) => {
133185
};
134186

135187
export {
188+
calculate,
189+
isPointInsidePolygon,
136190
updateImageDimensions,
137191
generateValidationMessage,
138192
getUpdatedShapes,

0 commit comments

Comments
 (0)