1
1
<template >
2
- <input-wrapper
3
- v-bind =" inputWrapperProps"
4
- >
2
+ <input-wrapper v-bind =" inputWrapperProps" >
5
3
<template #label >
6
4
<slot name =" label" />
7
5
</template >
8
6
9
- <div class =" flex w-full items-center justify-center transition-colors duration-40"
10
- :class =" [{'!cursor-not-allowed':disabled, 'cursor-pointer':!disabled,
11
- [theme.fileInput.inputHover.light + ' dark:'+theme.fileInput.inputHover.dark]: uploadDragoverEvent,
12
- ['hover:'+theme.fileInput.inputHover.light +' dark:hover:'+theme.fileInput.inputHover.dark]: !loading}, theme.fileInput.input]"
13
- @dragover.prevent =" uploadDragoverEvent=true"
14
- @dragleave.prevent =" uploadDragoverEvent=false"
15
- @drop.prevent =" onUploadDropEvent"
16
- @click =" openFileUpload"
17
- >
18
- <div
19
- v-if =" loading"
20
- class =" text-gray-600 dark:text-gray-400"
21
- >
7
+ <div class =" flex w-full items-center justify-center transition-colors duration-40" :class =" [{
8
+ '!cursor-not-allowed': disabled, 'cursor-pointer': !disabled,
9
+ [theme.fileInput.inputHover.light + ' dark:' + theme.fileInput.inputHover.dark]: uploadDragoverEvent,
10
+ ['hover:' + theme.fileInput.inputHover.light + ' dark:hover:' + theme.fileInput.inputHover.dark]: !loading
11
+ }, theme.fileInput.input]" @dragover.prevent =" uploadDragoverEvent = true"
12
+ @dragleave.prevent =" uploadDragoverEvent = false" @drop.prevent =" onUploadDropEvent" @click =" openFileUpload" >
13
+ <div v-if =" loading" class =" text-gray-600 dark:text-gray-400" >
22
14
<Loader class =" mx-auto h-6 w-6" />
23
15
<p class =" mt-2 text-center text-sm text-gray-500" >
24
16
Uploading your file...
25
17
</p >
26
18
</div >
27
19
<template v-else >
28
20
<div class =" text-center" >
29
- <input
30
- ref =" actual-input"
31
- class =" hidden"
32
- :multiple =" multiple"
33
- type =" file"
34
- :name =" name"
35
- :accept =" acceptExtensions"
36
- @change =" manualFileUpload"
37
- >
21
+ <input ref =" actual-input" class =" hidden" :multiple =" multiple" type =" file" :name =" name"
22
+ :accept =" acceptExtensions" @change =" manualFileUpload" >
38
23
<div v-if =" files.length" class =" flex flex-wrap items-center justify-center gap-4" >
39
- <uploaded-file
40
- v-for =" file in files"
41
- :key =" file.url"
42
- :file =" file"
43
- :theme =" theme"
44
- @remove =" clearFile(file)"
45
- />
24
+ <uploaded-file v-for =" file in files" :key =" file.url" :file =" file" :theme =" theme"
25
+ @remove =" clearFile(file)" />
46
26
</div >
47
27
<template v-else >
48
28
<div class =" text-gray-500 w-full flex justify-center" >
49
29
<svg xmlns =" http://www.w3.org/2000/svg" fill =" none" viewBox =" 0 0 24 24" stroke-width =" 1.5"
50
- stroke =" currentColor" class =" w-6 h-6"
51
- >
30
+ stroke =" currentColor" class =" w-6 h-6" >
52
31
<path stroke-linecap =" round" stroke-linejoin =" round"
53
- d =" M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5"
54
- />
32
+ d =" M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5" />
55
33
</svg >
56
34
</div >
57
35
79
57
import { inputProps , useFormInput } from ' ./useFormInput.js'
80
58
import InputWrapper from ' ./components/InputWrapper.vue'
81
59
import UploadedFile from ' ./components/UploadedFile.vue'
82
- import {storeFile } from " ~/lib/file-uploads.js"
60
+ import { storeFile } from " ~/lib/file-uploads.js"
83
61
84
62
export default {
85
63
name: ' FileInput' ,
@@ -93,7 +71,7 @@ export default {
93
71
moveToFormAssets: { type: Boolean , default: false }
94
72
},
95
73
96
- setup (props , context ) {
74
+ setup (props , context ) {
97
75
return {
98
76
... useFormInput (props, context)
99
77
}
@@ -106,10 +84,10 @@ export default {
106
84
}),
107
85
108
86
computed: {
109
- currentUrl () {
87
+ currentUrl () {
110
88
return this .form [this .name ]
111
89
},
112
- acceptExtensions () {
90
+ acceptExtensions () {
113
91
if (! this .accept ) {
114
92
return null
115
93
}
@@ -126,13 +104,13 @@ export default {
126
104
watch: {
127
105
files: {
128
106
deep: true ,
129
- handler (files ) {
107
+ handler (files ) {
130
108
this .compVal = files .map ((file ) => file .url )
131
109
}
132
110
}
133
111
},
134
112
135
- async created () {
113
+ async created () {
136
114
if (typeof this .compVal === ' string' || this .compVal instanceof String ) {
137
115
await this .getFileFromUrl (this .compVal ).then ((fileObj ) => {
138
116
this .files = [{
@@ -157,34 +135,34 @@ export default {
157
135
},
158
136
159
137
methods: {
160
- clearAll () {
138
+ clearAll () {
161
139
this .files = []
162
140
},
163
- clearFile (index ) {
141
+ clearFile (index ) {
164
142
this .files .splice (index, 1 )
165
143
},
166
- onUploadDropEvent (e ) {
144
+ onUploadDropEvent (e ) {
167
145
this .uploadDragoverEvent = false
168
146
this .droppedFiles (e .dataTransfer .files )
169
147
},
170
- droppedFiles (droppedFiles ) {
148
+ droppedFiles (droppedFiles ) {
171
149
if (! droppedFiles || this .disabled ) return
172
150
173
151
for (let i = 0 ; i < droppedFiles .length ; i++ ) {
174
152
this .uploadFileToServer (droppedFiles .item (i))
175
153
}
176
154
},
177
- openFileUpload () {
155
+ openFileUpload () {
178
156
if (this .disabled || ! this .$refs [' actual-input' ]) return
179
157
this .$refs [' actual-input' ].click ()
180
158
},
181
- manualFileUpload (e ) {
159
+ manualFileUpload (e ) {
182
160
const files = e .target .files
183
161
for (let i = 0 ; i < files .length ; i++ ) {
184
162
this .uploadFileToServer (files .item (i))
185
163
}
186
164
},
187
- uploadFileToServer (file ) {
165
+ uploadFileToServer (file ) {
188
166
if (this .disabled ) return
189
167
this .loading = true
190
168
storeFile (file)
@@ -224,15 +202,18 @@ export default {
224
202
this .loading = false
225
203
})
226
204
},
227
- async getFileFromUrl (url , defaultType = ' image/jpeg' ) {
205
+ async getFileFromUrl (url , defaultType = ' image/jpeg' ) {
206
+ if (typeof url === ' object' ) {
207
+ url = url? .file_url
208
+ }
228
209
const response = await fetch (url)
229
210
const data = await response .blob ()
230
211
const name = url .replace (/ ^ . * (\\ | \/ | \: )/ , ' ' )
231
212
return new File ([data], name, {
232
213
type: data .type || defaultType
233
214
})
234
215
},
235
- getFileSrc (file ) {
216
+ getFileSrc (file ) {
236
217
if (file .type && file .type .split (' /' )[0 ] === ' image' ) {
237
218
return URL .createObjectURL (file)
238
219
}
0 commit comments