Skip to content

Commit 4d299cc

Browse files
xitij2000farhaanbukhsh
authored andcommitted
feat: retain image dimensions during re-edit and handle null dimensions
Ensure width and height are preserved for re-edited images. Introduce fallback to original dimensions when selection dimensions are null. Update tests to validate new behaviors.
1 parent ad86f7a commit 4d299cc

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

src/editors/sharedComponents/ImageUploadModal/ImageSettingsModal/index.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,43 @@ describe('ImageSettingsModal', () => {
108108
expect(props.saveToEditor).toHaveBeenCalled();
109109
});
110110

111+
test('Preserve dimensions when reediting', async () => {
112+
render(
113+
<ImageSettingsModal
114+
{...props}
115+
selection={{ ...props.selection, width: '1280', height: '720' }}
116+
/>,
117+
);
118+
mockImageLoad(1920, 1080);
119+
const widthInput = await screen.findByRole('textbox', { name: /width/i });
120+
const heightInput = screen.getByRole('textbox', { name: /height/i });
121+
expect(widthInput).toHaveValue('1280');
122+
expect(heightInput).toHaveValue('720');
123+
});
124+
125+
test('Use original dimensions for new images', async () => {
126+
render(<ImageSettingsModal {...props} />);
127+
mockImageLoad(1920, 1080);
128+
const widthInput = await screen.findByRole('textbox', { name: /width/i });
129+
const heightInput = screen.getByRole('textbox', { name: /height/i });
130+
expect(widthInput).toHaveValue('1920');
131+
expect(heightInput).toHaveValue('1080');
132+
});
133+
134+
test('Use original dimensions when selection dimensions are null', async () => {
135+
render(
136+
<ImageSettingsModal
137+
{...props}
138+
selection={{ ...props.selection, width: null, height: null }}
139+
/>,
140+
);
141+
mockImageLoad(1920, 1080);
142+
const widthInput = await screen.findByRole('textbox', { name: /width/i });
143+
const heightInput = screen.getByRole('textbox', { name: /height/i });
144+
expect(widthInput).toHaveValue('1920');
145+
expect(heightInput).toHaveValue('1080');
146+
});
147+
111148
describe('Dimension Locking', () => {
112149
test('When dimensions are locked it maintains the original ratio', async () => {
113150
render(<ImageSettingsModal {...props} />);

src/editors/sharedComponents/ImageUploadModal/ImageSettingsModal/index.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ interface ImageSettingsModalCommonProps {
2929
externalUrl: string;
3030
url: string;
3131
classList: string[];
32+
width?: string | number | null;
33+
height?: string | number | null;
3234
};
3335
}
3436

@@ -61,12 +63,17 @@ const ImageSettingsModalBase = ({
6163
const intl = useIntl();
6264
const formik = useFormikContext<ImageConfig>();
6365

64-
const setOriginalDimensions = React.useCallback(async (event: React.ChangeEvent<HTMLImageElement>) => {
65-
const img = event.currentTarget as HTMLImageElement;
66-
setImgDimensions({ width: img.naturalWidth, height: img.naturalHeight });
67-
await formik.setFieldValue('width', img.naturalWidth, false);
68-
await formik.setFieldValue('height', img.naturalHeight, false);
69-
}, [formik]);
66+
const setOriginalDimensions = React.useCallback(
67+
async (event: React.ChangeEvent<HTMLImageElement>) => {
68+
const img = event.currentTarget as HTMLImageElement;
69+
setImgDimensions({ width: img.naturalWidth, height: img.naturalHeight });
70+
if (!selection.width || !selection.height) {
71+
await formik.setFieldValue('width', img.naturalWidth, false);
72+
await formik.setFieldValue('height', img.naturalHeight, false);
73+
}
74+
},
75+
[formik, selection.width, selection.height],
76+
);
7077

7178
const handleDismissError = React.useCallback(() => formik.setFieldError('altText', ''), [formik]);
7279

@@ -176,8 +183,8 @@ const ImageSettingsModal = ({
176183
const [imgDimensions, setImgDimensions] = React.useState<OrigImageDimensions>({ width: 0, height: 0 });
177184
const initialValues: ImageConfig = {
178185
isLocked: true,
179-
width: imgDimensions.width,
180-
height: imgDimensions.height,
186+
width: selection.width ?? imgDimensions.width,
187+
height: selection.height ?? imgDimensions.height,
181188
isDecorative: !selection.altText,
182189
altText: selection.altText || '',
183190
classList: selection.classList,

0 commit comments

Comments
 (0)