@yorab/react-paint 0.7.4 → 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.
@@ -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.addSizeAndConvertSvgToBase64 = void 0;
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 addSizeAndConvertSvgToBase64 = svgFileContent => {
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
- return 'data:image/svg+xml;base64,' + btoa(svgFileContent);
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 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
- 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.addSizeAndConvertSvgToBase64 = addSizeAndConvertSvgToBase64;
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 => addSizeAndConvertSvgToBase64(svgFileContent)) : fetch(url).then(response => response.blob()).then(myBlob => URL.createObjectURL(myBlob));
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
 
@@ -19,7 +19,7 @@ var _canvas = require("../canvas");
19
19
 
20
20
  var _rectangle = require("./rectangle");
21
21
 
22
- const createPictureShape = (img, maxPictureWidth, maxPictureHeight) => {
22
+ const createPictureShape = (img, storedSrc, maxPictureWidth, maxPictureHeight) => {
23
23
  const [width, height] = (0, _transform.fitContentInsideContainer)(img.width, img.height, maxPictureWidth, maxPictureHeight, true);
24
24
  return {
25
25
  toolId: _tools.DEFAULT_SHAPE_PICTURE.id,
@@ -29,33 +29,39 @@ const createPictureShape = (img, maxPictureWidth, maxPictureHeight) => {
29
29
  y: (maxPictureHeight - height) / 2,
30
30
  width,
31
31
  height,
32
- src: img.src,
32
+ src: storedSrc,
33
33
  img,
34
34
  rotation: 0
35
35
  };
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 svgFile = (0, _file.addSizeAndConvertSvgToBase64)(svgFileContent);
43
- if (svgFile === img.src) return; // prevent infinite loop
46
+ const svgUrl = (0, _file.addSizeAndConvertSvgToObjectUrl)(svgFileContent);
44
47
 
45
- img.src = svgFile;
46
- return;
48
+ if (initSrc === img.src) {
49
+ img.src = svgUrl;
50
+ return;
51
+ }
47
52
  }
48
53
 
49
- const pictureShape = createPictureShape(img, maxPictureWidth, maxPictureHeight);
54
+ const pictureShape = createPictureShape(img, img.src, maxPictureWidth, maxPictureHeight);
50
55
  resolve(pictureShape);
51
56
  };
52
57
 
53
- img.src = URL.createObjectURL(file);
58
+ img.src = initSrc;
54
59
  };
55
60
 
56
61
  const createUrlPicture = (img, resolve, url, maxPictureWidth, maxPictureHeight) => {
57
62
  img.onload = () => {
58
- const pictureShape = createPictureShape(img, maxPictureWidth, maxPictureHeight);
63
+ URL.revokeObjectURL(img.src);
64
+ const pictureShape = createPictureShape(img, url, maxPictureWidth, maxPictureHeight);
59
65
  resolve(pictureShape);
60
66
  };
61
67
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yorab/react-paint",
3
- "version": "0.7.4",
3
+ "version": "0.7.6",
4
4
  "keywords": [],
5
5
  "author": "Yohann Rabatel",
6
6
  "license": "MIT",
@@ -75,4 +75,4 @@
75
75
  "^.+\\.[t|j]sx?$": "babel-jest"
76
76
  }
77
77
  }
78
- }
78
+ }