frontPanel/src/ui_kit/Modal/CropModal.tsx

314 lines
11 KiB
TypeScript
Raw Normal View History

2023-09-13 14:14:27 +00:00
import {
2023-10-12 15:20:46 +00:00
Box,
Button,
2023-10-19 14:11:07 +00:00
IconButton,
2023-10-12 15:20:46 +00:00
Modal,
Slider,
2023-10-19 14:11:07 +00:00
SxProps,
Theme,
2023-10-12 15:20:46 +00:00
Typography,
useMediaQuery,
useTheme,
2023-09-13 14:14:27 +00:00
} from "@mui/material";
2023-10-19 14:11:07 +00:00
import React, { FC, useEffect, useRef, useState } from "react";
import ReactCrop, { Crop, PixelCrop } from "react-image-crop";
2023-09-07 12:20:59 +00:00
import { canvasPreview } from "./utils/canvasPreview";
import { ResetIcon } from "@icons/ResetIcon";
2023-09-07 15:09:50 +00:00
import { CropIcon } from "@icons/CropIcon";
2023-10-19 14:11:07 +00:00
import "react-image-crop/dist/ReactCrop.css";
const styleSlider: SxProps<Theme> = {
color: "#7E2AEA",
height: "12px",
"& .MuiSlider-track": {
border: "none",
},
"& .MuiSlider-rail": {
backgroundColor: "#F2F3F7",
border: `1px solid #9A9AAF`,
},
"& .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`,
},
},
};
2023-09-07 12:20:59 +00:00
interface Iprops {
2023-10-12 15:20:46 +00:00
opened: boolean;
2023-10-19 14:11:07 +00:00
onClose: () => void;
2023-10-12 15:20:46 +00:00
picture?: string;
onCropPress?: (imageUrl: string) => void;
2023-09-07 12:20:59 +00:00
}
2023-10-12 15:20:46 +00:00
export const CropModal: FC<Iprops> = ({ opened, onClose, picture, onCropPress }) => {
2023-10-19 14:11:07 +00:00
const theme = useTheme();
2023-10-12 15:20:46 +00:00
const [crop, setCrop] = useState<Crop>();
const [completedCrop, setCompletedCrop] = useState<PixelCrop>();
const [darken, setDarken] = useState(0);
2023-10-19 14:11:07 +00:00
const [rotate, setRotate] = useState(0);
const [imgSrc, setImgSrc] = useState("");
const [width, setWidth] = useState<number>(0);
const blobUrlRef = useRef("");
const fileInputRef = useRef<HTMLInputElement>(null);
const imgRef = useRef<HTMLImageElement>(null);
2023-10-12 15:20:46 +00:00
const isMobile = useMediaQuery(theme.breakpoints.down(786));
2023-09-07 12:20:59 +00:00
2023-10-12 15:20:46 +00:00
useEffect(() => {
2023-10-19 14:11:07 +00:00
if (picture) setImgSrc(picture);
2023-10-12 15:20:46 +00:00
}, [picture]);
2023-09-13 14:14:27 +00:00
2023-10-19 14:11:07 +00:00
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (!event.target.files?.length) return;
2023-09-07 12:20:59 +00:00
2023-10-19 14:11:07 +00:00
setCrop(undefined);
try {
const url = URL.createObjectURL(event.target.files[0]);
setImgSrc(url);
} catch (error) {
console.error("Failed to create object url for image", error);
2023-10-12 15:20:46 +00:00
}
};
2023-09-07 12:20:59 +00:00
2023-10-19 14:11:07 +00:00
const handleCropClick = () => {
if (!completedCrop) throw new Error("No completed crop");
if (!imgRef.current) throw new Error("No image");
2023-09-07 12:20:59 +00:00
2023-10-12 15:20:46 +00:00
const canvasCopy = document.createElement("canvas");
const ctx = canvasCopy.getContext("2d");
2023-10-19 14:11:07 +00:00
if (!ctx) throw new Error("No 2d context");
canvasCopy.width = completedCrop.width;
canvasCopy.height = completedCrop.height;
ctx.filter = `brightness(${100 - darken}%)`;
canvasPreview(imgRef.current, canvasCopy, completedCrop, rotate);
2023-09-07 12:20:59 +00:00
2023-10-12 15:20:46 +00:00
canvasCopy.toBlob((blob) => {
if (!blob) {
throw new Error("Failed to create blob");
}
if (blobUrlRef.current) {
URL.revokeObjectURL(blobUrlRef.current);
}
blobUrlRef.current = URL.createObjectURL(blob);
2023-09-08 20:17:05 +00:00
2023-10-12 15:20:46 +00:00
setImgSrc(blobUrlRef.current);
onCropPress?.(blobUrlRef.current);
2023-10-19 14:11:07 +00:00
setCrop(undefined);
2023-10-12 15:20:46 +00:00
});
};
2023-09-07 12:20:59 +00:00
2023-10-12 15:20:46 +00:00
const getImageSize = () => {
if (imgRef.current) {
const imageWidth = imgRef.current.naturalWidth;
const imageHeight = imgRef.current.naturalHeight;
2023-09-07 15:09:50 +00:00
2023-10-12 15:20:46 +00:00
const aspect = imageWidth / imageHeight;
2023-09-07 15:09:50 +00:00
2023-09-08 20:17:05 +00:00
2023-10-12 15:20:46 +00:00
if (aspect <= 1.333) {
setWidth(240);
}
if (aspect >= 1.5) {
setWidth(580);
}
if (aspect >= 1.778) {
setWidth(580);
}
}
};
2023-09-07 15:09:50 +00:00
2023-10-19 14:11:07 +00:00
function handleSaveClick() {
onClose();
}
2023-10-12 15:20:46 +00:00
return (
2023-10-19 14:11:07 +00:00
<Modal
open={opened}
onClose={onClose}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<Box sx={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
bgcolor: "background.paper",
boxShadow: 24,
padding: "20px",
borderRadius: "8px",
width: isMobile ? "343px" : "620px",
}}>
<Box
sx={{
height: "320px",
padding: "10px",
backgroundSize: "cover",
backgroundRepeat: "no-repeat",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
{imgSrc && (
<ReactCrop
crop={crop}
onChange={(_, percentCrop) => setCrop(percentCrop)}
onComplete={(c) => setCompletedCrop(c)}
maxWidth={500}
minWidth={50}
maxHeight={320}
minHeight={50}
>
<img
onLoad={getImageSize}
ref={imgRef}
alt="Crop me"
src={imgSrc}
style={{
filter: `brightness(${100 - darken}%)`,
transform: ` rotate(${rotate}deg)`,
maxWidth: "580px",
maxHeight: "320px",
}}
width={width}
/>
</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" }}>
{crop?.height ? Math.round(crop.height) + "px" : ""}
</Typography>
</Box>
2023-09-07 12:20:59 +00:00
2023-10-19 14:11:07 +00:00
<Box
sx={{
display: isMobile ? "block" : "flex",
alignItems: "end",
justifyContent: "space-between",
}}
>
<IconButton onClick={() => setRotate(r => (r + 90) % 360)}>
<ResetIcon />
</IconButton>
<Box>
<Typography sx={{ color: "#9A9AAF", fontSize: "16px" }}>
Размер
2023-10-12 15:20:46 +00:00
</Typography>
2023-10-19 14:11:07 +00:00
<Slider
sx={[styleSlider, {
width: isMobile ? "350px" : "250px",
}]}
value={width}
min={50}
max={580}
step={1}
onChange={(_, newValue) => {
setWidth(newValue as number);
}}
/>
</Box>
<Box>
<Typography sx={{ color: "#9A9AAF", fontSize: "16px" }}>
Затемнение
2023-10-12 15:20:46 +00:00
</Typography>
2023-10-19 14:11:07 +00:00
<Slider
sx={[styleSlider, {
width: isMobile ? "350px" : "250px",
}]}
value={darken}
min={0}
max={100}
step={1}
onChange={(_, newValue) => setDarken(newValue as number)}
/>
2023-10-12 15:20:46 +00:00
</Box>
2023-10-19 14:11:07 +00:00
</Box>
<Box
sx={{
marginTop: "40px",
width: "100%",
display: "flex",
}}
>
<Button
onClick={handleSaveClick}
disableRipple
2023-10-12 15:20:46 +00:00
sx={{
2023-10-19 14:11:07 +00:00
height: "48px",
color: "#7E2AEA",
borderRadius: "8px",
border: "1px solid #7E2AEA",
marginRight: "10px",
px: "20px",
2023-10-12 15:20:46 +00:00
}}
2023-10-19 14:11:07 +00:00
>Сохранить</Button>
<Button
onClick={() => fileInputRef.current?.click()}
disableRipple
2023-10-12 15:20:46 +00:00
sx={{
2023-10-19 14:11:07 +00:00
width: "215px",
height: "48px",
color: "#7E2AEA",
borderRadius: "8px",
border: "1px solid #7E2AEA",
marginRight: "10px",
ml: "auto",
2023-10-12 15:20:46 +00:00
}}
>
2023-10-19 14:11:07 +00:00
Загрузить оригинал
2023-10-12 15:20:46 +00:00
<input
style={{ display: "none", zIndex: "-999" }}
ref={fileInputRef}
type="file"
accept="image/*"
2023-10-19 14:11:07 +00:00
onChange={handleFileChange}
2023-10-12 15:20:46 +00:00
/>
2023-10-19 14:11:07 +00:00
</Button>
<Button
onClick={handleCropClick}
disableRipple
variant="contained"
sx={{
padding: "10px 20px",
borderRadius: "8px",
background: theme.palette.brightPurple.main,
fontSize: "18px",
2023-10-12 15:20:46 +00:00
}}
2023-10-19 14:11:07 +00:00
>
<CropIcon />
Обрезать
</Button>
</Box>
</Box>
</Modal>
2023-10-12 15:20:46 +00:00
);
2023-09-07 12:20:59 +00:00
};