/
FieldImageUploadInput.vue
154 lines (135 loc) · 4.05 KB
/
FieldImageUploadInput.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
<script setup lang="ts">
import { computed, ref, toRef } from 'vue'
import { useDropZone } from '@vueuse/core'
import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
import CommonDivider from '#desktop/components/CommonDivider/CommonDivider.vue'
import useValue from '#shared/components/Form/composables/useValue.ts'
import { i18n } from '#shared/i18n.ts'
import type { FormFieldContext } from '#shared/components/Form/types/field.ts'
export interface Props {
context: FormFieldContext<{
placeholderImagePath?: string
}>
}
const props = defineProps<Props>()
const contextReactive = toRef(props, 'context')
const { localValue } = useValue(contextReactive)
const imageUpload = computed<string>({
get() {
return localValue.value || ''
},
set(value) {
localValue.value = value
},
})
const imageUploadOrPlaceholder = computed<string>(() => {
if (props.context.placeholderImagePath && !imageUpload.value) {
return props.context.placeholderImagePath || ''
}
return imageUpload.value
})
const MAX_IMAGE_SIZE_IN_MB = 8
const imageUploadInput = ref<HTMLInputElement>()
const reset = () => {
imageUpload.value = ''
const input = imageUploadInput.value
if (!input) return
input.value = ''
input.files = null
}
const loadImages = async (files: FileList | File[] | null) => {
Array.from(files || []).forEach((file) => {
const reader = new FileReader()
reader.onload = (e) => {
if (!e.target || !e.target.result) return
imageUpload.value = e.target.result as string
}
if (file.size && file.size > 1024 * 1024 * MAX_IMAGE_SIZE_IN_MB) {
props.context.node.setErrors(
i18n.t(
'File too big, max. %s MB allowed.',
MAX_IMAGE_SIZE_IN_MB.toString(),
),
)
return
}
reader.readAsDataURL(file)
})
}
const onFileChanged = async ($event: Event) => {
const input = $event.target as HTMLInputElement
const { files } = input
if (files) await loadImages(files)
}
const dropZoneRef = ref<HTMLDivElement>()
const { isOverDropZone } = useDropZone(dropZoneRef, {
onDrop: loadImages,
dataTypes: (types) => types.every((type) => type.startsWith('image/')),
})
</script>
<template>
<div
ref="dropZoneRef"
class="flex w-full flex-col items-center gap-2 p-2"
:class="context.classes.input"
>
<div
v-if="isOverDropZone"
class="w-full rounded text-center outline-dashed outline-1 outline-blue-800"
>
<CommonLabel
class="py-2 text-blue-800 dark:text-blue-800"
prefix-icon="upload"
>
{{ $t('Drop image file here') }}
</CommonLabel>
</div>
<template v-else>
<template v-if="imageUploadOrPlaceholder">
<div
class="grid w-full grid-cols-[20px_auto_20px] items-center justify-items-center gap-2.5 p-2.5"
>
<img
class="col-start-2 max-h-32"
:src="imageUploadOrPlaceholder"
:alt="$t('Image preview')"
/>
<CommonButton
v-if="imageUpload"
variant="remove"
size="small"
icon="x-lg"
:aria-label="$t('Remove image')"
@click="!context.disabled && reset()"
/>
</div>
<CommonDivider padding />
</template>
<CommonButton
variant="secondary"
size="medium"
prefix-icon="image"
:disabled="context.disabled"
@click="!context.disabled && imageUploadInput?.click()"
@blur="context.handlers.blur"
>{{ $t('Upload image') }}</CommonButton
>
</template>
<input
:id="context.id"
ref="imageUploadInput"
data-test-id="imageUploadInput"
type="file"
:name="context.node.name"
:disabled="context.disabled"
class="hidden"
:class="context.classes.input"
tabindex="-1"
aria-hidden="true"
accept="image/*"
v-bind="context.attrs"
@change="!context.disabled && onFileChanged($event)"
/>
</div>
</template>