2023-06-27 22:26:23 +00:00
|
|
|
|
import { useState } from "react";
|
|
|
|
|
import {Box, ButtonBase, useTheme, Typography, SxProps, Theme} from "@mui/material";
|
|
|
|
|
import UploadIcon from "../../assets/icons/UploadIcon";
|
|
|
|
|
import { SnackbarProvider, enqueueSnackbar } from 'notistack';
|
|
|
|
|
import Resizer from "react-image-file-resizer";
|
2023-07-12 23:34:10 +00:00
|
|
|
|
import JSZip from "jszip"
|
|
|
|
|
import saveAs from "file-saver"
|
2023-06-27 22:26:23 +00:00
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
text?: string;
|
|
|
|
|
sx?: SxProps<Theme>;
|
|
|
|
|
heightImg: string;
|
|
|
|
|
widthImg?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ({text, sx, heightImg, widthImg}: Props) => {
|
|
|
|
|
const theme = useTheme();
|
2023-07-12 23:34:10 +00:00
|
|
|
|
const callback = async (uri:any) => {
|
|
|
|
|
|
|
|
|
|
const zip = new JSZip();//создание зип архива
|
|
|
|
|
|
|
|
|
|
const idx = uri.indexOf('base64,') + 'base64,'.length; //обработка строки картинки
|
|
|
|
|
const content = uri.substring(idx); //обработка строки картинки
|
|
|
|
|
zip.file("fav.jpg", content, {base64: true}); //сохранение картинки в архив с именем "fav.jpg"
|
|
|
|
|
|
|
|
|
|
zip.generateAsync({type:"blob"}).then(function(content) { // скачивание архива
|
|
|
|
|
saveAs(content, "fav.zip"); // скачивание архива
|
|
|
|
|
}); // скачивание архива
|
2023-07-12 22:14:06 +00:00
|
|
|
|
};
|
2023-06-27 22:26:23 +00:00
|
|
|
|
const resizeImage = (imageFile:any) => {
|
2023-07-12 22:14:06 +00:00
|
|
|
|
console.log(imageFile[0])
|
2023-06-27 22:26:23 +00:00
|
|
|
|
try {
|
|
|
|
|
Resizer.imageFileResizer(
|
2023-07-12 22:14:06 +00:00
|
|
|
|
imageFile[0],
|
2023-06-27 22:26:23 +00:00
|
|
|
|
150,
|
|
|
|
|
150,
|
2023-07-12 23:34:10 +00:00
|
|
|
|
"jpg",
|
2023-06-27 22:26:23 +00:00
|
|
|
|
100,
|
|
|
|
|
0,
|
|
|
|
|
callback,
|
|
|
|
|
);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
2023-07-12 22:14:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-06-27 22:26:23 +00:00
|
|
|
|
const imgHC = (imgInp:any) => {
|
|
|
|
|
const [file] = imgInp.files
|
|
|
|
|
if (file.size < 5242880) {
|
|
|
|
|
setData(URL.createObjectURL(file))
|
|
|
|
|
} else {enqueueSnackbar('Размер картинки слишком велик')}
|
2023-07-12 22:14:06 +00:00
|
|
|
|
resizeImage(imgInp.files)
|
2023-06-27 22:26:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [data, setData] = useState("")
|
|
|
|
|
const [ready, setReady] = useState(false);
|
|
|
|
|
|
|
|
|
|
const dragenterHC = () => {
|
|
|
|
|
// console.log("onDragEnter")
|
|
|
|
|
setReady(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dragexitHC = () => {
|
|
|
|
|
// console.log("onDragExit")
|
|
|
|
|
setReady(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dropHC = (event: any) => {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
// console.log("onDrop")
|
|
|
|
|
setReady(false)
|
|
|
|
|
const file = event.dataTransfer.files[0]
|
|
|
|
|
console.log(event.dataTransfer.files[0])
|
|
|
|
|
if (file.size < 5242880) {
|
|
|
|
|
setData(URL.createObjectURL(file))
|
|
|
|
|
} else {enqueueSnackbar('Размер картинки слишком велик')}
|
|
|
|
|
// try {
|
|
|
|
|
// Resizer.imageFileResizer(
|
|
|
|
|
// file,
|
|
|
|
|
// 50,
|
|
|
|
|
// 50,
|
|
|
|
|
// "JPEG",
|
|
|
|
|
// 100,
|
|
|
|
|
// 0,
|
|
|
|
|
// callback,
|
|
|
|
|
// );
|
|
|
|
|
//
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.log(err);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dragOverHC = (event: any) => {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
// console.log("onDragOver")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ButtonBase component="label" sx={{justifyContent: 'flex-start'}} >
|
|
|
|
|
<input onChange={event => imgHC(event.target)} hidden accept="image/*" multiple type="file" />
|
|
|
|
|
<Box
|
|
|
|
|
|
|
|
|
|
onDragEnter={dragenterHC}
|
|
|
|
|
onDragExit={dragexitHC}
|
|
|
|
|
onDrop={dropHC}
|
|
|
|
|
onDragOver={dragOverHC}
|
|
|
|
|
|
|
|
|
|
sx={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
height: "120px",
|
|
|
|
|
position: "relative",
|
|
|
|
|
display: "flex",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
backgroundColor: theme.palette.background.default,
|
|
|
|
|
border: `1px solid ${ready? "red" : theme.palette.grey2.main}`,
|
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
opacity: data ? "0.5" : 1,
|
|
|
|
|
...sx
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
<UploadIcon />
|
|
|
|
|
<Typography sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
right: "10px",
|
|
|
|
|
bottom: "10px",
|
|
|
|
|
color: theme.palette.orange.main,
|
|
|
|
|
fontSize: "16px",
|
|
|
|
|
lineHeight: "19px",
|
|
|
|
|
textDecoration: "underline",
|
|
|
|
|
}}>{text}</Typography>
|
|
|
|
|
<SnackbarProvider style={{backgroundColor: theme.palette.brightPurple.main}}/>
|
|
|
|
|
{data ?
|
|
|
|
|
<img height={heightImg} width={widthImg} src={data}
|
|
|
|
|
style={{
|
|
|
|
|
position: "absolute", zIndex: "-1",
|
|
|
|
|
objectFit: "revert-layer",
|
|
|
|
|
}}/>
|
|
|
|
|
:
|
|
|
|
|
null
|
|
|
|
|
}
|
|
|
|
|
</Box>
|
|
|
|
|
</ButtonBase>
|
|
|
|
|
)
|
|
|
|
|
}
|