Skip to content

Commit bff1f67

Browse files
committed
Fix inline image collapse when file is not ready
Use the file's width to determine whether actual dimensions are available instead of just checking file presence. An unprocessed image file exists but has no dimensions yet, causing the aspect ratio fallback to be skipped and the image to collapse to zero height.
1 parent 39b066f commit bff1f67

7 files changed

Lines changed: 60 additions & 8 deletions

File tree

entry_types/scrolled/package/spec/contentElements/inlineImage/InlineImage-spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,5 +337,23 @@ describe('InlineImage', () => {
337337
const contentElement = getContentElement();
338338
expect(contentElement.getFitViewportAspectRatio()).toEqual('0.75');
339339
});
340+
341+
it('uses default aspect ratio when file is not ready', () => {
342+
const {getContentElement} = renderContentElement({
343+
typeName: 'inlineImage',
344+
configuration: {
345+
id: 100
346+
},
347+
imageFiles: [{
348+
permaId: 100,
349+
isReady: false,
350+
width: null,
351+
height: null
352+
}]
353+
});
354+
355+
const contentElement = getContentElement();
356+
expect(contentElement.getFitViewportAspectRatio()).toEqual('0.75');
357+
});
340358
});
341359
});

entry_types/scrolled/package/spec/frontend/FitViewport-spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ describe('FitViewport', () => {
9292
expect(container.querySelector(`.${fullscreenStyles.root}`)).not.toBeNull();
9393
});
9494

95+
it('uses fallback aspect ratio when file is present but not ready', () => {
96+
const file = {isReady: false};
97+
const {container} = render(
98+
<FitViewport file={file} fallbackAspectRatio={0.75}>
99+
<FitViewport.Content />
100+
</FitViewport>
101+
);
102+
103+
expect(getOuter(container)).toHaveStyle('--fit-viewport-aspect-ratio: 0.75');
104+
});
105+
106+
it('uses file aspect ratio when file is ready even if fallback is given', () => {
107+
const file = {width: 200, height: 100};
108+
const {container} = render(
109+
<FitViewport file={file} fallbackAspectRatio={0.75}>
110+
<FitViewport.Content />
111+
</FitViewport>
112+
);
113+
114+
expect(getOuter(container)).toHaveStyle('--fit-viewport-aspect-ratio: 0.5');
115+
});
116+
117+
it('uses fallback aspect ratio when no file is present', () => {
118+
const {container} = render(
119+
<FitViewport fallbackAspectRatio={0.75}>
120+
<FitViewport.Content />
121+
</FitViewport>
122+
);
123+
124+
expect(getOuter(container)).toHaveStyle('--fit-viewport-aspect-ratio: 0.75');
125+
});
126+
95127
function getOuter(container) {
96128
return container.querySelector(`.${styles.container}`);
97129
}

entry_types/scrolled/package/src/contentElements/hotspots/Hotspots.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export function HotspotsImage({
235235
activeIndex={activeIndex}
236236
activateArea={activateArea}>
237237
<FitViewport file={imageFile}
238-
aspectRatio={imageFile ? undefined : 0.75}
238+
fallbackAspectRatio={0.75}
239239
fill={configuration.position === 'backdrop'}
240240
opaque={!imageFile}>
241241
<Composite activeIndex={activeIndex + 1}

entry_types/scrolled/package/src/contentElements/imageGallery/ImageGallery.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function ItemImageWithCaption({item, imageFile, configuration, current, onClick,
250250

251251
return (
252252
<FitViewport file={imageFile}
253-
aspectRatio={imageFile ? undefined : 0.75}
253+
fallbackAspectRatio={0.75}
254254
scale={0.8}
255255
opaque={!imageFile}>
256256
<ContentElementBox>

entry_types/scrolled/package/src/contentElements/inlineImage/InlineImage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ function ImageWithCaption({
9090

9191
return (
9292
<FitViewport file={imageFile}
93-
aspectRatio={aspectRatio || (imageFile ? undefined : 0.75)}
93+
aspectRatio={aspectRatio}
94+
fallbackAspectRatio={0.75}
9495
opaque={!imageFile}>
9596
<ContentElementBox borderRadius={isCircleCrop ? 'none' : rounded}>
9697
<ContentElementFigure configuration={configuration}>

entry_types/scrolled/package/src/contentElements/inlineVideo/InlineVideo.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ function OrientationUnawareInlineVideo({
123123
return (
124124
<MediaInteractionTracking playerState={playerState} playerActions={playerActions}>
125125
<FitViewport file={videoFile}
126-
aspectRatio={videoFile ?
127-
undefined : fallbackAspectRatio}
126+
fallbackAspectRatio={fallbackAspectRatio}
128127
fill={configuration.position === 'backdrop'}
129128
opaque={!videoFile}>
130129
<ContentElementBox>

entry_types/scrolled/package/src/frontend/FitViewport.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ const AspectRatioContext = React.createContext();
3232
* @param {Object} [props.opaque] - Render black background behind content.
3333
* @param {string} [props.fill] - Ignore aspect ration and fill viewport vertically.
3434
*/
35-
export function FitViewport({file, aspectRatio, opaque, children, fill, scale}) {
36-
if (!file && !aspectRatio) return children;
35+
export function FitViewport({file, aspectRatio, fallbackAspectRatio, opaque, children, fill, scale}) {
36+
if (!file && !aspectRatio && !fallbackAspectRatio) return children;
3737

3838
if (typeof aspectRatio === 'string') {
3939
aspectRatio = `var(--theme-aspect-ratio-${aspectRatio})`;
4040
}
4141

42-
aspectRatio = fill ? 'fill' : aspectRatio || (file.height / file.width);
42+
aspectRatio = fill ? 'fill' :
43+
aspectRatio ||
44+
(file?.width ? file.height / file.width : fallbackAspectRatio);
4345

4446
return (
4547
<div className={classNames(styles.container, {[styles.opaque]: opaque})}

0 commit comments

Comments
 (0)