2023-09-13 14:14:27 +00:00
|
|
|
|
import React, { useState, useRef, useEffect, FC } from "react";
|
2023-09-07 12:20:59 +00:00
|
|
|
|
import ReactCrop, { Crop, PixelCrop } from "react-image-crop";
|
2023-09-13 14:14:27 +00:00
|
|
|
|
import {
|
|
|
|
|
Box,
|
|
|
|
|
Button,
|
|
|
|
|
Modal,
|
|
|
|
|
Slider,
|
|
|
|
|
Typography,
|
|
|
|
|
useMediaQuery,
|
|
|
|
|
useTheme,
|
|
|
|
|
} from "@mui/material";
|
2023-09-07 12:20:59 +00:00
|
|
|
|
|
|
|
|
|
import { canvasPreview } from "./utils/canvasPreview";
|
|
|
|
|
import { useDebounceEffect } from "./utils/useDebounceEffect";
|
|
|
|
|
|
|
|
|
|
import { ResetIcon } from "@icons/ResetIcon";
|
|
|
|
|
|
|
|
|
|
import "react-image-crop/dist/ReactCrop.css";
|
2023-09-07 15:09:50 +00:00
|
|
|
|
import { CropIcon } from "@icons/CropIcon";
|
2023-09-07 12:20:59 +00:00
|
|
|
|
|
|
|
|
|
interface Iprops {
|
|
|
|
|
opened: boolean;
|
|
|
|
|
onClose: React.Dispatch<React.SetStateAction<boolean>>;
|
2023-09-13 14:14:27 +00:00
|
|
|
|
picture?: string;
|
2023-09-07 12:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 14:14:27 +00:00
|
|
|
|
export const CropModal: FC<Iprops> = ({ opened, onClose, picture }) => {
|
2023-09-07 12:20:59 +00:00
|
|
|
|
const [imgSrc, setImgSrc] = useState("");
|
2023-09-08 20:17:05 +00:00
|
|
|
|
|
2023-09-07 12:20:59 +00:00
|
|
|
|
const imgRef = useRef<HTMLImageElement>(null);
|
|
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
const previewCanvasRef = useRef<HTMLCanvasElement>(null);
|
|
|
|
|
const hiddenAnchorRef = useRef<HTMLAnchorElement>(null);
|
|
|
|
|
const blobUrlRef = useRef("");
|
|
|
|
|
const [crop, setCrop] = useState<Crop>();
|
|
|
|
|
const [completedCrop, setCompletedCrop] = useState<PixelCrop>();
|
|
|
|
|
const [rotate, setRotate] = useState(0);
|
|
|
|
|
const [darken, setDarken] = useState(0);
|
|
|
|
|
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(786));
|
|
|
|
|
|
2023-09-13 14:14:27 +00:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (picture) {
|
|
|
|
|
setImgSrc(picture);
|
|
|
|
|
}
|
|
|
|
|
}, [picture]);
|
|
|
|
|
|
2023-09-07 15:09:50 +00:00
|
|
|
|
const styleModal = {
|
2023-09-07 12:20:59 +00:00
|
|
|
|
position: "absolute",
|
|
|
|
|
top: "50%",
|
|
|
|
|
left: "50%",
|
|
|
|
|
transform: "translate(-50%, -50%)",
|
|
|
|
|
width: isMobile ? "343px" : "620px",
|
|
|
|
|
bgcolor: "background.paper",
|
|
|
|
|
boxShadow: 24,
|
|
|
|
|
padding: "20px",
|
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const styleSlider = {
|
|
|
|
|
width: isMobile ? "350px" : "250px",
|
|
|
|
|
color: "#7E2AEA",
|
|
|
|
|
height: "12px",
|
|
|
|
|
"& .MuiSlider-track": {
|
|
|
|
|
border: "none",
|
|
|
|
|
},
|
|
|
|
|
"& .MuiSlider-rail": {
|
|
|
|
|
backgroundColor: "#F2F3F7",
|
2023-09-07 15:09:50 +00:00
|
|
|
|
border: `1px solid #9A9AAF`,
|
2023-09-07 12:20:59 +00:00
|
|
|
|
},
|
|
|
|
|
"& .MuiSlider-thumb": {
|
|
|
|
|
height: 26,
|
|
|
|
|
width: 26,
|
|
|
|
|
border: `6px solid #7E2AEA`,
|
|
|
|
|
backgroundColor: "white",
|
|
|
|
|
boxShadow: `0px 0px 0px 3px white,
|
|
|
|
|
0px 4px 4px 3px #C3C8DD`,
|
|
|
|
|
"&:focus, &:hover, &.Mui-active, &.Mui-focusVisible": {
|
|
|
|
|
boxShadow: `0px 0px 0px 3px white,
|
|
|
|
|
0px 4px 4px 3px #C3C8DD`,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const rotateImage = () => {
|
|
|
|
|
const newRotation = (rotate + 90) % 360;
|
|
|
|
|
setRotate(newRotation);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSelectFile = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
if (event.target.files && event.target.files.length > 0) {
|
|
|
|
|
setCrop(undefined);
|
|
|
|
|
const reader = new FileReader();
|
2023-09-13 14:14:27 +00:00
|
|
|
|
reader.addEventListener("load", () =>
|
|
|
|
|
setImgSrc(reader.result?.toString() || "")
|
|
|
|
|
);
|
2023-09-07 12:20:59 +00:00
|
|
|
|
reader.readAsDataURL(event.target.files[0]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onDownloadCropClick = () => {
|
|
|
|
|
if (!previewCanvasRef.current) {
|
|
|
|
|
throw new Error("Crop canvas does not exist");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const canvasCopy = document.createElement("canvas");
|
|
|
|
|
const ctx = canvasCopy.getContext("2d");
|
|
|
|
|
canvasCopy.width = previewCanvasRef.current.width;
|
|
|
|
|
canvasCopy.height = previewCanvasRef.current.height;
|
|
|
|
|
ctx!.filter = `brightness(${100 - darken}%)`;
|
|
|
|
|
ctx!.drawImage(previewCanvasRef.current, 0, 0);
|
|
|
|
|
|
|
|
|
|
canvasCopy.toBlob((blob) => {
|
|
|
|
|
if (!blob) {
|
|
|
|
|
throw new Error("Failed to create blob");
|
|
|
|
|
}
|
|
|
|
|
if (blobUrlRef.current) {
|
|
|
|
|
URL.revokeObjectURL(blobUrlRef.current);
|
|
|
|
|
}
|
|
|
|
|
blobUrlRef.current = URL.createObjectURL(blob);
|
|
|
|
|
hiddenAnchorRef.current!.href = blobUrlRef.current;
|
|
|
|
|
hiddenAnchorRef.current!.click();
|
2023-09-08 20:17:05 +00:00
|
|
|
|
|
|
|
|
|
setImgSrc(blobUrlRef.current);
|
2023-09-07 12:20:59 +00:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useDebounceEffect(
|
|
|
|
|
async () => {
|
2023-09-13 14:14:27 +00:00
|
|
|
|
if (
|
|
|
|
|
completedCrop?.width &&
|
|
|
|
|
completedCrop?.height &&
|
|
|
|
|
imgRef.current &&
|
|
|
|
|
previewCanvasRef.current
|
|
|
|
|
) {
|
|
|
|
|
canvasPreview(
|
|
|
|
|
imgRef.current,
|
|
|
|
|
previewCanvasRef.current,
|
|
|
|
|
completedCrop,
|
|
|
|
|
rotate
|
|
|
|
|
);
|
2023-09-07 12:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
100,
|
|
|
|
|
[completedCrop, rotate]
|
|
|
|
|
);
|
|
|
|
|
|
2023-09-08 20:17:05 +00:00
|
|
|
|
const [width, setWidth] = useState<number>(0);
|
2023-09-07 15:09:50 +00:00
|
|
|
|
|
|
|
|
|
const getImageSize = () => {
|
|
|
|
|
if (imgRef.current) {
|
|
|
|
|
const imageWidth = imgRef.current.naturalWidth;
|
|
|
|
|
const imageHeight = imgRef.current.naturalHeight;
|
|
|
|
|
|
|
|
|
|
const aspect = imageWidth / imageHeight;
|
|
|
|
|
|
2023-09-08 20:17:05 +00:00
|
|
|
|
|
2023-09-07 15:09:50 +00:00
|
|
|
|
if (aspect <= 1.333) {
|
|
|
|
|
setWidth(240);
|
|
|
|
|
}
|
|
|
|
|
if (aspect >= 1.5) {
|
|
|
|
|
setWidth(580);
|
|
|
|
|
}
|
|
|
|
|
if (aspect >= 1.778) {
|
|
|
|
|
setWidth(580);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-07 12:20:59 +00:00
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Modal
|
|
|
|
|
open={opened}
|
|
|
|
|
onClose={onClose}
|
|
|
|
|
aria-labelledby="modal-modal-title"
|
|
|
|
|
aria-describedby="modal-modal-description"
|
|
|
|
|
>
|
2023-09-07 15:09:50 +00:00
|
|
|
|
<Box sx={styleModal}>
|
2023-09-07 12:20:59 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
height: "320px",
|
|
|
|
|
padding: "10px",
|
|
|
|
|
backgroundSize: "cover",
|
|
|
|
|
backgroundRepeat: "no-repeat",
|
|
|
|
|
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-09-07 15:09:50 +00:00
|
|
|
|
{imgSrc && (
|
2023-09-07 12:20:59 +00:00
|
|
|
|
<ReactCrop
|
|
|
|
|
crop={crop}
|
|
|
|
|
onChange={(_, percentCrop) => setCrop(percentCrop)}
|
|
|
|
|
onComplete={(c) => setCompletedCrop(c)}
|
|
|
|
|
maxWidth={500}
|
2023-09-08 20:17:05 +00:00
|
|
|
|
minWidth={50}
|
2023-09-07 12:20:59 +00:00
|
|
|
|
maxHeight={320}
|
2023-09-08 20:17:05 +00:00
|
|
|
|
minHeight={50}
|
2023-09-07 12:20:59 +00:00
|
|
|
|
>
|
|
|
|
|
<img
|
2023-09-07 15:09:50 +00:00
|
|
|
|
onLoad={getImageSize}
|
2023-09-07 12:20:59 +00:00
|
|
|
|
ref={imgRef}
|
|
|
|
|
alt="Crop me"
|
|
|
|
|
src={imgSrc}
|
|
|
|
|
style={{
|
|
|
|
|
filter: `brightness(${100 - darken}%)`,
|
|
|
|
|
transform: ` rotate(${rotate}deg)`,
|
|
|
|
|
maxWidth: "580px",
|
|
|
|
|
maxHeight: "320px",
|
|
|
|
|
}}
|
2023-09-08 20:17:05 +00:00
|
|
|
|
width={width}
|
2023-09-07 12:20:59 +00:00
|
|
|
|
/>
|
|
|
|
|
</ReactCrop>
|
|
|
|
|
)}
|
|
|
|
|
</Box>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
color: "#7E2AEA",
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
fontSize: "16xp",
|
|
|
|
|
fontWeight: "600",
|
|
|
|
|
marginBottom: "50px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography sx={{ color: "#7E2AEA", lineHeight: "0px" }}>
|
|
|
|
|
{crop?.width ? Math.round(crop.width) + "px" : ""}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography sx={{ color: "#7E2AEA", lineHeight: "0px" }}>
|
2023-09-07 15:09:50 +00:00
|
|
|
|
{crop?.height ? Math.round(crop.height) + "px" : ""}
|
2023-09-07 12:20:59 +00:00
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
|
2023-09-13 14:14:27 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: isMobile ? "block" : "flex",
|
|
|
|
|
alignItems: "end",
|
|
|
|
|
justifyContent: "space-between",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<ResetIcon
|
|
|
|
|
onClick={rotateImage}
|
|
|
|
|
style={{ marginBottom: "10px", cursor: "pointer" }}
|
|
|
|
|
/>
|
2023-09-07 12:20:59 +00:00
|
|
|
|
<Box>
|
2023-09-13 14:14:27 +00:00
|
|
|
|
<Typography sx={{ color: "#9A9AAF", fontSize: "16px" }}>
|
|
|
|
|
Размер
|
|
|
|
|
</Typography>
|
2023-09-07 12:20:59 +00:00
|
|
|
|
<Slider
|
|
|
|
|
sx={styleSlider}
|
2023-09-08 20:17:05 +00:00
|
|
|
|
value={width}
|
2023-09-07 12:20:59 +00:00
|
|
|
|
min={50}
|
2023-09-08 20:17:05 +00:00
|
|
|
|
max={580}
|
2023-09-07 12:20:59 +00:00
|
|
|
|
step={1}
|
|
|
|
|
onChange={(_, newValue) => {
|
2023-09-08 20:17:05 +00:00
|
|
|
|
setWidth(newValue as number);
|
2023-09-07 12:20:59 +00:00
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box>
|
2023-09-13 14:14:27 +00:00
|
|
|
|
<Typography sx={{ color: "#9A9AAF", fontSize: "16px" }}>
|
|
|
|
|
Затемнение
|
|
|
|
|
</Typography>
|
2023-09-07 12:20:59 +00:00
|
|
|
|
<Slider
|
|
|
|
|
sx={styleSlider}
|
|
|
|
|
value={darken}
|
|
|
|
|
min={0}
|
|
|
|
|
max={100}
|
|
|
|
|
step={1}
|
|
|
|
|
onChange={(_, newValue) => setDarken(newValue as number)}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
2023-09-13 14:14:27 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
marginTop: "40px",
|
|
|
|
|
width: "100%",
|
|
|
|
|
display: "flex",
|
|
|
|
|
justifyContent: "end",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-09-07 12:20:59 +00:00
|
|
|
|
<input
|
|
|
|
|
style={{ display: "none", zIndex: "-999" }}
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
type="file"
|
|
|
|
|
accept="image/*"
|
|
|
|
|
onChange={onSelectFile}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => fileInputRef.current?.click()}
|
|
|
|
|
disableRipple
|
|
|
|
|
sx={{
|
|
|
|
|
width: "215px",
|
|
|
|
|
height: "48px",
|
|
|
|
|
color: "#7E2AEA",
|
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
border: "1px solid #7E2AEA",
|
|
|
|
|
marginRight: "10px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Загрузить оригинал
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={onDownloadCropClick}
|
|
|
|
|
disableRipple
|
2023-09-15 13:48:58 +00:00
|
|
|
|
variant="contained"
|
2023-09-07 12:20:59 +00:00
|
|
|
|
sx={{
|
2023-09-15 13:48:58 +00:00
|
|
|
|
padding: "10px 20px",
|
2023-09-07 12:20:59 +00:00
|
|
|
|
borderRadius: "8px",
|
2023-09-15 13:48:58 +00:00
|
|
|
|
background: theme.palette.brightPurple.main,
|
|
|
|
|
fontSize: "18px",
|
2023-09-07 12:20:59 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
2023-09-13 14:14:27 +00:00
|
|
|
|
<CropIcon />
|
2023-09-07 12:20:59 +00:00
|
|
|
|
Обрезать
|
|
|
|
|
</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</Modal>
|
2023-09-07 12:58:10 +00:00
|
|
|
|
{completedCrop && (
|
|
|
|
|
<div>
|
|
|
|
|
<canvas
|
|
|
|
|
ref={previewCanvasRef}
|
|
|
|
|
style={{
|
|
|
|
|
display: "none",
|
|
|
|
|
zIndex: "-999",
|
|
|
|
|
border: "1px solid black",
|
|
|
|
|
objectFit: "contain",
|
|
|
|
|
width: completedCrop.width,
|
|
|
|
|
height: completedCrop.height,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<div>
|
|
|
|
|
<a
|
|
|
|
|
href="#hidden"
|
|
|
|
|
ref={hiddenAnchorRef}
|
|
|
|
|
download
|
|
|
|
|
style={{
|
|
|
|
|
display: "none",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Hidden download
|
|
|
|
|
</a>
|
|
|
|
|
</div>
|
2023-09-07 12:20:59 +00:00
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|