@yorab/react-paint 0.2.2

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 (103) hide show
  1. package/Changelog.md +26 -0
  2. package/README.md +99 -0
  3. package/dist/components/App.d.ts +20 -0
  4. package/dist/components/App.js +533 -0
  5. package/dist/components/Canvas.d.ts +28 -0
  6. package/dist/components/Canvas.js +188 -0
  7. package/dist/components/common/Button.d.ts +14 -0
  8. package/dist/components/common/Button.js +97 -0
  9. package/dist/components/common/Loading.d.ts +6 -0
  10. package/dist/components/common/Loading.js +66 -0
  11. package/dist/components/common/Modal.d.ts +8 -0
  12. package/dist/components/common/Modal.js +41 -0
  13. package/dist/components/common/Panel.d.ts +10 -0
  14. package/dist/components/common/Panel.js +48 -0
  15. package/dist/components/common/Snackbar.d.ts +7 -0
  16. package/dist/components/common/Snackbar.js +75 -0
  17. package/dist/components/settings/ColorField.d.ts +12 -0
  18. package/dist/components/settings/ColorField.js +128 -0
  19. package/dist/components/settings/DeleteButton.d.ts +9 -0
  20. package/dist/components/settings/DeleteButton.js +36 -0
  21. package/dist/components/settings/FontFamilyField.d.ts +10 -0
  22. package/dist/components/settings/FontFamilyField.js +57 -0
  23. package/dist/components/settings/LayoutButton.d.ts +8 -0
  24. package/dist/components/settings/LayoutButton.js +37 -0
  25. package/dist/components/settings/Layouts.d.ts +18 -0
  26. package/dist/components/settings/Layouts.js +218 -0
  27. package/dist/components/settings/LineArrowField.d.ts +10 -0
  28. package/dist/components/settings/LineArrowField.js +59 -0
  29. package/dist/components/settings/LineTypeField.d.ts +14 -0
  30. package/dist/components/settings/LineTypeField.js +57 -0
  31. package/dist/components/settings/PointsNumberField.d.ts +11 -0
  32. package/dist/components/settings/PointsNumberField.js +51 -0
  33. package/dist/components/settings/RangeField.d.ts +16 -0
  34. package/dist/components/settings/RangeField.js +90 -0
  35. package/dist/components/settings/SelectField.d.ts +16 -0
  36. package/dist/components/settings/SelectField.js +82 -0
  37. package/dist/components/settings/SettingsBar.d.ts +16 -0
  38. package/dist/components/settings/SettingsBar.js +262 -0
  39. package/dist/components/toolbox/EditTextBox.d.ts +10 -0
  40. package/dist/components/toolbox/EditTextBox.js +89 -0
  41. package/dist/components/toolbox/LoadFileTool.d.ts +11 -0
  42. package/dist/components/toolbox/LoadFileTool.js +58 -0
  43. package/dist/components/toolbox/MenuGroup.d.ts +23 -0
  44. package/dist/components/toolbox/MenuGroup.js +173 -0
  45. package/dist/components/toolbox/PictureUrlInput.d.ts +7 -0
  46. package/dist/components/toolbox/PictureUrlInput.js +96 -0
  47. package/dist/components/toolbox/Tool.d.ts +13 -0
  48. package/dist/components/toolbox/Tool.js +41 -0
  49. package/dist/components/toolbox/Toolbar.d.ts +20 -0
  50. package/dist/components/toolbox/Toolbar.js +205 -0
  51. package/dist/components/toolbox/ToolbarGroup.d.ts +18 -0
  52. package/dist/components/toolbox/ToolbarGroup.js +139 -0
  53. package/dist/constants/draw.d.ts +2 -0
  54. package/dist/constants/draw.js +10 -0
  55. package/dist/constants/icons.d.ts +36 -0
  56. package/dist/constants/icons.js +80 -0
  57. package/dist/constants/shapes.d.ts +5 -0
  58. package/dist/constants/shapes.js +14 -0
  59. package/dist/constants/snackbar.d.ts +11 -0
  60. package/dist/constants/snackbar.js +23 -0
  61. package/dist/constants/style.d.ts +12 -0
  62. package/dist/constants/style.js +30 -0
  63. package/dist/hooks/useComponent.d.ts +9 -0
  64. package/dist/hooks/useComponent.js +48 -0
  65. package/dist/hooks/useDrag.d.ts +15 -0
  66. package/dist/hooks/useDrag.js +100 -0
  67. package/dist/hooks/useDrawableCanvas.d.ts +30 -0
  68. package/dist/hooks/useDrawableCanvas.js +250 -0
  69. package/dist/hooks/useKeyboard.d.ts +15 -0
  70. package/dist/hooks/useKeyboard.js +123 -0
  71. package/dist/hooks/useShapes.d.ts +22 -0
  72. package/dist/hooks/useShapes.js +229 -0
  73. package/dist/hooks/useSnackbar.d.ts +16 -0
  74. package/dist/hooks/useSnackbar.js +86 -0
  75. package/dist/schemas/drawableShape.json +1021 -0
  76. package/dist/styles.css +88 -0
  77. package/dist/types/Mode.d.ts +51 -0
  78. package/dist/types/Mode.js +17 -0
  79. package/dist/types/Shapes.d.ts +122 -0
  80. package/dist/types/Shapes.js +35 -0
  81. package/dist/types/keyboard.d.ts +15 -0
  82. package/dist/types/keyboard.js +28 -0
  83. package/dist/utils/data.d.ts +4 -0
  84. package/dist/utils/data.js +178 -0
  85. package/dist/utils/draw.d.ts +18 -0
  86. package/dist/utils/draw.js +422 -0
  87. package/dist/utils/file.d.ts +33 -0
  88. package/dist/utils/file.js +220 -0
  89. package/dist/utils/intersect.d.ts +8 -0
  90. package/dist/utils/intersect.js +194 -0
  91. package/dist/utils/selection.d.ts +7 -0
  92. package/dist/utils/selection.js +88 -0
  93. package/dist/utils/shapeData.d.ts +5 -0
  94. package/dist/utils/shapeData.js +178 -0
  95. package/dist/utils/string.d.ts +3 -0
  96. package/dist/utils/string.js +42 -0
  97. package/dist/utils/style.d.ts +2 -0
  98. package/dist/utils/style.js +46 -0
  99. package/dist/utils/toolbar.d.ts +12 -0
  100. package/dist/utils/toolbar.js +37 -0
  101. package/dist/utils/transform.d.ts +271 -0
  102. package/dist/utils/transform.js +371 -0
  103. package/package.json +79 -0
