@@ -3,10 +3,11 @@ import Cropper from 'react-easy-crop'
33
44import PropTypes from 'prop-types'
55
6+ import { debounce } from '@s-ui/js/lib/function/debounce.js'
67import AtomSlider from '@s-ui/react-atom-slider'
78
89import getCroppedImg from './utils/cropImage.js'
9- import { baseClass , DEFAULT_ASPECT , noop } from './config.js'
10+ import { baseClass , DEBOUNCING_TIME , DEFAULT_ASPECT , noop } from './config.js'
1011
1112const MoleculeImageEditor = ( {
1213 aspect = DEFAULT_ASPECT ,
@@ -18,24 +19,30 @@ const MoleculeImageEditor = ({
1819 rotateLabelIcon,
1920 rotateLabelText
2021} ) => {
21- const [ crop , setCrop ] = useState ( { x : 0 , y : 0 } )
22- const [ rotation , setRotation ] = useState ( 0 )
23- const [ zoom , setZoom ] = useState ( 0 )
22+ const [ crop , cropSetter ] = useState ( { x : 0 , y : 0 } )
23+ const [ rotation , rotationSetter ] = useState ( 0 )
24+ const [ zoom , zoomSetter ] = useState ( 0 )
25+
26+ const setCrop = debounce ( cropSetter , DEBOUNCING_TIME )
27+ const setRotation = debounce ( rotationSetter , DEBOUNCING_TIME )
28+ const setZoom = debounce ( zoomSetter , DEBOUNCING_TIME )
2429
2530 const getRotationDegrees = rotation => ( rotation * 360 ) / 100
2631
32+ const cropCompleteHandler = async ( croppedArea , croppedAreaPixels ) => {
33+ const rotationDegrees = getRotationDegrees ( rotation )
34+ onCropping ( true )
35+ const [ croppedImageUrl , croppedImageBlobObject ] = await getCroppedImg (
36+ image ,
37+ croppedAreaPixels ,
38+ rotationDegrees
39+ )
40+ onChange ( croppedImageUrl , croppedImageBlobObject )
41+ onCropping ( false )
42+ }
43+
2744 const onCropComplete = useCallback (
28- async ( croppedArea , croppedAreaPixels ) => {
29- const rotationDegrees = getRotationDegrees ( rotation )
30- onCropping ( true )
31- const [ croppedImageUrl , croppedImageBlobObject ] = await getCroppedImg (
32- image ,
33- croppedAreaPixels ,
34- rotationDegrees
35- )
36- onChange ( croppedImageUrl , croppedImageBlobObject )
37- onCropping ( false )
38- } ,
45+ debounce ( cropCompleteHandler , DEBOUNCING_TIME ) ,
3946 [ rotation , onCropping , image , onChange ]
4047 )
4148
@@ -67,7 +74,7 @@ const MoleculeImageEditor = ({
6774 ) }
6875 < AtomSlider
6976 onChange = { ( event , { value} ) => setZoom ( value ) }
70- value = { zoom }
77+ defaultValue = { zoom }
7178 hideMarks
7279 />
7380 </ div >
@@ -88,7 +95,7 @@ const MoleculeImageEditor = ({
8895 ) }
8996 < AtomSlider
9097 onChange = { ( event , { value} ) => setRotation ( value ) }
91- value = { rotation }
98+ defaultValue = { rotation }
9299 hideMarks
93100 />
94101 </ div >
0 commit comments