Skip to content

Commit c68658d

Browse files
[PE-56] fix: image aspect ratio (#5794)
* regression: image aspect ratio fix * fix: name of variables changed for clarity
1 parent 5573d85 commit c68658d

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

packages/editor/src/core/extensions/custom-image/components/image-block.tsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
5757
editorContainer,
5858
setEditorContainer,
5959
} = props;
60-
const { src: remoteImageSrc, width, height, aspectRatio } = node.attrs;
60+
const { src: remoteImageSrc, width: nodeWidth, height: nodeHeight, aspectRatio: nodeAspectRatio } = node.attrs;
6161
// states
6262
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,
6666
});
6767
const [isResizing, setIsResizing] = useState(false);
6868
const [initialResizeComplete, setInitialResizeComplete] = useState(false);
@@ -102,17 +102,17 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
102102
}
103103

104104
setEditorContainer(closestEditorContainer);
105-
const aspectRatio = img.naturalWidth / img.naturalHeight;
105+
const aspectRatioCalculated = img.naturalWidth / img.naturalHeight;
106106

107-
if (width === "35%") {
107+
if (nodeWidth === "35%") {
108108
const editorWidth = closestEditorContainer.clientWidth;
109109
const initialWidth = Math.max(editorWidth * 0.35, MIN_SIZE);
110-
const initialHeight = initialWidth / aspectRatio;
110+
const initialHeight = initialWidth / aspectRatioCalculated;
111111

112112
const initialComputedSize = {
113113
width: `${Math.round(initialWidth)}px` satisfies Pixel,
114114
height: `${Math.round(initialHeight)}px` satisfies Pixel,
115-
aspectRatio: aspectRatio,
115+
aspectRatio: aspectRatioCalculated,
116116
};
117117

118118
setSize(initialComputedSize);
@@ -122,9 +122,10 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
122122
);
123123
} else {
124124
// 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) {
126127
setSize((prevSize) => {
127-
const newSize = { ...prevSize, aspectRatio };
128+
const newSize = { ...prevSize, aspectRatio: aspectRatioCalculated };
128129
updateAttributesSafely(
129130
newSize,
130131
"Failed to update attributes while initializing images with width but no aspect ratio:"
@@ -134,16 +135,16 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
134135
}
135136
}
136137
setInitialResizeComplete(true);
137-
}, [width, updateAttributes, editorContainer, aspectRatio]);
138+
}, [nodeWidth, updateAttributes, editorContainer, nodeAspectRatio]);
138139

139140
// for real time resizing
140141
useLayoutEffect(() => {
141142
setSize((prevSize) => ({
142143
...prevSize,
143-
width: ensurePixelString(width),
144-
height: ensurePixelString(height),
144+
width: ensurePixelString(nodeWidth),
145+
height: ensurePixelString(nodeHeight),
145146
}));
146-
}, [width, height]);
147+
}, [nodeWidth, nodeHeight]);
147148

148149
const handleResize = useCallback(
149150
(e: MouseEvent | TouchEvent) => {
@@ -215,7 +216,7 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
215216
onMouseDown={handleImageMouseDown}
216217
style={{
217218
width: size.width,
218-
aspectRatio: size.aspectRatio,
219+
...(size.aspectRatio && { aspectRatio: size.aspectRatio }),
219220
}}
220221
>
221222
{showImageLoader && (
@@ -241,7 +242,7 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
241242
})}
242243
style={{
243244
width: size.width,
244-
aspectRatio: size.aspectRatio,
245+
...(size.aspectRatio && { aspectRatio: size.aspectRatio }),
245246
}}
246247
/>
247248
{showImageUtils && (

0 commit comments

Comments
 (0)