@@ -57,12 +57,12 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
57
57
editorContainer,
58
58
setEditorContainer,
59
59
} = props ;
60
- const { src : remoteImageSrc , width, height, aspectRatio } = node . attrs ;
60
+ const { src : remoteImageSrc , width : nodeWidth , height : nodeHeight , aspectRatio : nodeAspectRatio } = node . attrs ;
61
61
// states
62
62
const [ size , setSize ] = useState < Size > ( {
63
- width : ensurePixelString ( width , "35%" ) ,
64
- height : ensurePixelString ( height , "auto" ) ,
65
- aspectRatio : aspectRatio || 1 ,
63
+ width : ensurePixelString ( nodeWidth , "35%" ) ,
64
+ height : ensurePixelString ( nodeHeight , "auto" ) ,
65
+ aspectRatio : nodeAspectRatio || null ,
66
66
} ) ;
67
67
const [ isResizing , setIsResizing ] = useState ( false ) ;
68
68
const [ initialResizeComplete , setInitialResizeComplete ] = useState ( false ) ;
@@ -102,17 +102,17 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
102
102
}
103
103
104
104
setEditorContainer ( closestEditorContainer ) ;
105
- const aspectRatio = img . naturalWidth / img . naturalHeight ;
105
+ const aspectRatioCalculated = img . naturalWidth / img . naturalHeight ;
106
106
107
- if ( width === "35%" ) {
107
+ if ( nodeWidth === "35%" ) {
108
108
const editorWidth = closestEditorContainer . clientWidth ;
109
109
const initialWidth = Math . max ( editorWidth * 0.35 , MIN_SIZE ) ;
110
- const initialHeight = initialWidth / aspectRatio ;
110
+ const initialHeight = initialWidth / aspectRatioCalculated ;
111
111
112
112
const initialComputedSize = {
113
113
width : `${ Math . round ( initialWidth ) } px` satisfies Pixel ,
114
114
height : `${ Math . round ( initialHeight ) } px` satisfies Pixel ,
115
- aspectRatio : aspectRatio ,
115
+ aspectRatio : aspectRatioCalculated ,
116
116
} ;
117
117
118
118
setSize ( initialComputedSize ) ;
@@ -122,9 +122,10 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
122
122
) ;
123
123
} else {
124
124
// as the aspect ratio in not stored for old images, we need to update the attrs
125
- if ( ! aspectRatio ) {
125
+ // or if aspectRatioCalculated from the image's width and height doesn't match stored aspectRatio then also we'll update the attrs
126
+ if ( ! nodeAspectRatio || nodeAspectRatio !== aspectRatioCalculated ) {
126
127
setSize ( ( prevSize ) => {
127
- const newSize = { ...prevSize , aspectRatio } ;
128
+ const newSize = { ...prevSize , aspectRatio : aspectRatioCalculated } ;
128
129
updateAttributesSafely (
129
130
newSize ,
130
131
"Failed to update attributes while initializing images with width but no aspect ratio:"
@@ -134,16 +135,16 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
134
135
}
135
136
}
136
137
setInitialResizeComplete ( true ) ;
137
- } , [ width , updateAttributes , editorContainer , aspectRatio ] ) ;
138
+ } , [ nodeWidth , updateAttributes , editorContainer , nodeAspectRatio ] ) ;
138
139
139
140
// for real time resizing
140
141
useLayoutEffect ( ( ) => {
141
142
setSize ( ( prevSize ) => ( {
142
143
...prevSize ,
143
- width : ensurePixelString ( width ) ,
144
- height : ensurePixelString ( height ) ,
144
+ width : ensurePixelString ( nodeWidth ) ,
145
+ height : ensurePixelString ( nodeHeight ) ,
145
146
} ) ) ;
146
- } , [ width , height ] ) ;
147
+ } , [ nodeWidth , nodeHeight ] ) ;
147
148
148
149
const handleResize = useCallback (
149
150
( e : MouseEvent | TouchEvent ) => {
@@ -215,7 +216,7 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
215
216
onMouseDown = { handleImageMouseDown }
216
217
style = { {
217
218
width : size . width ,
218
- aspectRatio : size . aspectRatio ,
219
+ ... ( size . aspectRatio && { aspectRatio : size . aspectRatio } ) ,
219
220
} }
220
221
>
221
222
{ showImageLoader && (
@@ -241,7 +242,7 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
241
242
} ) }
242
243
style = { {
243
244
width : size . width ,
244
- aspectRatio : size . aspectRatio ,
245
+ ... ( size . aspectRatio && { aspectRatio : size . aspectRatio } ) ,
245
246
} }
246
247
/>
247
248
{ showImageUtils && (
0 commit comments