2023-01-05 15:38:22 +03:00
|
|
|
<script setup lang="ts">
|
|
|
|
import { decode } from 'blurhash'
|
|
|
|
|
2023-02-15 13:34:23 +03:00
|
|
|
const { blurhash, src, srcset, shouldLoadImage = true } = defineProps<{
|
2023-01-05 19:48:20 +03:00
|
|
|
blurhash?: string | null | undefined
|
2023-01-05 15:38:22 +03:00
|
|
|
src: string
|
|
|
|
srcset?: string
|
2023-02-15 13:34:23 +03:00
|
|
|
shouldLoadImage?: boolean
|
2023-01-05 15:38:22 +03:00
|
|
|
}>()
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
inheritAttrs: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
const isLoaded = ref(false)
|
2023-01-05 19:48:20 +03:00
|
|
|
const placeholderSrc = $computed(() => {
|
|
|
|
if (!blurhash)
|
|
|
|
return ''
|
|
|
|
const pixels = decode(blurhash, 32, 32)
|
|
|
|
return getDataUrlFromArr(pixels, 32, 32)
|
|
|
|
})
|
2023-01-05 15:38:22 +03:00
|
|
|
|
2023-02-15 13:34:23 +03:00
|
|
|
function loadImage() {
|
2023-01-05 15:38:22 +03:00
|
|
|
const img = document.createElement('img')
|
|
|
|
|
|
|
|
img.onload = () => {
|
|
|
|
isLoaded.value = true
|
|
|
|
}
|
|
|
|
|
|
|
|
img.src = src
|
|
|
|
|
|
|
|
if (srcset)
|
|
|
|
img.srcset = srcset
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
isLoaded.value = true
|
|
|
|
}, 3_000)
|
2023-02-15 13:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
if (shouldLoadImage)
|
|
|
|
loadImage()
|
|
|
|
})
|
|
|
|
|
|
|
|
watch(() => shouldLoadImage, () => {
|
|
|
|
if (shouldLoadImage)
|
|
|
|
loadImage()
|
2023-01-05 15:38:22 +03:00
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2023-01-13 12:07:10 +03:00
|
|
|
<img v-if="isLoaded || !placeholderSrc" v-bind="$attrs" :src="src" :srcset="srcset">
|
|
|
|
<img v-else v-bind="$attrs" :src="placeholderSrc">
|
2023-01-05 15:38:22 +03:00
|
|
|
</template>
|