2023-12-16 17:04:05 +00:00
|
|
|
|
import { devlog } from "@frontend/kitui";
|
2023-10-21 13:16:57 +00:00
|
|
|
|
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";
|
2023-12-16 17:04:05 +00:00
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
|
import { FC, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
|
import ReactCrop, { PercentCrop, PixelCrop } from "react-image-crop";
|
2023-10-19 14:11:07 +00:00
|
|
|
|
import "react-image-crop/dist/ReactCrop.css";
|
2023-12-16 17:04:05 +00:00
|
|
|
|
import { getCroppedImageBlob, getDarkenedAndResizedImageBlob, getRotatedImageBlob } from "./utils/imageManipulation";
|
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-12-15 23:02:51 +00:00
|
|
|
|
},
|
2023-10-19 14:11:07 +00:00
|
|
|
|
};
|
2023-09-07 12:20:59 +00:00
|
|
|
|
|
2023-10-21 13:16:57 +00:00
|
|
|
|
interface Props {
|
2023-12-02 09:35:35 +00:00
|
|
|
|
isOpen: boolean;
|
2023-12-04 11:57:54 +00:00
|
|
|
|
imageBlob: Blob | null;
|
|
|
|
|
originalImageUrl: string | null;
|
|
|
|
|
setCropModalImageBlob: (imageBlob: Blob) => void;
|
2023-12-02 09:35:35 +00:00
|
|
|
|
onClose: () => void;
|
2023-12-04 11:57:54 +00:00
|
|
|
|
onSaveImageClick: (imageBlob: Blob) => void;
|
2023-09-07 12:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 11:57:54 +00:00
|
|
|
|
export const CropModal: FC<Props> = ({ isOpen, imageBlob, originalImageUrl, setCropModalImageBlob, onSaveImageClick, onClose }) => {
|
2023-10-19 14:11:07 +00:00
|
|
|
|
const theme = useTheme();
|
2023-12-16 17:04:05 +00:00
|
|
|
|
const [percentCrop, setPercentCrop] = useState<PercentCrop>();
|
2023-10-12 15:20:46 +00:00
|
|
|
|
const [completedCrop, setCompletedCrop] = useState<PixelCrop>();
|
|
|
|
|
const [darken, setDarken] = useState(0);
|
2023-12-16 17:04:05 +00:00
|
|
|
|
const [scale, setScale] = useState<number>(1);
|
|
|
|
|
const [imageWidth, setImageWidth] = useState<number | null>(null);
|
|
|
|
|
const [imageHeight, setImageHeight] = useState<number | null>(null);
|
2023-10-21 13:16:57 +00:00
|
|
|
|
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
|
|
|
|
|
2023-12-01 18:05:59 +00:00
|
|
|
|
const imageUrl = useMemo(() => imageBlob && URL.createObjectURL(imageBlob), [imageBlob]);
|
|
|
|
|
|
2023-12-16 17:04:05 +00:00
|
|
|
|
function resetEditState() {
|
|
|
|
|
setPercentCrop(undefined);
|
|
|
|
|
setCompletedCrop(undefined);
|
|
|
|
|
setDarken(0);
|
|
|
|
|
setScale(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleCropClick() {
|
2023-10-21 13:16:57 +00:00
|
|
|
|
if (!cropImageElementRef.current) throw new Error("No image");
|
2023-12-16 17:04:05 +00:00
|
|
|
|
if (!completedCrop) return;
|
2023-09-07 12:20:59 +00:00
|
|
|
|
|
2023-12-16 17:04:05 +00:00
|
|
|
|
try {
|
|
|
|
|
const blob = await getCroppedImageBlob(cropImageElementRef.current, completedCrop);
|
2023-10-19 14:11:07 +00:00
|
|
|
|
|
2023-12-16 17:04:05 +00:00
|
|
|
|
setCropModalImageBlob(blob);
|
|
|
|
|
setPercentCrop(undefined);
|
|
|
|
|
setCompletedCrop(undefined);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
devlog("getCroppedImageBlob error", error);
|
|
|
|
|
enqueueSnackbar("Не удалось изменить изображение");
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-10-19 14:11:07 +00:00
|
|
|
|
|
2023-12-16 17:04:05 +00:00
|
|
|
|
async function handleRotateClick() {
|
|
|
|
|
if (!cropImageElementRef.current) throw new Error("No image");
|
2023-09-07 12:20:59 +00:00
|
|
|
|
|
2023-12-16 17:04:05 +00:00
|
|
|
|
try {
|
|
|
|
|
const blob = await getRotatedImageBlob(cropImageElementRef.current);
|
2023-09-08 20:17:05 +00:00
|
|
|
|
|
2023-12-01 18:05:59 +00:00
|
|
|
|
setCropModalImageBlob(blob);
|
2023-12-16 17:04:05 +00:00
|
|
|
|
setPercentCrop(undefined);
|
2023-10-21 13:16:57 +00:00
|
|
|
|
setCompletedCrop(undefined);
|
2023-12-16 17:04:05 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
devlog("getRotatedImageBlob error", error);
|
|
|
|
|
enqueueSnackbar("Не удалось изменить изображение");
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-07 12:20:59 +00:00
|
|
|
|
|
2023-12-16 17:04:05 +00:00
|
|
|
|
async function handleSaveClick() {
|
|
|
|
|
if (!cropImageElementRef.current) throw new Error("No image");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const blob = await getDarkenedAndResizedImageBlob(cropImageElementRef.current, scale, darken / 100);
|
|
|
|
|
onSaveImageClick?.(blob);
|
|
|
|
|
resetEditState();
|
|
|
|
|
onClose();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
devlog("getDarkenedImageBlob error", error);
|
|
|
|
|
enqueueSnackbar("Не удалось сохранить изображение");
|
|
|
|
|
}
|
2023-10-21 13:16:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 18:05:59 +00:00
|
|
|
|
async function handleLoadOriginalImage() {
|
|
|
|
|
if (!originalImageUrl) return;
|
|
|
|
|
|
|
|
|
|
const response = await fetch(originalImageUrl);
|
|
|
|
|
const blob = await response.blob();
|
|
|
|
|
|
|
|
|
|
setCropModalImageBlob(blob);
|
2023-12-16 17:04:05 +00:00
|
|
|
|
resetEditState();
|
2023-10-23 15:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 15:20:46 +00:00
|
|
|
|
return (
|
2023-10-19 14:11:07 +00:00
|
|
|
|
<Modal
|
2023-12-04 11:57:54 +00:00
|
|
|
|
open={isOpen}
|
2023-12-16 17:04:05 +00:00
|
|
|
|
onClose={() => {
|
|
|
|
|
resetEditState();
|
|
|
|
|
onClose();
|
|
|
|
|
}}
|
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",
|
|
|
|
|
}}>
|
2023-12-16 17:04:05 +00:00
|
|
|
|
<Box sx={{
|
|
|
|
|
height: "320px",
|
|
|
|
|
backgroundSize: "cover",
|
|
|
|
|
backgroundRepeat: "no-repeat",
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
}}>
|
2023-10-21 13:16:57 +00:00
|
|
|
|
{imageUrl && (
|
2023-10-19 14:11:07 +00:00
|
|
|
|
<ReactCrop
|
2023-12-16 17:04:05 +00:00
|
|
|
|
crop={percentCrop}
|
|
|
|
|
onChange={(_, percentCrop) => setPercentCrop(percentCrop)}
|
2023-10-19 14:11:07 +00:00
|
|
|
|
onComplete={(c) => setCompletedCrop(c)}
|
|
|
|
|
minWidth={50}
|
|
|
|
|
minHeight={50}
|
|
|
|
|
>
|
|
|
|
|
<img
|
2023-12-16 17:04:05 +00:00
|
|
|
|
onLoad={e => {
|
|
|
|
|
setImageWidth(e.currentTarget.naturalWidth);
|
|
|
|
|
setImageHeight(e.currentTarget.naturalHeight);
|
|
|
|
|
}}
|
2023-10-21 13:16:57 +00:00
|
|
|
|
ref={cropImageElementRef}
|
2023-10-19 14:11:07 +00:00
|
|
|
|
alt="Crop me"
|
2023-10-21 13:16:57 +00:00
|
|
|
|
src={imageUrl}
|
2023-10-19 14:11:07 +00:00
|
|
|
|
style={{
|
|
|
|
|
filter: `brightness(${100 - darken}%)`,
|
2023-12-16 17:04:05 +00:00
|
|
|
|
width: imageWidth ? imageWidth * scale : undefined,
|
|
|
|
|
height: "100%",
|
|
|
|
|
maxWidth: "100%",
|
2023-10-19 14:11:07 +00:00
|
|
|
|
maxHeight: "320px",
|
2023-12-16 17:04:05 +00:00
|
|
|
|
display: "block",
|
|
|
|
|
objectFit: "contain",
|
2023-10-19 14:11:07 +00:00
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</ReactCrop>
|
|
|
|
|
)}
|
|
|
|
|
</Box>
|
2023-12-16 17:04:05 +00:00
|
|
|
|
<Box sx={{
|
|
|
|
|
color: "#7E2AEA",
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
fontSize: "16px",
|
|
|
|
|
fontWeight: "600",
|
|
|
|
|
my: "20px",
|
|
|
|
|
}}>
|
|
|
|
|
{imageWidth && imageHeight && ((percentCrop?.height && percentCrop?.width)
|
|
|
|
|
? <Typography sx={{ color: "#7E2AEA" }}>
|
|
|
|
|
{`${Math.round(percentCrop.width / 100 * imageWidth * scale)} x ${Math.round(percentCrop.height / 100 * imageHeight * scale)} px`}
|
|
|
|
|
</Typography>
|
|
|
|
|
: <Typography sx={{ color: "#7E2AEA" }}>
|
|
|
|
|
{`${Math.round(imageWidth * scale)} x ${Math.round(imageHeight * scale)} px`}
|
|
|
|
|
</Typography>
|
|
|
|
|
)}
|
2023-10-19 14:11:07 +00:00
|
|
|
|
</Box>
|
2023-12-16 17:04:05 +00:00
|
|
|
|
<Box sx={{
|
|
|
|
|
display: isMobile ? "block" : "flex",
|
|
|
|
|
alignItems: "end",
|
|
|
|
|
justifyContent: "space-between",
|
|
|
|
|
}}>
|
|
|
|
|
<IconButton onClick={handleRotateClick}>
|
2023-10-19 14:11:07 +00:00
|
|
|
|
<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",
|
|
|
|
|
}]}
|
2023-12-16 17:04:05 +00:00
|
|
|
|
value={scale * 100}
|
|
|
|
|
min={1}
|
|
|
|
|
max={200}
|
2023-10-19 14:11:07 +00:00
|
|
|
|
step={1}
|
|
|
|
|
onChange={(_, newValue) => {
|
2023-12-16 17:04:05 +00:00
|
|
|
|
setScale((newValue as number) * 0.01);
|
2023-10-19 14:11:07 +00:00
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</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>
|
2023-12-16 17:04:05 +00:00
|
|
|
|
<Box sx={{
|
|
|
|
|
marginTop: "40px",
|
|
|
|
|
width: "100%",
|
|
|
|
|
display: "flex",
|
|
|
|
|
}}>
|
2023-10-19 14:11:07 +00:00
|
|
|
|
<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
|
2023-10-23 15:38:23 +00:00
|
|
|
|
onClick={handleLoadOriginalImage}
|
2023-10-19 14:11:07 +00:00
|
|
|
|
disableRipple
|
2023-12-01 18:05:59 +00:00
|
|
|
|
disabled={!originalImageUrl}
|
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"
|
2023-12-16 17:04:05 +00:00
|
|
|
|
disabled={!completedCrop?.width || !completedCrop?.height}
|
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-12-04 11:57:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function useCropModalState(initialOpenState = false) {
|
|
|
|
|
const [isCropModalOpen, setOpened] = useState(initialOpenState);
|
|
|
|
|
const [imageBlob, setCropModalImageBlob] = useState<Blob | null>(null);
|
|
|
|
|
const [originalImageUrl, setOriginalImageUrl] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
const closeCropModal = () => {
|
|
|
|
|
setOpened(false);
|
|
|
|
|
setCropModalImageBlob(null);
|
|
|
|
|
setOriginalImageUrl(null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function openCropModal(image: Blob | string, originalImageUrl: string | null | undefined = null) {
|
|
|
|
|
if (typeof image === "string") {
|
|
|
|
|
const response = await fetch(image);
|
|
|
|
|
image = await response.blob();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setCropModalImageBlob(image);
|
|
|
|
|
setOriginalImageUrl(originalImageUrl);
|
|
|
|
|
setOpened(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
isCropModalOpen,
|
|
|
|
|
openCropModal,
|
|
|
|
|
closeCropModal,
|
|
|
|
|
imageBlob,
|
|
|
|
|
setCropModalImageBlob,
|
|
|
|
|
originalImageUrl,
|
|
|
|
|
} as const;
|
|
|
|
|
}
|