From bf06e4123800f2d5797535c13fa87e983f9ec59b Mon Sep 17 00:00:00 2001 From: Avior Date: Mon, 9 Oct 2023 08:16:47 +0000 Subject: [PATCH] feat: Publish Picture.astro --- src/components/Picture.astro | 88 ++++++++++++++++++++++++++++++++ src/components/Picture.astro.tmp | 42 --------------- 2 files changed, 88 insertions(+), 42 deletions(-) create mode 100644 src/components/Picture.astro delete mode 100644 src/components/Picture.astro.tmp diff --git a/src/components/Picture.astro b/src/components/Picture.astro new file mode 100644 index 0000000..8855d84 --- /dev/null +++ b/src/components/Picture.astro @@ -0,0 +1,88 @@ +--- +import { getImage } from 'astro:assets' +import AstroUtils from '../libs/AstroUtils' +import { objectOmit } from '@dzeio/object-util' + +const formats = [ + 'avif', + 'webp' +] + +export interface Props extends Omit { + src: ImageMetadata | string + srcDark?: ImageMetadata | string + width?: number + height?: number +} + +type PictureResult = { + format: 'new' + formats: Array<{format: string, img: Awaited>}> + src: Awaited> +} | { + format: 'raw' + src: string +} + +interface Result { + light: PictureResult + dark?: PictureResult | undefined +} + +async function resolvePicture(image: ImageMetadata | string): Promise { + const ext = typeof image === 'string' ? image.substring(image.lastIndexOf('.')) : image.format + if (ext === 'svg') { + return { + format: 'raw', + src: typeof image === 'string' ? image : image.src + } + } + + const imageFormats: Array<{format: string, img: Awaited>}> = await Promise.all( + formats.map(async (it) => ({ + img: await getImage({src: Astro.props.src, format: it, width: Astro.props.width, height: Astro.props.height}), + format: it + })) + ) + + const orig = await getImage({src: Astro.props.src, format: ext, width: Astro.props.width, height: Astro.props.height}) + + return { + format: 'new', + formats: imageFormats, + src: orig + } +} + +const res = await AstroUtils.wrap(async () => { + return { + light: await resolvePicture(Astro.props.src), + dark: Astro.props.srcDark ? await resolvePicture(Astro.props.srcDark) : undefined + } +}) + +const props = objectOmit(Astro.props, 'src', 'srcDark', 'class') + +--- + +{res.light.format === 'new' && ( + + {res.light.formats.map((it) => ( + + ))} + + +) || ( + +)} + +{res.dark && res.dark.format === 'new' && ( + + {res.dark.formats.map((it) => ( + + ))} + + +) || (res.dark && ( + +))} diff --git a/src/components/Picture.astro.tmp b/src/components/Picture.astro.tmp deleted file mode 100644 index c06f65e..0000000 --- a/src/components/Picture.astro.tmp +++ /dev/null @@ -1,42 +0,0 @@ ---- -import { LocalImageProps, RemoteImageProps, getImage } from 'astro:assets' -import AstroUtils from '../libs/AstroUtils' -type ImageProps = LocalImageProps | RemoteImageProps -export type Props = ImageProps - -const res = await AstroUtils.wrap(async () => { - const image = Astro.props.src - const ext = typeof image === 'string' ? image.substring(image.lastIndexOf('.')) : image.format - if (ext === 'svg') { - return { - format: 'raw', - props: { - ...Astro.props, - src: typeof image === 'string' ? image : image.src - } - } - } - - const avif = await getImage({src: Astro.props.src, format: 'avif'}) - const webp = await getImage({src: Astro.props.src, format: 'webp'}) - const orig = await getImage({src: Astro.props.src, format: ext}) - - return { - format: 'new', - avif, - webp, - orig - } -}) - ---- - -{res.format === 'new' && ( - - - - - -) || ( - -)}