@yorab/react-paint 0.2.2 → 0.4.1

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 (79) hide show
  1. package/Changelog.md +44 -0
  2. package/LICENSE.txt +21 -0
  3. package/README.md +9 -90
  4. package/dist/components/App.js +233 -113
  5. package/dist/components/Canvas.js +45 -21
  6. package/dist/components/settings/ColorField.js +3 -7
  7. package/dist/components/settings/LayoutButton.js +3 -3
  8. package/dist/components/settings/Layouts.js +6 -19
  9. package/dist/components/settings/SettingsBar.js +3 -3
  10. package/dist/components/toolbox/EditTextBox.js +20 -13
  11. package/dist/components/toolbox/MenuGroup.js +48 -5
  12. package/dist/components/toolbox/PictureUrlInput.js +8 -7
  13. package/dist/components/toolbox/Toolbar.js +56 -33
  14. package/dist/components/toolbox/ToolbarGroup.js +3 -3
  15. package/dist/constants/shapes.js +7 -3
  16. package/dist/hooks/useDrawableCanvas.js +23 -20
  17. package/dist/hooks/useResizeObserver.js +40 -0
  18. package/dist/hooks/useShapes.js +14 -9
  19. package/dist/styles.css +32 -35
  20. package/dist/types/utils.js +5 -0
  21. package/dist/utils/data.js +71 -19
  22. package/dist/utils/draw.js +90 -51
  23. package/dist/utils/file.js +26 -60
  24. package/dist/utils/intersect.js +22 -18
  25. package/dist/utils/selection.js +6 -6
  26. package/dist/utils/shapeData.js +49 -54
  27. package/dist/utils/transform.js +48 -34
  28. package/package.json +6 -7
  29. package/dist/components/App.d.ts +0 -20
  30. package/dist/components/Canvas.d.ts +0 -28
  31. package/dist/components/common/Button.d.ts +0 -14
  32. package/dist/components/common/Loading.d.ts +0 -6
  33. package/dist/components/common/Modal.d.ts +0 -8
  34. package/dist/components/common/Panel.d.ts +0 -10
  35. package/dist/components/common/Snackbar.d.ts +0 -7
  36. package/dist/components/settings/ColorField.d.ts +0 -12
  37. package/dist/components/settings/DeleteButton.d.ts +0 -9
  38. package/dist/components/settings/FontFamilyField.d.ts +0 -10
  39. package/dist/components/settings/LayoutButton.d.ts +0 -8
  40. package/dist/components/settings/Layouts.d.ts +0 -18
  41. package/dist/components/settings/LineArrowField.d.ts +0 -10
  42. package/dist/components/settings/LineTypeField.d.ts +0 -14
  43. package/dist/components/settings/PointsNumberField.d.ts +0 -11
  44. package/dist/components/settings/RangeField.d.ts +0 -16
  45. package/dist/components/settings/SelectField.d.ts +0 -16
  46. package/dist/components/settings/SettingsBar.d.ts +0 -16
  47. package/dist/components/toolbox/EditTextBox.d.ts +0 -10
  48. package/dist/components/toolbox/LoadFileTool.d.ts +0 -11
  49. package/dist/components/toolbox/MenuGroup.d.ts +0 -23
  50. package/dist/components/toolbox/PictureUrlInput.d.ts +0 -7
  51. package/dist/components/toolbox/Tool.d.ts +0 -13
  52. package/dist/components/toolbox/Toolbar.d.ts +0 -20
  53. package/dist/components/toolbox/ToolbarGroup.d.ts +0 -18
  54. package/dist/constants/draw.d.ts +0 -2
  55. package/dist/constants/draw.js +0 -10
  56. package/dist/constants/icons.d.ts +0 -36
  57. package/dist/constants/shapes.d.ts +0 -5
  58. package/dist/constants/snackbar.d.ts +0 -11
  59. package/dist/constants/style.d.ts +0 -12
  60. package/dist/hooks/useComponent.d.ts +0 -9
  61. package/dist/hooks/useDrag.d.ts +0 -15
  62. package/dist/hooks/useDrawableCanvas.d.ts +0 -30
  63. package/dist/hooks/useKeyboard.d.ts +0 -15
  64. package/dist/hooks/useShapes.d.ts +0 -22
  65. package/dist/hooks/useSnackbar.d.ts +0 -16
  66. package/dist/schemas/drawableShape.json +0 -1021
  67. package/dist/types/Mode.d.ts +0 -51
  68. package/dist/types/Shapes.d.ts +0 -122
  69. package/dist/types/keyboard.d.ts +0 -15
  70. package/dist/utils/data.d.ts +0 -4
  71. package/dist/utils/draw.d.ts +0 -18
  72. package/dist/utils/file.d.ts +0 -33
  73. package/dist/utils/intersect.d.ts +0 -8
  74. package/dist/utils/selection.d.ts +0 -7
  75. package/dist/utils/shapeData.d.ts +0 -5
  76. package/dist/utils/string.d.ts +0 -3
  77. package/dist/utils/style.d.ts +0 -2
  78. package/dist/utils/toolbar.d.ts +0 -12
  79. package/dist/utils/transform.d.ts +0 -271
@@ -7,18 +7,13 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports.getShapeInfos = void 0;
9
9
 
10
- var _shapes = require("../constants/shapes");
11
-
12
10
  var _fp = _interopRequireDefault(require("lodash/fp"));
13
11
 
