@pie-element/hotspot 11.1.2-next.5 → 11.1.3

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 (234) hide show
  1. package/CHANGELOG.json +997 -0
  2. package/CHANGELOG.md +2226 -0
  3. package/LICENSE.md +5 -0
  4. package/README.md +1 -0
  5. package/configure/CHANGELOG.json +682 -0
  6. package/configure/CHANGELOG.md +1963 -0
  7. package/configure/lib/DeleteWidget.js +64 -0
  8. package/configure/lib/DeleteWidget.js.map +1 -0
  9. package/configure/lib/button.js +42 -0
  10. package/configure/lib/button.js.map +1 -0
  11. package/configure/lib/buttons/circle.js +33 -0
  12. package/configure/lib/buttons/circle.js.map +1 -0
  13. package/configure/lib/buttons/polygon.js +39 -0
  14. package/configure/lib/buttons/polygon.js.map +1 -0
  15. package/configure/lib/buttons/rectangle.js +39 -0
  16. package/configure/lib/buttons/rectangle.js.map +1 -0
  17. package/configure/lib/defaults.js +155 -0
  18. package/configure/lib/defaults.js.map +1 -0
  19. package/configure/lib/hotspot-circle.js +192 -0
  20. package/configure/lib/hotspot-circle.js.map +1 -0
  21. package/configure/lib/hotspot-container.js +320 -0
  22. package/configure/lib/hotspot-container.js.map +1 -0
  23. package/configure/lib/hotspot-drawable.js +519 -0
  24. package/configure/lib/hotspot-drawable.js.map +1 -0
  25. package/configure/lib/hotspot-palette.js +107 -0
  26. package/configure/lib/hotspot-palette.js.map +1 -0
  27. package/configure/lib/hotspot-polygon.js +293 -0
  28. package/configure/lib/hotspot-polygon.js.map +1 -0
  29. package/configure/lib/hotspot-rectangle.js +190 -0
  30. package/configure/lib/hotspot-rectangle.js.map +1 -0
  31. package/configure/lib/icons.js +7 -0
  32. package/configure/lib/icons.js.map +1 -0
  33. package/configure/lib/image-konva.js +66 -0
  34. package/configure/lib/image-konva.js.map +1 -0
  35. package/configure/lib/index.js +194 -0
  36. package/configure/lib/index.js.map +1 -0
  37. package/configure/lib/root.js +330 -0
  38. package/configure/lib/root.js.map +1 -0
  39. package/configure/lib/shapes/circle.js +84 -0
  40. package/configure/lib/shapes/circle.js.map +1 -0
  41. package/configure/lib/shapes/index.js +50 -0
  42. package/configure/lib/shapes/index.js.map +1 -0
  43. package/configure/lib/shapes/polygon.js +82 -0
  44. package/configure/lib/shapes/polygon.js.map +1 -0
  45. package/configure/lib/shapes/rectagle.js +84 -0
  46. package/configure/lib/shapes/rectagle.js.map +1 -0
  47. package/configure/lib/shapes/utils.js +21 -0
  48. package/configure/lib/shapes/utils.js.map +1 -0
  49. package/configure/lib/upload-control.js +41 -0
  50. package/configure/lib/upload-control.js.map +1 -0
  51. package/configure/lib/utils.js +185 -0
  52. package/configure/lib/utils.js.map +1 -0
  53. package/configure/package.json +26 -0
  54. package/configure/src/DeleteWidget.jsx +51 -0
  55. package/configure/src/__tests__/DeleteWidget.test.jsx +366 -0
  56. package/configure/src/__tests__/button.test.jsx +198 -0
  57. package/configure/src/__tests__/hotspot-circle.test.jsx +259 -0
  58. package/configure/src/__tests__/hotspot-container.test.js +366 -0
  59. package/configure/src/__tests__/hotspot-drawable.test.js +271 -0
  60. package/configure/src/__tests__/hotspot-palette.test.jsx +71 -0
  61. package/configure/src/__tests__/image-konva.test.jsx +226 -0
  62. package/configure/src/__tests__/index.test.js +329 -0
  63. package/configure/src/__tests__/root.test.js +400 -0
  64. package/configure/src/__tests__/utils.test.js +241 -0
  65. package/configure/src/button.jsx +35 -0
  66. package/configure/src/buttons/circle.jsx +18 -0
  67. package/configure/src/buttons/polygon.jsx +29 -0
  68. package/configure/src/buttons/rectangle.jsx +29 -0
  69. package/configure/src/defaults.js +109 -0
  70. package/configure/src/hotspot-circle.jsx +183 -0
  71. package/configure/src/hotspot-container.jsx +330 -0
  72. package/configure/src/hotspot-drawable.jsx +527 -0
  73. package/configure/src/hotspot-palette.jsx +90 -0
  74. package/configure/src/hotspot-polygon.jsx +294 -0
  75. package/configure/src/hotspot-rectangle.jsx +169 -0
  76. package/configure/src/icons.js +5 -0
  77. package/configure/src/image-konva.jsx +63 -0
  78. package/configure/src/index.js +208 -0
  79. package/configure/src/root.jsx +346 -0
  80. package/configure/src/shapes/circle.js +81 -0
  81. package/configure/src/shapes/index.js +4 -0
  82. package/configure/src/shapes/polygon.js +81 -0
  83. package/configure/src/shapes/rectagle.js +82 -0
  84. package/configure/src/shapes/utils.js +16 -0
  85. package/configure/src/upload-control.jsx +33 -0
  86. package/configure/src/utils.js +210 -0
  87. package/controller/CHANGELOG.json +362 -0
  88. package/controller/CHANGELOG.md +1304 -0
  89. package/controller/lib/defaults.js +33 -0
  90. package/controller/lib/defaults.js.map +1 -0
  91. package/controller/lib/index.js +341 -0
  92. package/controller/lib/index.js.map +1 -0
  93. package/controller/lib/utils.js +32 -0
  94. package/controller/lib/utils.js.map +1 -0
  95. package/controller/package.json +18 -0
  96. package/controller/src/__tests__/index.test.js +419 -0
  97. package/controller/src/__tests__/utils.test.js +5 -0
  98. package/controller/src/defaults.js +19 -0
  99. package/controller/src/index.js +328 -0
  100. package/controller/src/utils.js +29 -0
  101. package/docs/config-schema.json +2023 -0
  102. package/docs/config-schema.json.md +1495 -0
  103. package/docs/demo/config.js +8 -0
  104. package/docs/demo/generate.js +118 -0
  105. package/docs/demo/index.html +1 -0
  106. package/docs/demo/session.js +11 -0
  107. package/docs/pie-schema.json +1204 -0
  108. package/docs/pie-schema.json.md +851 -0
  109. package/lib/hotspot/circle.js +156 -0
  110. package/lib/hotspot/circle.js.map +1 -0
  111. package/lib/hotspot/container.js +206 -0
  112. package/lib/hotspot/container.js.map +1 -0
  113. package/lib/hotspot/icons.js +8 -0
  114. package/lib/hotspot/icons.js.map +1 -0
  115. package/lib/hotspot/image-konva-tooltip.js +86 -0
  116. package/lib/hotspot/image-konva-tooltip.js.map +1 -0
  117. package/lib/hotspot/index.js +163 -0
  118. package/lib/hotspot/index.js.map +1 -0
  119. package/lib/hotspot/polygon.js +203 -0
  120. package/lib/hotspot/polygon.js.map +1 -0
  121. package/lib/hotspot/rectangle.js +175 -0
  122. package/lib/hotspot/rectangle.js.map +1 -0
  123. package/lib/index.js +213 -0
  124. package/lib/index.js.map +1 -0
  125. package/lib/session-updater.js +42 -0
  126. package/lib/session-updater.js.map +1 -0
  127. package/package.json +18 -83
  128. package/src/__tests__/container.test.jsx +58 -0
  129. package/src/__tests__/index.test.js +123 -0
  130. package/src/__tests__/session-updater.test.jsx +69 -0
  131. package/src/hotspot/__tests__/circle.test.jsx +464 -0
  132. package/src/hotspot/__tests__/container.test.jsx +546 -0
  133. package/src/hotspot/__tests__/image-konva-tooltip.test.jsx +510 -0
  134. package/src/hotspot/__tests__/polygon.test.jsx +502 -0
  135. package/src/hotspot/__tests__/rectangle.test.jsx +418 -0
  136. package/src/hotspot/circle.jsx +152 -0
  137. package/src/hotspot/container.jsx +217 -0
  138. package/src/hotspot/icons.js +7 -0
  139. package/src/hotspot/image-konva-tooltip.jsx +76 -0
  140. package/src/hotspot/index.jsx +165 -0
  141. package/src/hotspot/polygon.jsx +195 -0
  142. package/src/hotspot/rectangle.jsx +171 -0
  143. package/src/index.js +226 -0
  144. package/src/session-updater.js +29 -0
  145. package/configure.js +0 -2
  146. package/controller.js +0 -1
  147. package/dist/author/DeleteWidget.d.ts +0 -38
  148. package/dist/author/DeleteWidget.js +0 -46
  149. package/dist/author/button.d.ts +0 -31
  150. package/dist/author/button.js +0 -27
  151. package/dist/author/buttons/circle.d.ts +0 -18
  152. package/dist/author/buttons/circle.js +0 -25
  153. package/dist/author/buttons/polygon.d.ts +0 -18
  154. package/dist/author/buttons/polygon.js +0 -36
  155. package/dist/author/buttons/rectangle.d.ts +0 -18
  156. package/dist/author/buttons/rectangle.js +0 -36
  157. package/dist/author/defaults.d.ts +0 -157
  158. package/dist/author/defaults.js +0 -119
  159. package/dist/author/hotspot-circle.d.ts +0 -21
  160. package/dist/author/hotspot-circle.js +0 -124
  161. package/dist/author/hotspot-container.d.ts +0 -29
  162. package/dist/author/hotspot-container.js +0 -210
  163. package/dist/author/hotspot-drawable.d.ts +0 -31
  164. package/dist/author/hotspot-drawable.js +0 -312
  165. package/dist/author/hotspot-palette.d.ts +0 -14
  166. package/dist/author/hotspot-palette.js +0 -72
  167. package/dist/author/hotspot-polygon.d.ts +0 -38
  168. package/dist/author/hotspot-polygon.js +0 -200
  169. package/dist/author/hotspot-rectangle.d.ts +0 -20
  170. package/dist/author/hotspot-rectangle.js +0 -119
  171. package/dist/author/icons.d.ts +0 -9
  172. package/dist/author/icons.js +0 -4
  173. package/dist/author/image-konva.d.ts +0 -19
  174. package/dist/author/image-konva.js +0 -49
  175. package/dist/author/index.d.ts +0 -52
  176. package/dist/author/index.js +0 -143
  177. package/dist/author/root.d.ts +0 -15
  178. package/dist/author/root.js +0 -215
  179. package/dist/author/shapes/circle.d.ts +0 -18
  180. package/dist/author/shapes/circle.js +0 -47
  181. package/dist/author/shapes/index.d.ts +0 -12
  182. package/dist/author/shapes/polygon.d.ts +0 -19
  183. package/dist/author/shapes/polygon.js +0 -51
  184. package/dist/author/shapes/rectagle.d.ts +0 -18
  185. package/dist/author/shapes/rectagle.js +0 -57
  186. package/dist/author/shapes/utils.d.ts +0 -19
  187. package/dist/author/shapes/utils.js +0 -16
  188. package/dist/author/upload-control.d.ts +0 -29
  189. package/dist/author/upload-control.js +0 -28
  190. package/dist/author/utils.d.ts +0 -24
  191. package/dist/author/utils.js +0 -83
  192. package/dist/browser/ReactKonva-CFo7dxdy.js +0 -19336
  193. package/dist/browser/ReactKonva-CFo7dxdy.js.map +0 -1
  194. package/dist/browser/author/index.js +0 -47549
  195. package/dist/browser/author/index.js.map +0 -1
  196. package/dist/browser/browser-CfnAFove.js +0 -219
  197. package/dist/browser/browser-CfnAFove.js.map +0 -1
  198. package/dist/browser/controller/index.js +0 -198
  199. package/dist/browser/controller/index.js.map +0 -1
  200. package/dist/browser/delivery/index.js +0 -2460
  201. package/dist/browser/delivery/index.js.map +0 -1
  202. package/dist/browser/dist-CeB-1djc.js +0 -100
  203. package/dist/browser/dist-CeB-1djc.js.map +0 -1
  204. package/dist/browser/hotspot.css +0 -2
  205. package/dist/controller/defaults.d.ts +0 -35
  206. package/dist/controller/defaults.js +0 -29
  207. package/dist/controller/index.d.ts +0 -22
  208. package/dist/controller/index.js +0 -154
  209. package/dist/controller/utils.d.ts +0 -10
  210. package/dist/controller/utils.js +0 -12
  211. package/dist/delivery/hotspot/circle.d.ts +0 -19
  212. package/dist/delivery/hotspot/circle.js +0 -100
  213. package/dist/delivery/hotspot/container.d.ts +0 -16
  214. package/dist/delivery/hotspot/container.js +0 -150
  215. package/dist/delivery/hotspot/icons.d.ts +0 -10
  216. package/dist/delivery/hotspot/icons.js +0 -4
  217. package/dist/delivery/hotspot/image-konva-tooltip.d.ts +0 -19
  218. package/dist/delivery/hotspot/image-konva-tooltip.js +0 -66
  219. package/dist/delivery/hotspot/index.d.ts +0 -17
  220. package/dist/delivery/hotspot/index.js +0 -114
  221. package/dist/delivery/hotspot/polygon.d.ts +0 -21
  222. package/dist/delivery/hotspot/polygon.js +0 -108
  223. package/dist/delivery/hotspot/rectangle.d.ts +0 -19
  224. package/dist/delivery/hotspot/rectangle.js +0 -104
  225. package/dist/delivery/index.d.ts +0 -20
  226. package/dist/delivery/index.js +0 -107
  227. package/dist/delivery/session-updater.d.ts +0 -10
  228. package/dist/delivery/session-updater.js +0 -14
  229. package/dist/index.d.ts +0 -1
  230. package/dist/index.iife.d.ts +0 -8
  231. package/dist/index.iife.js +0 -169
  232. package/dist/index.js +0 -2
  233. package/dist/runtime-support.d.ts +0 -12
  234. package/dist/runtime-support.js +0 -12
