@yorab/react-paint 0.7.5 → 0.7.6
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/dist/utils/file.js +19 -13
- package/dist/utils/shapes/picture.js +11 -5
- package/package.json +1 -1
package/dist/utils/file.js
CHANGED
|
@@ -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.isSvg = exports.getCanvasImage = exports.getBase64Image = exports.fetchAndStringify = exports.encodeShapesInString = exports.downloadFile = exports.decodeJson = exports.decodeImportedData = exports.
|
|
8
|
+
exports.isSvg = exports.getCanvasImage = exports.getBase64Image = exports.fetchAndStringify = exports.encodeShapesInString = exports.downloadFile = exports.decodeJson = exports.decodeImportedData = exports.addSizeAndConvertSvgToObjectUrl = void 0;
|
|
9
9
|
|
|
10
10
|
var _shapes = require("../constants/shapes");
|
|
11
11
|
|
|
@@ -21,35 +21,40 @@ var _shapes2 = require("./shapes");
|
|
|
21
21
|
|
|
22
22
|
var _picture = require("../constants/picture");
|
|
23
23
|
|
|
24
|
-
const
|
|
25
|
-
var _inlineSVG$getAttribu;
|
|
26
|
-
|
|
24
|
+
const addSizeAndConvertSvgToObjectUrl = svgFileContent => {
|
|
27
25
|
const parser = new DOMParser();
|
|
28
26
|
const result = parser.parseFromString(svgFileContent, 'text/xml');
|
|
29
27
|
const inlineSVG = result.getElementsByTagName('svg')[0];
|
|
30
28
|
const svgWidth = inlineSVG.getAttribute('width');
|
|
31
29
|
const svgHeight = inlineSVG.getAttribute('height');
|
|
30
|
+
let svgContentToConvert;
|
|
32
31
|
|
|
33
32
|
if (svgWidth || svgHeight) {
|
|
34
|
-
|
|
33
|
+
svgContentToConvert = svgFileContent;
|
|
34
|
+
} else {
|
|
35
|
+
var _inlineSVG$getAttribu;
|
|
36
|
+
|
|
37
|
+
const viewBox = (_inlineSVG$getAttribu = inlineSVG.getAttribute('viewBox')) !== null && _inlineSVG$getAttribu !== void 0 ? _inlineSVG$getAttribu : "0 0 ".concat(_picture.PICTURE_DEFAULT_SIZE, " ").concat(_picture.PICTURE_DEFAULT_SIZE);
|
|
38
|
+
const [,, width, height] = viewBox.split(' ');
|
|
39
|
+
inlineSVG.setAttribute('width', width !== null && width !== void 0 ? width : _picture.PICTURE_DEFAULT_SIZE);
|
|
40
|
+
inlineSVG.setAttribute('height', height !== null && height !== void 0 ? height : _picture.PICTURE_DEFAULT_SIZE);
|
|
41
|
+
svgContentToConvert = new XMLSerializer().serializeToString(inlineSVG);
|
|
35
42
|
}
|
|
36
43
|
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const svg64 = btoa(new XMLSerializer().serializeToString(inlineSVG));
|
|
42
|
-
return 'data:image/svg+xml;base64,' + svg64;
|
|
44
|
+
const blob = new Blob([svgContentToConvert], {
|
|
45
|
+
type: 'image/svg+xml'
|
|
46
|
+
});
|
|
47
|
+
return URL.createObjectURL(blob);
|
|
43
48
|
};
|
|
44
49
|
|
|
45
|
-
exports.
|
|
50
|
+
exports.addSizeAndConvertSvgToObjectUrl = addSizeAndConvertSvgToObjectUrl;
|
|
46
51
|
|
|
47
52
|
const isSvg = fileOrUrl => (fileOrUrl instanceof File ? fileOrUrl.name : fileOrUrl).includes('.svg');
|
|
48
53
|
|
|
49
54
|
exports.isSvg = isSvg;
|
|
50
55
|
|
|
51
56
|
const fetchAndStringify = url => {
|
|
52
|
-
return isSvg(url) ? fetch(url).then(response => response.text()).then(svgFileContent =>
|
|
57
|
+
return isSvg(url) ? fetch(url).then(response => response.text()).then(svgFileContent => addSizeAndConvertSvgToObjectUrl(svgFileContent)) : fetch(url).then(response => response.blob()).then(myBlob => URL.createObjectURL(myBlob));
|
|
53
58
|
};
|
|
54
59
|
|
|
55
60
|
exports.fetchAndStringify = fetchAndStringify;
|
|
@@ -137,6 +142,7 @@ const decodeImportedData = async shapesForJson => {
|
|
|
137
142
|
const img = new Image();
|
|
138
143
|
const newPromise = new Promise((resolve, reject) => {
|
|
139
144
|
img.onload = () => {
|
|
145
|
+
URL.revokeObjectURL(img.src);
|
|
140
146
|
resolve();
|
|
141
147
|
};
|
|
142
148
|
|
|
@@ -36,25 +36,31 @@ const createPictureShape = (img, storedSrc, maxPictureWidth, maxPictureHeight) =
|
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
const createFilePicture = (img, resolve, file, maxPictureWidth, maxPictureHeight) => {
|
|
39
|
+
const initSrc = URL.createObjectURL(file);
|
|
40
|
+
|
|
39
41
|
img.onload = async () => {
|
|
42
|
+
URL.revokeObjectURL(img.src);
|
|
43
|
+
|
|
40
44
|
if ((0, _file.isSvg)(file) && !img.width && !img.height) {
|
|
41
45
|
const svgFileContent = await file.text();
|
|
42
|
-
const
|
|
43
|
-
if (svgFile === img.src) return; // prevent infinite loop
|
|
46
|
+
const svgUrl = (0, _file.addSizeAndConvertSvgToObjectUrl)(svgFileContent);
|
|
44
47
|
|
|
45
|
-
img.src
|
|
46
|
-
|
|
48
|
+
if (initSrc === img.src) {
|
|
49
|
+
img.src = svgUrl;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
const pictureShape = createPictureShape(img, img.src, maxPictureWidth, maxPictureHeight);
|
|
50
55
|
resolve(pictureShape);
|
|
51
56
|
};
|
|
52
57
|
|
|
53
|
-
img.src =
|
|
58
|
+
img.src = initSrc;
|
|
54
59
|
};
|
|
55
60
|
|
|
56
61
|
const createUrlPicture = (img, resolve, url, maxPictureWidth, maxPictureHeight) => {
|
|
57
62
|
img.onload = () => {
|
|
63
|
+
URL.revokeObjectURL(img.src);
|
|
58
64
|
const pictureShape = createPictureShape(img, url, maxPictureWidth, maxPictureHeight);
|
|
59
65
|
resolve(pictureShape);
|
|
60
66
|
};
|