import type { Meta, StoryObj } from '@storybook/vue3-vite'
import { ref } from 'vue'
import VvInputFile from '@/components/VvInputFile/VvInputFile.vue'
import { useBlurhash } from '@/composables/useBlurhash'
const meta: Meta = {
title: 'Composables/useBlurhash',
tags: ['autodocs'],
}
export default meta
export const Default: StoryObj = {
parameters: {
docs: {
canvas: {
sourceState: 'shown',
},
source: {
code: `
`,
},
},
},
render: args => ({
components: { VvInputFile },
setup() {
const isLoading = ref(false)
const { encode, decode, loadImage } = useBlurhash()
const file = ref({})
const canvas = ref()
const isImgLoaded = ref(false)
const blurhash = ref('')
const imageUrl = ref('')
const image = ref()
return {
args,
isLoading,
canvas,
encode,
decode,
file,
blurhash,
isImgLoaded,
loadImage,
image,
imageUrl,
}
},
watch: {
file: {
immediate: true,
async handler(newValue) {
if (!newValue?.size) {
this.image = null
this.imageUrl = ''
this.blurhash = ''
return
}
this.isLoading = true
this.imageUrl = URL.createObjectURL(newValue)
this.image = await this.loadImage(this.imageUrl)
this.blurhash = await this.encode(newValue)
this.isLoading = false
},
},
blurhash: {
async handler(newValue) {
if (this.image) {
this.isLoading = true
const blurhashDecoded = await this.decode(
newValue,
this.image.width,
this.image.height,
)
this.isLoading = false
if (this.canvas) {
this.canvas.width = this.image.width
this.canvas.height = this.image.height
const ctx = this.canvas.getContext('2d')
const imageData = ctx.createImageData(
this.canvas.width,
this.canvas.height,
)
imageData.data.set(blurhashDecoded)
ctx.putImageData(imageData, 0, 0)
}
}
},
},
},
template: /* html */ `
`,
}),
}