1
1
import * as React from "react" ;
2
- import { Crop , PixelCrop } from "react-image-crop" ;
2
+ import { convertToPixelCrop , Crop , PixelCrop } from "react-image-crop" ;
3
3
import "react-image-crop/dist/ReactCrop.css" ;
4
4
import { usePcfContext } from "../services/pcfContext" ;
5
5
import { IImageCropControlProps } from "../types/imageCropTypes" ;
@@ -18,52 +18,64 @@ import {
18
18
useCropToBase64 ,
19
19
useKeepSelection ,
20
20
useRotation ,
21
- useScaling
21
+ useScaling ,
22
+ useImageLoaded
22
23
} from "../hooks" ;
23
- import { useDefaultCrop } from "../hooks/useDefaultCrop" ;
24
+ import { getDefaultCrop , useDefaultCrop } from "../hooks/useDefaultCrop" ;
24
25
import CropWrapper from "./imageCropWrapper" ;
25
26
26
27
const ImageCropControl : React . FC < IImageCropControlProps > = ( props ) => {
27
28
const pcfContext = usePcfContext ( ) ;
28
- const [ crop , setCrop ] = React . useState < Crop > ( ) ;
29
- // Set crop from manifest defaults on first load (modular)
30
- useDefaultCrop ( pcfContext . context , setCrop , crop ) ;
31
29
const [ completedCrop , setCompletedCrop ] = React . useState < PixelCrop > ( )
30
+ const [ crop , setCrop ] = React . useState < Crop > ( ) ;
31
+
32
+ // Image loaded state (modular)
33
+ const [ imageLoaded , handleImageLoad , handleImageError , handleImageSrcChange ] = useImageLoaded ( ) ;
34
+
32
35
const imgRef = React . useRef < HTMLImageElement > ( null ) as React . RefObject < HTMLImageElement > ;
33
36
const appScaling = useResponsiveAppScaling ( pcfContext . context , imgRef ) ;
34
37
35
38
// Get the locked property from PCF context
36
39
const locked = useLocked ( pcfContext . context ) ;
37
-
38
40
// Get the disabled property from PCF context
39
41
const disabled = useDisabled ( pcfContext . context ) ;
40
-
41
42
// Get the ruleOfThirds property from PCF context
42
43
const ruleOfThirds = useRuleOfThirds ( pcfContext . context ) ;
43
-
44
44
// Get the circularCrop property from PCF context
45
45
const circularCrop = useCircularCrop ( pcfContext . context ) ;
46
-
47
46
// Get min/max width/height from PCF context, scaled for browser
48
47
const minWidth = useMinWidth ( pcfContext . context ) ;
49
48
const maxWidth = useMaxWidth ( pcfContext . context ) ;
50
49
const minHeight = useMinHeight ( pcfContext . context ) ;
51
50
const maxHeight = useMaxHeight ( pcfContext . context ) ;
52
-
53
51
// Get the aspect ratio from PCF context and helper to center crop
54
52
const [ aspect , centerCropIfNeeded ] = useAspect ( pcfContext . context , imgRef , setCrop ) ;
55
53
// Get the keepSelection property from PCF context
56
54
const keepSelection = useKeepSelection ( pcfContext . context ) ;
57
-
58
55
// Get the image from the PCF context property (should be base64)
59
56
const imageSrc = useImageSrc ( pcfContext . context ) ;
60
-
61
57
// Get the rotation property from PCF context
62
58
const rotation = useRotation ( pcfContext . context ) ;
63
-
64
59
// Get the scaling property from PCF context
65
60
const scaling = useScaling ( pcfContext . context ) ;
66
61
62
+ // Get the default crop object (not a hook)
63
+ const defaultCrop = useDefaultCrop ( pcfContext . context ) ;
64
+
65
+ // Reset imageLoaded state and crop when imageSrc changes
66
+ React . useEffect ( ( ) => {
67
+ handleImageSrcChange ( imageSrc ) ;
68
+ setCrop ( undefined ) ;
69
+ } , [ imageSrc ] ) ;
70
+
71
+ // Set crop to default when image is loaded and crop is undefined
72
+ React . useEffect ( ( ) => {
73
+ if ( imageLoaded && ! crop && defaultCrop ) {
74
+ setCrop ( defaultCrop ) ;
75
+ setCompletedCrop ( convertToPixelCrop ( defaultCrop , imgRef . current . width , imgRef . current . height ) ) ;
76
+ }
77
+ } , [ imageLoaded ] ) ;
78
+
67
79
// Use custom hook to handle crop-to-base64 conversion and callback
68
80
useCropToBase64 ( imgRef , completedCrop , props . onCropComplete , rotation , scaling , circularCrop ) ;
69
81
@@ -90,6 +102,8 @@ const ImageCropControl: React.FC<IImageCropControlProps> = (props) => {
90
102
ref = { imgRef }
91
103
alt = "Crop"
92
104
src = { imageSrc || '' }
105
+ onLoad = { handleImageLoad }
106
+ onError = { handleImageError }
93
107
style = { {
94
108
maxWidth : '100%' ,
95
109
maxHeight : '100%' ,
0 commit comments