datasync-blob 1.1.2 → 1.1.4
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.
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
11
|
+
import { Component } from "react";
|
|
12
|
+
export class DsBlob extends Component {
|
|
13
|
+
constructor(props) {
|
|
14
|
+
super(props);
|
|
15
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
16
|
+
this.reduceImage = (e) => {
|
|
17
|
+
let imageSource = (e.path && e.path[0]) || e.srcElement; //Safari compliancy
|
|
18
|
+
if (this.debugging)
|
|
19
|
+
console.log("imageSource = ", imageSource);
|
|
20
|
+
var canvas = document.createElement('canvas'), context, width = imageSource.width, height = imageSource.height;
|
|
21
|
+
//Exact size props are set
|
|
22
|
+
if (this.props.width && this.props.height) {
|
|
23
|
+
//Check image size
|
|
24
|
+
if (width > this.props.width || height > this.props.height) {
|
|
25
|
+
alert("L'image est trop grande, le format requis est " + this.props.width + "x" + this.props.height);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
//Check image size
|
|
29
|
+
if (width < this.props.width || height < this.props.height) {
|
|
30
|
+
alert("L'image est trop petite, le format requis est " + this.props.width + "x" + this.props.height);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//Max size props are set
|
|
35
|
+
if (this.props.maxWidth && this.props.maxHeight) {
|
|
36
|
+
if (width > height) {
|
|
37
|
+
//Check resize is enabled
|
|
38
|
+
if (!this.props.reduceImage) {
|
|
39
|
+
alert("L'image est trop large ! la largeur maximale est de " + this.props.maxWidth);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (width > this.props.maxWidth) {
|
|
43
|
+
height *= this.props.maxWidth / width;
|
|
44
|
+
width = this.props.maxWidth;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
if (height > this.props.maxHeight) {
|
|
49
|
+
//Check resize is enabled
|
|
50
|
+
if (!this.props.reduceImage) {
|
|
51
|
+
alert("L'image est trop haute, la hauteur maximale est de " + this.props.maxHeight);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
width *= this.props.maxHeight / height;
|
|
55
|
+
height = this.props.maxHeight;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//Build 2d picture
|
|
60
|
+
canvas.width = width;
|
|
61
|
+
canvas.height = height;
|
|
62
|
+
context = canvas.getContext('2d');
|
|
63
|
+
context.drawImage(imageSource, 0, 0, width, height);
|
|
64
|
+
//Jpeg conversion
|
|
65
|
+
let jpegCompressionRatio = (this.props.jpegQuality && this.props.jpegQuality > 0 && this.props.jpegQuality < 1) ? this.props.jpegQuality : 1;
|
|
66
|
+
let canvasData = canvas.toDataURL('image/jpg', jpegCompressionRatio); //Jpeg conversion
|
|
67
|
+
if (this.debugging)
|
|
68
|
+
console.log("jpegCompressionRatio->", jpegCompressionRatio, " canvasData.length = ", canvasData.length);
|
|
69
|
+
if (this.props.removebase64) {
|
|
70
|
+
//remove base64 prefix if property is set
|
|
71
|
+
this.props.uploadPicture({ data: canvasData ? canvasData.substring("data:image/png;base64".length) : "", item_id: this.props.item_id });
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
//Keep base 64 prefix as it is
|
|
75
|
+
this.props.uploadPicture({ data: canvasData ? canvasData : "", item_id: this.props.item_id });
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
79
|
+
this.storeImageToImg = (e) => {
|
|
80
|
+
var image = document.createElement('img');
|
|
81
|
+
image.onload = this.reduceImage;
|
|
82
|
+
image.src = e.currentTarget.result;
|
|
83
|
+
};
|
|
84
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
85
|
+
this.readImage = (aPictureFile) => __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
let _reader = new FileReader();
|
|
87
|
+
_reader.onload = this.storeImageToImg;
|
|
88
|
+
_reader.readAsDataURL(aPictureFile);
|
|
89
|
+
});
|
|
90
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
91
|
+
this.resetUpload = () => {
|
|
92
|
+
this.props.uploadPicture({ data: "", item_id: this.props.item_id });
|
|
93
|
+
};
|
|
94
|
+
//-----------------------------------------------------------------------------------------------------------------------------------------
|
|
95
|
+
this.onInputChange = (e) => {
|
|
96
|
+
const errs = [];
|
|
97
|
+
const files = Array.from(e.target.files || []);
|
|
98
|
+
const types = ['image/png', 'image/jpeg', 'image/gif'];
|
|
99
|
+
const _1Mo = 1024 * 1024;
|
|
100
|
+
const SizeLimit = 9;
|
|
101
|
+
//Reset input cache value - to assure trigger if user select the same file again
|
|
102
|
+
e.target.value = "";
|
|
103
|
+
if (this.debugging)
|
|
104
|
+
console.log("onInputChange");
|
|
105
|
+
if (files.length > 1) {
|
|
106
|
+
const msg = 'Pas plus d\'une image à la fois';
|
|
107
|
+
alert(msg);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
files.forEach((file, i) => {
|
|
111
|
+
let errCount = errs.length;
|
|
112
|
+
if (types.every(type => file.type !== type)) {
|
|
113
|
+
errs.push(`'${file.type}' : format non supporté`);
|
|
114
|
+
}
|
|
115
|
+
if (file.size > (SizeLimit * _1Mo)) {
|
|
116
|
+
errs.push(`'${file.name}' image trop volumineuse (max:${SizeLimit}Méga-octets)`);
|
|
117
|
+
}
|
|
118
|
+
if (this.debugging)
|
|
119
|
+
console.log(`errCount = ${errCount} vs errs.length = ${errs.length}`);
|
|
120
|
+
if (errCount == errs.length) { //None new error occurs
|
|
121
|
+
if (this.debugging)
|
|
122
|
+
console.log("readImage::", file.name);
|
|
123
|
+
this.readImage(file);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
if (errs.length) {
|
|
127
|
+
return errs.forEach(err => alert(err));
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
this.state = { hover_input: false, hover_image: false };
|
|
131
|
+
this.debugging = false;
|
|
132
|
+
}
|
|
133
|
+
render() {
|
|
134
|
+
const upload_picture_label = {
|
|
135
|
+
cursor: "pointer"
|
|
136
|
+
};
|
|
137
|
+
const upload_picture_label_input = {
|
|
138
|
+
opacity: "0",
|
|
139
|
+
width: "0px",
|
|
140
|
+
height: "0px"
|
|
141
|
+
};
|
|
142
|
+
const div_show_image_hover = {
|
|
143
|
+
position: "relative",
|
|
144
|
+
margin: "0px",
|
|
145
|
+
opacity: "0.5"
|
|
146
|
+
};
|
|
147
|
+
const div_show_image = {
|
|
148
|
+
position: "relative",
|
|
149
|
+
margin: "0px"
|
|
150
|
+
};
|
|
151
|
+
const div_show_image_hover_input = {
|
|
152
|
+
display: "block",
|
|
153
|
+
top: "0px",
|
|
154
|
+
left: "0px",
|
|
155
|
+
position: "absolute"
|
|
156
|
+
};
|
|
157
|
+
const div_show_image_input = {
|
|
158
|
+
position: "absolute",
|
|
159
|
+
display: "none",
|
|
160
|
+
cursor: "pointer"
|
|
161
|
+
};
|
|
162
|
+
return (_jsxs("div", { children: [(!this.props.data) &&
|
|
163
|
+
_jsxs("label", { id: "upload-picture-label", className: this.props.buttonStyle, style: upload_picture_label, children: [this.props.Caption, _jsx("input", { style: upload_picture_label_input, id: "nested-input", type: "file", accept: "image/*", onChange: this.onInputChange, multiple: true })] }), (this.props.data && this.props.data.length > 0) &&
|
|
164
|
+
_jsxs("div", { className: "show-image", style: div_show_image, onMouseOver: () => { this.setState({ hover_input: true, hover_image: true }, () => { if (this.debugging)
|
|
165
|
+
console.log("onMouseOver"); }); }, onMouseLeave: () => { this.setState({ hover_input: false, hover_image: false }, () => { if (this.debugging)
|
|
166
|
+
console.log("onMouseLeave"); }); }, children: [_jsx("img", { style: this.state.hover_image ? div_show_image_hover : div_show_image, src: (this.props.removebase64) ? "data:image/png;base64" : "" + this.props.data, className: this.props.pictureStyle }), !this.props.readOnly &&
|
|
167
|
+
_jsx("input", { style: this.state.hover_input ? div_show_image_hover_input : div_show_image_input, className: "btn btn-primary delete", type: "button", value: "Effacer", onClick: this.resetUpload })] })] }));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Component, ChangeEvent } from "react";
|
|
2
|
+
interface DsBlobProps {
|
|
3
|
+
width?: number;
|
|
4
|
+
height?: number;
|
|
5
|
+
maxWidth?: number;
|
|
6
|
+
maxHeight?: number;
|
|
7
|
+
reduceImage?: boolean;
|
|
8
|
+
jpegQuality?: number;
|
|
9
|
+
removebase64?: boolean;
|
|
10
|
+
uploadPicture: (data: {
|
|
11
|
+
data: string;
|
|
12
|
+
item_id: any;
|
|
13
|
+
}) => void;
|
|
14
|
+
item_id: any;
|
|
15
|
+
data?: string;
|
|
16
|
+
buttonStyle?: string;
|
|
17
|
+
Caption?: string;
|
|
18
|
+
pictureStyle?: string;
|
|
19
|
+
readOnly?: boolean;
|
|
20
|
+
}
|
|
21
|
+
interface DsBlobState {
|
|
22
|
+
hover_input: boolean;
|
|
23
|
+
hover_image: boolean;
|
|
24
|
+
}
|
|
25
|
+
export declare class DsBlob extends Component<DsBlobProps, DsBlobState> {
|
|
26
|
+
private debugging;
|
|
27
|
+
constructor(props: DsBlobProps);
|
|
28
|
+
reduceImage: (e: Event) => void;
|
|
29
|
+
storeImageToImg: (e: ProgressEvent<FileReader>) => void;
|
|
30
|
+
readImage: (aPictureFile: File) => Promise<void>;
|
|
31
|
+
resetUpload: () => void;
|
|
32
|
+
onInputChange: (e: ChangeEvent<HTMLInputElement>) => void;
|
|
33
|
+
render(): import("react/jsx-runtime").JSX.Element;
|
|
34
|
+
}
|
|
35
|
+
export {};
|