From d66c47192ed82de3e96bcc52b8d235ab302d594c Mon Sep 17 00:00:00 2001 From: Abhishek P Anil Date: Fri, 18 Aug 2023 11:46:59 +0530 Subject: [PATCH] feat: added initial value feature to the image crop --- src/components/ImageCrop/ImageCrop.jsx | 166 ++++++++++++++------- src/components/ImageUpload/ImageUpload.jsx | 2 +- 2 files changed, 117 insertions(+), 51 deletions(-) diff --git a/src/components/ImageCrop/ImageCrop.jsx b/src/components/ImageCrop/ImageCrop.jsx index 37907ca55..bfdcca7dc 100644 --- a/src/components/ImageCrop/ImageCrop.jsx +++ b/src/components/ImageCrop/ImageCrop.jsx @@ -1,4 +1,4 @@ -import React, { useState, useCallback } from 'react'; +import React, { useState, useCallback, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import './imageCrop.css'; import { Row, Col, Space, Radio, Button } from 'antd'; @@ -25,37 +25,49 @@ function ImageCrop(props) { type: 'THUMBNAIL', }, }; - const [crop, onCropChange] = useState(cropValues?.large); - const [zoom, onZoomChange] = useState(1); - const [aspectRatio, setAspectRatio] = useState(ASPECT_RATIO_TYPE.large.value); + const [largeCrop, onLargeCropChange] = useState({ x: 0, y: 0 }); + const [thumbnailCrop, onThumbnailCropChange] = useState({ x: 0, y: 0 }); + + const [largeZoom, onLargeZoomChange] = useState(1); + const [thumbnailZoom, onThumbnailZoomChange] = useState(1); + const [aspectRatioType, setAspectRatioType] = useState(ASPECT_RATIO_TYPE.large.type); + const [initialLargeCroppedArea, setInitialLargeCroppedArea] = useState(undefined); + const [initialThumbnailCroppedArea, setInitialThumbnailCroppedArea] = useState(undefined); const onCropAreaChange = (croppedArea, croppedAreaPixel) => { - switch (aspectRatioType) { - case ASPECT_RATIO_TYPE.large.type: - setCropValues({ - ...cropValues, - large: { - x: croppedAreaPixel?.x, - y: croppedAreaPixel?.y, - height: croppedAreaPixel?.height, - width: croppedAreaPixel?.width, - }, - }); - break; - case ASPECT_RATIO_TYPE.thumbnail.type: - setCropValues({ - ...cropValues, - thumbnail: { - x: croppedAreaPixel?.x, - y: croppedAreaPixel?.y, - height: croppedAreaPixel?.height, - width: croppedAreaPixel?.width, - }, - }); - break; - default: - break; + if ( + !isNaN(croppedAreaPixel?.x) && + !isNaN(croppedAreaPixel?.y) && + !isNaN(croppedAreaPixel?.height) && + !isNaN(croppedAreaPixel?.width) + ) { + switch (aspectRatioType) { + case ASPECT_RATIO_TYPE.large.type: + setCropValues({ + ...cropValues, + large: { + x: croppedAreaPixel?.x, + y: croppedAreaPixel?.y, + height: croppedAreaPixel?.height, + width: croppedAreaPixel?.width, + }, + }); + break; + case ASPECT_RATIO_TYPE.thumbnail.type: + setCropValues({ + ...cropValues, + thumbnail: { + x: croppedAreaPixel?.x, + y: croppedAreaPixel?.y, + height: croppedAreaPixel?.height, + width: croppedAreaPixel?.width, + }, + }); + break; + default: + break; + } } }; @@ -63,11 +75,9 @@ function ImageCrop(props) { switch (type) { case ASPECT_RATIO_TYPE.large.type: setAspectRatioType(ASPECT_RATIO_TYPE.large.type); - setAspectRatio(ASPECT_RATIO_TYPE.large.value); break; case ASPECT_RATIO_TYPE.thumbnail.type: setAspectRatioType(ASPECT_RATIO_TYPE.thumbnail.type); - setAspectRatio(ASPECT_RATIO_TYPE.thumbnail.value); break; default: break; @@ -78,8 +88,20 @@ function ImageCrop(props) { form.setFieldsValue({ imageCrop: cropValues, }); - setOpen(false); showCroppedImage(); + setInitialLargeCroppedArea(undefined); + setInitialThumbnailCroppedArea(undefined); + setAspectRatioType(ASPECT_RATIO_TYPE.large.type); + setOpen(false); + }; + + const onCancel = () => { + setInitialLargeCroppedArea(undefined); + setInitialThumbnailCroppedArea(undefined); + setAspectRatioType(ASPECT_RATIO_TYPE.large.type); + onLargeCropChange({ x: 0, y: 0 }); + onThumbnailCropChange({ x: 0, y: 0 }); + setOpen(false); }; const showCroppedImage = useCallback(async () => { @@ -91,6 +113,13 @@ function ImageCrop(props) { } }, [cropValues?.large]); + useEffect(() => { + if (open) { + setInitialLargeCroppedArea(cropValues?.large); + setInitialThumbnailCroppedArea(cropValues?.thumbnail); + } + }, [open]); + return ( setOpen(false)} + onClick={() => onCancel()} />, aspectRatioControl(event.target.value)} style={{ color: '#222732' }}> @@ -147,41 +177,77 @@ function ImageCrop(props) {
-
- + {aspectRatioType === ASPECT_RATIO_TYPE.large.type && ( + + )} + {aspectRatioType === ASPECT_RATIO_TYPE.thumbnail.type && ( + + )}
diff --git a/src/components/ImageUpload/ImageUpload.jsx b/src/components/ImageUpload/ImageUpload.jsx index b5edfe1ec..771a4404d 100644 --- a/src/components/ImageUpload/ImageUpload.jsx +++ b/src/components/ImageUpload/ImageUpload.jsx @@ -146,7 +146,7 @@ function ImageUpload(props) { )} - {(props?.imageUrl || imageUrl) && ( + {!props?.imageReadOnly && imageUrl && (