@yorab/react-paint 0.6.4 → 0.7.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.
- package/README.md +5 -5
- package/dist/components/App.js +236 -423
- package/dist/components/Canvas.js +120 -139
- package/dist/components/common/Button.js +41 -50
- package/dist/components/common/Loading.js +24 -33
- package/dist/components/common/Modal.js +18 -17
- package/dist/components/common/Panel.js +20 -28
- package/dist/components/common/Snackbar.js +31 -38
- package/dist/components/settings/ColorField.js +59 -68
- package/dist/components/settings/DeleteButton.js +10 -9
- package/dist/components/settings/FontFamilyField.js +25 -20
- package/dist/components/settings/LayoutButton.js +11 -9
- package/dist/components/settings/Layouts.js +111 -111
- package/dist/components/settings/LineArrowField.js +25 -22
- package/dist/components/settings/LineTypeField.js +23 -20
- package/dist/components/settings/PointsNumberField.js +14 -13
- package/dist/components/settings/RangeField.js +35 -47
- package/dist/components/settings/SelectField.js +34 -43
- package/dist/components/settings/SettingsBar.js +100 -121
- package/dist/components/settings/ToggleField.js +18 -25
- package/dist/components/toolbox/EditTextBox.js +62 -49
- package/dist/components/toolbox/LoadFileTool.js +19 -21
- package/dist/components/toolbox/MenuGroup.js +66 -100
- package/dist/components/toolbox/PictureUrlInput.js +33 -62
- package/dist/components/toolbox/Tool.js +13 -13
- package/dist/components/toolbox/Toolbar.js +79 -113
- package/dist/components/toolbox/ToolbarGroup.js +60 -63
- package/dist/constants/app.js +51 -0
- package/dist/constants/icons.js +42 -48
- package/dist/constants/loading.js +8 -0
- package/dist/constants/shapes.js +10 -8
- package/dist/constants/snackbar.js +7 -15
- package/dist/constants/style.js +21 -18
- package/dist/constants/tools.js +99 -103
- package/dist/hooks/useComponent.js +15 -20
- package/dist/hooks/useDoubleClick.js +12 -12
- package/dist/hooks/useDrag.js +24 -32
- package/dist/hooks/useDrawableCanvas.js +106 -122
- package/dist/hooks/useKeyboard.js +80 -63
- package/dist/hooks/useResizeObserver.js +18 -16
- package/dist/hooks/useShapes.js +92 -144
- package/dist/hooks/useSnackbar.js +28 -58
- package/dist/styles.css +16 -13
- package/dist/types/Mode.js +1 -13
- package/dist/types/Shapes.js +1 -17
- package/dist/types/snackbar.js +5 -0
- package/dist/types/tools.js +1 -18
- package/dist/utils/canvas.js +49 -0
- package/dist/utils/data.js +15 -274
- package/dist/utils/dom.js +12 -0
- package/dist/utils/file.js +75 -120
- package/dist/utils/intersect.js +54 -131
- package/dist/utils/migration.js +59 -0
- package/dist/utils/selection.js +18 -20
- package/dist/utils/shapes/brush.js +135 -0
- package/dist/utils/shapes/circle.js +93 -0
- package/dist/utils/shapes/curve.js +55 -0
- package/dist/utils/shapes/default.js +73 -0
- package/dist/utils/shapes/ellipse.js +101 -0
- package/dist/utils/shapes/grid.js +36 -0
- package/dist/utils/shapes/index.js +341 -0
- package/dist/utils/shapes/line.js +179 -0
- package/dist/utils/shapes/picture.js +88 -0
- package/dist/utils/shapes/polygon.js +69 -0
- package/dist/utils/shapes/rectangle.js +100 -0
- package/dist/utils/shapes/text.js +128 -0
- package/dist/utils/shapes/triangle.js +24 -0
- package/dist/utils/string.js +10 -16
- package/dist/utils/style.js +11 -13
- package/dist/utils/toolbar.js +35 -49
- package/dist/utils/transform.js +47 -339
- package/dist/utils/trigo.js +42 -0
- package/package.json +6 -6
- package/Changelog.md +0 -119
- package/dist/types/keyboard.js +0 -28
- package/dist/utils/draw.js +0 -461
- package/dist/utils/shapeData.js +0 -173
package/dist/types/tools.js
CHANGED
|
@@ -2,21 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.ActionsEnum = void 0;
|
|
7
|
-
// default
|
|
8
|
-
var ActionsEnum;
|
|
9
|
-
exports.ActionsEnum = ActionsEnum;
|
|
10
|
-
|
|
11
|
-
(function (ActionsEnum) {
|
|
12
|
-
ActionsEnum["selection"] = "selection";
|
|
13
|
-
ActionsEnum["undo"] = "undo";
|
|
14
|
-
ActionsEnum["redo"] = "redo";
|
|
15
|
-
ActionsEnum["clear"] = "clear";
|
|
16
|
-
ActionsEnum["export"] = "export";
|
|
17
|
-
ActionsEnum["loadFile"] = "loadfile";
|
|
18
|
-
ActionsEnum["saveFile"] = "savefile";
|
|
19
|
-
ActionsEnum["move"] = "move";
|
|
20
|
-
ActionsEnum["uploadPicture"] = "uploadpicture";
|
|
21
|
-
ActionsEnum["addUrlPicture"] = "addurlpicture";
|
|
22
|
-
})(ActionsEnum || (exports.ActionsEnum = ActionsEnum = {}));
|
|
5
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.updateCanvasContext = exports.transformCanvas = exports.initCanvasContext = void 0;
|
|
7
|
+
|
|
8
|
+
var _shapes = require("../constants/shapes");
|
|
9
|
+
|
|
10
|
+
var _style = require("../constants/style");
|
|
11
|
+
|
|
12
|
+
const initCanvasContext = ctx => {
|
|
13
|
+
ctx.lineCap = 'round';
|
|
14
|
+
ctx.lineJoin = 'round';
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
exports.initCanvasContext = initCanvasContext;
|
|
18
|
+
|
|
19
|
+
const updateCanvasContext = function (ctx) {
|
|
20
|
+
let style = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
21
|
+
const {
|
|
22
|
+
fillColor = 'transparent',
|
|
23
|
+
globalAlpha = 100,
|
|
24
|
+
strokeColor = _shapes.SELECTION_DEFAULT_COLOR,
|
|
25
|
+
lineWidth = _shapes.SELECTION_DEFAULT_WIDTH,
|
|
26
|
+
lineDash = 0
|
|
27
|
+
} = style;
|
|
28
|
+
globalAlpha !== undefined && (ctx.globalAlpha = globalAlpha / 100);
|
|
29
|
+
fillColor && (ctx.fillStyle = fillColor);
|
|
30
|
+
strokeColor && (ctx.strokeStyle = strokeColor);
|
|
31
|
+
lineWidth && (ctx.lineWidth = lineWidth);
|
|
32
|
+
lineDash !== undefined && lineWidth && ctx.setLineDash([_style.LINE_DASH_DATA[lineDash][0] * lineWidth, _style.LINE_DASH_DATA[lineDash][1] * lineWidth]);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
exports.updateCanvasContext = updateCanvasContext;
|
|
36
|
+
|
|
37
|
+
const transformCanvas = (ctx, responsiveScale, canvasOffset, rotation, translation) => {
|
|
38
|
+
ctx.save();
|
|
39
|
+
ctx.scale(responsiveScale, responsiveScale);
|
|
40
|
+
ctx.translate(canvasOffset[0], canvasOffset[1]);
|
|
41
|
+
|
|
42
|
+
if (rotation !== 0) {
|
|
43
|
+
ctx.translate(translation[0], translation[1]);
|
|
44
|
+
ctx.rotate(rotation);
|
|
45
|
+
ctx.translate(-translation[0], -translation[1]);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
exports.transformCanvas = transformCanvas;
|
package/dist/utils/data.js
CHANGED
|
@@ -5,304 +5,45 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports.
|
|
9
|
-
|
|
10
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
8
|
+
exports.cleanShapesBeforeExport = exports.buildDataToExport = exports.addDefaultAndTempShapeProps = void 0;
|
|
13
9
|
|
|
14
10
|
var _fp = _interopRequireDefault(require("lodash/fp"));
|
|
15
11
|
|
|
16
|
-
var _transform = require("./transform");
|
|
17
|
-
|
|
18
|
-
var _Shapes = require("../types/Shapes");
|
|
19
|
-
|
|
20
12
|
var _file = require("./file");
|
|
21
13
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
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; }
|
|
27
|
-
|
|
28
|
-
var createPicture = function createPicture(fileOrUrl, maxPictureWidth, maxPictureHeight) {
|
|
29
|
-
return new Promise(function (resolve, reject) {
|
|
30
|
-
var img = new Image();
|
|
31
|
-
|
|
32
|
-
img.onload = function () {
|
|
33
|
-
var _fitContentInsideCont = (0, _transform.fitContentInsideContainer)(img.width, img.height, maxPictureWidth, maxPictureHeight, true),
|
|
34
|
-
_fitContentInsideCont2 = (0, _slicedToArray2["default"])(_fitContentInsideCont, 2),
|
|
35
|
-
width = _fitContentInsideCont2[0],
|
|
36
|
-
height = _fitContentInsideCont2[1];
|
|
37
|
-
|
|
38
|
-
var pictureShape = {
|
|
39
|
-
toolId: _tools.DEFAULT_SHAPE_PICTURE.id,
|
|
40
|
-
type: _Shapes.ShapeEnum.picture,
|
|
41
|
-
id: _fp["default"].uniqueId("".concat(_Shapes.ShapeEnum.picture, "_")),
|
|
42
|
-
x: (maxPictureWidth - width) / 2,
|
|
43
|
-
y: (maxPictureHeight - height) / 2,
|
|
44
|
-
width: width,
|
|
45
|
-
height: height,
|
|
46
|
-
src: fileOrUrl instanceof File ? img.src : fileOrUrl,
|
|
47
|
-
img: img,
|
|
48
|
-
translation: [0, 0],
|
|
49
|
-
rotation: 0
|
|
50
|
-
};
|
|
51
|
-
resolve(pictureShape);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
img.onerror = function () {
|
|
55
|
-
reject(new Error('Some images could not be loaded'));
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
if (fileOrUrl instanceof File) {
|
|
59
|
-
img.src = URL.createObjectURL(fileOrUrl);
|
|
60
|
-
} else {
|
|
61
|
-
(0, _file.fetchAndStringify)(fileOrUrl).then(function (picData) {
|
|
62
|
-
img.src = picData;
|
|
63
|
-
})["catch"](function () {
|
|
64
|
-
img.src = ''; // to trigger onerror
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
img.onerror = function () {
|
|
69
|
-
reject(new Error('Error while loading the src'));
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
setTimeout(function () {
|
|
73
|
-
reject(new Error('Timeout while loading the src'));
|
|
74
|
-
}, 4000);
|
|
75
|
-
});
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
exports.createPicture = createPicture;
|
|
79
|
-
|
|
80
|
-
var createShape = function createShape(ctx, shape, cursorPosition) {
|
|
81
|
-
var _shape$settings$fontB, _shape$settings, _shape$settings$fontI, _shape$settings2;
|
|
82
|
-
|
|
83
|
-
switch (shape.type) {
|
|
84
|
-
case _Shapes.ShapeEnum.brush:
|
|
85
|
-
shape.settings;
|
|
86
|
-
return {
|
|
87
|
-
toolId: shape.id,
|
|
88
|
-
type: _Shapes.ShapeEnum.brush,
|
|
89
|
-
id: _fp["default"].uniqueId("".concat(shape.type, "_")),
|
|
90
|
-
points: [[cursorPosition]],
|
|
91
|
-
translation: [0, 0],
|
|
92
|
-
rotation: 0,
|
|
93
|
-
style: {
|
|
94
|
-
globalAlpha: shape.settings.opacity["default"],
|
|
95
|
-
strokeColor: shape.settings.strokeColor["default"],
|
|
96
|
-
lineWidth: shape.settings.lineWidth["default"],
|
|
97
|
-
lineDash: shape.settings.lineDash["default"]
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
case _Shapes.ShapeEnum.line:
|
|
102
|
-
return {
|
|
103
|
-
toolId: shape.id,
|
|
104
|
-
type: _Shapes.ShapeEnum.line,
|
|
105
|
-
id: _fp["default"].uniqueId("".concat(shape.type, "_")),
|
|
106
|
-
points: [cursorPosition, cursorPosition],
|
|
107
|
-
translation: [0, 0],
|
|
108
|
-
rotation: 0,
|
|
109
|
-
style: {
|
|
110
|
-
globalAlpha: shape.settings.opacity["default"],
|
|
111
|
-
strokeColor: shape.settings.strokeColor["default"],
|
|
112
|
-
lineWidth: shape.settings.lineWidth["default"],
|
|
113
|
-
lineDash: shape.settings.lineDash["default"],
|
|
114
|
-
lineArrow: shape.settings.lineArrow["default"]
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
case _Shapes.ShapeEnum.polygon:
|
|
119
|
-
return {
|
|
120
|
-
toolId: shape.id,
|
|
121
|
-
type: _Shapes.ShapeEnum.polygon,
|
|
122
|
-
id: _fp["default"].uniqueId("".concat(shape.type, "_")),
|
|
123
|
-
points: _fp["default"].flow(_fp["default"].range(0), _fp["default"].map(function () {
|
|
124
|
-
return cursorPosition;
|
|
125
|
-
}))(shape.settings.pointsCount["default"]),
|
|
126
|
-
translation: [0, 0],
|
|
127
|
-
rotation: 0,
|
|
128
|
-
style: {
|
|
129
|
-
globalAlpha: shape.settings.opacity["default"],
|
|
130
|
-
fillColor: shape.settings.fillColor["default"],
|
|
131
|
-
strokeColor: shape.settings.strokeColor["default"],
|
|
132
|
-
lineWidth: shape.settings.lineWidth["default"],
|
|
133
|
-
lineDash: shape.settings.lineDash["default"],
|
|
134
|
-
pointsCount: shape.settings.pointsCount["default"]
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
case _Shapes.ShapeEnum.curve:
|
|
139
|
-
return {
|
|
140
|
-
toolId: shape.id,
|
|
141
|
-
type: _Shapes.ShapeEnum.curve,
|
|
142
|
-
id: _fp["default"].uniqueId("".concat(shape.type, "_")),
|
|
143
|
-
points: _fp["default"].flow(_fp["default"].range(0), _fp["default"].map(function () {
|
|
144
|
-
return cursorPosition;
|
|
145
|
-
}))(shape.settings.pointsCount["default"]),
|
|
146
|
-
translation: [0, 0],
|
|
147
|
-
rotation: 0,
|
|
148
|
-
style: {
|
|
149
|
-
globalAlpha: shape.settings.opacity["default"],
|
|
150
|
-
fillColor: shape.settings.fillColor["default"],
|
|
151
|
-
strokeColor: shape.settings.strokeColor["default"],
|
|
152
|
-
lineWidth: shape.settings.lineWidth["default"],
|
|
153
|
-
lineDash: shape.settings.lineDash["default"],
|
|
154
|
-
pointsCount: shape.settings.pointsCount["default"]
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
case _Shapes.ShapeEnum.rect:
|
|
159
|
-
case _Shapes.ShapeEnum.square:
|
|
160
|
-
return {
|
|
161
|
-
toolId: shape.id,
|
|
162
|
-
type: shape.type,
|
|
163
|
-
id: _fp["default"].uniqueId("".concat(shape.type, "_")),
|
|
164
|
-
x: cursorPosition[0],
|
|
165
|
-
y: cursorPosition[1],
|
|
166
|
-
width: 1,
|
|
167
|
-
height: 1,
|
|
168
|
-
translation: [0, 0],
|
|
169
|
-
rotation: 0,
|
|
170
|
-
style: {
|
|
171
|
-
globalAlpha: shape.settings.opacity["default"],
|
|
172
|
-
fillColor: shape.settings.fillColor["default"],
|
|
173
|
-
strokeColor: shape.settings.strokeColor["default"],
|
|
174
|
-
lineWidth: shape.settings.lineWidth["default"],
|
|
175
|
-
lineDash: shape.settings.lineDash["default"]
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
case _Shapes.ShapeEnum.text:
|
|
180
|
-
var defaultValue = ['Texte'];
|
|
181
|
-
var fontSize = (0, _transform.calculateTextFontSize)(ctx, defaultValue, 50, (_shape$settings$fontB = (_shape$settings = shape.settings) === null || _shape$settings === void 0 ? void 0 : _shape$settings.fontBold["default"]) !== null && _shape$settings$fontB !== void 0 ? _shape$settings$fontB : false, (_shape$settings$fontI = (_shape$settings2 = shape.settings) === null || _shape$settings2 === void 0 ? void 0 : _shape$settings2.fontItalic["default"]) !== null && _shape$settings$fontI !== void 0 ? _shape$settings$fontI : false, shape.settings.fontFamily["default"]);
|
|
182
|
-
return {
|
|
183
|
-
toolId: shape.id,
|
|
184
|
-
type: _Shapes.ShapeEnum.text,
|
|
185
|
-
id: _fp["default"].uniqueId("".concat(shape.type, "_")),
|
|
186
|
-
x: cursorPosition[0],
|
|
187
|
-
y: cursorPosition[1],
|
|
188
|
-
value: defaultValue,
|
|
189
|
-
fontSize: fontSize,
|
|
190
|
-
width: 50,
|
|
191
|
-
height: fontSize * (defaultValue.length || 1),
|
|
192
|
-
translation: [0, 0],
|
|
193
|
-
rotation: 0,
|
|
194
|
-
style: {
|
|
195
|
-
globalAlpha: shape.settings.opacity["default"],
|
|
196
|
-
strokeColor: shape.settings.strokeColor["default"],
|
|
197
|
-
fontFamily: shape.settings.fontFamily["default"]
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
case _Shapes.ShapeEnum.ellipse:
|
|
202
|
-
return {
|
|
203
|
-
toolId: shape.id,
|
|
204
|
-
type: _Shapes.ShapeEnum.ellipse,
|
|
205
|
-
id: _fp["default"].uniqueId("".concat(shape.type, "_")),
|
|
206
|
-
x: cursorPosition[0],
|
|
207
|
-
y: cursorPosition[1],
|
|
208
|
-
radiusX: 0,
|
|
209
|
-
radiusY: 0,
|
|
210
|
-
translation: [0, 0],
|
|
211
|
-
rotation: 0,
|
|
212
|
-
style: {
|
|
213
|
-
globalAlpha: shape.settings.opacity["default"],
|
|
214
|
-
fillColor: shape.settings.fillColor["default"],
|
|
215
|
-
strokeColor: shape.settings.strokeColor["default"],
|
|
216
|
-
lineWidth: shape.settings.lineWidth["default"],
|
|
217
|
-
lineDash: shape.settings.lineDash["default"]
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
case _Shapes.ShapeEnum.circle:
|
|
222
|
-
return {
|
|
223
|
-
toolId: shape.id,
|
|
224
|
-
type: _Shapes.ShapeEnum.circle,
|
|
225
|
-
id: _fp["default"].uniqueId("".concat(shape.type, "_")),
|
|
226
|
-
x: cursorPosition[0],
|
|
227
|
-
y: cursorPosition[1],
|
|
228
|
-
radius: 0,
|
|
229
|
-
translation: [0, 0],
|
|
230
|
-
rotation: 0,
|
|
231
|
-
style: {
|
|
232
|
-
globalAlpha: shape.settings.opacity["default"],
|
|
233
|
-
fillColor: shape.settings.fillColor["default"],
|
|
234
|
-
strokeColor: shape.settings.strokeColor["default"],
|
|
235
|
-
lineWidth: shape.settings.lineWidth["default"],
|
|
236
|
-
lineDash: shape.settings.lineDash["default"]
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
exports.createShape = createShape;
|
|
243
|
-
|
|
244
|
-
var copyShape = function copyShape(shape) {
|
|
245
|
-
return _objectSpread(_objectSpread({}, shape), {}, {
|
|
246
|
-
id: _fp["default"].uniqueId("".concat(shape.type, "_")),
|
|
247
|
-
translation: [shape.translation[0] + 20, shape.translation[1] + 20]
|
|
248
|
-
});
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
exports.copyShape = copyShape;
|
|
252
|
-
|
|
253
|
-
var roundValues = function roundValues(prop) {
|
|
254
|
-
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
|
|
255
|
-
|
|
256
|
-
if (_fp["default"].isArray(prop)) {
|
|
257
|
-
return prop.map(roundValues);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
if (_fp["default"].isObject(prop)) {
|
|
261
|
-
return _fp["default"].mapValues(roundValues, prop);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (_fp["default"].isNumber(prop)) {
|
|
265
|
-
return +prop.toFixed(precision);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return prop;
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
exports.roundValues = roundValues;
|
|
272
|
-
|
|
273
|
-
var cleanShapesBeforeExport = function cleanShapesBeforeExport(shapes) {
|
|
274
|
-
var propsToOmit = ['img', 'id'];
|
|
275
|
-
return shapes.map(function (shape) {
|
|
276
|
-
if (shape.type === _Shapes.ShapeEnum.picture) {
|
|
14
|
+
const cleanShapesBeforeExport = shapes => {
|
|
15
|
+
const propsToOmit = ['img', 'id'];
|
|
16
|
+
return shapes.map(shape => {
|
|
17
|
+
if (shape.type === 'picture') {
|
|
277
18
|
if (!shape.src.startsWith('http')) {
|
|
278
|
-
return
|
|
19
|
+
return _fp.default.omit(propsToOmit, { ...shape,
|
|
279
20
|
src: (0, _file.getBase64Image)(shape.img)
|
|
280
|
-
})
|
|
21
|
+
});
|
|
281
22
|
}
|
|
282
23
|
}
|
|
283
24
|
|
|
284
|
-
return
|
|
25
|
+
return _fp.default.omit(propsToOmit, shape);
|
|
285
26
|
});
|
|
286
27
|
};
|
|
287
28
|
|
|
288
29
|
exports.cleanShapesBeforeExport = cleanShapesBeforeExport;
|
|
289
30
|
|
|
290
|
-
|
|
31
|
+
const buildDataToExport = (shapes, width, height) => {
|
|
291
32
|
return {
|
|
292
33
|
shapes: cleanShapesBeforeExport(shapes),
|
|
293
34
|
config: {
|
|
294
|
-
width
|
|
295
|
-
height
|
|
35
|
+
width,
|
|
36
|
+
height
|
|
296
37
|
}
|
|
297
38
|
};
|
|
298
39
|
};
|
|
299
40
|
|
|
300
41
|
exports.buildDataToExport = buildDataToExport;
|
|
301
42
|
|
|
302
|
-
|
|
303
|
-
return
|
|
304
|
-
id: _fp
|
|
305
|
-
}
|
|
43
|
+
const addDefaultAndTempShapeProps = shape => {
|
|
44
|
+
return { ...shape,
|
|
45
|
+
id: _fp.default.uniqueId("".concat(shape.type, "_"))
|
|
46
|
+
};
|
|
306
47
|
};
|
|
307
48
|
|
|
308
49
|
exports.addDefaultAndTempShapeProps = addDefaultAndTempShapeProps;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isEventInsideNode = void 0;
|
|
7
|
+
|
|
8
|
+
const isEventInsideNode = (event, node) => {
|
|
9
|
+
return !(event.target instanceof Node) || !node ? false : node.contains(event.target);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
exports.isEventInsideNode = isEventInsideNode;
|
package/dist/utils/file.js
CHANGED
|
@@ -7,36 +7,26 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports.getCanvasImage = exports.getBase64Image = exports.fetchAndStringify = exports.encodeShapesInString = exports.downloadFile = exports.decodeJson = exports.decodeImportedData = void 0;
|
|
9
9
|
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
-
|
|
14
|
-
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
10
|
+
var _shapes = require("../constants/shapes");
|
|
15
11
|
|
|
16
12
|
var _fp = _interopRequireDefault(require("lodash/fp"));
|
|
17
13
|
|
|
18
|
-
var _Shapes = require("../types/Shapes");
|
|
19
|
-
|
|
20
14
|
var _data = require("./data");
|
|
21
15
|
|
|
22
|
-
var
|
|
16
|
+
var _canvas = require("./canvas");
|
|
23
17
|
|
|
24
|
-
|
|
18
|
+
var _migration = require("./migration");
|
|
25
19
|
|
|
26
|
-
|
|
20
|
+
var _shapes2 = require("./shapes");
|
|
27
21
|
|
|
28
|
-
|
|
29
|
-
return fetch(url).then(
|
|
30
|
-
return response.blob();
|
|
31
|
-
}).then(function (myBlob) {
|
|
32
|
-
return URL.createObjectURL(myBlob);
|
|
33
|
-
});
|
|
22
|
+
const fetchAndStringify = url => {
|
|
23
|
+
return fetch(url).then(response => response.blob()).then(myBlob => URL.createObjectURL(myBlob));
|
|
34
24
|
};
|
|
35
25
|
|
|
36
26
|
exports.fetchAndStringify = fetchAndStringify;
|
|
37
27
|
|
|
38
|
-
|
|
39
|
-
|
|
28
|
+
const downloadFile = (content, fileName) => {
|
|
29
|
+
const a = document.createElement('a');
|
|
40
30
|
a.href = content;
|
|
41
31
|
a.download = fileName;
|
|
42
32
|
document.body.appendChild(a);
|
|
@@ -46,33 +36,34 @@ var downloadFile = function downloadFile(content, fileName) {
|
|
|
46
36
|
|
|
47
37
|
exports.downloadFile = downloadFile;
|
|
48
38
|
|
|
49
|
-
|
|
50
|
-
|
|
39
|
+
const getBase64Image = img => {
|
|
40
|
+
const canvas = document.createElement('canvas');
|
|
51
41
|
canvas.width = img.width;
|
|
52
42
|
canvas.height = img.height;
|
|
53
|
-
|
|
43
|
+
const ctx = canvas.getContext('2d');
|
|
54
44
|
if (!ctx) return '';
|
|
55
45
|
ctx.drawImage(img, 0, 0);
|
|
56
|
-
|
|
46
|
+
const dataURL = canvas.toDataURL('image/png');
|
|
57
47
|
return dataURL;
|
|
58
48
|
};
|
|
59
49
|
|
|
60
50
|
exports.getBase64Image = getBase64Image;
|
|
61
51
|
|
|
62
|
-
|
|
52
|
+
const encodeObjectToString = objectToEncode => {
|
|
63
53
|
return 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(objectToEncode));
|
|
64
54
|
};
|
|
65
55
|
|
|
66
|
-
|
|
67
|
-
|
|
56
|
+
const getCanvasImage = (shapes, canvasOffset, width, height, selectionPadding) => {
|
|
57
|
+
const newCanvas = document.createElement('canvas');
|
|
68
58
|
newCanvas.width = width;
|
|
69
59
|
newCanvas.height = height;
|
|
70
|
-
|
|
60
|
+
const ctx = newCanvas.getContext('2d');
|
|
71
61
|
if (!ctx) return '';
|
|
72
62
|
ctx.clearRect(0, 0, width, height);
|
|
63
|
+
(0, _canvas.initCanvasContext)(ctx);
|
|
73
64
|
|
|
74
|
-
for (
|
|
75
|
-
(0,
|
|
65
|
+
for (let i = shapes.length - 1; i >= 0; i--) {
|
|
66
|
+
(0, _shapes2.drawShape)(ctx, shapes[i], 1, canvasOffset, selectionPadding);
|
|
76
67
|
}
|
|
77
68
|
|
|
78
69
|
return newCanvas.toDataURL('image/png');
|
|
@@ -80,108 +71,72 @@ var getCanvasImage = function getCanvasImage(shapes, canvasOffset, width, height
|
|
|
80
71
|
|
|
81
72
|
exports.getCanvasImage = getCanvasImage;
|
|
82
73
|
|
|
83
|
-
|
|
84
|
-
|
|
74
|
+
const encodeShapesInString = (shapes, width, height) => {
|
|
75
|
+
const dataToExport = (0, _data.buildDataToExport)(shapes, width, height);
|
|
85
76
|
return encodeObjectToString(dataToExport);
|
|
86
77
|
};
|
|
87
78
|
|
|
88
79
|
exports.encodeShapesInString = encodeShapesInString;
|
|
89
80
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return _context.abrupt("return", new Promise(function (resolve, reject) {
|
|
97
|
-
var reader = new FileReader();
|
|
98
|
-
|
|
99
|
-
reader.onload = function (event) {
|
|
100
|
-
var _event$target;
|
|
101
|
-
|
|
102
|
-
var result = (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.result;
|
|
103
|
-
if (!result) return reject('Impossible de récupérer les données du fichier');
|
|
104
|
-
var json = JSON.parse(result);
|
|
105
|
-
resolve(json);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
reader.readAsText(file);
|
|
109
|
-
}));
|
|
110
|
-
|
|
111
|
-
case 1:
|
|
112
|
-
case "end":
|
|
113
|
-
return _context.stop();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}, _callee);
|
|
117
|
-
}));
|
|
81
|
+
const decodeJson = async file => {
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
const reader = new FileReader();
|
|
84
|
+
|
|
85
|
+
reader.onload = event => {
|
|
86
|
+
var _event$target;
|
|
118
87
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
88
|
+
const result = (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.result;
|
|
89
|
+
if (!result) return reject('Impossible de récupérer les données du fichier');
|
|
90
|
+
const json = JSON.parse(result);
|
|
91
|
+
resolve(json);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
reader.readAsText(file);
|
|
95
|
+
});
|
|
96
|
+
};
|
|
123
97
|
|
|
124
98
|
exports.decodeJson = decodeJson;
|
|
125
99
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
})["catch"](function () {
|
|
155
|
-
img.src = ''; // to trigger onerror
|
|
156
|
-
});
|
|
157
|
-
} else {
|
|
158
|
-
img.src = shape.src;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return (0, _data.addDefaultAndTempShapeProps)(_objectSpread(_objectSpread({}, shape), {}, {
|
|
162
|
-
img: img
|
|
163
|
-
}));
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return (0, _data.addDefaultAndTempShapeProps)(shape);
|
|
167
|
-
}), _fp["default"].compact)(jsonShapes);
|
|
168
|
-
_context2.next = 5;
|
|
169
|
-
return Promise.all(promisesArray);
|
|
170
|
-
|
|
171
|
-
case 5:
|
|
172
|
-
return _context2.abrupt("return", shapes);
|
|
173
|
-
|
|
174
|
-
case 6:
|
|
175
|
-
case "end":
|
|
176
|
-
return _context2.stop();
|
|
177
|
-
}
|
|
100
|
+
const decodeImportedData = async shapesForJson => {
|
|
101
|
+
const promisesArray = [];
|
|
102
|
+
const jsonShapes = shapesForJson.shapes;
|
|
103
|
+
|
|
104
|
+
const shapes = _fp.default.flow(shapes => (0, _migration.migrateShapesV065)(shapes), _fp.default.map(shape => {
|
|
105
|
+
if (!_shapes.ShapeTypeArray.includes(shape.type)) return null;
|
|
106
|
+
|
|
107
|
+
if (shape.type === 'picture') {
|
|
108
|
+
const img = new Image();
|
|
109
|
+
const newPromise = new Promise((resolve, reject) => {
|
|
110
|
+
img.onload = () => {
|
|
111
|
+
resolve();
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
img.onerror = () => {
|
|
115
|
+
reject(new Error('Some images could not be loaded'));
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
promisesArray.push(newPromise);
|
|
119
|
+
|
|
120
|
+
if (shape.src.startsWith('http')) {
|
|
121
|
+
fetchAndStringify(shape.src).then(picData => {
|
|
122
|
+
img.src = picData;
|
|
123
|
+
}).catch(() => {
|
|
124
|
+
img.src = ''; // to trigger onerror
|
|
125
|
+
});
|
|
126
|
+
} else {
|
|
127
|
+
img.src = shape.src;
|
|
178
128
|
}
|
|
179
|
-
}, _callee2);
|
|
180
|
-
}));
|
|
181
129
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
130
|
+
return (0, _data.addDefaultAndTempShapeProps)({ ...shape,
|
|
131
|
+
img
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return (0, _data.addDefaultAndTempShapeProps)(shape);
|
|
136
|
+
}), _fp.default.compact)(jsonShapes);
|
|
137
|
+
|
|
138
|
+
await Promise.all(promisesArray);
|
|
139
|
+
return shapes;
|
|
140
|
+
};
|
|
186
141
|
|
|
187
142
|
exports.decodeImportedData = decodeImportedData;
|