14
- var getLineBorder = function getLineBorder(line) {
15
- var x = Math.min(line.points[0][0], line.points[1][0]) - _shapes.SELECTION_PADDING;
16
-
17
- var width = Math.abs(line.points[0][0] - line.points[1][0]) + _shapes.SELECTION_PADDING * 2;
18
-
19
- var y = Math.min(line.points[0][1], line.points[1][1]) - _shapes.SELECTION_PADDING;
20
-
21
- var height = Math.abs(line.points[0][1] - line.points[1][1]) + _shapes.SELECTION_PADDING * 2;
12
+ var getLineBorder = function getLineBorder(line, selectionPadding) {
13
+ var x = Math.min(line.points[0][0], line.points[1][0]) - selectionPadding;
14
+ var width = Math.abs(line.points[0][0] - line.points[1][0]) + selectionPadding * 2;
15
+ var y = Math.min(line.points[0][1], line.points[1][1]) - selectionPadding;
16
+ var height = Math.abs(line.points[0][1] - line.points[1][1]) + selectionPadding * 2;
22
17
  return {
23
18
  x: x,
24
19
  width: width,
@@ -27,26 +22,26 @@ var getLineBorder = function getLineBorder(line) {
27
22
  };
28
23
  };
29
24
 
30
- var getBrushBorder = function getBrushBorder(brush) {
25
+ var getBrushBorder = function getBrushBorder(brush, selectionPadding) {
31
26
  var brushPoints = _fp["default"].flatMap(function (points) {
32
27
  return points;
33
28
  }, brush.points);
34
29
 
35
30
  var minX = _fp["default"].flow(_fp["default"].map(function (point) {
36
31
  return point[0];
37
- }), _fp["default"].min, _fp["default"].add(-_shapes.SELECTION_PADDING))(brushPoints);
32
+ }), _fp["default"].min, _fp["default"].add(-selectionPadding))(brushPoints);
38
33
 
39
34
  var maxX = _fp["default"].flow(_fp["default"].map(function (point) {
40
35
  return point[0];
41
- }), _fp["default"].max, _fp["default"].add(_shapes.SELECTION_PADDING))(brushPoints);
36
+ }), _fp["default"].max, _fp["default"].add(selectionPadding))(brushPoints);
42
37
 
43
38
  var minY = _fp["default"].flow(_fp["default"].map(function (point) {
44
39
  return point[1];
45
- }), _fp["default"].min, _fp["default"].add(-_shapes.SELECTION_PADDING))(brushPoints);
40
+ }), _fp["default"].min, _fp["default"].add(-selectionPadding))(brushPoints);
46
41
 
47
42
  var maxY = _fp["default"].flow(_fp["default"].map(function (point) {
48
43
  return point[1];
49
- }), _fp["default"].max, _fp["default"].add(_shapes.SELECTION_PADDING))(brushPoints);
44
+ }), _fp["default"].max, _fp["default"].add(selectionPadding))(brushPoints);
50
45
 
51
46
  return {
52
47
  x: minX,
@@ -56,22 +51,22 @@ var getBrushBorder = function getBrushBorder(brush) {
56
51
  };
57
52
  };
58
53
 
59
- var getPolygonBorder = function getPolygonBorder(polygon) {
54
+ var getPolygonBorder = function getPolygonBorder(polygon, selectionPadding) {
60
55
  var minX = _fp["default"].flow(_fp["default"].map(function (point) {
61
56
  return point[0];
62
- }), _fp["default"].min, _fp["default"].add(-_shapes.SELECTION_PADDING))(polygon.points);
57
+ }), _fp["default"].min, _fp["default"].add(-selectionPadding))(polygon.points);
63
58
 
64
59
  var maxX = _fp["default"].flow(_fp["default"].map(function (point) {
65
60
  return point[0];
66
- }), _fp["default"].max, _fp["default"].add(_shapes.SELECTION_PADDING))(polygon.points);
61
+ }), _fp["default"].max, _fp["default"].add(selectionPadding))(polygon.points);
67
62
 
68
63
  var minY = _fp["default"].flow(_fp["default"].map(function (point) {
69
64
  return point[1];
70
- }), _fp["default"].min, _fp["default"].add(-_shapes.SELECTION_PADDING))(polygon.points);
65
+ }), _fp["default"].min, _fp["default"].add(-selectionPadding))(polygon.points);
71
66
 
72
67
  var maxY = _fp["default"].flow(_fp["default"].map(function (point) {
73
68
  return point[1];
74
- }), _fp["default"].max, _fp["default"].add(_shapes.SELECTION_PADDING))(polygon.points);
69
+ }), _fp["default"].max, _fp["default"].add(selectionPadding))(polygon.points);
75
70
 
