refactor: use auto sizes with Directus

This commit is contained in:
2024-07-24 10:29:49 +02:00
parent e2f118d152
commit 40722719cd
3 changed files with 23 additions and 54 deletions

View File

@@ -5,78 +5,47 @@
export let id: string = undefined export let id: string = undefined
export let sizeKey: string = undefined export let sizeKey: string = undefined
export let sizes: Sizes = undefined export let sizes: Sizes = undefined
export let width: number = sizes && sizes.medium && sizes.medium.width export let width: number = sizes?.medium?.width
export let height: number = sizes && sizes.medium && sizes.medium.height export let height: number = sizes?.medium?.height
export let ratio: number = undefined export let ratio: number = undefined
export let alt: string export let alt: string
export let lazy = true export let lazy = true
export let decoding: 'auto' | 'sync' | 'async' = 'auto' export let decoding: 'auto' | 'sync' | 'async' = 'auto'
interface Sizes { interface Sizes {
small?: { width?: number, height?: number } small?: { width?: number; height?: number }
medium?: { width?: number, height?: number } medium?: { width?: number; height?: number }
large?: { width?: number, height?: number } large?: { width?: number; height?: number }
} }
let srcSet = { webp: [], jpg: [] } const setHeightFromRatio = (w: number, r: number = ratio) => Math.round(w / r)
/**
* Define height from origin ratio if not defined
*/
const setHeightFromRatio = (w: number, r: number = ratio) => {
return Math.round(w / r)
}
if (ratio && !height) { if (ratio && !height) {
// Set height from width using ratio
height = setHeightFromRatio(width) height = setHeightFromRatio(width)
// Add height to all sizes
if (sizes) { if (sizes) {
Object.entries(sizes).forEach(size => { for (const [key, value] of Object.entries(sizes)) {
const [key, value]: [string, { width?: number, height?: number }] = size
sizes[key].height = setHeightFromRatio(value.width) sizes[key].height = setHeightFromRatio(value.width)
}) }
} }
} }
$: imgWidth = sizes?.small?.width || width
/** $: imgHeight = sizes?.small?.height || height
* Image attributes $: imgSrc = id ? getAssetUrlKey(id, `${sizeKey}-small`) : src
*/ $: srcSet = sizes
$: imgWidth = sizes && sizes.small ? sizes.small.width : width
$: imgHeight = sizes && sizes.small ? sizes.small.height : height
$: imgSrc = id ? getAssetUrlKey(id, `${sizeKey}-small-jpg`) : src ? src : undefined
$: srcSet = {
// WebP
webp: sizes
? [ ? [
`${getAssetUrlKey(id, `${sizeKey}-small-webp`)} 345w`, `${getAssetUrlKey(id, `${sizeKey}-small`)} 345w`,
sizes.medium ? `${getAssetUrlKey(id, `${sizeKey}-medium-webp`)} 768w` : null, sizes.medium && `${getAssetUrlKey(id, `${sizeKey}-medium`)} 768w`,
sizes.large ? `${getAssetUrlKey(id, `${sizeKey}-large-webp`)} 1280w` : null, sizes.large && `${getAssetUrlKey(id, `${sizeKey}-large`)} 1280w`,
] ]
: [getAssetUrlKey(id, `${sizeKey}-webp`)], : [getAssetUrlKey(id, sizeKey)]
// JPG
jpg: sizes
? [
`${getAssetUrlKey(id, `${sizeKey}-small-jpg`)} 345w`,
sizes.medium ? `${getAssetUrlKey(id, `${sizeKey}-medium-jpg`)} 768w` : null,
sizes.large ? `${getAssetUrlKey(id, `${sizeKey}-large-jpg`)} 1280w` : null,
]
: [getAssetUrlKey(id, `${sizeKey}-jpg`)]
}
</script> </script>
<picture class={$$props.class}> <picture class={$$props.class}>
<source
type="image/webp"
srcset={srcSet.webp.join(', ').trim()}
>
<img <img
src={imgSrc} src={imgSrc}
sizes={sizes ? '(min-width: 1200px) 864px, (min-width: 992px) 708px, (min-width: 768px) 540px, 100%' : null} sizes={sizes ? '(min-width: 1200px) 864px, (min-width: 992px) 708px, (min-width: 768px) 540px, 100%' : undefined}
srcset={srcSet.jpg.join(', ').trim()} srcset={srcSet.join(', ')}
width={imgWidth} width={imgWidth}
height={imgHeight} height={imgHeight}
{alt} {alt}

View File

@@ -221,7 +221,7 @@
{about.intro_firstphoto_caption}<br> {about.intro_firstphoto_caption}<br>
in in
<a href="/{about.intro_firstlocation.country.slug}/{about.intro_firstlocation.slug}" data-sveltekit-noscroll> <a href="/{about.intro_firstlocation.country.slug}/{about.intro_firstlocation.slug}" data-sveltekit-noscroll>
<img src={getAssetUrlKey(about.intro_firstlocation.country.flag.id, 'square-small-jpg')} width="32" height="32" alt={about.intro_firstlocation.country.flag.title}> <img src={getAssetUrlKey(about.intro_firstlocation.country.flag.id, 'square-small')} width="32" height="32" alt={about.intro_firstlocation.country.flag.title}>
<span>Naarm Australia (Melbourne)</span> <span>Naarm Australia (Melbourne)</span>
</a> </a>
</figcaption> </figcaption>

View File

@@ -47,7 +47,7 @@ export const GET = async ({ url, setHeaders }) => {
slug: siteProduct.location.slug, slug: siteProduct.location.slug,
description: siteProduct.description, description: siteProduct.description,
price: product.price, price: product.price,
images: siteProduct.photos_product.map(({ directus_files_id: { id } }: any) => getAssetUrlKey(id, `product-large-jpg`)), images: siteProduct.photos_product.map(({ directus_files_id: { id } }: any) => getAssetUrlKey(id, `product-large`)),
gCategory: category.value, gCategory: category.value,
gType: category.type, gType: category.type,
}) })