@@ -0,0 +1,422 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.drawTriangle = exports.drawText = exports.drawShape = exports.drawSelection = exports.drawRect = exports.drawPolygon = exports.drawPicture = exports.drawLine = exports.drawEllipse = exports.drawCurve = exports.drawCircle = exports.drawBrush = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
+
14
+ var _shapes = require("../constants/shapes");
15
+
16
+ var _style = require("../constants/style");
17
+
18
+ var _intersect = require("./intersect");
19
+
20
+ var _shapeData = require("./shapeData");
21
+
22
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
23
+
24
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
25
+
26
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
27
+
28
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
29
+
30
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
31
+
32
+ var applyShapeTransformations = function applyShapeTransformations(ctx, marker, canvasOffset) {
33
+ ctx.save();
34
+ ctx.translate(marker.translation[0] + canvasOffset[0], marker.translation[1] + canvasOffset[1]);
35
+
36
+ if (marker.rotation !== 0) {
37
+ var _getShapeInfos = (0, _shapeData.getShapeInfos)(marker),
38
+ center = _getShapeInfos.center;
39
+
40
+ ctx.translate(center[0], center[1]);
41
+ ctx.rotate(marker.rotation);
42
+ ctx.translate(-center[0], -center[1]);
43
+ }
44
+ };
45
+
46
+ var restoreShapeTransformations = function restoreShapeTransformations(ctx) {
47
+ ctx.restore();
48
+ };
49
+
50
+ var updateDrawStyle = function updateDrawStyle(ctx) {
51
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
52
+ fillColor: 'transparent',
53
+ strokeColor: 'blue',
54
+ globalAlpha: 100,
55
+ lineWidth: 1,
56
+ lineDash: 0
57
+ },
58
+ fillColor = _ref.fillColor,
59
+ globalAlpha = _ref.globalAlpha,
60
+ strokeColor = _ref.strokeColor,
61
+ lineWidth = _ref.lineWidth,
62
+ lineDash = _ref.lineDash;
63
+
64
+ ctx.lineCap = 'round';
65
+ ctx.lineJoin = 'round';
66
+ globalAlpha !== undefined && (ctx.globalAlpha = globalAlpha / 100);
67
+ fillColor && (ctx.fillStyle = fillColor);
68
+ strokeColor && (ctx.strokeStyle = strokeColor);
69
+ lineWidth && (ctx.lineWidth = lineWidth);
70
+ lineDash !== undefined && lineWidth && ctx.setLineDash([_style.LINE_DASH_DATA[lineDash][0] * lineWidth, _style.LINE_DASH_DATA[lineDash][1] * lineWidth]);
71
+ };
72
+
73
+ var drawBrush = function drawBrush(ctx, brush) {
74
+ var _brush$style;
75
+
76
+ if (brush.points.length < 1) return;
77
+ updateDrawStyle(ctx, brush.style);
78
+ ctx.beginPath();
79
+ if (((_brush$style = brush.style) === null || _brush$style === void 0 ? void 0 : _brush$style.strokeColor) === 'transparent' || ctx.globalAlpha === 0) return;
80
+ brush.points.forEach(function (points) {
81
+ if (points.length === 1) {
82
+ ctx.rect(points[0][0], points[0][1], 1, 1);
83
+ } else {
84
+ ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(points[0]));
85
+ points.slice(1).forEach(function (point) {
86
+ ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(point));
87
+ });
88
+ }
89
+ });
90
+ ctx.stroke();
91
+ };
92
+
93
+ exports.drawBrush = drawBrush;
94
+
95
+ var drawTriangle = function drawTriangle(ctx, triangle) {
96
+ var _triangle$style, _triangle$style2;
97
+
98
+ updateDrawStyle(ctx, triangle.style);
99
+ if (ctx.globalAlpha === 0) return;
100
+ ctx.beginPath();
101
+ ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(triangle.points[0]));
102
+ ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(triangle.points[1]));
103
+ ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(triangle.points[2]));
104
+ ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(triangle.points[0]));
105
+ ((_triangle$style = triangle.style) === null || _triangle$style === void 0 ? void 0 : _triangle$style.fillColor) !== 'transparent' && ctx.fill();
106
+ ((_triangle$style2 = triangle.style) === null || _triangle$style2 === void 0 ? void 0 : _triangle$style2.strokeColor) !== 'transparent' && ctx.stroke();
107
+ };
108
+
109
+ exports.drawTriangle = drawTriangle;
110
+
111
+ var buildTriangleOnLine = function buildTriangleOnLine(center, angle, lineStyle) {
112
+ var _lineStyle$style$line, _lineStyle$style, _lineStyle$style$line2, _lineStyle$style2, _lineStyle$style$line3, _lineStyle$style3, _lineStyle$style$line4, _lineStyle$style4, _lineStyle$style$line5, _lineStyle$style5, _lineStyle$style6;
113
+
114
+ var trianglePoints = [(0, _intersect.applyRotationToVector)([0, -(10 + ((_lineStyle$style$line = (_lineStyle$style = lineStyle.style) === null || _lineStyle$style === void 0 ? void 0 : _lineStyle$style.lineWidth) !== null && _lineStyle$style$line !== void 0 ? _lineStyle$style$line : 0) * 1)], angle), (0, _intersect.applyRotationToVector)([-(5 + ((_lineStyle$style$line2 = (_lineStyle$style2 = lineStyle.style) === null || _lineStyle$style2 === void 0 ? void 0 : _lineStyle$style2.lineWidth) !== null && _lineStyle$style$line2 !== void 0 ? _lineStyle$style$line2 : 0) * 1), 5 + ((_lineStyle$style$line3 = (_lineStyle$style3 = lineStyle.style) === null || _lineStyle$style3 === void 0 ? void 0 : _lineStyle$style3.lineWidth) !== null && _lineStyle$style$line3 !== void 0 ? _lineStyle$style$line3 : 0) * 2], angle), (0, _intersect.applyRotationToVector)([5 + ((_lineStyle$style$line4 = (_lineStyle$style4 = lineStyle.style) === null || _lineStyle$style4 === void 0 ? void 0 : _lineStyle$style4.lineWidth) !== null && _lineStyle$style$line4 !== void 0 ? _lineStyle$style$line4 : 0) * 1, 5 + ((_lineStyle$style$line5 = (_lineStyle$style5 = lineStyle.style) === null || _lineStyle$style5 === void 0 ? void 0 : _lineStyle$style5.lineWidth) !== null && _lineStyle$style$line5 !== void 0 ? _lineStyle$style$line5 : 0) * 2], angle)];
115
+ return {
116
+ points: [[center[0] + trianglePoints[0][0], center[1] + trianglePoints[0][1]], [center[0] + trianglePoints[1][0], center[1] + trianglePoints[1][1]], [center[0] + trianglePoints[2][0], center[1] + trianglePoints[2][1]]],
117
+ style: _objectSpread(_objectSpread({}, lineStyle.style), {}, {
118
+ fillColor: (_lineStyle$style6 = lineStyle.style) === null || _lineStyle$style6 === void 0 ? void 0 : _lineStyle$style6.strokeColor,
119
+ strokeColor: 'transparent'
120
+ })
121
+ };
122
+ };
123
+
124
+ var drawLine = function drawLine(ctx, line) {
125
+ var _line$style, _line$style2, _line$style3, _line$style4, _line$style5, _line$style6;
126
+
127
+ updateDrawStyle(ctx, line.style);
128
+ if (ctx.globalAlpha === 0) return;
129
+ ctx.beginPath();
130
+ ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(line.points[0]));
131
+ ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(line.points[1]));
132
+ ((_line$style = line.style) === null || _line$style === void 0 ? void 0 : _line$style.fillColor) !== 'transparent' && ctx.fill();
133
+ ((_line$style2 = line.style) === null || _line$style2 === void 0 ? void 0 : _line$style2.strokeColor) !== 'transparent' && ctx.stroke();
134
+
135
+ if (((_line$style3 = line.style) === null || _line$style3 === void 0 ? void 0 : _line$style3.lineArrow) === 1 || ((_line$style4 = line.style) === null || _line$style4 === void 0 ? void 0 : _line$style4.lineArrow) === 3) {
136
+ var rotation = Math.PI / 2 - Math.atan2(line.points[1][1] - line.points[0][1], line.points[1][0] - line.points[0][0]);
137
+ drawTriangle(ctx, buildTriangleOnLine(line.points[0], rotation, {
138
+ style: line.style
139
+ }));
140
+ }
141
+
142
+ if (((_line$style5 = line.style) === null || _line$style5 === void 0 ? void 0 : _line$style5.lineArrow) === 2 || ((_line$style6 = line.style) === null || _line$style6 === void 0 ? void 0 : _line$style6.lineArrow) === 3) {
143
+ var _rotation = Math.PI / 2 - Math.atan2(line.points[0][1] - line.points[1][1], line.points[0][0] - line.points[1][0]);
144
+
145
+ drawTriangle(ctx, buildTriangleOnLine(line.points[1], _rotation, {
146
+ style: line.style
147
+ }));
148
+ }
149
+ };
150
+
151
+ exports.drawLine = drawLine;
152
+
153
+ var drawPolygon = function drawPolygon(ctx, polygon) {
154
+ var _polygon$style, _polygon$style2;
155
+
156
+ if (polygon.points.length < 1) return;
157
+ updateDrawStyle(ctx, polygon.style);
158
+ if (ctx.globalAlpha === 0) return;
159
+ ctx.beginPath();
160
+ ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(polygon.points[0]));
161
+ polygon.points.slice(1).forEach(function (point) {
162
+ ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(point));
163
+ });
164
+ ctx.lineTo.apply(ctx, (0, _toConsumableArray2["default"])(polygon.points[0]));
165
+ ((_polygon$style = polygon.style) === null || _polygon$style === void 0 ? void 0 : _polygon$style.fillColor) !== 'transparent' && ctx.fill();
166
+ ((_polygon$style2 = polygon.style) === null || _polygon$style2 === void 0 ? void 0 : _polygon$style2.strokeColor) !== 'transparent' && ctx.stroke();
167
+ };
168
+
169
+ exports.drawPolygon = drawPolygon;
170
+
171
+ var drawCurve = function drawCurve(ctx, curve) {
172
+ var _curve$style, _curve$style2;
173
+
174
+ if (curve.points.length < 3) return;
175
+ updateDrawStyle(ctx, curve.style);
176
+ if (ctx.globalAlpha === 0) return;
177
+ ctx.beginPath();
178
+ ctx.moveTo.apply(ctx, (0, _toConsumableArray2["default"])(curve.points[0]));
179
+
180
+ for (var i = 1; i < curve.points.length - 1; i++) {
181
+ ctx.quadraticCurveTo.apply(ctx, (0, _toConsumableArray2["default"])(curve.points[i]).concat([curve.points.length - 2 === i ? curve.points[i + 1][0] : (curve.points[i + 1][0] - curve.points[i][0]) / 2 + curve.points[i][0], curve.points.length - 2 === i ? curve.points[i + 1][1] : (curve.points[i + 1][1] - curve.points[i][1]) / 2 + curve.points[i][1]]));
182
+ }
183
+
184
+ ((_curve$style = curve.style) === null || _curve$style === void 0 ? void 0 : _curve$style.fillColor) !== 'transparent' && ctx.fill();
185
+ ((_curve$style2 = curve.style) === null || _curve$style2 === void 0 ? void 0 : _curve$style2.strokeColor) !== 'transparent' && ctx.stroke();
186
+ };
187
+
188
+ exports.drawCurve = drawCurve;
189
+
190
+ var drawCircle = function drawCircle(ctx, circle) {
191
+ var _circle$style, _circle$style2;
192
+
193
+ updateDrawStyle(ctx, circle.style);
194
+ ctx.beginPath();
195
+ ctx.arc(circle.x, circle.y, circle.radius, 0, 2 * Math.PI);
196
+ ((_circle$style = circle.style) === null || _circle$style === void 0 ? void 0 : _circle$style.fillColor) !== 'transparent' && ctx.fill();
197
+ ((_circle$style2 = circle.style) === null || _circle$style2 === void 0 ? void 0 : _circle$style2.strokeColor) !== 'transparent' && ctx.stroke();
198
+ };
199
+
200
+ exports.drawCircle = drawCircle;
201
+
202
+ var drawEllipse = function drawEllipse(ctx, ellipse) {
203
+ var _ellipse$style, _ellipse$style2;
204
+
205
+ updateDrawStyle(ctx, ellipse.style);
206
+ if (ctx.globalAlpha === 0) return;
207
+ ctx.beginPath();
208
+ ctx.ellipse(ellipse.x, ellipse.y, ellipse.radiusX, ellipse.radiusY, 0, 0, 2 * Math.PI);
209
+ ((_ellipse$style = ellipse.style) === null || _ellipse$style === void 0 ? void 0 : _ellipse$style.fillColor) !== 'transparent' && ctx.fill();
210
+ ((_ellipse$style2 = ellipse.style) === null || _ellipse$style2 === void 0 ? void 0 : _ellipse$style2.strokeColor) !== 'transparent' && ctx.stroke();
211
+ };
212
+
213
+ exports.drawEllipse = drawEllipse;
214
+
215
+ var drawRect = function drawRect(ctx, rect) {
216
+ var _rect$style, _rect$style2;
217
+
218
+ updateDrawStyle(ctx, rect.style);
219
+ if (ctx.globalAlpha === 0) return;
220
+ ctx.beginPath();
221
+ ctx.rect(rect.x, rect.y, rect.width, rect.height);
222
+ ((_rect$style = rect.style) === null || _rect$style === void 0 ? void 0 : _rect$style.fillColor) !== 'transparent' && ctx.fill();
223
+ ((_rect$style2 = rect.style) === null || _rect$style2 === void 0 ? void 0 : _rect$style2.strokeColor) !== 'transparent' && ctx.stroke();
224
+ };
225
+
226
+ exports.drawRect = drawRect;
227
+
228
+ var drawText = function drawText(ctx, text) {
229
+ var _text$style, _text$style$fontFamil, _text$style2;
230
+
231
+ updateDrawStyle(ctx, text.style);
232
+ if (ctx.globalAlpha === 0 || !((_text$style = text.style) !== null && _text$style !== void 0 && _text$style.strokeColor) || text.style.strokeColor === 'transparent') return;
233
+ ctx.font = "".concat(text.fontSize, "px ").concat((_text$style$fontFamil = (_text$style2 = text.style) === null || _text$style2 === void 0 ? void 0 : _text$style2.fontFamily) !== null && _text$style$fontFamil !== void 0 ? _text$style$fontFamil : _style.STYLE_FONT_DEFAULT);
234
+ ctx.textBaseline = 'hanging';
235
+ ctx.fillStyle = text.style.strokeColor;
236
+
237
+ for (var i = 0; i < text.value.length; i++) {
238
+ ctx.fillText(text.value[i], text.x, text.y + i * text.fontSize, text.width);
239
+ }
240
+ };
241
+
242
+ exports.drawText = drawText;
243
+
244
+ var drawPicture = function drawPicture(ctx, picture) {
245
+ updateDrawStyle(ctx, picture.style);
246
+ if (ctx.globalAlpha === 0) return;
247
+ ctx.beginPath();
248
+ ctx.drawImage(picture.img, picture.x, picture.y, picture.width, picture.height);
249
+ };
250
+
251
+ exports.drawPicture = drawPicture;
252
+
253
+ var drawShape = function drawShape(ctx, shape, canvasOffset) {
254
+ if (shape.visible === false) return;
255
+ applyShapeTransformations(ctx, shape, canvasOffset);
256
+
257
+ switch (shape.type) {
258
+ case 'brush':
259
+ drawBrush(ctx, shape);
260
+ break;
261
+
262
+ case 'text':
263
+ drawText(ctx, shape);
264
+ break;
265
+
266
+ case 'line':
267
+ drawLine(ctx, shape);
268
+ break;
269
+
270
+ case 'polygon':
271
+ drawPolygon(ctx, shape);
272
+ break;
273
+
274
+ case 'curve':
275
+ drawCurve(ctx, shape);
276
+ break;
277
+
278
+ case 'circle':
279
+ drawCircle(ctx, shape);
280
+ break;
281
+
282
+ case 'ellipse':
283
+ drawEllipse(ctx, shape);
284
+ break;
285
+
286
+ case 'rect':
287
+ case 'square':
288
+ drawRect(ctx, shape);
289
+ break;
290
+
291
+ case 'picture':
292
+ drawPicture(ctx, shape);
293
+ break;
294
+ }
295
+
296
+ restoreShapeTransformations(ctx);
297
+ };
298
+
299
+ exports.drawShape = drawShape;
300
+
301
+ var drawSelectionDefault = function drawSelectionDefault(_ref2) {
302
+ var ctx = _ref2.ctx,
303
+ shape = _ref2.shape,
304
+ withAnchors = _ref2.withAnchors;
305
+
306
+ var _getShapeInfos2 = (0, _shapeData.getShapeInfos)(shape),
307
+ borders = _getShapeInfos2.borders;
308
+
309
+ drawRect(ctx, borders);
310
+ if (!withAnchors || shape.locked) return;
311
+ drawLine(ctx, {
312
+ points: [[borders.x + borders.width / 2, borders.y], [borders.x + borders.width / 2, borders.y - _shapes.SELECTION_ANCHOR_SIZE / 2 - _shapes.SELECTION_ROTATED_ANCHOR_POSITION]]
313
+ });
314
+
315
+ var _iterator = _createForOfIteratorHelper(_shapes.SELECTION_RESIZE_ANCHOR_POSITIONS),
316
+ _step;
317
+
318
+ try {
319
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
320
+ var anchorPosition = _step.value;
321
+ drawCircle(ctx, {
322
+ x: borders.x + borders.width * anchorPosition[0],
323
+ y: borders.y + borders.height * anchorPosition[1],
324
+ radius: _shapes.SELECTION_ANCHOR_SIZE / 2,
325
+ style: {
326
+ fillColor: 'rgb(255,255,255)',
327
+ strokeColor: 'rgb(150,150,150)',
328
+ lineWidth: 2
329
+ }
330
+ });
331
+ }
332
+ } catch (err) {
333
+ _iterator.e(err);
334
+ } finally {
335
+ _iterator.f();
336
+ }
337
+
338
+ drawCircle(ctx, {
339
+ x: borders.x + borders.width / 2,
340
+ y: borders.y - _shapes.SELECTION_ANCHOR_SIZE / 2 - _shapes.SELECTION_ROTATED_ANCHOR_POSITION,
341
+ radius: _shapes.SELECTION_ANCHOR_SIZE / 2,
342
+ style: {
343
+ fillColor: 'rgb(255,255,255)',
344
+ strokeColor: 'rgb(150,150,150)',
345
+ lineWidth: 2
346
+ }
347
+ });
348
+ };
349
+
350
+ var drawLineSelection = function drawLineSelection(_ref3) {
351
+ var ctx = _ref3.ctx,
352
+ shape = _ref3.shape,
353
+ withAnchors = _ref3.withAnchors;
354
+
355
+ var _getShapeInfos3 = (0, _shapeData.getShapeInfos)(shape),
356
+ borders = _getShapeInfos3.borders;
357
+
358
+ drawRect(ctx, borders);
359
+ if (!withAnchors || shape.locked) return;
360
+
361
+ for (var i = 0; i < shape.points.length; i++) {
362
+ var coordinate = shape.points[i];
363
+
364
+ if (shape.type === 'curve' && i > 0 && i < shape.points.length - 1) {
365
+ drawRect(ctx, {
366
+ x: coordinate[0] - _shapes.SELECTION_ANCHOR_SIZE / 2,
367
+ y: coordinate[1] - _shapes.SELECTION_ANCHOR_SIZE / 2,
368
+ width: _shapes.SELECTION_ANCHOR_SIZE,
369
+ height: _shapes.SELECTION_ANCHOR_SIZE,
370
+ style: {
371
+ fillColor: 'rgb(255,255,255)',
372
+ strokeColor: 'rgb(150,150,150)',
373
+ lineWidth: 2
374
+ }
375
+ });
376
+ } else {
377
+ drawCircle(ctx, {
378
+ x: coordinate[0],
379
+ y: coordinate[1],
380
+ radius: _shapes.SELECTION_ANCHOR_SIZE / 2,
381
+ style: {
382
+ fillColor: 'rgb(255,255,255)',
383
+ strokeColor: 'rgb(150,150,150)',
384
+ lineWidth: 2
385
+ }
386
+ });
387
+ }
388
+ }
389
+ };
390
+
391
+ var drawSelection = function drawSelection(_ref4) {
392
+ var ctx = _ref4.ctx,
393
+ shape = _ref4.shape,
394
+ canvasOffset = _ref4.canvasOffset,
395
+ _ref4$withAnchors = _ref4.withAnchors,
396
+ withAnchors = _ref4$withAnchors === void 0 ? true : _ref4$withAnchors;
397
+ applyShapeTransformations(ctx, shape, canvasOffset);
398
+
399
+ switch (shape.type) {
400
+ case 'polygon':
401
+ case 'line':
402
+ case 'curve':
403
+ drawLineSelection({
404
+ ctx: ctx,
405
+ shape: shape,
406
+ withAnchors: withAnchors
407
+ });
408
+ break;
409
+
410
+ default:
411
+ drawSelectionDefault({
412
+ ctx: ctx,
413
+ shape: shape,
414
+ withAnchors: withAnchors
415
+ });
416
+ break;
417
+ }
418
+
419
+ restoreShapeTransformations(ctx);
420
+ };
421
+
422
+ exports.drawSelection = drawSelection;
@@ -0,0 +1,33 @@
1
+ /// <reference types="lodash" />
2
+ import { DrawableShape, DrawableShapeJson, ShapeEnum } from 'types/Shapes';
3
+ export declare const fetchAndStringify: (url: string) => Promise<string>;
4
+ export declare const downloadFile: (content: string, fileName: string) => void;
5
+ export declare const getCanvasImage: (canvas: HTMLCanvasElement) => string;
6
+ export declare const encodePicturesInShapes: (shapes: DrawableShape[]) => (import("types/Shapes").DrawableRect | import("types/Shapes").DrawableSquare | import("types/Shapes").DrawableText | import("types/Shapes").DrawableLine | import("types/Shapes").DrawablePolygon | import("types/Shapes").DrawableCurve | import("types/Shapes").DrawableBrush | import("types/Shapes").DrawableCircle | import("types/Shapes").DrawableEllipse | import("lodash").Omit<import("types/Shapes").DrawablePicture, "img"> | {
7
+ src: string;
8
+ img: undefined;
9
+ visible?: boolean | undefined;
10
+ locked?: boolean | undefined;
11
+ style?: {
12
+ fillColor?: string | undefined;
13
+ globalAlpha?: number | undefined;
14
+ strokeColor?: string | undefined;
15
+ lineWidth?: number | undefined;
16
+ lineDash?: number | undefined;
17
+ lineArrow?: number | undefined;
18
+ pointsCount?: number | undefined;
19
+ fontFamily?: string | undefined;
20
+ } | undefined;
21
+ x: number;
22
+ y: number;
23
+ width: number;
24
+ height: number;
25
+ id: string;
26
+ translation: import("types/Shapes").Point;
27
+ rotation: number;
28
+ type: ShapeEnum.picture;
29
+ })[];
30
+ export declare const encodeShapesInString: (shapes: DrawableShape[]) => string;
31
+ export declare const decodeJson: (file: File) => Promise<unknown>;
32
+ export declare const decodePicturesInShapes: (shapesForJson: DrawableShapeJson[]) => Promise<DrawableShape[]>;
33
+ export declare const validateJson: (json: unknown) => boolean;
@@ -0,0 +1,220 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.validateJson = exports.getCanvasImage = exports.fetchAndStringify = exports.encodeShapesInString = exports.encodePicturesInShapes = exports.downloadFile = exports.decodePicturesInShapes = exports.decodeJson = void 0;
9
+
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
+
16
+ var _ajv = _interopRequireDefault(require("ajv"));
17
+
18
+ var _fp = _interopRequireDefault(require("lodash/fp"));
19
+
20
+ var _drawableShape = _interopRequireDefault(require("../schemas/drawableShape.json"));
21
+
22
+ var _Shapes = require("../types/Shapes");
23
+
24
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
25
+
26
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
27
+
28
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
29
+
30
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
31
+
32
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
33
+
34
+ var fetchAndStringify = function fetchAndStringify(url) {
35
+ return fetch(url).then(function (response) {
36
+ return response.blob();
37
+ }).then(function (myBlob) {
38
+ return URL.createObjectURL(myBlob);
39
+ });
40
+ };
41
+
42
+ exports.fetchAndStringify = fetchAndStringify;
43
+
44
+ var downloadFile = function downloadFile(content, fileName) {
45
+ var a = document.createElement('a');
46
+ a.href = content;
47
+ a.download = fileName;
48
+ document.body.appendChild(a);
49
+ a.click();
50
+ document.body.removeChild(a);
51
+ };
52
+
53
+ exports.downloadFile = downloadFile;
54
+
55
+ var getBase64Image = function getBase64Image(img) {
56
+ var canvas = document.createElement('canvas');
57
+ canvas.width = img.width;
58
+ canvas.height = img.height;
59
+ var ctx = canvas.getContext('2d');
60
+ if (!ctx) return '';
61
+ ctx.drawImage(img, 0, 0);
62
+ var dataURL = canvas.toDataURL('image/png');
63
+ return dataURL;
64
+ };
65
+
66
+ var encodeObjectToString = function encodeObjectToString(objectToEncode) {
67
+ return 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(objectToEncode));
68
+ };
69
+
70
+ var getCanvasImage = function getCanvasImage(canvas) {
71
+ return canvas.toDataURL('image/png');
72
+ };
73
+
74
+ exports.getCanvasImage = getCanvasImage;
75
+
76
+ var encodePicturesInShapes = function encodePicturesInShapes(shapes) {
77
+ return shapes.map(function (shape) {
78
+ if (shape.type === _Shapes.ShapeEnum.picture) {
79
+ if (shape.src.startsWith('http')) {
80
+ return _fp["default"].omit('img', shape);
81
+ } else {
82
+ return _objectSpread(_objectSpread({}, shape), {}, {
83
+ src: getBase64Image(shape.img),
84
+ img: undefined
85
+ });
86
+ }
87
+ }
88
+
89
+ return shape;
90
+ });
91
+ };
92
+
93
+ exports.encodePicturesInShapes = encodePicturesInShapes;
94
+
95
+ var encodeShapesInString = function encodeShapesInString(shapes) {
96
+ var shapesWithStringifyPictures = encodePicturesInShapes(shapes);
97
+ return encodeObjectToString(shapesWithStringifyPictures);
98
+ };
99
+
100
+ exports.encodeShapesInString = encodeShapesInString;
101
+
102
+ var decodeJson = /*#__PURE__*/function () {
103
+ var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(file) {
104
+ return _regenerator["default"].wrap(function _callee$(_context) {
105
+ while (1) {
106
+ switch (_context.prev = _context.next) {
107
+ case 0:
108
+ return _context.abrupt("return", new Promise(function (resolve, reject) {
109
+ var reader = new FileReader();
110
+
111
+ reader.onload = function (event) {
112
+ var _event$target;
113
+
114
+ var result = (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.result;
115
+ if (!result) return reject('Impossible de récupérer les données du fichier');
116
+ var json = JSON.parse(result);
117
+ resolve(json);
118
+ };
119
+
120
+ reader.readAsText(file);
121
+ }));
122
+
123
+ case 1:
124
+ case "end":
125
+ return _context.stop();
126
+ }
127
+ }
128
+ }, _callee);
129
+ }));
130
+
131
+ return function decodeJson(_x) {
132
+ return _ref.apply(this, arguments);
133
+ };
134
+ }();
135
+
136
+ exports.decodeJson = decodeJson;
137
+
138
+ var decodePicturesInShapes = /*#__PURE__*/function () {
139
+ var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(shapesForJson) {
140
+ var promisesArray, shapes;
141
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
142
+ while (1) {
143
+ switch (_context2.prev = _context2.next) {
144
+ case 0:
145
+ promisesArray = [];
146
+ shapes = shapesForJson.map(function (shape) {
147
+ if (shape.type === _Shapes.ShapeEnum.picture) {
148
+ var img = new Image();
149
+ var newPromise = new Promise(function (resolve, reject) {
150
+ img.onload = function () {
151
+ resolve();
152
+ };
153
+
154
+ img.onerror = function () {
155
+ reject(new Error('Some images could not be loaded'));
156
+ };
157
+ });
158
+ promisesArray.push(newPromise);
159
+
160
+ if (shape.src.startsWith('http')) {
161
+ fetchAndStringify(shape.src).then(function (picData) {
162
+ img.src = picData;
163
+ })["catch"](function () {
164
+ img.src = ''; // to trigger onerror
165
+ });
166
+ } else {
167
+ img.src = shape.src;
168
+ }
169
+
170
+ return _objectSpread(_objectSpread({}, shape), {}, {
171
+ img: img
172
+ });
173
+ }
174
+
175
+ return shape;
176
+ });
177
+ _context2.next = 4;
178
+ return Promise.all(promisesArray);
179
+
180
+ case 4:
181
+ return _context2.abrupt("return", shapes);
182
+
183
+ case 5:
184
+ case "end":
185
+ return _context2.stop();
186
+ }
187
+ }
188
+ }, _callee2);
189
+ }));
190
+
191
+ return function decodePicturesInShapes(_x2) {
192
+ return _ref2.apply(this, arguments);
193
+ };
194
+ }();
195
+
196
+ exports.decodePicturesInShapes = decodePicturesInShapes;
197
+
198
+ var validateJson = function validateJson(json) {
199
+ var ajv = new _ajv["default"]();
200
+ var validate = ajv.compile(_drawableShape["default"]);
201
+ if (!_fp["default"].isArray(json)) return false;
202
+
203
+ var _iterator = _createForOfIteratorHelper(json),
204
+ _step;
205
+
206
+ try {
207
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
208
+ var shape = _step.value;
209
+ if (!validate(shape)) return false;
210
+ }
211
+ } catch (err) {
212
+ _iterator.e(err);
213
+ } finally {
214
+ _iterator.f();
215
+ }
216
+
217
+ return true;
218
+ };
219
+
220
+ exports.validateJson = validateJson;
@@ -0,0 +1,8 @@
1
+ import { HoverModeData } from 'types/Mode';
2
+ import { Point, DrawableShape } from 'types/Shapes';
3
+ export declare const getCursorPosition: (e: MouseEvent | TouchEvent, canvas: HTMLCanvasElement | null, givenWidth: number, givenHeight: number) => Point;
4
+ export declare const getSettingsPosition: (shape: DrawableShape, canvas: HTMLCanvasElement | null, givenWidth: number, givenHeight: number) => number[];
5
+ export declare const getPointPositionAfterCanvasTransformation: ([positionX, positionY]: Point, [canvasOffsetX, canvasOffsetY]: Point, [translationX, translationY]: Point, shapeRotation: number, [centerX, centerY]: Point) => Point;
6
+ export declare const getPointPositionBeforeCanvasTransformation: ([positionX, positionY]: Point, [translationX, translationY]: Point, shapeRotation: number, [centerX, centerY]: Point) => Point;
7
+ export declare const applyRotationToVector: (vector: Point, shapeRotation: number) => Point;
8
+ export declare const checkPositionIntersection: (shape: DrawableShape, position: Point, canvasOffset: Point, checkAnchors?: boolean) => false | HoverModeData;