@bagelink/vue 1.20.5 → 1.20.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets.d.ts +9 -0
- package/dist/assets.d.ts.map +1 -1
- package/dist/components/form/inputs/CodeEditor/Index.vue.d.ts +1 -1
- package/dist/components/form/inputs/Upload/AssetFileRow.vue.d.ts +37 -0
- package/dist/components/form/inputs/Upload/AssetFileRow.vue.d.ts.map +1 -0
- package/dist/components/form/inputs/Upload/AssetUploadInput.vue.d.ts +33 -3
- package/dist/components/form/inputs/Upload/AssetUploadInput.vue.d.ts.map +1 -1
- package/dist/components/form/inputs/Upload/ImageCropper.vue.d.ts +33 -0
- package/dist/components/form/inputs/Upload/ImageCropper.vue.d.ts.map +1 -0
- package/dist/components/form/inputs/Upload/UploadInput.vue.d.ts +13 -79
- package/dist/components/form/inputs/Upload/UploadInput.vue.d.ts.map +1 -1
- package/dist/components/form/inputs/Upload/upload.types.d.ts +14 -0
- package/dist/components/form/inputs/Upload/upload.types.d.ts.map +1 -1
- package/dist/components/lightbox/index.d.ts.map +1 -1
- package/dist/core.css +2 -2
- package/dist/i18n/index.d.ts +108 -0
- package/dist/i18n/index.d.ts.map +1 -1
- package/dist/index.cjs +99 -99
- package/dist/index.css +1 -1
- package/dist/index.mjs +15677 -14427
- package/dist/style.css +2 -2
- package/dist/types/BagelForm.d.ts +34 -0
- package/dist/types/BagelForm.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/assets.ts +27 -3
- package/src/components/dataTable/DataTable.vue +3 -3
- package/src/components/form/inputs/Upload/AssetFileRow.vue +150 -0
- package/src/components/form/inputs/Upload/AssetUploadInput.vue +694 -37
- package/src/components/form/inputs/Upload/ImageCropper.vue +525 -0
- package/src/components/form/inputs/Upload/UploadInput.vue +18 -3
- package/src/components/form/inputs/Upload/upload.css +109 -1
- package/src/components/form/inputs/Upload/upload.types.ts +15 -0
- package/src/components/lightbox/index.ts +6 -3
- package/src/i18n/locales/en.json +19 -1
- package/src/i18n/locales/es.json +19 -1
- package/src/i18n/locales/fr.json +21 -2
- package/src/i18n/locales/he.json +19 -1
- package/src/i18n/locales/it.json +19 -1
- package/src/i18n/locales/ru.json +19 -1
- package/src/styles/inputs.css +4 -0
- package/src/types/BagelForm.ts +34 -0
|
@@ -1,67 +1,724 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import { computed, onScopeDispose, ref } from 'vue'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { computed, inject, onScopeDispose, ref, useSlots } from 'vue'
|
|
3
|
+
import type { CSSProperties } from 'vue'
|
|
4
|
+
import { Btn, Card, Dialog, Icon, Progress, resolveI18n, useI18n } from '../../../../index'
|
|
5
|
+
import { ASSET_CLIENT_KEY, useAssetUpload, type AssetReference } from '../../../../assets'
|
|
5
6
|
import { useFileUpload } from './useFileUpload'
|
|
6
7
|
import { useFileDropzone } from '../../../file-drop/useFileDropzone'
|
|
8
|
+
import { useBagelInputShell } from '../bagelInputShell'
|
|
9
|
+
import AssetFileRow from './AssetFileRow.vue'
|
|
10
|
+
import ImageCropper from './ImageCropper.vue'
|
|
7
11
|
import type { UploadInputProps } from '../../../../types/BagelForm'
|
|
12
|
+
import type { QueueFile, UploadPreviewState } from './upload.types'
|
|
13
|
+
|
|
14
|
+
interface Row {
|
|
15
|
+
key: string
|
|
16
|
+
label: string
|
|
17
|
+
type: string
|
|
18
|
+
asset?: AssetReference
|
|
19
|
+
thumb?: string
|
|
20
|
+
progress?: number
|
|
21
|
+
success: boolean
|
|
22
|
+
downloadUrl?: string
|
|
23
|
+
/** Position inside `values`, so a row can be replaced or re-cropped in place. */
|
|
24
|
+
index?: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const props = withDefaults(defineProps<UploadInputProps & { previewState?: UploadPreviewState }>(), {
|
|
28
|
+
height: '215px',
|
|
29
|
+
theme: 'dropzone',
|
|
30
|
+
accept: '*',
|
|
31
|
+
showIcon: true,
|
|
32
|
+
icon: 'upload_2',
|
|
33
|
+
iconSize: 2.5,
|
|
34
|
+
iconWeight: 300,
|
|
35
|
+
uploadFrame: true,
|
|
36
|
+
// Re-cropping a stored image is on by default; forms opt out with `:crop-existing="false"`.
|
|
37
|
+
cropExisting: true,
|
|
38
|
+
showButton: 'auto',
|
|
39
|
+
showSuccess: true,
|
|
40
|
+
scroll: true,
|
|
41
|
+
previewLayout: 'inside',
|
|
42
|
+
previewType: 'list',
|
|
43
|
+
cropOutputSize: 2048,
|
|
44
|
+
})
|
|
8
45
|
|
|
9
|
-
const props = defineProps<UploadInputProps>()
|
|
10
46
|
const emit = defineEmits<{
|
|
11
47
|
'update:modelValue': [value: AssetReference | AssetReference[] | undefined]
|
|
12
48
|
addFileStart: []
|
|
49
|
+
uploadError: [error: unknown, file: File]
|
|
13
50
|
}>()
|
|
51
|
+
|
|
52
|
+
const { $t } = useI18n()
|
|
53
|
+
const slots = useSlots()
|
|
54
|
+
const client = inject(ASSET_CLIENT_KEY, undefined)
|
|
14
55
|
const { upload, uploading, cancel } = useAssetUpload()
|
|
56
|
+
const { shellClass, shellStyle } = useBagelInputShell(props)
|
|
57
|
+
|
|
15
58
|
const failure = ref('')
|
|
59
|
+
const locked = computed(() => !!props.disabled || !!props.readonly)
|
|
60
|
+
|
|
16
61
|
const values = computed<AssetReference[]>(() => [props.modelValue].flat().filter(
|
|
17
62
|
(value): value is AssetReference => !!value && typeof value === 'object' && 'asset_id' in value,
|
|
18
63
|
))
|
|
19
|
-
|
|
64
|
+
|
|
20
65
|
function output(items: AssetReference[]) {
|
|
21
66
|
emit('update:modelValue', props.multiple ? items : items[0])
|
|
22
67
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
68
|
+
|
|
69
|
+
function urlFor(value: AssetReference) {
|
|
70
|
+
return client?.assetUrl(value)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function labelFor(value: AssetReference) {
|
|
74
|
+
return value.name || `${value.asset_id.slice(0, 8)}…`
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Prefer the stored MIME type; fall back to the accepted types for older references. */
|
|
78
|
+
function typeFor(value: AssetReference): string {
|
|
79
|
+
const mime = value.content_type ?? ''
|
|
80
|
+
if (mime.startsWith('image/')) return 'image'
|
|
81
|
+
if (mime.startsWith('video/')) return 'video'
|
|
82
|
+
if (mime === 'application/pdf') return 'pdf'
|
|
83
|
+
if (mime) return 'file'
|
|
84
|
+
const accept = (props.accept ?? '').toLowerCase()
|
|
85
|
+
if (accept.includes('video/')) return 'video'
|
|
86
|
+
if (accept.includes('application/pdf')) return 'pdf'
|
|
87
|
+
return accept === '' || accept === '*' || accept.includes('image/') ? 'image' : 'file'
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function iconFor(type: string) {
|
|
91
|
+
return type === 'video' ? 'movie' : type === 'pdf' ? 'picture_as_pdf' : 'description'
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Keep the original base name when the cropper replaces the bytes (new extension). */
|
|
95
|
+
function renameLike(name: string, file: File) {
|
|
96
|
+
const base = name.replace(/\.[^./\\]+$/, '')
|
|
97
|
+
const ext = (file.name.split('.').pop() ?? '').toLowerCase()
|
|
98
|
+
return ext ? `${base}.${ext}` : base
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* ─── Transient success check ─────────────────────────────────────────────── */
|
|
102
|
+
|
|
103
|
+
const flashed = ref<string[]>([])
|
|
104
|
+
const flashTimers = new Set<ReturnType<typeof setTimeout>>()
|
|
105
|
+
|
|
106
|
+
function flash(assetId: string) {
|
|
107
|
+
if (props.showSuccess === false) return
|
|
108
|
+
flashed.value = [...flashed.value, assetId]
|
|
109
|
+
const timer = setTimeout(() => {
|
|
110
|
+
flashed.value = flashed.value.filter(id => id !== assetId)
|
|
111
|
+
flashTimers.delete(timer)
|
|
112
|
+
}, 1800)
|
|
113
|
+
flashTimers.add(timer)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* ─── Cropping (before upload, and again on an uploaded image) ────────────── */
|
|
117
|
+
|
|
118
|
+
const cropBeforeUpload = computed(() => !!props.crop)
|
|
119
|
+
/** Re-cropping is offered for any stored image; only an in-flight upload can't be cropped. */
|
|
120
|
+
const cropAfterUpload = computed(() => !!props.cropExisting)
|
|
121
|
+
const cropperAvailable = computed(() => cropBeforeUpload.value || cropAfterUpload.value)
|
|
122
|
+
|
|
123
|
+
const cropOpen = ref(false)
|
|
124
|
+
const cropSrc = ref('')
|
|
125
|
+
const cropMode = ref<'queue' | 'existing'>('queue')
|
|
126
|
+
const cropTarget = ref<number | null>(null)
|
|
127
|
+
const cropper = ref<InstanceType<typeof ImageCropper>>()
|
|
128
|
+
let cropResolve: ((file?: File) => void) | undefined
|
|
129
|
+
|
|
130
|
+
async function maybeCrop(file: File): Promise<File | undefined> {
|
|
131
|
+
if (!cropBeforeUpload.value || !file.type.startsWith('image/')) return file
|
|
132
|
+
cropMode.value = 'queue'
|
|
133
|
+
cropTarget.value = null
|
|
134
|
+
cropSrc.value = URL.createObjectURL(file)
|
|
135
|
+
cropOpen.value = true
|
|
136
|
+
const cropped = await new Promise<File | undefined>(resolve => { cropResolve = resolve })
|
|
137
|
+
if (!cropped) return undefined
|
|
138
|
+
return new File([cropped], renameLike(file.name, cropped), { type: cropped.type })
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function finishCrop(file?: File) {
|
|
142
|
+
const resolve = cropResolve
|
|
143
|
+
cropResolve = undefined
|
|
144
|
+
cropOpen.value = false
|
|
145
|
+
cropMode.value = 'queue'
|
|
146
|
+
cropTarget.value = null
|
|
147
|
+
if (cropSrc.value) URL.revokeObjectURL(cropSrc.value)
|
|
148
|
+
cropSrc.value = ''
|
|
149
|
+
resolve?.(file)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Open the cropper on a reference that is already stored (re-crop after upload). */
|
|
153
|
+
async function cropUploaded(row: Row) {
|
|
154
|
+
if (row.index === undefined || !row.asset) return
|
|
155
|
+
const url = urlFor(row.asset)
|
|
156
|
+
if (!url) return
|
|
157
|
+
let src = url
|
|
158
|
+
try {
|
|
159
|
+
// A same-origin blob keeps the crop canvas exportable (no tainted canvas).
|
|
160
|
+
const response = await fetch(url)
|
|
161
|
+
if (!response.ok) throw new Error(String(response.status))
|
|
162
|
+
src = URL.createObjectURL(await response.blob())
|
|
163
|
+
} catch { /* fall back to the delivery URL */ }
|
|
164
|
+
cropMode.value = 'existing'
|
|
165
|
+
cropTarget.value = row.index
|
|
166
|
+
cropSrc.value = src
|
|
167
|
+
cropOpen.value = true
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function confirmCrop() {
|
|
171
|
+
const mode = cropMode.value
|
|
172
|
+
const target = cropTarget.value
|
|
173
|
+
const file = await cropper.value?.getFile()
|
|
174
|
+
if (mode === 'existing' && target !== null && file) {
|
|
175
|
+
const previous = values.value[target]
|
|
176
|
+
finishCrop()
|
|
177
|
+
if (previous) await swapStored(target, previous, file)
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
finishCrop(file)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Upload the re-cropped bytes and put them in the slot the old reference held. */
|
|
184
|
+
async function swapStored(index: number, previous: AssetReference, file: File) {
|
|
185
|
+
failure.value = ''
|
|
186
|
+
try {
|
|
187
|
+
const reference = await upload(file, { visibility: props.assetVisibility })
|
|
188
|
+
const next = [...values.value]
|
|
189
|
+
next[index] = {
|
|
190
|
+
asset_id: reference.asset_id,
|
|
191
|
+
name: previous.name ? renameLike(previous.name, file) : file.name,
|
|
192
|
+
content_type: reference.content_type ?? file.type,
|
|
193
|
+
...(reference.size || file.size ? { size: reference.size ?? file.size } : {}),
|
|
194
|
+
}
|
|
195
|
+
output(next)
|
|
196
|
+
flash(next[index]!.asset_id)
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
const aborted = error instanceof DOMException && error.name === 'AbortError'
|
|
200
|
+
if (!aborted) {
|
|
201
|
+
failure.value = failure.value || $t('upload.failed')
|
|
202
|
+
emit('uploadError', error, file)
|
|
30
203
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/* ─── Queue → upload ──────────────────────────────────────────────────────── */
|
|
208
|
+
|
|
209
|
+
/** Row that the next picked file replaces; `null` appends (or replaces the whole value). */
|
|
210
|
+
const replaceTarget = ref<number | null>(null)
|
|
211
|
+
|
|
212
|
+
function discard(items: QueueFile[]) {
|
|
213
|
+
for (const item of items) {
|
|
214
|
+
if (item.url) URL.revokeObjectURL(item.url)
|
|
215
|
+
const index = fileQueue.value.indexOf(item)
|
|
216
|
+
if (index !== -1) fileQueue.value.splice(index, 1)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function process(queued: QueueFile[]) {
|
|
221
|
+
if (locked.value) { discard(queued); return }
|
|
222
|
+
failure.value = ''
|
|
223
|
+
emit('addFileStart')
|
|
224
|
+
const target = replaceTarget.value
|
|
225
|
+
replaceTarget.value = null
|
|
226
|
+
const next = props.multiple ? [...values.value] : []
|
|
227
|
+
|
|
228
|
+
for (const item of queued) {
|
|
229
|
+
const replacing = props.multiple && target !== null && !!next[target]
|
|
230
|
+
if (!replacing && props.multiple && props.maxFiles && next.length >= props.maxFiles) {
|
|
231
|
+
failure.value = $t('upload.tooMany', { max: props.maxFiles })
|
|
232
|
+
discard([item])
|
|
233
|
+
continue
|
|
234
|
+
}
|
|
235
|
+
if (props.maxSize && item.file.size > props.maxSize * 1024 * 1024) {
|
|
236
|
+
failure.value = $t('upload.tooLarge', { max: props.maxSize })
|
|
237
|
+
discard([item])
|
|
238
|
+
continue
|
|
239
|
+
}
|
|
240
|
+
if (props.beforeUpload) {
|
|
241
|
+
let allowed = true
|
|
242
|
+
try { allowed = await props.beforeUpload(item.file) }
|
|
243
|
+
catch { allowed = false }
|
|
244
|
+
if (!allowed) { discard([item]); continue }
|
|
245
|
+
}
|
|
246
|
+
const file = await maybeCrop(item.file)
|
|
247
|
+
if (!file) { discard([item]); continue }
|
|
34
248
|
try {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
249
|
+
const reference = await upload(file, {
|
|
250
|
+
visibility: props.assetVisibility,
|
|
251
|
+
onProgress: fraction => { item.progress = Math.round(fraction * 100) },
|
|
252
|
+
})
|
|
253
|
+
// Keep the descriptive bits on the stored reference so a form can render the
|
|
254
|
+
// file name/type after a reload, even when the transport only reports the id.
|
|
255
|
+
const stored: AssetReference = {
|
|
256
|
+
asset_id: reference.asset_id,
|
|
257
|
+
name: reference.name || file.name,
|
|
258
|
+
content_type: reference.content_type || file.type,
|
|
259
|
+
...(reference.size || file.size ? { size: reference.size ?? file.size } : {}),
|
|
38
260
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
fileQueue.value = []
|
|
261
|
+
if (replacing) next[target!] = stored
|
|
262
|
+
else next.push(stored)
|
|
263
|
+
output([...next])
|
|
264
|
+
flash(stored.asset_id)
|
|
44
265
|
}
|
|
45
|
-
|
|
266
|
+
catch (error) {
|
|
267
|
+
const aborted = error instanceof DOMException && error.name === 'AbortError'
|
|
268
|
+
if (!aborted) {
|
|
269
|
+
failure.value = failure.value || $t('upload.failed')
|
|
270
|
+
emit('uploadError', error, file)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
finally {
|
|
274
|
+
discard([item])
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const { browse, addFile, fileQueue } = useFileUpload({
|
|
280
|
+
multiple: props.multiple,
|
|
281
|
+
accept: props.accept,
|
|
282
|
+
capture: props.capture,
|
|
283
|
+
disabled: props.disabled || props.readonly,
|
|
284
|
+
onFileQueued: queued => { void process(queued) },
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
const { isDragging, handlers } = useFileDropzone({
|
|
288
|
+
disabled: () => locked.value || uploading.value,
|
|
289
|
+
onDrop: files => addFile(files),
|
|
46
290
|
})
|
|
47
|
-
|
|
291
|
+
|
|
292
|
+
function pick(target?: number | Event) {
|
|
293
|
+
if (locked.value || uploading.value) return
|
|
294
|
+
replaceTarget.value = typeof target === 'number' ? target : null
|
|
295
|
+
browse()
|
|
296
|
+
}
|
|
297
|
+
|
|
48
298
|
onScopeDispose(() => {
|
|
299
|
+
for (const timer of flashTimers) clearTimeout(timer)
|
|
49
300
|
for (const item of fileQueue.value) if (item.url) URL.revokeObjectURL(item.url)
|
|
301
|
+
if (cropSrc.value) URL.revokeObjectURL(cropSrc.value)
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
/* ─── Rows ────────────────────────────────────────────────────────────────── */
|
|
305
|
+
|
|
306
|
+
/** Lab-only synthetic row: mirrors an in-flight queue entry on an otherwise empty field. */
|
|
307
|
+
const previewQueue = computed<Row[]>(() => {
|
|
308
|
+
const preview = props.previewState
|
|
309
|
+
if (preview?.state !== 'uploading') return []
|
|
310
|
+
return [{
|
|
311
|
+
key: '__preview-upload',
|
|
312
|
+
label: preview.label ?? '',
|
|
313
|
+
type: 'image',
|
|
314
|
+
thumb: preview.thumb,
|
|
315
|
+
progress: preview.progress ?? 62,
|
|
316
|
+
success: false,
|
|
317
|
+
}]
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
/** Lab-only: hold the success check on every stored row instead of flashing it. */
|
|
321
|
+
const previewSuccess = computed(() => props.previewState?.state === 'success')
|
|
322
|
+
|
|
323
|
+
const rows = computed<Row[]>(() => [
|
|
324
|
+
...values.value.map((value, index): Row => ({
|
|
325
|
+
key: value.asset_id,
|
|
326
|
+
label: labelFor(value),
|
|
327
|
+
type: typeFor(value),
|
|
328
|
+
asset: value,
|
|
329
|
+
success: previewSuccess.value || flashed.value.includes(value.asset_id),
|
|
330
|
+
downloadUrl: urlFor(value),
|
|
331
|
+
index,
|
|
332
|
+
})),
|
|
333
|
+
...fileQueue.value.map((item): Row => ({
|
|
334
|
+
key: item.url ?? item.name ?? '',
|
|
335
|
+
label: item.name ?? '',
|
|
336
|
+
type: item.file.type.startsWith('image/')
|
|
337
|
+
? 'image'
|
|
338
|
+
: (item.file.type === 'application/pdf' ? 'pdf' : 'file'),
|
|
339
|
+
thumb: item.url,
|
|
340
|
+
progress: item.progress ?? 0,
|
|
341
|
+
success: false,
|
|
342
|
+
})),
|
|
343
|
+
...previewQueue.value,
|
|
344
|
+
])
|
|
345
|
+
|
|
346
|
+
/** A file in flight owns the single slot: you see what is uploading, then it lands in place. */
|
|
347
|
+
const single = computed(
|
|
348
|
+
() => rows.value.find(row => row.progress !== undefined) ?? rows.value[0],
|
|
349
|
+
)
|
|
350
|
+
/** Action buttons: a hair smaller than `Btn`'s default glyph (1.2 desktop / 1 mobile). */
|
|
351
|
+
const actionIconSize = 1.08
|
|
352
|
+
const actionIconMobileSize = 0.9
|
|
353
|
+
/** The "add another file" glyph reads heavy at full size — keep it lighter. */
|
|
354
|
+
const addIconSize = 0.9
|
|
355
|
+
const addIconMobileSize = 0.8
|
|
356
|
+
const singleSrc = computed(() => single.value?.asset
|
|
357
|
+
? urlFor(single.value.asset)
|
|
358
|
+
: single.value?.thumb)
|
|
359
|
+
const hasValue = computed(() => rows.value.length > 0)
|
|
360
|
+
const inlinePreview = computed(() => props.previewLayout !== 'below' && hasValue.value)
|
|
361
|
+
const showPlaceholder = computed(() => !inlinePreview.value)
|
|
362
|
+
const showRowsBelow = computed(() => props.previewLayout === 'below' && hasValue.value)
|
|
363
|
+
|
|
364
|
+
function srcFor(row: Row) {
|
|
365
|
+
return row.asset ? urlFor(row.asset) : row.thumb
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function removeRow(row: Row) {
|
|
369
|
+
if (row.asset) {
|
|
370
|
+
const index = row.index
|
|
371
|
+
?? values.value.findIndex(value => value.asset_id === row.asset?.asset_id)
|
|
372
|
+
if (index !== -1) output(values.value.filter((_, i) => i !== index))
|
|
373
|
+
return
|
|
374
|
+
}
|
|
375
|
+
const item = fileQueue.value.find(entry => (entry.url ?? entry.name) === row.key)
|
|
376
|
+
if (item) discard([item])
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function lightboxFor(row?: Row) {
|
|
380
|
+
if (!row) return {}
|
|
381
|
+
return row.asset
|
|
382
|
+
? { asset: row.asset, type: row.type, name: row.label, enableZoom: row.type === 'image' }
|
|
383
|
+
: { src: row.thumb, download: true }
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/* ─── Layout ──────────────────────────────────────────────────────────────── */
|
|
387
|
+
|
|
388
|
+
const placeholderText = computed(() => {
|
|
389
|
+
const raw = resolveI18n(props.dropPlaceholder ?? props.placeholder)
|
|
390
|
+
return raw || $t('upload.dropPlaceholder')
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
const frameCapped = computed(
|
|
394
|
+
() => !!props.maxHeight && props.previewLayout !== 'below' && !props.oval,
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
/** Oval and auto-height frames are image-shaped, so they cover instead of letterboxing. */
|
|
398
|
+
const fillImage = computed(() => !!props.fill || !!props.oval || props.height === 'auto')
|
|
399
|
+
|
|
400
|
+
/** The inner button is opt-in; `auto` (default) hides it for `oval`, where the circle is the affordance. */
|
|
401
|
+
const showInnerButton = computed(() => props.showButton === true
|
|
402
|
+
|| (props.showButton !== false && !props.oval))
|
|
403
|
+
|
|
404
|
+
/** A file list owns the frame, so hover feedback belongs on the add strip, not the rows. */
|
|
405
|
+
const listSplit = computed(
|
|
406
|
+
() => inlinePreview.value && !!props.multiple && props.previewType !== 'grid',
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
const frameStyle = computed<CSSProperties>(() => {
|
|
410
|
+
const height = props.height === 'auto' ? 'auto' : (props.height ?? '215px')
|
|
411
|
+
const style: CSSProperties = {
|
|
412
|
+
height,
|
|
413
|
+
width: props.width ?? (props.oval ? height : '100%'),
|
|
414
|
+
}
|
|
415
|
+
// A capped frame scrolls instead of growing, so the 215px floor (set both here
|
|
416
|
+
// and by `.fileUploadWrap[style*='height: auto;']`) would fight the cap.
|
|
417
|
+
if (props.height === 'auto' && !frameCapped.value) style.minHeight = '215px'
|
|
418
|
+
if (frameCapped.value) {
|
|
419
|
+
style.maxHeight = props.maxHeight!
|
|
420
|
+
style.minHeight = '0'
|
|
421
|
+
style.overflowY = props.scroll ? 'auto' : 'hidden'
|
|
422
|
+
}
|
|
423
|
+
return style
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
const listStyle = computed<CSSProperties | undefined>(() => {
|
|
427
|
+
if (!props.maxHeight || props.previewLayout !== 'below' || props.oval) return undefined
|
|
428
|
+
return { maxHeight: props.maxHeight, overflowY: props.scroll ? 'auto' : 'hidden' }
|
|
50
429
|
})
|
|
51
430
|
</script>
|
|
52
431
|
|
|
53
432
|
<template>
|
|
54
|
-
<
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
433
|
+
<div
|
|
434
|
+
class="bagel-input bgl_asset_upload"
|
|
435
|
+
:class="[shellClass, { 'has-error': !!(failure || error), 'has-value': hasValue }]"
|
|
436
|
+
:style="shellStyle" :data-field="name || id"
|
|
437
|
+
>
|
|
438
|
+
<label v-if="label">{{ label }}<span v-if="required"> *</span></label>
|
|
439
|
+
<input
|
|
440
|
+
v-if="required && !hasValue" :name="name || id" placeholder="required"
|
|
441
|
+
type="text" required class="pixel"
|
|
442
|
+
>
|
|
443
|
+
|
|
444
|
+
<!-- Compact: the same row list as the dropzone preview, inside a plain card -->
|
|
445
|
+
<Card
|
|
446
|
+
v-if="theme === 'basic'" outline v-on="handlers"
|
|
447
|
+
:class="[
|
|
448
|
+
'p-05 bgl-compact',
|
|
449
|
+
rows.length ? 'grid gap-05 align-content-start' : 'flex flex-wrap gap-1',
|
|
450
|
+
]"
|
|
451
|
+
>
|
|
452
|
+
<div v-if="!locked && !hasValue" class="bgl-add-more">
|
|
453
|
+
<Btn
|
|
454
|
+
thin icon="add" :value="btnPlaceholder || $t('upload.upload')"
|
|
455
|
+
:icon-size="addIconSize" :icon-mobile-size="addIconMobileSize"
|
|
456
|
+
@click.stop="pick()"
|
|
457
|
+
/>
|
|
458
|
+
</div>
|
|
459
|
+
<AssetFileRow
|
|
460
|
+
v-for="row in rows" :key="row.key" :label="row.label" :type="row.type"
|
|
461
|
+
:asset="row.asset" :thumb="row.thumb" :progress="row.progress" :success="row.success"
|
|
462
|
+
:download-url="row.downloadUrl" :readonly="readonly" :disabled="disabled"
|
|
463
|
+
:crop="cropAfterUpload" :icon-weight="iconWeight"
|
|
464
|
+
@remove="removeRow(row)" @replace="pick(row.index)" @crop="cropUploaded(row)"
|
|
465
|
+
/>
|
|
466
|
+
<span v-if="!rows.length" class="txt-gray txt-12">
|
|
467
|
+
{{ noFilePlaceholder || $t('upload.noFile') }}
|
|
468
|
+
</span>
|
|
469
|
+
<div v-if="!locked && hasValue && multiple" class="bgl-add-more">
|
|
470
|
+
<Btn
|
|
471
|
+
thin icon="add" :value="btnPlaceholder || $t('upload.upload')"
|
|
472
|
+
:icon-size="addIconSize" :icon-mobile-size="addIconMobileSize"
|
|
473
|
+
@click.stop="pick()"
|
|
474
|
+
/>
|
|
475
|
+
</div>
|
|
476
|
+
</Card>
|
|
477
|
+
|
|
478
|
+
<template v-else>
|
|
479
|
+
<!-- Read-only with nothing uploaded: no drop frame, just a quiet line of text -->
|
|
480
|
+
<div
|
|
481
|
+
v-if="readonly && !hasValue && !slots['drop-placeholder'] && !slots.placeholder"
|
|
482
|
+
class="flex gap-05 color-gray txt-12 py-025"
|
|
483
|
+
>
|
|
484
|
+
<Icon icon="block" :size="1.5" :weight="iconWeight" />
|
|
485
|
+
<span>{{ noFilePlaceholder || $t('upload.noFile') }}</span>
|
|
486
|
+
</div>
|
|
487
|
+
<div
|
|
488
|
+
v-else
|
|
489
|
+
class="fileUploadWrap"
|
|
490
|
+
:class="{
|
|
491
|
+
fileDropZone: showPlaceholder,
|
|
492
|
+
dragover: isDragging,
|
|
493
|
+
'bgl_oval-upload': oval,
|
|
494
|
+
'bgl_fill-image': fillImage && !multiple,
|
|
495
|
+
'bgl-list-split': listSplit,
|
|
496
|
+
'bgl_no-frame': !uploadFrame,
|
|
497
|
+
'border': uploadFrame,
|
|
498
|
+
// Oval keeps `overflow` visible so the action row can sit under the circle.
|
|
499
|
+
'overflow-hidden': uploadFrame && !frameCapped && !oval,
|
|
500
|
+
}"
|
|
501
|
+
:style="frameStyle" v-on="handlers" role="button"
|
|
502
|
+
:tabindex="locked ? undefined : 0" :aria-disabled="locked || undefined"
|
|
503
|
+
@click="pick()" @keydown.enter.prevent="pick()" @keydown.space.prevent="pick()"
|
|
504
|
+
>
|
|
505
|
+
<slot name="files" :files="values" :file-queue="fileQueue">
|
|
506
|
+
<!-- Single: the file fills the frame -->
|
|
507
|
+
<div v-if="inlinePreview && !multiple && single" class="bgl-single-preview">
|
|
508
|
+
<div
|
|
509
|
+
class="single-image-item-preview"
|
|
510
|
+
:class="{ 'bgl_fill-image': fillImage }"
|
|
511
|
+
>
|
|
512
|
+
<div
|
|
513
|
+
v-if="single.asset && !readonly"
|
|
514
|
+
class="flex opacity-8 z-90 gap-025 absolute"
|
|
515
|
+
:class="oval
|
|
516
|
+
? 'bgl-oval-actions start-0 end-0 justify-content-center'
|
|
517
|
+
: 'top-0 start-0 m-05'"
|
|
518
|
+
>
|
|
519
|
+
<Btn
|
|
520
|
+
v-if="!disabled" color="white" frame thin icon="delete"
|
|
521
|
+
:icon-size="actionIconSize" :icon-mobile-size="actionIconMobileSize"
|
|
522
|
+
:aria-label="$t('upload.delete')" @click.stop="removeRow(single)"
|
|
523
|
+
/>
|
|
524
|
+
<Btn
|
|
525
|
+
v-if="!disabled" color="white" frame thin icon="autorenew"
|
|
526
|
+
:icon-size="actionIconSize" :icon-mobile-size="actionIconMobileSize"
|
|
527
|
+
:aria-label="$t('upload.replace')" @click.stop="pick(single.index)"
|
|
528
|
+
/>
|
|
529
|
+
<Btn
|
|
530
|
+
v-if="!disabled && cropAfterUpload && single.type === 'image'"
|
|
531
|
+
color="white" frame thin icon="crop"
|
|
532
|
+
:icon-size="actionIconSize" :icon-mobile-size="actionIconMobileSize"
|
|
533
|
+
:aria-label="$t('upload.cropExisting')" @click.stop="cropUploaded(single)"
|
|
534
|
+
/>
|
|
535
|
+
<Btn
|
|
536
|
+
v-if="single.downloadUrl" color="white" frame thin icon="download"
|
|
537
|
+
:icon-size="actionIconSize" :icon-mobile-size="actionIconMobileSize"
|
|
538
|
+
target="_blank" :aria-label="$t('upload.download')"
|
|
539
|
+
:href="single.downloadUrl" :download="single.label" @click.stop
|
|
540
|
+
/>
|
|
541
|
+
</div>
|
|
542
|
+
|
|
543
|
+
<div v-if="single.type === 'image'" class="h-100">
|
|
544
|
+
<img
|
|
545
|
+
v-if="singleSrc" v-lightbox="lightboxFor(single)" :src="singleSrc"
|
|
546
|
+
:alt="single.label" class="single-preview"
|
|
547
|
+
>
|
|
548
|
+
</div>
|
|
549
|
+
<div v-else class="flex-center column gap-05 h-100p txt-center px-1">
|
|
550
|
+
<Icon
|
|
551
|
+
v-lightbox="lightboxFor(single)" :icon="iconFor(single.type)"
|
|
552
|
+
:size="4" :weight="iconWeight" class="color-primary line-height-1"
|
|
553
|
+
/>
|
|
554
|
+
<span class="txt12 ellipsis-1 w-100p">{{ single.label }}</span>
|
|
555
|
+
</div>
|
|
556
|
+
|
|
557
|
+
<!-- In-flight: the same slim bar along the bottom edge as before. -->
|
|
558
|
+
<Progress
|
|
559
|
+
v-if="single.progress !== undefined && !oval" :value="single.progress"
|
|
560
|
+
:size="3" class="absolute bottom-1 start-1 end-1"
|
|
561
|
+
/>
|
|
562
|
+
<!-- Oval has no room for a bar, so a ring sits in the middle of the circle. -->
|
|
563
|
+
<template v-else-if="single.progress !== undefined">
|
|
564
|
+
<div class="absolute inset round bg-black keep-black opacity-4 pointer-events-none" />
|
|
565
|
+
<Progress
|
|
566
|
+
ring color="white" :value="single.progress" :size="56" :thickness="4"
|
|
567
|
+
class="bgl-upload-ring absolute center-xy pointer-events-none"
|
|
568
|
+
>
|
|
569
|
+
<span class="txt12 semi color-white keep-white">
|
|
570
|
+
{{ Math.round(single.progress) }}%
|
|
571
|
+
</span>
|
|
572
|
+
</Progress>
|
|
573
|
+
</template>
|
|
574
|
+
<Icon
|
|
575
|
+
v-if="single.success" icon="check_circle" :size="3" :weight="iconWeight"
|
|
576
|
+
class="absolute center-xy color-green pointer-events-none z-90"
|
|
577
|
+
/>
|
|
578
|
+
</div>
|
|
579
|
+
</div>
|
|
580
|
+
|
|
581
|
+
<!-- Multiple: tile grid -->
|
|
582
|
+
<div
|
|
583
|
+
v-else-if="inlinePreview && multiple && previewType === 'grid'"
|
|
584
|
+
class="grid grid-wrap-3 m_grid-wrap-2 gap-05 m_gap-025 p-05 m_p-025"
|
|
585
|
+
>
|
|
586
|
+
<div
|
|
587
|
+
v-for="row in rows" :key="row.key" v-lightbox="lightboxFor(row)"
|
|
588
|
+
class="bgl-tile relative aspect-ratio-1 radius-1 overflow-hidden bg-gray-10 pointer"
|
|
589
|
+
>
|
|
590
|
+
<img
|
|
591
|
+
v-if="row.type === 'image' && srcFor(row)" :src="srcFor(row)"
|
|
592
|
+
:alt="row.label" class="absolute-fill w-100p h-100p object-fit-cover"
|
|
593
|
+
>
|
|
594
|
+
<div v-else class="flex-center h-100p">
|
|
595
|
+
<Icon :icon="iconFor(row.type)" :weight="iconWeight" />
|
|
596
|
+
</div>
|
|
597
|
+
|
|
598
|
+
<!-- Same affordance as the single-image overlay, one tile at a time -->
|
|
599
|
+
<div
|
|
600
|
+
v-if="row.asset && !readonly && !disabled"
|
|
601
|
+
class="flex flex-wrap gap-025 justify-content-center absolute bottom-025 start-025 end-025 opacity-8 z-90"
|
|
602
|
+
>
|
|
603
|
+
<Btn
|
|
604
|
+
color="white" frame thin icon="delete" :aria-label="$t('upload.delete')"
|
|
605
|
+
:icon-size="actionIconSize" :icon-mobile-size="actionIconMobileSize"
|
|
606
|
+
@click.stop="removeRow(row)"
|
|
607
|
+
/>
|
|
608
|
+
<Btn
|
|
609
|
+
color="white" frame thin icon="autorenew" :aria-label="$t('upload.replace')"
|
|
610
|
+
:icon-size="actionIconSize" :icon-mobile-size="actionIconMobileSize"
|
|
611
|
+
@click.stop="pick(row.index)"
|
|
612
|
+
/>
|
|
613
|
+
<Btn
|
|
614
|
+
v-if="cropAfterUpload && row.type === 'image'" color="white" frame thin
|
|
615
|
+
icon="crop" :aria-label="$t('upload.cropExisting')"
|
|
616
|
+
:icon-size="actionIconSize" :icon-mobile-size="actionIconMobileSize"
|
|
617
|
+
@click.stop="cropUploaded(row)"
|
|
618
|
+
/>
|
|
619
|
+
<Btn
|
|
620
|
+
v-if="row.downloadUrl" color="white" frame thin icon="download"
|
|
621
|
+
:icon-size="actionIconSize" :icon-mobile-size="actionIconMobileSize"
|
|
622
|
+
target="_blank" :aria-label="$t('upload.download')"
|
|
623
|
+
:href="row.downloadUrl" :download="row.label" @click.stop
|
|
624
|
+
/>
|
|
625
|
+
</div>
|
|
626
|
+
|
|
627
|
+
<!-- In-flight: a slim bar along the tile's bottom edge. -->
|
|
628
|
+
<Progress
|
|
629
|
+
v-if="row.progress !== undefined" :value="row.progress"
|
|
630
|
+
:size="3" class="absolute bottom-025 start-025 end-025"
|
|
631
|
+
/>
|
|
632
|
+
<Icon
|
|
633
|
+
v-if="row.success" icon="check_circle" :size="2" :weight="iconWeight"
|
|
634
|
+
class="absolute center-xy color-green pointer-events-none z-90"
|
|
635
|
+
/>
|
|
636
|
+
</div>
|
|
637
|
+
</div>
|
|
638
|
+
|
|
639
|
+
<!-- Multiple: one row per file, plus a calm strip that owns the hover -->
|
|
640
|
+
<div v-else-if="inlinePreview && multiple" class="grid gap-05 p-05 align-content-start">
|
|
641
|
+
<AssetFileRow
|
|
642
|
+
v-for="row in rows" :key="row.key" :label="row.label" :type="row.type"
|
|
643
|
+
:asset="row.asset" :thumb="row.thumb" :progress="row.progress" :success="row.success"
|
|
644
|
+
:readonly="readonly" :disabled="disabled" :download-url="row.downloadUrl"
|
|
645
|
+
:crop="cropAfterUpload" :icon-weight="iconWeight"
|
|
646
|
+
@remove="removeRow(row)" @replace="pick(row.index)" @crop="cropUploaded(row)"
|
|
647
|
+
/>
|
|
648
|
+
<div v-if="!locked" class="bgl-add-more">
|
|
649
|
+
<Btn
|
|
650
|
+
thin icon="add" :value="btnPlaceholder || $t('upload.upload')"
|
|
651
|
+
:icon-size="addIconSize" :icon-mobile-size="addIconMobileSize"
|
|
652
|
+
@click.stop="pick()"
|
|
653
|
+
/>
|
|
654
|
+
</div>
|
|
655
|
+
</div>
|
|
656
|
+
</slot>
|
|
657
|
+
|
|
658
|
+
<slot
|
|
659
|
+
v-if="showPlaceholder && slots['drop-placeholder']" name="drop-placeholder"
|
|
660
|
+
:files="values" :file-queue="fileQueue" :browse="pick"
|
|
661
|
+
/>
|
|
662
|
+
<div
|
|
663
|
+
v-else-if="showPlaceholder"
|
|
664
|
+
class="fileUploadPlaceHolder flex column justify-content-center gap-075 p-1 pointer user-select-none"
|
|
665
|
+
:class="oval ? 'bgl_oval-hint' : undefined"
|
|
666
|
+
>
|
|
667
|
+
<slot name="placeholder" :files="values" :file-queue="fileQueue" :browse="pick">
|
|
668
|
+
<Icon
|
|
669
|
+
v-if="showIcon !== false" :icon="icon" :size="oval ? 1.5 : iconSize"
|
|
670
|
+
:mobile-size="iconMobileSize" :weight="iconWeight"
|
|
671
|
+
class="line-height-1 user-select-none"
|
|
672
|
+
/>
|
|
673
|
+
<span class="pretty balance user-select-none" :class="oval ? 'txt12' : undefined">
|
|
674
|
+
{{ placeholderText }}
|
|
675
|
+
</span>
|
|
676
|
+
</slot>
|
|
677
|
+
<Btn
|
|
678
|
+
v-if="showInnerButton && !locked && !slots.placeholder" icon="upload"
|
|
679
|
+
:value="btnPlaceholder || $t('upload.upload')" @click.stop="pick()"
|
|
680
|
+
color="gray-40"
|
|
681
|
+
class="color-black"
|
|
682
|
+
/>
|
|
683
|
+
</div>
|
|
684
|
+
</div>
|
|
685
|
+
|
|
686
|
+
<!-- Files kept outside the drop frame -->
|
|
687
|
+
<div v-if="showRowsBelow" class="grid gap-05 pt-05" :style="listStyle">
|
|
688
|
+
<AssetFileRow
|
|
689
|
+
v-for="row in rows" :key="row.key" :label="row.label" :type="row.type"
|
|
690
|
+
:asset="row.asset" :thumb="row.thumb" :progress="row.progress" :success="row.success"
|
|
691
|
+
:readonly="readonly" :disabled="disabled" :download-url="row.downloadUrl"
|
|
692
|
+
:crop="cropAfterUpload" :icon-weight="iconWeight"
|
|
693
|
+
@remove="removeRow(row)" @replace="pick(row.index)" @crop="cropUploaded(row)"
|
|
694
|
+
/>
|
|
695
|
+
</div>
|
|
696
|
+
</template>
|
|
697
|
+
|
|
698
|
+
<div v-if="failure || error" class="error-message">{{ failure || error }}</div>
|
|
699
|
+
<div v-if="uploading" class="flex gap-05 pt-025">
|
|
700
|
+
<Btn flat thin color="gray" icon="close" :value="$t('upload.cancel')" @click="cancel" />
|
|
61
701
|
</div>
|
|
62
|
-
|
|
63
|
-
<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
702
|
+
|
|
703
|
+
<Dialog
|
|
704
|
+
v-if="cropperAvailable"
|
|
705
|
+
:open="cropOpen" :title="$t('upload.cropTitle')" width="m"
|
|
706
|
+
@update:open="value => { if (!value) finishCrop() }"
|
|
707
|
+
>
|
|
708
|
+
<ImageCropper
|
|
709
|
+
v-if="cropSrc" ref="cropper" :src="cropSrc" :aspect="cropAspect"
|
|
710
|
+
:output-size="cropOutputSize"
|
|
711
|
+
/>
|
|
712
|
+
<template #footer>
|
|
713
|
+
<Btn flat :value="$t('upload.cancel')" @click="finishCrop()" />
|
|
714
|
+
<Btn
|
|
715
|
+
color="primary" icon="crop"
|
|
716
|
+
:value="cropMode === 'existing' ? $t('upload.cropApplyExisting') : $t('upload.cropApply')"
|
|
717
|
+
@click="confirmCrop"
|
|
718
|
+
/>
|
|
719
|
+
</template>
|
|
720
|
+
</Dialog>
|
|
721
|
+
</div>
|
|
67
722
|
</template>
|
|
723
|
+
|
|
724
|
+
<style src="./upload.css" scoped></style>
|