Skip to content

Commit 482e902

Browse files
authored
Merge pull request #2290 from SUI-Components/feat/image-editor-preview-before-save
feat(components/molecule/imageEditor): Add a preview mode to molecule/imageEditor to optimize image
2 parents debf61c + a168c9d commit 482e902

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

components/molecule/imageEditor/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"author": "",
2020
"license": "MIT",
2121
"dependencies": {
22+
"@s-ui/js": "2",
2223
"@s-ui/react-atom-slider": "1",
2324
"react-easy-crop": "3.4.0"
2425
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const baseClass = 'sui-MoleculeImageEditor'
22
export const DEFAULT_ASPECT = 4 / 3
33
export const noop = () => {}
4+
export const DEBOUNCING_TIME = 100

components/molecule/imageEditor/src/index.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import Cropper from 'react-easy-crop'
33

44
import PropTypes from 'prop-types'
55

6+
import {debounce} from '@s-ui/js/lib/function/debounce.js'
67
import AtomSlider from '@s-ui/react-atom-slider'
78

89
import 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

1112
const 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

Comments
 (0)