@@ -0,0 +1,81 @@
1
+ export class PolygonShape {
2
+ static name = 'polygon'
3
+
4
+ static create(shapes, e) {
5
+ const newShapes = [...shapes];
6
+ const newPolygon = {
7
+ id: 'newPolygon',
8
+ points: [{ x: e.evt.layerX, y: e.evt.layerY }],
9
+ group: 'polygons',
10
+ index: newShapes.length,
11
+ };
12
+
13
+ newShapes.push(newPolygon);
14
+
15
+ return {
16
+ shapes: newShapes,
17
+ isDrawing: true,
18
+ isDrawingShapeId: newPolygon.id,
19
+ };
20
+ }
21
+
22
+ static addPoint(state, e, onPolygonComplete) {
23
+ // Number of pixels allowed to determine if the first point was clicked
24
+ const clickDelta = 5;
25
+
26
+ const shapesCopy = JSON.parse(JSON.stringify(state.shapes));
27
+ const currentShapeIndex = shapesCopy.findIndex((shape) => shape.id === state.isDrawingShapeId);
28
+
29
+ if (currentShapeIndex !== -1) {
30
+ const currentShape = shapesCopy[currentShapeIndex];
31
+ if (currentShape.points && Array.isArray(currentShape.points)) {
32
+ const firstPoint = currentShape.points[0];
33
+
34
+ // If click is close enough to the first point (within clickDelta pixels), close the polygon
35
+ if (
36
+ Math.abs(firstPoint.x - e.evt.layerX) <= clickDelta &&
37
+ Math.abs(firstPoint.y - e.evt.layerY) <= clickDelta
38
+ ) {
39
+ return PolygonShape.finalizeCreation(state, onPolygonComplete);
40
+ }
41
+
42
+ currentShape.points.push({ x: e.evt.layerX, y: e.evt.layerY });
43
+ shapesCopy[currentShapeIndex] = currentShape;
44
+
45
+ return {
46
+ shapes: shapesCopy,
47
+ };
48
+ }
49
+ }
50
+ return state;
51
+ }
52
+
53
+ static finalizeCreation(state, onPolygonComplete) {
54
+ const { shapes } = state;
55
+ const tempShapes = [...shapes];
56
+ const highestId = Math.max(...state.shapes.map((shape) => parseInt(shape.id) || 0), 0);
57
+
58
+ const polygonIndex = tempShapes.findIndex((shape) => shape.id === state.isDrawingShapeId);
59
+
60
+ if (polygonIndex !== -1 && tempShapes[polygonIndex].points.length > 2) {
61
+ const completedPolygon = tempShapes[polygonIndex];
62
+
63
+ completedPolygon.id = `${highestId + 1}`;
64
+ onPolygonComplete(tempShapes);
65
+
66
+ return {
67
+ isDrawing: false,
68
+ shapes: tempShapes,
69
+ isDrawingShapeId: undefined,
70
+ };
71
+ }
72
+
73
+ return state;
74
+ }
75
+
76
+ // No need to update anything on mouse move,
77
+ // but it's here if we need to add any logic later.
78
+ static handleMouseMove(state, e) {
79
+ return state;
80
+ }
81
+ }
@@ -0,0 +1,82 @@
1
+ export class RectangleShape {
2
+ static name = 'rectangle'
3
+
4
+ static create(shapes, e) {
5
+ const newShapes = [...shapes];
6
+ const highestId = Math.max(...newShapes.map((shape) => parseInt(shape.id)), 0) || 0;
7
+
8
+ const newRectangle = {
9
+ id: `${highestId + 1}`,
10
+ height: 0,
11
+ width: 0,
12
+ x: e.evt.layerX,
13
+ y: e.evt.layerY,
14
+ group: 'rectangles',
15
+ index: newShapes.length,
16
+ };
17
+
18
+ newShapes.push(newRectangle);
19
+
20
+ return {
21
+ shapes: newShapes,
22
+ isDrawing: true,
23
+ isDrawingShapeId: newRectangle.id,
24
+ };
25
+ }
26
+
27
+ static finalizeCreation(state, props) {
28
+ const currentShapeIndex = state.shapes.findIndex((shape) => shape.id === state.isDrawingShapeId);
29
+
30
+ if (currentShapeIndex !== -1) {
31
+ const currentShape = state.shapes[currentShapeIndex];
32
+
33
+ // Check if the shape is a valid rectangle (has more than 0 width and height) before finalizing
34
+ if (currentShape.width > 0 && currentShape.height > 0) {
35
+ return {
36
+ ...state,
37
+ isDrawing: false,
38
+ stateShapes: false,
39
+ isDrawingShapeId: undefined,
40
+ };
41
+ } else {
42
+ return {
43
+ ...state,
44
+ isDrawing: false,
45
+ stateShapes: false,
46
+ isDrawingShapeId: undefined,
47
+ shapes: state.shapes.filter((shape) => shape.id !== state.isDrawingShapeId),
48
+ };
49
+ }
50
+ }
51
+
52
+ // Return current state if not drawing a rectangle
53
+ return {
54
+ ...state,
55
+ isDrawing: false,
56
+ stateShapes: false,
57
+ isDrawingShapeId: undefined,
58
+ };
59
+ }
60
+
61
+ static handleMouseMove(state, e) {
62
+ const { isDrawing, isDrawingShapeId, shapes } = state;
63
+
64
+ if (isDrawing) {
65
+ const tempShapes = [...shapes];
66
+ const resizingShapeIndex = tempShapes.findIndex((shape) => shape.id === isDrawingShapeId);
67
+
68
+ if (resizingShapeIndex !== -1) {
69
+ const resizingShape = tempShapes[resizingShapeIndex];
70
+
71
+ resizingShape.width = e.evt.layerX - resizingShape.x;
72
+ resizingShape.height = e.evt.layerY - resizingShape.y;
73
+
74
+ return {
75
+ shapes: tempShapes,
76
+ };
77
+ }
78
+ }
79
+
80
+ return state;
81
+ }
82
+ }
@@ -0,0 +1,16 @@
1
+ import { CircleShape } from './circle';
2
+ import { PolygonShape } from './polygon';
3
+ import { RectangleShape } from './rectagle';
4
+
5
+ export const SUPPORTED_SHAPES = {
6
+ CIRCLE: CircleShape.name,
7
+ POLYGON: PolygonShape.name,
8
+ RECTANGLE: RectangleShape.name,
9
+ NONE: 'none',
10
+ };
11
+
12
+ export const SHAPE_GROUPS = {
13
+ CIRCLES: 'circles',
14
+ POLYGONS: 'polygons',
15
+ RECTANGLES: 'rectangles',
16
+ };
@@ -0,0 +1,33 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+
4
+ import Button from './button';
5
+
6
+ const UploadControl = ({ label, onInputClick, onUploadImage, setRef }) => (
7
+ <>
8
+ <Button label={label} onClick={onInputClick} />
9
+ <input
10
+ accept="image/*"
11
+ style={{ display: 'none' }}
12
+ onChange={onUploadImage}
13
+ ref={(ref) => {
14
+ setRef(ref);
15
+ }}
16
+ type="file"
17
+ />
18
+ </>
19
+ );
20
+
21
+ UploadControl.propTypes = {
22
+ label: PropTypes.string.isRequired,
23
+ onInputClick: PropTypes.func.isRequired,
24
+ onUploadImage: PropTypes.func.isRequired,
25
+ setRef: PropTypes.func.isRequired,
26
+ };
27
+
28
+ UploadControl.defaultProps = {
29
+ classNameButton: '',
30
+ classNameSection: '',
31
+ };
32
+
33
+ export default UploadControl;
@@ -0,0 +1,210 @@
1
+ import { cloneDeep } from 'lodash-es';
2
+ import { SHAPE_GROUPS } from './shapes';
3
+
4
+ const updateImageDimensions = (initialDim, nextDim, keepAspectRatio, resizeType) => {
5
+ // if we want to keep image aspect ratio
6
+ if (keepAspectRatio) {
7
+ const imageAspectRatio = initialDim.width / initialDim.height;
8
+
9
+ if (resizeType === 'height') {
10
+ // if we want to change image height => we update the width accordingly
11
+ return {
12
+ width: nextDim.height * imageAspectRatio,
13
+ height: nextDim.height,
14
+ };
15
+ }
16
+
17
+ // if we want to change image width => we update the height accordingly
18
+ return {
19
+ width: nextDim.width,
20
+ height: nextDim.width / imageAspectRatio,
21
+ };
22
+ }
23
+
24
+ // if we don't want to keep aspect ratio, we just update both values
25
+ return {
26
+ width: nextDim.width,
27
+ height: nextDim.height,
28
+ };
29
+ };
30
+
31
+ // referenceInitialValue = the initial value of the Stage
32
+ // referenceNextValue = the next value of the Stage
33
+ // currentValue = the value that has to be re-sized influenced by the changes that were made on the Stage
34
+ const getDelta = (referenceInitialValue, referenceNextValue, currentValue) =>
35
+ (referenceNextValue / referenceInitialValue) * currentValue;
36
+
37
+ const getUpdatedRectangle = (initialDim, nextDim, shape) => ({
38
+ ...shape,
39
+ width: getDelta(initialDim.width, nextDim.width, shape.width),
40
+ height: getDelta(initialDim.height, nextDim.height, shape.height),
41
+ x: getDelta(initialDim.width, nextDim.width, shape.x),
42
+ y: getDelta(initialDim.height, nextDim.height, shape.y),
43
+ });
44
+
45
+ const getUpdatedCircles = (initialDim, nextDim, shape) => ({
46
+ ...shape,
47
+ radius: getDelta(initialDim.width, nextDim.width, shape.radius),
48
+ x: getDelta(initialDim.width, nextDim.width, shape.x),
49
+ y: getDelta(initialDim.height, nextDim.height, shape.y),
50
+ });
51
+
52
+ const getUpdatedPolygon = (initialDim, nextDim, shape) => ({
53
+ ...shape,
54
+ points: shape.points.map((point) => ({
55
+ x: getDelta(initialDim.width, nextDim.width, point.x),
56
+ y: getDelta(initialDim.height, nextDim.height, point.y),
57
+ })),
58
+ });
59
+
60
+ // initialDim = the initial dimensions: { width, height } of the Stage
61
+ // nextDim = the next dimensions: { width, height } of the Stage
62
+ // shapes = array of shapes that have to be re-sized and re-positioned
63
+ const getUpdatedShapes = (initialDim, nextDim, shapes) => {
64
+ return shapes.map((shape) => {
65
+ if (shape.group === SHAPE_GROUPS.RECTANGLES) {
66
+ return getUpdatedRectangle(initialDim, nextDim, shape);
67
+ }
68
+
69
+ if (shape.group === SHAPE_GROUPS.POLYGONS) {
70
+ return getUpdatedPolygon(initialDim, nextDim, shape);
71
+ }
72
+
73
+ if (shape.group === SHAPE_GROUPS.CIRCLES) {
74
+ return getUpdatedCircles(initialDim, nextDim, shape);
75
+ }
76
+ });
77
+ };
78
+
79
+ // converts shapes map to shapes array
80
+ // example:
81
+ // from: { rectangles: [r1], polygons: [p1, p2]}
82
+ // to: [{ ...r1, group: 'rectangles' }, { ...p1, group: 'polygons' }, { ...p2, group: 'polygons' }]
83
+ // if a shape has index defined, keep it, otherwise initialize it
84
+ // index is used for the UNDO function
85
+ const getAllShapes = (shapesMap) => {
86
+ shapesMap = shapesMap || {};
87
+ const shapesArray = [];
88
+ const shapesKeys = Object.keys(shapesMap);
89
+
90
+ return shapesKeys.length
91
+ ? shapesKeys.reduce(
92
+ (acc, currentShapeKey) =>
93
+ acc.concat(
94
+ shapesMap[currentShapeKey]
95
+ ? shapesMap[currentShapeKey].map((shape, index) => ({
96
+ ...shape,
97
+ group: currentShapeKey,
98
+ index: shape.index || acc.length + index,
99
+ }))
100
+ : [],
101
+ ),
102
+ shapesArray,
103
+ )
104
+ : shapesArray;
105
+ };
106
+
107
+ // converts shapes array to shapes map
108
+ // is the reverse of getAllShapes function
109
+ // example:
110
+ // from: [{ ...r1, group: 'rectangles' }, { ...p1, group: 'polygons' }, { ...p2, group: 'polygons' }]
111
+ // to: { rectangles: [r1], polygons: [p1, p2]}
112
+ const groupShapes = (shapesArray) => {
113
+ shapesArray = shapesArray || [];
114
+ const shapesMap = {
115
+ rectangles: [],
116
+ polygons: [],
117
+ circles: [],
118
+ };
119
+
120
+ if (shapesArray.length) {
121
+ return shapesArray.reduce((acc, { group, ...shapeProps }) => {
122
+ acc[group] = [...(acc[group] || []), shapeProps];
123
+ return acc;
124
+ }, shapesMap);
125
+ }
126
+
127
+ return cloneDeep(shapesMap);
128
+ };
129
+
130
+ const isPointInsidePolygon = (polygon, x, y) => {
131
+ let inside = false;
132
+
133
+ if (!polygon || polygon.length <= 0) {
134
+ return inside;
135
+ }
136
+
137
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
138
+ const xi = polygon[i].x;
139
+ const yi = polygon[i].y;
140
+ const xj = polygon[j].x;
141
+ const yj = polygon[j].y;
142
+
143
+ const intersect = yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
144
+
145
+ if (intersect) {
146
+ inside = !inside;
147
+ }
148
+ }
149
+
150
+ return inside;
151
+ };
152
+
153
+ const calculate = (polygonPoints) => {
154
+ if (!polygonPoints || polygonPoints.length <= 0) {
155
+ return { x: 0, y: 0 };
156
+ }
157
+
158
+ const xPoints = polygonPoints.map((point) => point.x);
159
+ const yPoints = polygonPoints.map((point) => point.y);
160
+ const minX = Math.min(...xPoints);
161
+ const minY = Math.min(...yPoints);
162
+ const maxX = Math.max(...xPoints);
163
+ const maxY = Math.max(...yPoints);
164
+
165
+ // Find a suitable position for the text element within the polygon
166
+ let textX, textY;
167
+
168
+ for (let x = minX; x <= maxX - 20; x++) {
169
+ for (let y = maxY - 20; y > minY; y--) {
170
+ // Check if the text element's position (x, y) is within the polygon
171
+ if (isPointInsidePolygon(polygonPoints, x, y)) {
172
+ textX = x - 10;
173
+ textY = y;
174
+ break;
175
+ }
176
+ }
177
+ }
178
+
179
+ return { x: textX, y: textY };
180
+ };
181
+
182
+ const generateValidationMessage = (config) => {
183
+ const { minShapes, maxShapes, maxSelections } = config;
184
+
185
+ const shapesMessage =
186
+ `\nThere should be at least ${minShapes} ` + (maxShapes ? `and at most ${maxShapes} ` : '') + 'shapes defined.';
187
+
188
+ const selectionsMessage =
189
+ '\nThere should be at least 1 ' +
190
+ (maxSelections ? `and at most ${maxSelections} ` : '') +
191
+ 'shape' +
192
+ (maxSelections ? 's' : '') +
193
+ ' selected.';
194
+
195
+ const message = 'Validation requirements:' + shapesMessage + selectionsMessage;
196
+
197
+ return message;
198
+ };
199
+
200
+ export {
201
+ calculate,
202
+ isPointInsidePolygon,
203
+ updateImageDimensions,
204
+ generateValidationMessage,
205
+ getUpdatedShapes,
206
+ getAllShapes,
207
+ groupShapes,
208
+ getUpdatedRectangle,
209
+ getUpdatedPolygon,
210
+ };