datasync-blob 1.0.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/dist/DsBlob.css +33 -0
- package/dist/components/DsBlob.js +138 -0
- package/package.json +38 -0
package/dist/DsBlob.css
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#upload-picture-label{
|
|
2
|
+
cursor: pointer;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
#upload-picture-label input{
|
|
6
|
+
opacity: 0;
|
|
7
|
+
width: 0px;
|
|
8
|
+
height: 0px;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
div.show-image {
|
|
12
|
+
position: relative;
|
|
13
|
+
/*float:left;*/
|
|
14
|
+
margin:5px;
|
|
15
|
+
}
|
|
16
|
+
div.show-image:hover img{
|
|
17
|
+
opacity:0.5;
|
|
18
|
+
}
|
|
19
|
+
div.show-image:hover input {
|
|
20
|
+
display: block;
|
|
21
|
+
}
|
|
22
|
+
div.show-image input {
|
|
23
|
+
position:absolute;
|
|
24
|
+
display:none;
|
|
25
|
+
}
|
|
26
|
+
div.show-image input.update {
|
|
27
|
+
top:0;
|
|
28
|
+
left:0;
|
|
29
|
+
}
|
|
30
|
+
div.show-image input.delete {
|
|
31
|
+
top:0;
|
|
32
|
+
left:0px;
|
|
33
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DsBlob = void 0;
|
|
7
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
8
|
+
require("../DsBlob.css");
|
|
9
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
10
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
11
|
+
class DsBlob extends _react.Component {
|
|
12
|
+
constructor(props) {
|
|
13
|
+
super(props);
|
|
14
|
+
this.state = {};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
18
|
+
reduceImage = e => {
|
|
19
|
+
let imageSource = e.path && e.path[0] || e.srcElement; //Safari compliancy
|
|
20
|
+
//console.log("imageSource = ",imageSource);
|
|
21
|
+
var canvas = document.createElement('canvas'),
|
|
22
|
+
context,
|
|
23
|
+
maxWidth = 800,
|
|
24
|
+
maxHeight = 800,
|
|
25
|
+
width = imageSource.width,
|
|
26
|
+
height = imageSource.height;
|
|
27
|
+
if (width > height) {
|
|
28
|
+
if (width > maxWidth) {
|
|
29
|
+
height *= maxWidth / width;
|
|
30
|
+
width = maxWidth;
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
if (height > maxHeight) {
|
|
34
|
+
width *= maxHeight / height;
|
|
35
|
+
height = maxHeight;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
canvas.width = width;
|
|
39
|
+
canvas.height = height;
|
|
40
|
+
context = canvas.getContext('2d');
|
|
41
|
+
context.drawImage(imageSource, 0, 0, width, height);
|
|
42
|
+
let canvasData = canvas.toDataURL('image/jpg', 0.6); //Jpeg quality reduced from 0.8 down to 0.6
|
|
43
|
+
if (this.props.removebase64) {
|
|
44
|
+
//remove base64 prefix if property is set
|
|
45
|
+
this.props.uploadPicture({
|
|
46
|
+
data: canvasData ? canvasData.substring("data:image/png;base64".length) : "",
|
|
47
|
+
item_id: this.props.item_id
|
|
48
|
+
});
|
|
49
|
+
} else {
|
|
50
|
+
//Keep base 64 prefix as it is
|
|
51
|
+
this.props.uploadPicture({
|
|
52
|
+
data: canvasData ? canvasData : "",
|
|
53
|
+
item_id: this.props.item_id
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
59
|
+
storeImageToImg = e => {
|
|
60
|
+
var image = document.createElement('img');
|
|
61
|
+
image.onload = this.reduceImage;
|
|
62
|
+
image.src = e.currentTarget.result;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
66
|
+
readImage = async aPictureFile => {
|
|
67
|
+
let _reader = new FileReader();
|
|
68
|
+
_reader.onload = this.storeImageToImg;
|
|
69
|
+
_reader.readAsDataURL(aPictureFile);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
73
|
+
resetUpload = () => {
|
|
74
|
+
this.props.uploadPicture({
|
|
75
|
+
data: "",
|
|
76
|
+
item_id: this.props.item_id
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
81
|
+
onInputChange = e => {
|
|
82
|
+
const errs = [];
|
|
83
|
+
const files = Array.from(e.target.files);
|
|
84
|
+
const types = ['image/png', 'image/jpeg'];
|
|
85
|
+
const _1Mo = 1024 * 1024;
|
|
86
|
+
const SizeLimit = 9;
|
|
87
|
+
console.clear();
|
|
88
|
+
//Reset input cache value - to assure trigger if user select the same file again
|
|
89
|
+
e.target.value = null;
|
|
90
|
+
console.log("onInputChange");
|
|
91
|
+
if (files.length > 1) {
|
|
92
|
+
const msg = 'Pas plus d\'une image à la fois';
|
|
93
|
+
alert(msg);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
files.forEach((file, i) => {
|
|
97
|
+
let errCount = errs.length;
|
|
98
|
+
if (types.every(type => file.type !== type)) {
|
|
99
|
+
errs.push(`'${file.type}' : format non supporté`);
|
|
100
|
+
}
|
|
101
|
+
if (file.size > SizeLimit * _1Mo) {
|
|
102
|
+
errs.push(`'${file.name}' image trop volumineuse (max:${SizeLimit}Méga-octets)`);
|
|
103
|
+
}
|
|
104
|
+
console.log(`errCount = ${errCount} vs errs.length = ${errs.length}`);
|
|
105
|
+
if (errCount == errs.length) {
|
|
106
|
+
//None new error occurs
|
|
107
|
+
console.log("readImage:", file.name);
|
|
108
|
+
this.readImage(file);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
if (errs.length) {
|
|
112
|
+
return errs.forEach(err => alert(err));
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
render() {
|
|
116
|
+
return /*#__PURE__*/_react.default.createElement("div", null, !this.props.data && /*#__PURE__*/_react.default.createElement("label", {
|
|
117
|
+
id: "upload-picture-label",
|
|
118
|
+
className: this.props.buttonStyle
|
|
119
|
+
}, this.props.Caption, /*#__PURE__*/_react.default.createElement("input", {
|
|
120
|
+
id: "nested-input",
|
|
121
|
+
type: "file",
|
|
122
|
+
accept: "image/*",
|
|
123
|
+
onChange: this.onInputChange,
|
|
124
|
+
multiple: true
|
|
125
|
+
})), this.props.data && this.props.data.length > 0 && /*#__PURE__*/_react.default.createElement("div", {
|
|
126
|
+
class: "show-image"
|
|
127
|
+
}, /*#__PURE__*/_react.default.createElement("img", {
|
|
128
|
+
src: this.props.removebase64 ? "data:image/png;base64" : "" + this.props.data,
|
|
129
|
+
className: this.props.pictureStyle
|
|
130
|
+
}), !this.props.readOnly && /*#__PURE__*/_react.default.createElement("input", {
|
|
131
|
+
className: "btn btn-primary delete",
|
|
132
|
+
type: "button",
|
|
133
|
+
value: "Effacer",
|
|
134
|
+
onClick: this.resetUpload
|
|
135
|
+
})));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
exports.DsBlob = DsBlob;
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "datasync-blob",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Datasync Blob component",
|
|
5
|
+
"main": "./dist/components/DsBlob.js",
|
|
6
|
+
"files": ["./dist"],
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/pmabiala/datasync-blob.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/pmabiala/datasync-blob.git/issues",
|
|
13
|
+
"email": "pmabiala@me.com"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@babel/cli": "^7.24.1",
|
|
17
|
+
"@babel/core": "^7.24.4",
|
|
18
|
+
"@babel/preset-env": "^7.24.4",
|
|
19
|
+
"@babel/preset-react": "^7.24.1",
|
|
20
|
+
"@testing-library/react": "^14.2.2",
|
|
21
|
+
"jest": "^29.7.0",
|
|
22
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
23
|
+
"jest-transform-css": "^6.0.1",
|
|
24
|
+
"react": "^18.2.0",
|
|
25
|
+
"react-pdf": "^7.7.1"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": "^ 18.2.0"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "rm -r dist && babel src -d dist --ignore src/**/*.test.jsx && npm run copy-files",
|
|
32
|
+
"copy-files": "cp ./src/*.css dist/",
|
|
33
|
+
"test": "jest --config jest.config.json"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [],
|
|
36
|
+
"author": "",
|
|
37
|
+
"license": "ISC"
|
|
38
|
+
}
|