Line data Source code
1 16 : <template>
2 0 : <div class="image-upload-wrapper">
3 : <div class="flex items-center gap-2">
4 : <div
5 : class="preview-box flex size-16 items-center justify-center rounded-sm border p-2"
6 : :class="{ 'bg-background': !modelValue }"
7 : >
8 : <img
9 : v-if="modelValue"
10 : :src="modelValue"
11 : class="max-h-full max-w-full object-contain"
12 : />
13 : <i v-else class="icon-[lucide--image] size-5 text-muted-foreground" />
14 : </div>
15 :
16 : <div class="flex flex-col gap-2">
17 : <Button size="sm" @click="triggerFileInput">
18 : <i class="icon-[lucide--upload]" />
19 : {{ $t('g.upload') }}
20 : </Button>
21 : <Button
22 : v-if="modelValue"
23 : class="w-full"
24 : variant="destructive"
25 : size="sm"
26 : :aria-label="$t('g.delete')"
27 : @click="clearImage"
28 : >
29 : <i class="icon-[lucide--trash-2]" />
30 : </Button>
31 : </div>
32 : </div>
33 : <input
34 : ref="fileInput"
35 : type="file"
36 : class="hidden"
37 : accept="image/*"
38 : @change="handleFileUpload"
39 : />
40 : </div>
41 : </template>
42 :
43 : <script setup lang="ts">
44 : import { ref } from 'vue'
45 :
46 : import Button from '@comfyorg/design-system/button/Button'
47 :
48 : defineProps<{
49 : modelValue: string
50 : }>()
51 :
52 0 : const emit = defineEmits<{
53 : (e: 'update:modelValue', value: string): void
54 : }>()
55 :
56 0 : const fileInput = ref<HTMLInputElement | null>(null)
57 :
58 0 : const triggerFileInput = () => {
59 0 : fileInput.value?.click()
60 : }
61 :
62 0 : const handleFileUpload = (event: Event) => {
63 0 : const target = event.target as HTMLInputElement
64 0 : if (target.files && target.files[0]) {
65 0 : const file = target.files[0]
66 0 : const reader = new FileReader()
67 0 : reader.onload = (e) => {
68 0 : emit('update:modelValue', e.target?.result as string)
69 : }
70 0 : reader.readAsDataURL(file)
71 : }
72 : }
73 :
74 0 : const clearImage = () => {
75 0 : emit('update:modelValue', '')
76 0 : if (fileInput.value) {
77 0 : fileInput.value.value = ''
78 : }
79 : }
80 : </script>
|