frontPanel/src/ui_kit/Modal/CropModal.tsx

292 lines
10 KiB
TypeScript
Raw Normal View History

import { CropIcon } from "@icons/CropIcon";
import { ResetIcon } from "@icons/ResetIcon";
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";
import { closeCropModal, resetToOriginalImage, setCropModalImageUrl, useCropModalStore } from "@root/cropModal";
import { FC, useRef, useState } from "react";
2023-10-19 14:11:07 +00:00
import ReactCrop, { Crop, PixelCrop } from "react-image-crop";
import "react-image-crop/dist/ReactCrop.css";
import { canvasPreview } from "./utils/canvasPreview";
import { enqueueSnackbar } from "notistack";
2023-10-19 14:11:07 +00:00
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 Props {
onSaveImageClick?: (imageUrl: string) => void;
2023-09-07 12:20:59 +00:00
}
export const CropModal: FC<Props> = ({ onSaveImageClick }) => {
2023-10-19 14:11:07 +00:00
const theme = useTheme();
const isCropModalOpen = useCropModalStore(state => state.isCropModalOpen);
const imageUrl = useCropModalStore(state => state.imageUrl);
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 [width, setWidth] = useState<number>(0);
const cropImageElementRef = 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
const handleCropClick = async () => {
2023-10-19 14:11:07 +00:00
if (!completedCrop) throw new Error("No completed crop");
if (!cropImageElementRef.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}%)`;
await canvasPreview(cropImageElementRef.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");
}
const newImageUrl = URL.createObjectURL(blob);
2023-09-08 20:17:05 +00:00
setCropModalImageUrl(newImageUrl);
2023-10-19 14:11:07 +00:00
setCrop(undefined);
setCompletedCrop(undefined);
2023-10-12 15:20:46 +00:00
});
};
2023-09-07 12:20:59 +00:00
function handleSaveClick() {
if (imageUrl) onSaveImageClick?.(imageUrl);
setCrop(undefined);
setCompletedCrop(undefined);
closeCropModal();
}
function handleLoadOriginalImage() {
const isSuccess = resetToOriginalImage();
if (!isSuccess) enqueueSnackbar("Не удалось восстановить оригинал. Приносим глубочайшие извинения");
}
2023-10-12 15:20:46 +00:00
const getImageSize = () => {
if (cropImageElementRef.current) {
const imageWidth = cropImageElementRef.current.naturalWidth;
const imageHeight = cropImageElementRef.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-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-12 15:20:46 +00:00
return (
2023-10-19 14:11:07 +00:00
<Modal
open={isCropModalOpen}
onClose={closeCropModal}
2023-10-19 14:11:07 +00:00
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",
}}
>
{imageUrl && (
2023-10-19 14:11:07 +00:00
<ReactCrop
crop={crop}
onChange={(_, percentCrop) => setCrop(percentCrop)}
onComplete={(c) => setCompletedCrop(c)}
maxWidth={500}
minWidth={50}
maxHeight={320}
minHeight={50}
>
<img
onLoad={getImageSize}
ref={cropImageElementRef}
2023-10-19 14:11:07 +00:00
alt="Crop me"
src={imageUrl}
2023-10-19 14:11:07 +00:00
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-11-04 17:04:24 +00:00
data-cy="crop-modal-save-button"
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={handleLoadOriginalImage}
2023-10-19 14:11:07 +00:00
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
Загрузить оригинал
</Button>
<Button
onClick={handleCropClick}
disableRipple
variant="contained"
disabled={!completedCrop}
2023-10-19 14:11:07 +00:00
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
};