-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): teaser block uses highest resolution available to it
- Loading branch information
Showing
3 changed files
with
67 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { isInternalURL } from '@plone/volto/helpers'; | ||
// import config from '@plone/volto/registry'; | ||
|
||
function findNameOfBiggestImage(images) { | ||
let maxHeight = 0; | ||
let objectNameWithMaxHeight = 'teaser'; | ||
|
||
for (const [key, value] of Object.entries(images)) { | ||
if (value.height > maxHeight) { | ||
maxHeight = value.height; | ||
objectNameWithMaxHeight = key; | ||
} | ||
} | ||
|
||
return objectNameWithMaxHeight; | ||
} | ||
|
||
export function getTeaserImageURL({ href, image }) { | ||
// The default scale used in teasers is the 'teaser' scale | ||
// except if it's customized otherwise in the teaser block settings | ||
// or if the teaser is center (top) | ||
// const imageScale = isHero ? 'great' : 'larger'; | ||
const { scales } = href.image_scales?.[href.image_field]?.[0] || {}; | ||
const imageScale = findNameOfBiggestImage(scales); | ||
|
||
if (image) { | ||
// If the image is overriden locally in the teaser block | ||
if (isInternalURL(image['@id'])) { | ||
// If it's internal check if image_scales catalog info is present | ||
if (image?.image_scales?.[image?.image_field]) { | ||
return `${image['@id']}/${ | ||
image.image_scales[image.image_field]?.[0].scales[imageScale] | ||
?.download || image.image_scales[image.image_field]?.[0].download | ||
}`; | ||
} else { | ||
// If not, fallback to content scale URL shortcut | ||
return `${image['@id']}/@@images/${image.image_field}/${imageScale}`; | ||
} | ||
} else { | ||
// If it's external, return the plain URL | ||
return image['@id']; | ||
} | ||
} else { | ||
// If the image is not overriden | ||
if (href?.image_scales?.[href?.image_field]) { | ||
return `${href['@id']}/${ | ||
href.image_scales[href.image_field]?.[0].scales[imageScale]?.download || | ||
href.image_scales[href.image_field]?.[0].download | ||
}`; | ||
} else { | ||
// If not, fallback to content scale URL shortcut | ||
return `${href['@id']}/@@images/${ | ||
href.image_field || 'preview_image' | ||
}/${imageScale}`; | ||
} | ||
} | ||
} |