@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
134 lines • 5.71 kB
JavaScript
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { Button, Upload, message } from 'antd';
import { ChevronLeftIcon, ImageUpIcon } from 'lucide-react';
import { memo, useCallback, useRef, useState } from 'react';
import AvatarEditor from 'react-avatar-editor';
import { Center, Flexbox } from 'react-layout-kit';
import Icon from "../Icon";
import { useStyles } from "./style";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var Dragger = Upload.Dragger;
var createUploadImageHandler = function createUploadImageHandler(onUploadImage) {
return function (file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.addEventListener('load', function () {
onUploadImage(String(reader.result));
});
};
};
var AvatarUploader = /*#__PURE__*/memo(function (_ref) {
var onChange = _ref.onChange,
texts = _ref.texts,
_ref$compressSize = _ref.compressSize,
compressSize = _ref$compressSize === void 0 ? 256 : _ref$compressSize,
onUpload = _ref.onUpload;
var editor = useRef(null);
var _useState = useState(''),
_useState2 = _slicedToArray(_useState, 2),
previewImage = _useState2[0],
setPreviewImage = _useState2[1];
var _useStyles = useStyles(),
styles = _useStyles.styles;
var beforeUpload = useCallback(function (file) {
var isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
if (!isJpgOrPng) {
message.error((texts === null || texts === void 0 ? void 0 : texts.fileTypeError) || 'You can only upload JPG/PNG file!');
return;
}
return createUploadImageHandler(function (avatar) {
setPreviewImage(avatar);
})(file);
}, []);
var handleUpload = function handleUpload() {
if (!editor.current) return;
var canvasScaled = editor.current.getImageScaledToCanvas();
var dataUrl = canvasScaled.toDataURL();
onChange(dataUrl);
if (!onUpload) return;
var file = new File([dataUrl], 'avatar.webp', {
type: 'image/webp'
});
onUpload === null || onUpload === void 0 || onUpload(file);
};
return /*#__PURE__*/_jsxs(Flexbox, {
padding: 10,
style: {
position: 'relative'
},
width: '100%',
children: [!previewImage && /*#__PURE__*/_jsx(Dragger, {
accept: 'image',
beforeUpload: beforeUpload,
itemRender: function itemRender() {
return void 0;
},
maxCount: 1,
multiple: false,
children: /*#__PURE__*/_jsxs(Center, {
gap: 16,
height: compressSize,
width: compressSize,
children: [/*#__PURE__*/_jsx(Icon, {
icon: ImageUpIcon,
size: {
fontSize: 48,
strokeWidth: 1.5
}
}), /*#__PURE__*/_jsx("div", {
children: (texts === null || texts === void 0 ? void 0 : texts.draggerDesc) || 'Click or Drag image to this area to upload'
})]
})
}), previewImage && /*#__PURE__*/_jsxs(Center, {
gap: 8,
style: {
position: 'relative'
},
width: '100%',
children: [/*#__PURE__*/_jsx(Flexbox, {
className: styles.editor,
children: /*#__PURE__*/_jsx(AvatarEditor, {
border: 0,
borderRadius: compressSize / 2,
height: compressSize,
image: previewImage,
ref: editor,
width: compressSize
})
}), /*#__PURE__*/_jsxs(Flexbox, {
gap: 8,
horizontal: true,
style: {
position: 'relative'
},
width: '100%',
children: [/*#__PURE__*/_jsx(Button, {
icon: /*#__PURE__*/_jsx(Icon, {
icon: ChevronLeftIcon
}),
onClick: function onClick() {
return setPreviewImage('');
},
style: {
flex: 'none'
}
}), /*#__PURE__*/_jsx(Button, {
onClick: handleUpload,
style: {
flex: 1,
fontWeight: 500
},
type: 'primary',
children: (texts === null || texts === void 0 ? void 0 : texts.uploadBtn) || 'Crop and Upload'
})]
})]
})]
});
});
export default AvatarUploader;