@@ -24,7 +24,8 @@ import Slider from '../../ui/Slider';
2424import { TEXT_COLOR_KEYS , TextColors , TextVariants , TextWeights } from '../../../types/typography' ;
2525import { useEditorStore } from '../../../store/useEditorStore' ;
2626import { useEditorActions } from '../../../hooks/useEditorActions' ;
27- import { calculateCenteredCrop } from '../../../utils/cropUtils' ;
27+ import { calculateAreaPreservingCrop , calculateCenteredCrop } from '../../../utils/cropUtils' ;
28+ import { Crop } from 'react-image-crop' ;
2829
2930const BASE_RATIO = 1.618 ;
3031const ORIGINAL_RATIO = 0 ;
@@ -243,13 +244,26 @@ export default function CropPanel() {
243244
244245 const applyAspectRatio = useCallback (
245246 ( newAspectRatio : number | null ) => {
246- const newCrop =
247- selectedImage ?. width && selectedImage ?. height
248- ? calculateCenteredCrop ( selectedImage . width , selectedImage . height , orientationSteps , newAspectRatio , rotation )
249- : null ;
247+ if ( newAspectRatio === null ) {
248+ setAdjustments ( ( prev : Adjustments ) => ( { ...prev , aspectRatio : null } ) ) ;
249+ return ;
250+ }
251+ let newCrop : Crop | null = null ;
252+ if ( selectedImage ?. width && selectedImage ?. height ) {
253+ newCrop =
254+ calculateAreaPreservingCrop (
255+ selectedImage . width ,
256+ selectedImage . height ,
257+ orientationSteps ,
258+ newAspectRatio ,
259+ rotation ,
260+ adjustments . crop ,
261+ ) ??
262+ calculateCenteredCrop ( selectedImage . width , selectedImage . height , orientationSteps , newAspectRatio , rotation ) ;
263+ }
250264 setAdjustments ( ( prev : Adjustments ) => ( { ...prev , aspectRatio : newAspectRatio , crop : newCrop } ) ) ;
251265 } ,
252- [ selectedImage , orientationSteps , rotation , setAdjustments ] ,
266+ [ selectedImage , orientationSteps , rotation , adjustments . crop , setAdjustments ] ,
253267 ) ;
254268
255269 useEffect ( ( ) => {
0 commit comments