76
71
  return {
77
72
  x: minX,
@@ -83,75 +78,75 @@ var getPolygonBorder = function getPolygonBorder(polygon) {
83
78
 
84
79
  var getCurveBorder = getPolygonBorder;
85
80
 
86
- var getRectBorder = function getRectBorder(rect) {
81
+ var getRectBorder = function getRectBorder(rect, selectionPadding) {
87
82
  return {
88
- x: rect.x - _shapes.SELECTION_PADDING,
89
- width: rect.width + _shapes.SELECTION_PADDING * 2,
90
- y: rect.y - _shapes.SELECTION_PADDING,
91
- height: rect.height + _shapes.SELECTION_PADDING * 2
83
+ x: rect.x - selectionPadding,
84
+ width: rect.width + selectionPadding * 2,
85
+ y: rect.y - selectionPadding,
86
+ height: rect.height + selectionPadding * 2
92
87
  };
93
88
  };
94
89
 
95
- var getTextBorder = function getTextBorder(text) {
90
+ var getTextBorder = function getTextBorder(text, selectionPadding) {
96
91
  return {
97
- x: text.x - _shapes.SELECTION_PADDING,
98
- width: text.width + _shapes.SELECTION_PADDING * 2,
99
- y: text.y - _shapes.SELECTION_PADDING,
100
- height: text.height + _shapes.SELECTION_PADDING * 2
92
+ x: text.x - selectionPadding,
93
+ width: text.width + selectionPadding * 2,
94
+ y: text.y - selectionPadding,
95
+ height: text.height + selectionPadding * 2
101
96
  };
102
97
  };
103
98
 
104
- var getPictureBorder = function getPictureBorder(picture) {
105
- return getRectBorder(picture);
99
+ var getPictureBorder = function getPictureBorder(picture, selectionPadding) {
100
+ return getRectBorder(picture, selectionPadding);
106
101
  };
107
102
 
108
- var getCircleBorder = function getCircleBorder(circle) {
103
+ var getCircleBorder = function getCircleBorder(circle, selectionPadding) {
109
104
  return {
110
- x: circle.x - circle.radius - _shapes.SELECTION_PADDING,
111
- width: (circle.radius + _shapes.SELECTION_PADDING) * 2,
112
- y: circle.y - circle.radius - _shapes.SELECTION_PADDING,
113
- height: (circle.radius + _shapes.SELECTION_PADDING) * 2
105
+ x: circle.x - circle.radius - selectionPadding,
106
+ width: (circle.radius + selectionPadding) * 2,
107
+ y: circle.y - circle.radius - selectionPadding,
108
+ height: (circle.radius + selectionPadding) * 2
114
109
  };
115
110
  };
116
111
 
117
- var getEllipseBorder = function getEllipseBorder(ellipse) {
112
+ var getEllipseBorder = function getEllipseBorder(ellipse, selectionPadding) {
118
113
  return {
119
- x: ellipse.x - ellipse.radiusX - _shapes.SELECTION_PADDING,
120
- width: (ellipse.radiusX + _shapes.SELECTION_PADDING) * 2,
121
- y: ellipse.y - ellipse.radiusY - _shapes.SELECTION_PADDING,
122
- height: (ellipse.radiusY + _shapes.SELECTION_PADDING) * 2
114
+ x: ellipse.x - ellipse.radiusX - selectionPadding,
115
+ width: (ellipse.radiusX + selectionPadding) * 2,
116
+ y: ellipse.y - ellipse.radiusY - selectionPadding,
117
+ height: (ellipse.radiusY + selectionPadding) * 2
123
118
  };
124
119
  };
125
120
 
126
- var getShapeBorders = function getShapeBorders(marker) {
121
+ var getShapeBorders = function getShapeBorders(marker, selectionPadding) {
127
122
  switch (marker.type) {
128
123
  case 'brush':
129
- return getBrushBorder(marker);
124
+ return getBrushBorder(marker, selectionPadding);
130
125
 
131
126
  case 'line':
132
- return getLineBorder(marker);
127
+ return getLineBorder(marker, selectionPadding);
133
128
 
134
129
  case 'polygon':
135
- return getPolygonBorder(marker);
130
+ return getPolygonBorder(marker, selectionPadding);
136
131
 
137
132
  case 'curve':
138
- return getCurveBorder(marker);
133
+ return getCurveBorder(marker, selectionPadding);
139
134
 
140
135
  case 'circle':
141
- return getCircleBorder(marker);
136
+ return getCircleBorder(marker, selectionPadding);
142
137
 
143
138
  case 'ellipse':
144
- return getEllipseBorder(marker);
139
+ return getEllipseBorder(marker, selectionPadding);
145
140
 
146
141
  case 'rect':
147
142
  case 'square':
148
- return getRectBorder(marker);
143
+ return getRectBorder(marker, selectionPadding);
149
144
 
150
145
  case 'text':
151
- return getTextBorder(marker);
146
+ return getTextBorder(marker, selectionPadding);
152
147
 
153
148
  case 'picture':
154
- return getPictureBorder(marker);
149
+ return getPictureBorder(marker, selectionPadding);
155
150
  }
156
151
 
157
152
  return {
@@ -166,8 +161,8 @@ var getShapeCenter = function getShapeCenter(borders) {
166
161
  return [borders.x + borders.width / 2, borders.y + borders.height / 2];
167
162
  };
168
163
 
169
- var getShapeInfos = function getShapeInfos(shape) {
170
- var borders = getShapeBorders(shape);
164
+ var getShapeInfos = function getShapeInfos(shape, selectionPadding) {
165
+ var borders = getShapeBorders(shape, selectionPadding);
171
166
  var center = getShapeCenter(borders);
172
167
  return {
173
168
  borders: borders,
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.updatePolygonLinesCount = exports.translateShape = exports.transformShape = exports.rotateShape = exports.resizeText = exports.resizeShape = exports.resizeRect = exports.resizePicture = exports.resizeLine = exports.resizeEllipse = exports.resizeCircle = exports.resizeBrush = exports.radiansToDegrees = exports.paintNewPointToShape = exports.getNormalizedSize = exports.degreesToRadians = exports.createNewPointGroupToShape = exports.calculateTextWidth = exports.calculateTextFontSize = void 0;
8
+ exports.updatePolygonLinesCount = exports.translateShape = exports.transformShape = exports.rotateShape = exports.resizeText = exports.resizeShape = exports.resizeRect = exports.resizePicture = exports.resizeLine = exports.resizeEllipse = exports.resizeCircle = exports.resizeBrush = exports.radiansToDegrees = exports.paintNewPointToShape = exports.getNormalizedSize = exports.fitContentInsideContainer = exports.degreesToRadians = exports.createNewPointGroupToShape = exports.calculateTextWidth = exports.calculateTextFontSize = void 0;
9
9
 
10
10
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
11
 
@@ -17,8 +17,6 @@ var _fp = _interopRequireDefault(require("lodash/fp"));
17
17
 
18
18
  var _shapeData = require("./shapeData");
19
19
 
20
- var _shapes = require("../constants/shapes");
21
-
22
20
  var _Mode = require("../types/Mode");
23
21
 
24
22
  var _intersect = require("./intersect");
@@ -81,14 +79,14 @@ var createNewPointGroupToShape = function createNewPointGroupToShape(shape, curs
81
79
 
82
80
  exports.createNewPointGroupToShape = createNewPointGroupToShape;
83
81
 
84
- var resizeBrush = function resizeBrush(cursorPosition, canvasOffset, originalShape, selectionMode) {
85
- var _getShapeInfos = (0, _shapeData.getShapeInfos)(originalShape),
82
+ var resizeBrush = function resizeBrush(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
83
+ var _getShapeInfos = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
86
84
  center = _getShapeInfos.center,
87
85
  borders = _getShapeInfos.borders;
88
86
 
89
87
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
90
- var scaledWidth = selectionMode.anchor[0] === 0.5 ? 1 : selectionMode.anchor[0] === 0 ? (borders.x + borders.width - cursorPositionBeforeResize[0] + _shapes.SELECTION_PADDING * -2) / (borders.width + _shapes.SELECTION_PADDING * -2) : (cursorPositionBeforeResize[0] - borders.x - _shapes.SELECTION_PADDING * 2) / (borders.width - _shapes.SELECTION_PADDING * 2);
91
- var scaledHeight = selectionMode.anchor[1] === 0.5 ? 1 : selectionMode.anchor[1] === 0 ? (borders.y + borders.height - cursorPositionBeforeResize[1] + _shapes.SELECTION_PADDING * -2) / (borders.height + _shapes.SELECTION_PADDING * -2) : (cursorPositionBeforeResize[1] - borders.y - _shapes.SELECTION_PADDING * 2) / (borders.height - _shapes.SELECTION_PADDING * 2);
88
+ var scaledWidth = selectionMode.anchor[0] === 0.5 ? 1 : selectionMode.anchor[0] === 0 ? (borders.x + borders.width - cursorPositionBeforeResize[0] + selectionPadding * -2) / (borders.width + selectionPadding * -2) : (cursorPositionBeforeResize[0] - borders.x - selectionPadding * 2) / (borders.width - selectionPadding * 2);
89
+ var scaledHeight = selectionMode.anchor[1] === 0.5 ? 1 : selectionMode.anchor[1] === 0 ? (borders.y + borders.height - cursorPositionBeforeResize[1] + selectionPadding * -2) / (borders.height + selectionPadding * -2) : (cursorPositionBeforeResize[1] - borders.y - selectionPadding * 2) / (borders.height - selectionPadding * 2);
92
90
 
93
91
  var shapeWithNewDimensions = _objectSpread(_objectSpread({}, originalShape), {}, {
94
92
  points: originalShape.points.map(function (points) {
@@ -98,7 +96,7 @@ var resizeBrush = function resizeBrush(cursorPosition, canvasOffset, originalSha
98
96
  })
99
97
  });
100
98
 
101
- var _getShapeInfos2 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions),
99
+ var _getShapeInfos2 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding),
102
100
  shapeWithNewDimensionsCenter = _getShapeInfos2.center,
103
101
  shapeWithNewDimensionsBorders = _getShapeInfos2.borders;
104
102
 
@@ -119,8 +117,8 @@ var resizeBrush = function resizeBrush(cursorPosition, canvasOffset, originalSha
119
117
 
120
118
  exports.resizeBrush = resizeBrush;
121
119
 
122
- var resizeLine = function resizeLine(cursorPosition, canvasOffset, originalShape, selectionMode) {
123
- var _getShapeInfos3 = (0, _shapeData.getShapeInfos)(originalShape),
120
+ var resizeLine = function resizeLine(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
121
+ var _getShapeInfos3 = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
124
122
  center = _getShapeInfos3.center;
125
123
 
126
124
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
@@ -152,20 +150,20 @@ var updatePolygonLinesCount = function updatePolygonLinesCount(shape, newPointsC
152
150
 
153
151
  exports.updatePolygonLinesCount = updatePolygonLinesCount;
154
152
 
155
- var resizeCircle = function resizeCircle(cursorPosition, canvasOffset, originalShape, selectionMode) {
156
- var _getShapeInfos4 = (0, _shapeData.getShapeInfos)(originalShape),
153
+ var resizeCircle = function resizeCircle(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
154
+ var _getShapeInfos4 = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
157
155
  center = _getShapeInfos4.center,
158
156
  borders = _getShapeInfos4.borders;
159
157
 
160
158
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
161
159
  var newCursorPosition = [cursorPositionBeforeResize[0], cursorPositionBeforeResize[1]];
162
- var scaledRadius = selectionMode.anchor[1] === 0.5 ? (selectionMode.anchor[0] === 0 ? borders.x + borders.width - newCursorPosition[0] + _shapes.SELECTION_PADDING * (selectionMode.anchor[0] === 0 ? -2 : 2) : newCursorPosition[0] - borders.x - _shapes.SELECTION_PADDING * (selectionMode.anchor[0] === 0 ? -2 : 2)) / 2 : (selectionMode.anchor[1] === 0 ? borders.y + borders.height - newCursorPosition[1] + _shapes.SELECTION_PADDING * (selectionMode.anchor[1] === 0 ? -2 : 2) : newCursorPosition[1] - borders.y - _shapes.SELECTION_PADDING * (selectionMode.anchor[1] === 0 ? -2 : 2)) / 2;
160
+ var scaledRadius = selectionMode.anchor[1] === 0.5 ? (selectionMode.anchor[0] === 0 ? borders.x + borders.width - newCursorPosition[0] + selectionPadding * (selectionMode.anchor[0] === 0 ? -2 : 2) : newCursorPosition[0] - borders.x - selectionPadding * (selectionMode.anchor[0] === 0 ? -2 : 2)) / 2 : (selectionMode.anchor[1] === 0 ? borders.y + borders.height - newCursorPosition[1] + selectionPadding * (selectionMode.anchor[1] === 0 ? -2 : 2) : newCursorPosition[1] - borders.y - selectionPadding * (selectionMode.anchor[1] === 0 ? -2 : 2)) / 2;
163
161
 
164
162
  var shapeWithNewDimensions = _objectSpread(_objectSpread({}, originalShape), {
165
163
  radius: Math.abs(scaledRadius)
166
164
  });
167
165
 
168
- var _getShapeInfos5 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions),
166
+ var _getShapeInfos5 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding),
169
167
  shapeWithNewDimensionsCenter = _getShapeInfos5.center;
170
168
 
171
169
  var _getCircleOppositeAnc = getCircleOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape),
@@ -185,22 +183,22 @@ var resizeCircle = function resizeCircle(cursorPosition, canvasOffset, originalS
185
183
 
186
184
  exports.resizeCircle = resizeCircle;
187
185
 
188
- var resizeEllipse = function resizeEllipse(cursorPosition, canvasOffset, originalShape, selectionMode) {
189
- var _getShapeInfos6 = (0, _shapeData.getShapeInfos)(originalShape),
186
+ var resizeEllipse = function resizeEllipse(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
187
+ var _getShapeInfos6 = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
190
188
  center = _getShapeInfos6.center,
191
189
  borders = _getShapeInfos6.borders;
192
190
 
193
191
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
194
192
  var newCursorPosition = [cursorPositionBeforeResize[0], cursorPositionBeforeResize[1]];
195
- var scaledRadiusX = selectionMode.anchor[0] === 0.5 ? originalShape.radiusX : (selectionMode.anchor[0] === 0 ? borders.x + borders.width - newCursorPosition[0] + _shapes.SELECTION_PADDING * (selectionMode.anchor[0] === 0 ? -2 : 2) : newCursorPosition[0] - borders.x - _shapes.SELECTION_PADDING * (selectionMode.anchor[0] === 0 ? -2 : 2)) / 2;
196
- var scaledRadiusY = selectionMode.anchor[1] === 0.5 ? originalShape.radiusY : (selectionMode.anchor[1] === 0 ? borders.y + borders.height - newCursorPosition[1] + _shapes.SELECTION_PADDING * (selectionMode.anchor[1] === 0 ? -2 : 2) : newCursorPosition[1] - borders.y - _shapes.SELECTION_PADDING * (selectionMode.anchor[1] === 0 ? -2 : 2)) / 2;
193
+ var scaledRadiusX = selectionMode.anchor[0] === 0.5 ? originalShape.radiusX : (selectionMode.anchor[0] === 0 ? borders.x + borders.width - newCursorPosition[0] + selectionPadding * (selectionMode.anchor[0] === 0 ? -2 : 2) : newCursorPosition[0] - borders.x - selectionPadding * (selectionMode.anchor[0] === 0 ? -2 : 2)) / 2;
194
+ var scaledRadiusY = selectionMode.anchor[1] === 0.5 ? originalShape.radiusY : (selectionMode.anchor[1] === 0 ? borders.y + borders.height - newCursorPosition[1] + selectionPadding * (selectionMode.anchor[1] === 0 ? -2 : 2) : newCursorPosition[1] - borders.y - selectionPadding * (selectionMode.anchor[1] === 0 ? -2 : 2)) / 2;
197
195
 
198
196
  var shapeWithNewDimensions = _objectSpread(_objectSpread({}, originalShape), {
199
197
  radiusX: Math.abs(scaledRadiusX),
200
198
  radiusY: Math.abs(scaledRadiusY)
201
199
  });
202
200
 
203
- var _getShapeInfos7 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions),
201
+ var _getShapeInfos7 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding),
204
202
  shapeWithNewDimensionsCenter = _getShapeInfos7.center;
205
203
 
206
204
  var _getEllipseOppositeAn = getEllipseOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape),
@@ -220,16 +218,16 @@ var resizeEllipse = function resizeEllipse(cursorPosition, canvasOffset, origina
220
218
 
221
219
  exports.resizeEllipse = resizeEllipse;
222
220
 
223
- var resizeRect = function resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode) {
224
- var keepRatio = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
221
+ var resizeRect = function resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
222
+ var keepRatio = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
225
223
 
226
- var _getShapeInfos8 = (0, _shapeData.getShapeInfos)(originalShape),
224
+ var _getShapeInfos8 = (0, _shapeData.getShapeInfos)(originalShape, selectionPadding),
227
225
  center = _getShapeInfos8.center,
228
226
  borders = _getShapeInfos8.borders;
229
227
 
230
228
  var cursorPositionBeforeResize = (0, _intersect.getPointPositionAfterCanvasTransformation)(cursorPosition, canvasOffset, originalShape.translation, originalShape.rotation, center);
231
- var scaledWidth = selectionMode.anchor[0] === 0.5 ? originalShape.width : selectionMode.anchor[0] === 0 ? borders.x + borders.width - cursorPositionBeforeResize[0] + _shapes.SELECTION_PADDING * -2 : cursorPositionBeforeResize[0] - borders.x - _shapes.SELECTION_PADDING * 2;
232
- var scaledHeight = selectionMode.anchor[1] === 0.5 ? originalShape.height : selectionMode.anchor[1] === 0 ? borders.y + borders.height - cursorPositionBeforeResize[1] + _shapes.SELECTION_PADDING * -2 : cursorPositionBeforeResize[1] - borders.y - _shapes.SELECTION_PADDING * 2;
229
+ var scaledWidth = selectionMode.anchor[0] === 0.5 ? originalShape.width : selectionMode.anchor[0] === 0 ? borders.x + borders.width - cursorPositionBeforeResize[0] + selectionPadding * -2 : cursorPositionBeforeResize[0] - borders.x - selectionPadding * 2;
230
+ var scaledHeight = selectionMode.anchor[1] === 0.5 ? originalShape.height : selectionMode.anchor[1] === 0 ? borders.y + borders.height - cursorPositionBeforeResize[1] + selectionPadding * -2 : cursorPositionBeforeResize[1] - borders.y - selectionPadding * 2;
233
231
 
234
232
  var _ref = keepRatio ? getNormalizedSize(originalShape, scaledWidth, scaledHeight) : [scaledWidth, scaledHeight],
235
233
  _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
@@ -241,7 +239,7 @@ var resizeRect = function resizeRect(cursorPosition, canvasOffset, originalShape
241
239
  height: Math.abs(heightWithRatio)
242
240
  });
243
241
 
244
- var _getShapeInfos9 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions),
242
+ var _getShapeInfos9 = (0, _shapeData.getShapeInfos)(shapeWithNewDimensions, selectionPadding),
245
243
  shapeWithNewDimensionsCenter = _getShapeInfos9.center;
246
244
 
247
245
  var _getRectOppositeAncho5 = getRectOppositeAnchorAbsolutePosition(selectionMode.anchor, center, originalShape),
@@ -261,10 +259,10 @@ var resizeRect = function resizeRect(cursorPosition, canvasOffset, originalShape
261
259
 
262
260
  exports.resizeRect = resizeRect;
263
261
 
264
- var resizeText = function resizeText(ctx, cursorPosition, canvasOffset, originalShape, selectionMode) {
262
+ var resizeText = function resizeText(ctx, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
265
263
  var _newRect$style;
266
264
 
267
- var newRect = resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, true);
265
+ var newRect = resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);
268
266
  return _objectSpread(_objectSpread({}, newRect), {}, {
269
267
  fontSize: calculateTextFontSize(ctx, newRect.value, newRect.width, (_newRect$style = newRect.style) === null || _newRect$style === void 0 ? void 0 : _newRect$style.fontFamily)
270
268
  });
@@ -305,20 +303,20 @@ var getCircleOppositeAnchorAbsolutePosition = function getCircleOppositeAnchorAb
305
303
  return (0, _intersect.getPointPositionBeforeCanvasTransformation)([oppositeX, oppositeY], shape.translation, shape.rotation, center);
306
304
  };
307
305
 
308
- var resizePicture = function resizePicture(cursorPosition, canvasOffset, originalShape, selectionMode) {
309
- return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, true);
306
+ var resizePicture = function resizePicture(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
307
+ return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);
310
308
  };
311
309
 
312
310
  exports.resizePicture = resizePicture;
313
311
 
314
- var resizeShape = function resizeShape(ctx, shape, cursorPosition, canvasOffset, originalShape, selectionMode) {
315
- if (shape.type === 'line' || shape.type === 'polygon' || shape.type === 'curve') return resizeLine(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'brush') return resizeBrush(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'circle') return resizeCircle(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'ellipse') return resizeEllipse(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'rect') return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'square') return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, true);else if (shape.type === 'text') return resizeText(ctx, cursorPosition, canvasOffset, originalShape, selectionMode);else if (shape.type === 'picture') return resizePicture(cursorPosition, canvasOffset, originalShape, selectionMode);
312
+ var resizeShape = function resizeShape(ctx, shape, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding) {
313
+ if (shape.type === 'line' || shape.type === 'polygon' || shape.type === 'curve') return resizeLine(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'brush') return resizeBrush(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'circle') return resizeCircle(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'ellipse') return resizeEllipse(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'rect') return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'square') return resizeRect(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding, true);else if (shape.type === 'text') return resizeText(ctx, cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);else if (shape.type === 'picture') return resizePicture(cursorPosition, canvasOffset, originalShape, selectionMode, selectionPadding);
316
314
  return shape;
317
315
  };
318
316
 
319
317
  exports.resizeShape = resizeShape;
320
318
 
321
- var transformShape = function transformShape(ctx, shape, cursorPosition, canvasOffset, selectionMode) {
319
+ var transformShape = function transformShape(ctx, shape, cursorPosition, canvasOffset, selectionMode, selectionPadding) {
322
320
  if (selectionMode.mode === _Mode.SelectionModeLib.brush) {
323
321
  return paintNewPointToShape(shape, cursorPosition);
324
322
  } else if (selectionMode.mode === _Mode.SelectionModeLib.translate) {
@@ -326,7 +324,7 @@ var transformShape = function transformShape(ctx, shape, cursorPosition, canvasO
326
324
  } else if (selectionMode.mode === _Mode.SelectionModeLib.rotate) {
327
325
  return rotateShape(shape, cursorPosition, selectionMode.originalShape, selectionMode.cursorStartPosition, selectionMode.center);
328
326
  } else if (selectionMode.mode === _Mode.SelectionModeLib.resize) {
329
- return resizeShape(ctx, shape, cursorPosition, canvasOffset, selectionMode.originalShape, selectionMode);
327
+ return resizeShape(ctx, shape, cursorPosition, canvasOffset, selectionMode.originalShape, selectionMode, selectionPadding);
330
328
  }
331
329
 
332
330
  return shape;
@@ -368,4 +366,20 @@ var radiansToDegrees = function radiansToDegrees(radians) {
368
366
  return radians * 180 / Math.PI;
369
367
  };
370
368
 
371
- exports.radiansToDegrees = radiansToDegrees;
369
+ exports.radiansToDegrees = radiansToDegrees;
370
+
371
+ var fitContentInsideContainer = function fitContentInsideContainer(contentWidth, contentHeight, containerWidth, containerHeight) {
372
+ var shouldFillContainer = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
373
+ var contentRatio = contentWidth / contentHeight;
374
+ var containerRatio = containerWidth / containerHeight;
375
+
376
+ if (contentRatio > containerRatio) {
377
+ var newWidth = shouldFillContainer ? containerWidth : Math.min(contentWidth, containerWidth);
378
+ return [newWidth, newWidth / contentRatio];
379
+ } else {
380
+ var newHeight = shouldFillContainer ? containerHeight : Math.min(contentHeight, containerHeight);
381
+ return [newHeight * contentRatio, newHeight];
382
+ }
383
+ };
384
+
385
+ exports.fitContentInsideContainer = fitContentInsideContainer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yorab/react-paint",
3
- "version": "0.2.2",
3
+ "version": "0.4.1",
4
4
  "keywords": [],
5
5
  "author": "Yohann Rabatel",
6
6
  "license": "MIT",
@@ -12,11 +12,8 @@
12
12
  "dist/"
13
13
  ],
14
14
  "dependencies": {
15
- "@linaria/babel-preset": "^3.0.0-beta.15",
16
15
  "@linaria/core": "^3.0.0-beta.15",
17
16
  "@linaria/react": "^3.0.0-beta.15",
18
- "@linaria/shaker": "^3.0.0-beta.15",
19
- "ajv": "8.11.0",
20
17
  "lodash": "^4.17.20"
21
18
  },
22
19
  "devDependencies": {
@@ -27,6 +24,8 @@
27
24
  "@babel/preset-react": "^7.16.7",
28
25
  "@babel/preset-typescript": "^7.16.7",
29
26
  "@babel/runtime": "^7.16.7",
27
+ "@linaria/babel-preset": "^3.0.0-beta.15",
28
+ "@linaria/shaker": "^3.0.0-beta.15",
30
29
  "@linaria/webpack-loader": "^3.0.0-beta.16",
31
30
  "@testing-library/jest-dom": "^5.11.4",
32
31
  "@testing-library/react": "^11.1.0",
@@ -54,7 +53,6 @@
54
53
  "react": "18.1.0",
55
54
  "react-dom": "18.1.0",
56
55
  "typescript": "^4.7.2",
57
- "typescript-json-schema": "^0.52.0",
58
56
  "webpack": "^5.66.0",
59
57
  "webpack-cli": "^4.9.1",
60
58
  "webpack-dev-server": "^4.7.3"
@@ -68,8 +66,9 @@
68
66
  "start": "webpack serve --config webpack.config.js --open",
69
67
  "test": "echo \"Error: no test specified\" && exit 1",
70
68
  "buildexamples": "webpack --config webpack.prod.config.js",
71
- "build": "npx babel src/ --extensions '.js,.ts,.tsx,.jsx' --copy-files --delete-dir-on-start -d dist && npm run declarationfiles",
72
- "buildwithcss" : "npm run build && npm run buildexamples && mv build/styles.css dist/styles.css"
69
+ "build": "npx babel src/ --extensions '.js,.ts,.tsx,.jsx' --copy-files --delete-dir-on-start -d dist",
70
+ "buildwithcss": "npm run build && npm run buildexamples && mv build/styles.css dist/styles.css",
71
+ "publish": "npm run buildwithcss && npm publish --access public"
73
72
  },
74
73
  "jest": {
75
74
  "transform": {
@@ -1,20 +0,0 @@
1
- import { MutableRefObject } from 'react';
2
- import { DrawableShapeJson, ShapeEnum } from 'types/Shapes';
3
- declare type AppType = {
4
- width?: number;
5
- height?: number;
6
- canGrow?: boolean;
7
- canShrink?: boolean;
8
- withLayouts?: 'always' | 'never' | 'visible' | 'hidden';
9
- shapes?: DrawableShapeJson[];
10
- className?: string;
11
- disabled?: boolean;
12
- onDataChanged?: () => void;
13
- availableTools?: ShapeEnum[];
14
- apiRef?: MutableRefObject<undefined | {
15
- getCurrentImage: () => string | undefined;
16
- getCurrentData: () => DrawableShapeJson[];
17
- }>;
18
- };
19
- declare const App: ({ width, height, canGrow, canShrink, withLayouts, shapes: shapesFromProps, className: classNameFromProps, disabled, onDataChanged, availableTools: availableToolsFromProps, apiRef }: AppType) => JSX.Element;
20
- export default App;
@@ -1,28 +0,0 @@
1
- import React from 'react';
2
- import { DrawableShape, Point, StyledShape, ToolsType } from 'types/Shapes';
3
- import { SelectionModeData } from 'types/Mode';
4
- declare type DrawerType = {
5
- withGrid: boolean;
6
- disabled?: boolean;
7
- canGrow?: boolean;
8
- width: number;
9
- height: number;
10
- shapes: DrawableShape[];
11
- saveShapes: () => void;
12
- addShape: (newShape: DrawableShape) => void;
13
- updateSingleShape: (updatedShape: DrawableShape) => void;
14
- selectedShape: DrawableShape | undefined;
15
- setSelectedShape: React.Dispatch<React.SetStateAction<DrawableShape | undefined>>;
16
- activeTool: ToolsType;
17
- setActiveTool: React.Dispatch<React.SetStateAction<ToolsType>>;
18
- canvasOffsetStartPosition: Point | undefined;
19
- setCanvasOffsetStartPosition: React.Dispatch<React.SetStateAction<Point | undefined>>;
20
- canvasOffset: Point;
21
- setCanvasOffset: React.Dispatch<React.SetStateAction<Point>>;
22
- defaultConf: StyledShape;
23
- isInsideComponent: boolean;
24
- selectionMode: SelectionModeData<number | Point>;
25
- setSelectionMode: React.Dispatch<React.SetStateAction<SelectionModeData<number | Point>>>;
26
- };
27
- declare const Canvas: React.ForwardRefExoticComponent<DrawerType & React.RefAttributes<HTMLCanvasElement>>;
28
- export default Canvas;
@@ -1,14 +0,0 @@
1
- import React from 'react';
2
- declare type CommonType = {
3
- hidden?: boolean;
4
- disabled?: boolean;
5
- selected?: boolean;
6
- icon?: string;
7
- };
8
- declare type ButtonType = CommonType & React.ButtonHTMLAttributes<HTMLButtonElement>;
9
- declare type FileInputType = CommonType & {
10
- type: 'file' | 'color';
11
- accept?: string;
12
- } & React.InputHTMLAttributes<HTMLInputElement | HTMLButtonElement>;
13
- declare const Button: React.ForwardRefExoticComponent<(ButtonType | FileInputType) & React.RefAttributes<HTMLButtonElement | HTMLInputElement>>;
14
- export default Button;
@@ -1,6 +0,0 @@
1
- /// <reference types="react" />
2
- export declare type LoadingType = {
3
- isLoading: boolean;
4
- };
5
- declare const Loading: ({ isLoading }: LoadingType) => JSX.Element | null;
6
- export default Loading;
@@ -1,8 +0,0 @@
1
- import { ReactNode } from 'react';
2
- declare type ModalType = {
3
- children: ReactNode;
4
- onClose: () => void;
5
- className?: string;
6
- };
7
- declare const Modal: ({ children, onClose, className }: ModalType) => JSX.Element;
8
- export default Modal;
@@ -1,10 +0,0 @@
1
- import React from 'react';
2
- declare type PanelType = {
3
- vertical?: boolean;
4
- children: React.ReactNode;
5
- title?: string;
6
- className?: string;
7
- alignment?: 'left' | 'right' | 'center';
8
- };
9
- declare const Panel: ({ children, vertical, title, className, alignment }: PanelType) => JSX.Element;
10
- export default Panel;
@@ -1,7 +0,0 @@
1
- /// <reference types="react" />
2
- import { SnackBarType } from 'hooks/useSnackbar';
3
- declare type SnackbarContainerType = {
4
- snackbarList: SnackBarType[];
5
- };
6
- declare const SnackbarContainer: ({ snackbarList }: SnackbarContainerType) => JSX.Element;
7
- export default SnackbarContainer;
@@ -1,12 +0,0 @@
1
- import React from 'react';
2
- declare type ShapeStyleColorType = {
3
- selectedSettings: string | undefined;
4
- setSelectedSettings: React.Dispatch<React.SetStateAction<string | undefined>>;
5
- title?: string;
6
- disabled?: boolean;
7
- field: string;
8
- value?: string | undefined;
9
- valueChanged: (field: string, value: string | number) => void;
10
- };
11
- declare const ColorField: ({ selectedSettings, setSelectedSettings, title, disabled, field, value, valueChanged }: ShapeStyleColorType) => JSX.Element;
12
- export default ColorField;
@@ -1,9 +0,0 @@
1
- /// <reference types="react" />
2
- import { DrawableShape } from 'types/Shapes';
3
- declare type DeleteShapeButtonType = {
4
- disabled?: boolean;
5
- selectedShape: DrawableShape;
6
- removeShape: (shape: DrawableShape) => void;
7
- };
8
- declare const DeleteButton: ({ disabled, selectedShape, removeShape }: DeleteShapeButtonType) => JSX.Element;
9
- export default DeleteButton;
@@ -1,10 +0,0 @@
1
- import React from 'react';
2
- declare type FontFamilyFieldType = {
3
- selectedSettings: string | undefined;
4
- setSelectedSettings: React.Dispatch<React.SetStateAction<string | undefined>>;
5
- disabled?: boolean;
6
- defaultValue?: number | string | undefined;
7
- valueChanged: (field: string, value: string | number) => void;
8
- };
9
- declare const FontFamilyField: ({ selectedSettings, setSelectedSettings, disabled, defaultValue, valueChanged }: FontFamilyFieldType) => JSX.Element;
10
- export default FontFamilyField;
@@ -1,8 +0,0 @@
1
- /// <reference types="react" />
2
- declare type LayoutButtonType = {
3
- disabled: boolean;
4
- withLayouts?: 'always' | 'never' | 'visible' | 'hidden';
5
- toggleLayoutPanel: () => void;
6
- };
7
- declare const LayoutButton: ({ withLayouts, disabled, toggleLayoutPanel }: LayoutButtonType) => JSX.Element;
8
- export default LayoutButton;
@@ -1,18 +0,0 @@
1
- import React from 'react';
2
- import { DrawableShape } from 'types/Shapes';
3
- declare type LayoutsType = {
4
- withGrid: boolean;
5
- setWithGrid: React.Dispatch<React.SetStateAction<boolean>>;
6
- disabled?: boolean;
7
- shapes: DrawableShape[];
8
- removeShape: (shape: DrawableShape) => void;
9
- toggleShapeVisibility: (shape: DrawableShape) => void;
10
- toggleShapeLock: (shape: DrawableShape) => void;
11
- selectedShape: DrawableShape | undefined;
12
- selectShape: (shape: DrawableShape) => void;
13
- moveShapes: (firstShapeId: string, lastShapeId: string) => void;
14
- withLayouts?: 'always' | 'never' | 'visible' | 'hidden';
15
- isLayoutPanelShown: boolean;
16
- };
17
- declare const Layouts: ({ withGrid, setWithGrid, disabled, shapes, removeShape, toggleShapeVisibility, toggleShapeLock, selectedShape, moveShapes, selectShape, withLayouts, isLayoutPanelShown }: LayoutsType) => JSX.Element | null;
18
- export default Layouts;