2022-11-21 16:21:53 +03:00
|
|
|
import { decode } from 'blurhash'
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
inheritAttrs: false,
|
|
|
|
props: {
|
|
|
|
blurhash: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
src: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
},
|
2022-12-06 18:10:53 +03:00
|
|
|
srcset: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
},
|
2022-11-21 16:21:53 +03:00
|
|
|
},
|
|
|
|
setup(props, { attrs }) {
|
|
|
|
const placeholderSrc = ref<string>()
|
|
|
|
const isLoaded = ref(false)
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
const img = document.createElement('img')
|
|
|
|
img.onload = () => {
|
|
|
|
isLoaded.value = true
|
|
|
|
}
|
|
|
|
img.src = props.src
|
2022-12-06 18:10:53 +03:00
|
|
|
if (props.srcset)
|
|
|
|
img.srcset = props.srcset
|
2022-11-24 07:13:13 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
isLoaded.value = true
|
|
|
|
}, 3_000)
|
2022-11-21 16:21:53 +03:00
|
|
|
|
|
|
|
if (props.blurhash) {
|
|
|
|
const pixels = decode(props.blurhash, 32, 32)
|
|
|
|
placeholderSrc.value = getDataUrlFromArr(pixels, 32, 32)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return () => isLoaded.value || !placeholderSrc.value
|
2022-12-06 18:10:53 +03:00
|
|
|
? h('img', { ...attrs, src: props.src, srcset: props.srcset })
|
2022-11-21 16:21:53 +03:00
|
|
|
: h('img', { ...attrs, src: placeholderSrc.value })
|
|
|
|
},
|
|
|
|
})
|