@etsoo/materialui 1.4.29 → 1.4.31

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.
@@ -8,7 +8,7 @@ export interface UserAvatarEditorToBlob {
8
8
  * User avatar editor on done handler
9
9
  */
10
10
  export interface UserAvatarEditorOnDoneHandler {
11
- (canvas: HTMLCanvasElement, toBlob: UserAvatarEditorToBlob, type: string): void | false;
11
+ (canvas: HTMLCanvasElement, toBlob: UserAvatarEditorToBlob, type: string): Promise<void | false | undefined>;
12
12
  }
13
13
  /**
14
14
  * User avatar editor props
@@ -38,6 +38,7 @@ export function UserAvatarEditor(props) {
38
38
  const buttonRef = React.createRef();
39
39
  // Preview image state
40
40
  const [previewImage, setPreviewImage] = React.useState(image);
41
+ React.useEffect(() => setPreviewImage(image), [image]);
41
42
  // Is ready state
42
43
  const [ready, setReady] = React.useState(false);
43
44
  // Editor states
@@ -75,6 +76,9 @@ export function UserAvatarEditor(props) {
75
76
  };
76
77
  // Handle image load
77
78
  const handleLoad = (imageInfo) => {
79
+ // Ignore small images
80
+ if (imageInfo.height < 10 || imageInfo.width < 10)
81
+ return;
78
82
  if (noHeight) {
79
83
  setHeight((imageInfo.height * width) / imageInfo.width);
80
84
  }
@@ -134,23 +138,25 @@ export function UserAvatarEditor(props) {
134
138
  to.height = heightCalculated;
135
139
  // Large photo, resize it
136
140
  // https://github.com/nodeca/pica
137
- picaInstance
138
- .resize(data, to, {
141
+ const canvas = await picaInstance.resize(data, to, {
139
142
  unsharpAmount: 160,
140
143
  unsharpRadius: 0.6,
141
144
  unsharpThreshold: 1
142
- })
143
- .then((result) => {
144
- if (onDone(result, toBlob, type.current) === false) {
145
- resetUI();
146
- }
147
145
  });
146
+ const result = await onDone(canvas, toBlob, type.current);
147
+ if (result === false) {
148
+ resetUI();
149
+ }
148
150
  }
149
- else if (onDone(data, toBlob, type.current) === false) {
150
- resetUI();
151
+ else {
152
+ const result = await onDone(data, toBlob, type.current);
153
+ if (result === false) {
154
+ resetUI();
155
+ }
151
156
  }
152
157
  };
153
158
  // Load the component
154
159
  const AE = React.lazy(() => import("react-avatar-editor"));
155
- return (_jsxs(Stack, { direction: "column", spacing: 0.5, width: containerWidth, children: [_jsx(FileUploadButton, { variant: "outlined", size: "medium", startIcon: _jsx(ImageIcon, {}), fullWidth: true, onUploadFiles: handleFileUpload, inputProps: { multiple: false, accept: "image/png, image/jpeg" }, children: selectFileLabel }), _jsxs(Stack, { direction: "row", spacing: 0.5, children: [_jsx(React.Suspense, { fallback: _jsx(Skeleton, { variant: "rounded", width: width, height: localHeight }), children: _jsx(AE, { ref: ref, border: border, width: width, height: localHeight, onLoadSuccess: handleLoad, image: previewImage ?? "", scale: editorState.scale, rotate: editorState.rotate }) }), _jsxs(ButtonGroup, { size: "small", orientation: "vertical", disabled: !ready, children: [_jsx(Button, { onClick: () => handleRotate(90), title: labels.rotateRight, children: _jsx(RotateRightIcon, {}) }), _jsx(Button, { onClick: () => handleRotate(-90), title: labels.rotateLeft, children: _jsx(RotateLeftIcon, {}) }), _jsx(Button, { onClick: handleReset, title: labels.reset, children: _jsx(ClearAllIcon, {}) })] })] }), _jsxs(Stack, { spacing: 0.5, direction: "row", sx: { paddingBottom: 2 }, alignItems: "center", children: [_jsx(IconButton, { size: "small", disabled: !ready || editorState.scale <= min, onClick: () => adjustScale(false), children: _jsx(RemoveIcon, {}) }), _jsx(Slider, { title: labels.zoom, disabled: !ready, min: min, max: max, step: step, value: editorState.scale, valueLabelDisplay: "auto", valueLabelFormat: (value) => `${Math.round(100 * value) / 100}`, marks: marks, onChange: handleZoom }), _jsx(IconButton, { size: "small", disabled: !ready || editorState.scale >= max, onClick: () => adjustScale(true), children: _jsx(AddIcon, {}) })] }), _jsx(Button, { ref: buttonRef, variant: "contained", startIcon: _jsx(DoneIcon, {}), disabled: !ready, onClick: handleDone, children: labels.done })] }));
160
+ return (_jsxs(Stack, { direction: "column", spacing: 0.5, width: containerWidth, children: [_jsx(FileUploadButton, { variant: "outlined", size: "medium", startIcon: _jsx(ImageIcon, {}), fullWidth: true, onUploadFiles: handleFileUpload, inputProps: { multiple: false, accept: "image/png, image/jpeg" }, children: selectFileLabel }), _jsxs(Stack, { direction: "row", spacing: 0.5, children: [_jsx(React.Suspense, { fallback: _jsx(Skeleton, { variant: "rounded", width: width, height: localHeight }), children: _jsx(AE, { ref: ref, border: border, width: width, height: localHeight, onLoadSuccess: handleLoad, image: previewImage ??
161
+ "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=", scale: editorState.scale, rotate: editorState.rotate }) }), _jsxs(ButtonGroup, { size: "small", orientation: "vertical", disabled: !ready, children: [_jsx(Button, { onClick: () => handleRotate(90), title: labels.rotateRight, children: _jsx(RotateRightIcon, {}) }), _jsx(Button, { onClick: () => handleRotate(-90), title: labels.rotateLeft, children: _jsx(RotateLeftIcon, {}) }), _jsx(Button, { onClick: handleReset, title: labels.reset, children: _jsx(ClearAllIcon, {}) })] })] }), _jsxs(Stack, { spacing: 0.5, direction: "row", sx: { paddingBottom: 2 }, alignItems: "center", children: [_jsx(IconButton, { size: "small", disabled: !ready || editorState.scale <= min, onClick: () => adjustScale(false), children: _jsx(RemoveIcon, {}) }), _jsx(Slider, { title: labels.zoom, disabled: !ready, min: min, max: max, step: step, value: editorState.scale, valueLabelDisplay: "auto", valueLabelFormat: (value) => `${Math.round(100 * value) / 100}`, marks: marks, onChange: handleZoom }), _jsx(IconButton, { size: "small", disabled: !ready || editorState.scale >= max, onClick: () => adjustScale(true), children: _jsx(AddIcon, {}) })] }), _jsx(Button, { ref: buttonRef, variant: "contained", startIcon: _jsx(DoneIcon, {}), disabled: !ready, onClick: handleDone, children: labels.done })] }));
156
162
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/materialui",
3
- "version": "1.4.29",
3
+ "version": "1.4.31",
4
4
  "description": "TypeScript Material-UI Implementation",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -36,9 +36,11 @@ export interface UserAvatarEditorToBlob {
36
36
  * User avatar editor on done handler
37
37
  */
38
38
  export interface UserAvatarEditorOnDoneHandler {
39
- (canvas: HTMLCanvasElement, toBlob: UserAvatarEditorToBlob, type: string):
40
- | void
41
- | false;
39
+ (
40
+ canvas: HTMLCanvasElement,
41
+ toBlob: UserAvatarEditorToBlob,
42
+ type: string
43
+ ): Promise<void | false | undefined>;
42
44
  }
43
45
 
44
46
  /**
@@ -143,6 +145,8 @@ export function UserAvatarEditor(props: UserAvatarEditorProps) {
143
145
  // Preview image state
144
146
  const [previewImage, setPreviewImage] = React.useState(image);
145
147
 
148
+ React.useEffect(() => setPreviewImage(image), [image]);
149
+
146
150
  // Is ready state
147
151
  const [ready, setReady] = React.useState(false);
148
152
 
@@ -194,6 +198,9 @@ export function UserAvatarEditor(props: UserAvatarEditorProps) {
194
198
 
195
199
  // Handle image load
196
200
  const handleLoad = (imageInfo: ImageState) => {
201
+ // Ignore small images
202
+ if (imageInfo.height < 10 || imageInfo.width < 10) return;
203
+
197
204
  if (noHeight) {
198
205
  setHeight((imageInfo.height * width) / imageInfo.width);
199
206
  }
@@ -269,19 +276,21 @@ export function UserAvatarEditor(props: UserAvatarEditorProps) {
269
276
 
270
277
  // Large photo, resize it
271
278
  // https://github.com/nodeca/pica
272
- picaInstance
273
- .resize(data, to, {
274
- unsharpAmount: 160,
275
- unsharpRadius: 0.6,
276
- unsharpThreshold: 1
277
- })
278
- .then((result) => {
279
- if (onDone(result, toBlob, type.current) === false) {
280
- resetUI();
281
- }
282
- });
283
- } else if (onDone(data, toBlob, type.current) === false) {
284
- resetUI();
279
+ const canvas = await picaInstance.resize(data, to, {
280
+ unsharpAmount: 160,
281
+ unsharpRadius: 0.6,
282
+ unsharpThreshold: 1
283
+ });
284
+
285
+ const result = await onDone(canvas, toBlob, type.current);
286
+ if (result === false) {
287
+ resetUI();
288
+ }
289
+ } else {
290
+ const result = await onDone(data, toBlob, type.current);
291
+ if (result === false) {
292
+ resetUI();
293
+ }
285
294
  }
286
295
  };
287
296
 
@@ -312,7 +321,10 @@ export function UserAvatarEditor(props: UserAvatarEditorProps) {
312
321
  width={width}
313
322
  height={localHeight}
314
323
  onLoadSuccess={handleLoad}
315
- image={previewImage ?? ""}
324
+ image={
325
+ previewImage ??
326
+ "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="
327
+ }
316
328
  scale={editorState.scale}
317
329
  rotate={editorState.rotate}
318
330
  />