@zooui/nuxt 0.2.0 → 0.3.0
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/README.md +13 -1
- package/components/ConfirmButton.vue +124 -0
- package/components/ConfirmDialog.vue +201 -0
- package/components/DateField.vue +16 -8
- package/components/FileDropOverlay.vue +275 -0
- package/components/FileList.vue +155 -0
- package/components/FilterSelect.vue +340 -0
- package/components/OptionalDate.vue +180 -0
- package/components/OrderList.vue +495 -0
- package/components/RepeatingList.vue +133 -21
- package/components/SelectMenu.vue +325 -0
- package/components/confirm.ts +163 -0
- package/components/row-snapshot.ts +48 -0
- package/components/select-typeahead.ts +79 -0
- package/composables/useConfirm.ts +68 -0
- package/nuxt.config.ts +5 -1
- package/package.json +2 -1
- package/utils/confirm-queue.ts +85 -0
- package/utils/date-entry.ts +9 -2
- package/utils/file-drop.ts +151 -0
- package/utils/filter-match.ts +62 -0
- package/utils/order-list.ts +42 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Drag events an overlay nearer the event's target has already counted. Every instance shares it: listeners on an
|
|
4
|
+
* inner target run before those on an outer one (or the window), so the innermost overlay under the pointer claims each
|
|
5
|
+
* event and the outer ones leave it alone. That splits the drag's count by region: the dialog's overlay counts what
|
|
6
|
+
* happens over the dialog, the page's what happens over the rest, and each shows only while the pointer is over its own.
|
|
7
|
+
*/
|
|
8
|
+
const claimed = new WeakSet<Event>()
|
|
9
|
+
function claim(e: Event) {
|
|
10
|
+
if (claimed.has(e)) return false
|
|
11
|
+
claimed.add(e)
|
|
12
|
+
return true
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The targets of every contained overlay attached now, for the modal check below. */
|
|
16
|
+
const containedTargets = new Set<HTMLElement>()
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The open modal dialog `el` is in, if any: a `<dialog>` opened with showModal(), an `aria-modal` dialog, or one that
|
|
20
|
+
* hides the rest of the page from assistive tech, which is how reka-ui marks its modals (Nuxt UI's UModal, USlideover):
|
|
21
|
+
* it sets `data-aria-hidden` on everything outside them rather than `aria-modal` on them.
|
|
22
|
+
*/
|
|
23
|
+
function modalOf(el: HTMLElement): HTMLElement | null {
|
|
24
|
+
const dialog = el.closest<HTMLElement>('dialog, [role="dialog"], [role="alertdialog"]')
|
|
25
|
+
if (!dialog?.isConnected) return null
|
|
26
|
+
if (dialog instanceof HTMLDialogElement) {
|
|
27
|
+
try { return dialog.matches(':modal') ? dialog : null } catch { return null }
|
|
28
|
+
}
|
|
29
|
+
if (dialog.getAttribute('aria-modal') === 'true') return dialog
|
|
30
|
+
for (let n: HTMLElement | null = dialog; n?.parentElement && n !== document.body; n = n.parentElement) {
|
|
31
|
+
for (const sibling of n.parentElement.children) {
|
|
32
|
+
if (sibling !== n && sibling.getAttribute('data-aria-hidden') === 'true') return dialog
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Whether an overlay contained in an open modal that `container` is outside holds the page: this one stands down. */
|
|
39
|
+
function outrankedByModal(container: HTMLElement) {
|
|
40
|
+
for (const target of containedTargets) {
|
|
41
|
+
if (target === container) continue
|
|
42
|
+
const modal = modalOf(target)
|
|
43
|
+
if (modal && !modal.contains(container)) return true
|
|
44
|
+
}
|
|
45
|
+
return false
|
|
46
|
+
}
|
|
47
|
+
</script>
|
|
48
|
+
|
|
49
|
+
<script setup lang="ts">
|
|
50
|
+
// Files dropped anywhere in the window — or in one element, a dialog say — rather than on a box: while files are
|
|
51
|
+
// dragged over it an overlay says what dropping does, and the drop hands the files back, sorted by `accept` and
|
|
52
|
+
// `multiple` into @drop and @rejected. Nuxt UI's UFileUpload is the box; this is the page. Written on Nuxt UI's tokens.
|
|
53
|
+
//
|
|
54
|
+
// <ZFileDropOverlay accept="image/*,.pdf" multiple label="Drop photos and PDFs to attach them" @drop="upload" @rejected="warn" />
|
|
55
|
+
// <div ref="body"> … <ZFileDropOverlay :target="body" @drop="add" /></div> <!-- contained in the element -->
|
|
56
|
+
//
|
|
57
|
+
// The drag is counted, not tracked by target: dragenter and dragleave fire for every element the pointer crosses,
|
|
58
|
+
// enter on the new one before leave on the old, so the overlay is shown while the count is above zero and the page's
|
|
59
|
+
// own children cannot make it flicker. Only drags that carry files count; text, links and elements dragged within the
|
|
60
|
+
// page are left alone. The overlay is only a picture (`pointer-events: none`): the drag's events stay on the page's
|
|
61
|
+
// own elements, so showing or hiding it never moves the pointer off one, a drop zone under it still gets its drop, and
|
|
62
|
+
// an overlay nested inside it can be reached.
|
|
63
|
+
import { dragCarriesFiles, droppedFiles, sortDroppedFiles, type FileFilter, type RejectedFile } from '../utils/file-drop'
|
|
64
|
+
|
|
65
|
+
defineOptions({ inheritAttrs: false })
|
|
66
|
+
|
|
67
|
+
const props = withDefaults(
|
|
68
|
+
defineProps<{
|
|
69
|
+
/** Files to take, as the native input's `accept`: MIME types, `image/*`, `.pdf`, comma-separated or a list. */
|
|
70
|
+
accept?: string | string[]
|
|
71
|
+
/** Take every matching file; without it the first is taken and the rest go to @rejected. */
|
|
72
|
+
multiple?: boolean
|
|
73
|
+
/** A rule of the product's own, run on each file `accept` takes (a size limit, a name rule): `true` takes it;
|
|
74
|
+
* `false`, or a message saying why, sends it to @rejected as `filter`, with the message. */
|
|
75
|
+
filter?: FileFilter
|
|
76
|
+
/** No overlay and no @drop; a file dropped is still kept from opening in the tab. */
|
|
77
|
+
disabled?: boolean
|
|
78
|
+
/** What the overlay says, and what a screen reader hears when it appears. */
|
|
79
|
+
label?: string
|
|
80
|
+
/** A second line under the label. */
|
|
81
|
+
description?: string
|
|
82
|
+
/** The overlay's icon, such as `i-lucide-upload`; none by default. */
|
|
83
|
+
icon?: string | false
|
|
84
|
+
/** Take drops in this element only, with the overlay drawn inside it, rather than the window: an element (a template
|
|
85
|
+
* ref to one), a component, or a selector. `null` takes no drops, for a ref that isn't set yet. A selector is
|
|
86
|
+
* looked up when the overlay mounts: for an element rendered later (a dialog's content), pass a template ref. */
|
|
87
|
+
target?: HTMLElement | { $el: unknown } | string | null
|
|
88
|
+
}>(),
|
|
89
|
+
{
|
|
90
|
+
accept: undefined,
|
|
91
|
+
multiple: false,
|
|
92
|
+
filter: undefined,
|
|
93
|
+
disabled: false,
|
|
94
|
+
label: 'Drop your files',
|
|
95
|
+
description: undefined,
|
|
96
|
+
icon: false,
|
|
97
|
+
target: undefined,
|
|
98
|
+
},
|
|
99
|
+
)
|
|
100
|
+
const emit = defineEmits<{
|
|
101
|
+
/** The files taken, in drop order; not emitted when none were. */
|
|
102
|
+
drop: [files: File[]]
|
|
103
|
+
/** The files refused, each with its reason (and `filter`'s message); not emitted when none were. */
|
|
104
|
+
rejected: [files: RejectedFile[]]
|
|
105
|
+
}>()
|
|
106
|
+
defineSlots<{
|
|
107
|
+
/** The overlay's message, in place of the icon, label and description. */
|
|
108
|
+
default?(props: { label: string; accept?: string | string[]; multiple: boolean }): unknown
|
|
109
|
+
}>()
|
|
110
|
+
|
|
111
|
+
/** Files are being dragged over the window (or the target), outside any overlay nested in it. */
|
|
112
|
+
const dragging = ref(false)
|
|
113
|
+
/** Escape was pressed during this drag: the overlay stays hidden until the drag ends. */
|
|
114
|
+
const dismissed = ref(false)
|
|
115
|
+
/** An overlay contained in an open modal this one is outside holds the page: this one shows and takes nothing, as if
|
|
116
|
+
* disabled. Read again on every drag event, so it lifts by itself when the modal closes. */
|
|
117
|
+
const standingDown = ref(false)
|
|
118
|
+
const visible = computed(() => dragging.value && !dismissed.value && !props.disabled && !standingDown.value)
|
|
119
|
+
|
|
120
|
+
let depth = 0
|
|
121
|
+
function reset() {
|
|
122
|
+
depth = 0
|
|
123
|
+
dragging.value = false
|
|
124
|
+
dismissed.value = false
|
|
125
|
+
standingDown.value = false
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The element the overlay is contained in, when `target` names one; null for the window. */
|
|
129
|
+
function resolveTarget(): HTMLElement | null {
|
|
130
|
+
const t = props.target
|
|
131
|
+
if (!t) return null
|
|
132
|
+
if (typeof t === 'string') return document.querySelector<HTMLElement>(t)
|
|
133
|
+
if (t instanceof HTMLElement) return t
|
|
134
|
+
return t.$el instanceof HTMLElement ? t.$el : null
|
|
135
|
+
}
|
|
136
|
+
const scoped = shallowRef<HTMLElement | null>(null)
|
|
137
|
+
|
|
138
|
+
// Every handler claims the file drag's event first: one an overlay nested deeper already claimed is its own, so this
|
|
139
|
+
// one neither counts nor answers it. Claimed whether or not this one is disabled, so the count stays whole when
|
|
140
|
+
// `disabled` changes mid-drag.
|
|
141
|
+
function onEnter(e: DragEvent) {
|
|
142
|
+
if (!dragCarriesFiles(e.dataTransfer) || !claim(e)) return
|
|
143
|
+
depth += 1
|
|
144
|
+
standingDown.value = !!bound && outrankedByModal(bound)
|
|
145
|
+
dragging.value = true
|
|
146
|
+
}
|
|
147
|
+
function onLeave(e: DragEvent) {
|
|
148
|
+
if (!dragCarriesFiles(e.dataTransfer) || !claim(e)) return
|
|
149
|
+
if (!depth) return
|
|
150
|
+
depth -= 1
|
|
151
|
+
if (!depth) reset()
|
|
152
|
+
}
|
|
153
|
+
function onOver(e: DragEvent) {
|
|
154
|
+
if (!dragCarriesFiles(e.dataTransfer) || !claim(e)) return
|
|
155
|
+
// A drop zone under the pointer that took the event (a UFileUpload) keeps its own dropEffect: 'none' here would
|
|
156
|
+
// cancel its drop, and 'copy' would let through one it refused.
|
|
157
|
+
if (e.defaultPrevented) return
|
|
158
|
+
standingDown.value = !!bound && outrankedByModal(bound)
|
|
159
|
+
// Without this the drop never fires and the browser opens the file in the tab.
|
|
160
|
+
e.preventDefault()
|
|
161
|
+
if (e.dataTransfer) e.dataTransfer.dropEffect = visible.value ? 'copy' : 'none'
|
|
162
|
+
}
|
|
163
|
+
function onDrop(e: DragEvent) {
|
|
164
|
+
if (!dragCarriesFiles(e.dataTransfer)) return
|
|
165
|
+
// The drop ends the drag for every overlay; only the one it was claimed by can take it.
|
|
166
|
+
if (!claim(e)) return reset()
|
|
167
|
+
// A drop zone under the pointer took these files itself: handing them out again would add them twice.
|
|
168
|
+
const takenByZone = e.defaultPrevented
|
|
169
|
+
e.preventDefault()
|
|
170
|
+
const take = visible.value && !takenByZone && !(bound && outrankedByModal(bound))
|
|
171
|
+
reset()
|
|
172
|
+
if (!take || !e.dataTransfer) return
|
|
173
|
+
const { accepted, rejected } = sortDroppedFiles(droppedFiles(e.dataTransfer), { accept: props.accept, multiple: props.multiple, filter: props.filter })
|
|
174
|
+
if (accepted.length) emit('drop', accepted)
|
|
175
|
+
if (rejected.length) emit('rejected', rejected)
|
|
176
|
+
}
|
|
177
|
+
function onKeydown(e: KeyboardEvent) {
|
|
178
|
+
if (e.key === 'Escape' && visible.value) dismissed.value = true
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let bound: HTMLElement | null = null
|
|
182
|
+
/** The target's own `position` before it was made a positioning context for the overlay, to put back. */
|
|
183
|
+
let ownPosition: string | null = null
|
|
184
|
+
function bind(el: HTMLElement) {
|
|
185
|
+
el.addEventListener('dragenter', onEnter)
|
|
186
|
+
el.addEventListener('dragleave', onLeave)
|
|
187
|
+
el.addEventListener('dragover', onOver)
|
|
188
|
+
el.addEventListener('drop', onDrop)
|
|
189
|
+
if (el !== document.documentElement) containedTargets.add(el)
|
|
190
|
+
if (el !== document.documentElement && getComputedStyle(el).position === 'static') {
|
|
191
|
+
ownPosition = el.style.position
|
|
192
|
+
el.style.position = 'relative'
|
|
193
|
+
}
|
|
194
|
+
bound = el
|
|
195
|
+
}
|
|
196
|
+
function unbind() {
|
|
197
|
+
if (!bound) return
|
|
198
|
+
bound.removeEventListener('dragenter', onEnter)
|
|
199
|
+
bound.removeEventListener('dragleave', onLeave)
|
|
200
|
+
bound.removeEventListener('dragover', onOver)
|
|
201
|
+
bound.removeEventListener('drop', onDrop)
|
|
202
|
+
containedTargets.delete(bound)
|
|
203
|
+
if (ownPosition !== null) bound.style.position = ownPosition
|
|
204
|
+
ownPosition = null
|
|
205
|
+
bound = null
|
|
206
|
+
}
|
|
207
|
+
function rebind() {
|
|
208
|
+
unbind()
|
|
209
|
+
reset()
|
|
210
|
+
scoped.value = resolveTarget()
|
|
211
|
+
// A target that names nothing (yet) takes no drops, rather than the whole window.
|
|
212
|
+
if (props.target !== undefined && !scoped.value) return
|
|
213
|
+
bind(scoped.value ?? document.documentElement)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Client only: nothing is attached during SSR, and everything is taken off on unmount.
|
|
217
|
+
let mounted = false
|
|
218
|
+
onMounted(() => {
|
|
219
|
+
mounted = true
|
|
220
|
+
rebind()
|
|
221
|
+
document.addEventListener('keydown', onKeydown)
|
|
222
|
+
})
|
|
223
|
+
watch(() => props.target, () => { if (mounted) rebind() }, { flush: 'post' })
|
|
224
|
+
onBeforeUnmount(() => {
|
|
225
|
+
mounted = false
|
|
226
|
+
unbind()
|
|
227
|
+
document.removeEventListener('keydown', onKeydown)
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
// The announcement, set as the overlay appears and cleared as it goes, so the next drag is heard again.
|
|
231
|
+
const announcement = ref('')
|
|
232
|
+
watch(visible, (v) => { announcement.value = v ? props.label : '' })
|
|
233
|
+
|
|
234
|
+
defineExpose({
|
|
235
|
+
/** Whether the overlay is showing. */
|
|
236
|
+
visible,
|
|
237
|
+
/** Hide the overlay for the rest of this drag, as Escape does. */
|
|
238
|
+
dismiss: () => { if (dragging.value) dismissed.value = true },
|
|
239
|
+
})
|
|
240
|
+
</script>
|
|
241
|
+
|
|
242
|
+
<template>
|
|
243
|
+
<span class="sr-only" role="status" aria-live="polite" data-file-drop-announce>{{ announcement }}</span>
|
|
244
|
+
<!-- Over the window, in <body>; contained, inside the target itself, so it sits in the target's stacking context
|
|
245
|
+
(a dialog's, over the dialog's content) with no measuring. -->
|
|
246
|
+
<Teleport :to="scoped ?? 'body'">
|
|
247
|
+
<!-- Fades in with the message growing from a little smaller, and out a little faster; none with reduced motion. -->
|
|
248
|
+
<Transition
|
|
249
|
+
enter-active-class="transition-opacity duration-150 ease-out *:transition-transform *:duration-150 *:ease-out motion-reduce:duration-0 motion-reduce:*:duration-0"
|
|
250
|
+
enter-from-class="opacity-0 *:scale-95"
|
|
251
|
+
leave-active-class="transition-opacity duration-100 ease-in *:transition-transform *:duration-100 *:ease-in motion-reduce:duration-0 motion-reduce:*:duration-0"
|
|
252
|
+
leave-to-class="opacity-0 *:scale-95"
|
|
253
|
+
>
|
|
254
|
+
<div
|
|
255
|
+
v-if="visible"
|
|
256
|
+
v-bind="$attrs"
|
|
257
|
+
data-file-drop-overlay
|
|
258
|
+
:data-contained="scoped ? '' : undefined"
|
|
259
|
+
aria-hidden="true"
|
|
260
|
+
:class="[
|
|
261
|
+
'pointer-events-none flex items-center justify-center bg-default/75 backdrop-blur-sm',
|
|
262
|
+
scoped ? 'absolute inset-0 z-10 rounded-[inherit] p-3' : 'fixed inset-0 z-50 p-6',
|
|
263
|
+
]"
|
|
264
|
+
>
|
|
265
|
+
<div class="flex max-w-full flex-col items-center gap-3 rounded-lg border-2 border-dashed border-accented bg-elevated px-10 py-8 text-center shadow-lg" data-file-drop-message>
|
|
266
|
+
<slot :label="label" :accept="accept" :multiple="multiple">
|
|
267
|
+
<UIcon v-if="icon" :name="icon" class="size-10 text-primary" data-file-drop-icon />
|
|
268
|
+
<p class="text-base font-semibold text-highlighted">{{ label }}</p>
|
|
269
|
+
<p v-if="description" class="text-sm text-muted">{{ description }}</p>
|
|
270
|
+
</slot>
|
|
271
|
+
</div>
|
|
272
|
+
</div>
|
|
273
|
+
</Transition>
|
|
274
|
+
</Teleport>
|
|
275
|
+
</template>
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// The files a form holds, one row each: a file-type icon, the name, the size in the locale's words, and Remove. A row
|
|
3
|
+
// is a `File` just picked or dropped, or a file already on the server (`{ id, name, size, type }`), in the order given,
|
|
4
|
+
// so an edit form lists what was sent before and what is being added in one list. Nuxt UI's UFileUpload lists only the
|
|
5
|
+
// `File`s it holds itself; this lists whatever the form has. Written on Nuxt UI's tokens.
|
|
6
|
+
//
|
|
7
|
+
// <ZFileList :items="[...saved, ...added]" :remove="(item) => (item instanceof File ? drop(item) : api.delete(item.id))" />
|
|
8
|
+
//
|
|
9
|
+
// `remove` may return a promise (a server delete): the row shows a spinner and its button is disabled until it
|
|
10
|
+
// settles. A rejected one leaves the row as it was and is handed to @remove-error, for the product's toast.
|
|
11
|
+
//
|
|
12
|
+
// With `preview`, an image `File` shows a thumbnail through an object URL, made on the client when the row first shows
|
|
13
|
+
// and revoked when the row goes or the list unmounts; a stored file shows its `thumbnail` URL, if it has one.
|
|
14
|
+
import { useLocale } from '@nuxt/ui/composables'
|
|
15
|
+
import { formatFileSize, type FileListItem } from '../utils/file-drop'
|
|
16
|
+
|
|
17
|
+
const props = withDefaults(
|
|
18
|
+
defineProps<{
|
|
19
|
+
/** The rows, in order: `File`s and files already stored (`{ id, name, size, type? }`). */
|
|
20
|
+
items: readonly FileListItem[]
|
|
21
|
+
/** Removes one row; may return a promise, shown as the row's spinner. Without it, no Remove buttons. */
|
|
22
|
+
remove?: (item: FileListItem, index: number) => unknown
|
|
23
|
+
/** The sizes' locale (BCP 47). Unset: Nuxt UI's locale. */
|
|
24
|
+
locale?: string
|
|
25
|
+
/** The Remove button's word; its accessible name is this and the file's name: "Remove agenda.pdf". */
|
|
26
|
+
removeLabel?: string
|
|
27
|
+
/** Sizes in steps of 1024 (`1.2 KB`); `false` for decimal units (`1.2 kB`, 1 kB = 1000 bytes). */
|
|
28
|
+
binary?: boolean
|
|
29
|
+
/** Show a thumbnail in place of the icon: an image `File`'s own, or a stored file's `thumbnail` URL. */
|
|
30
|
+
preview?: boolean
|
|
31
|
+
}>(),
|
|
32
|
+
{ remove: undefined, locale: undefined, removeLabel: 'Remove', binary: true, preview: false },
|
|
33
|
+
)
|
|
34
|
+
const emit = defineEmits<{
|
|
35
|
+
/** A `remove` promise rejected: the row stays. */
|
|
36
|
+
removeError: [error: unknown, item: FileListItem]
|
|
37
|
+
}>()
|
|
38
|
+
defineSlots<{
|
|
39
|
+
/** Before the row's icon or thumbnail: a checkbox, say. */
|
|
40
|
+
leading?(props: { item: FileListItem; index: number; removing: boolean }): unknown
|
|
41
|
+
/** After the size, before Remove: a badge, say. */
|
|
42
|
+
trailing?(props: { item: FileListItem; index: number; removing: boolean }): unknown
|
|
43
|
+
}>()
|
|
44
|
+
|
|
45
|
+
const { locale: uiLocale } = useLocale()
|
|
46
|
+
const lang = computed(() => props.locale ?? uiLocale.value.code)
|
|
47
|
+
|
|
48
|
+
// `File` isn't one of the globals a template can see, so the test is made here.
|
|
49
|
+
const isStored = (item: FileListItem) => !(item instanceof File)
|
|
50
|
+
|
|
51
|
+
// A stored file is keyed by its id; a File, which has none, by identity.
|
|
52
|
+
const fileKeys = new WeakMap<File, string>()
|
|
53
|
+
let counter = 0
|
|
54
|
+
function keyOf(item: FileListItem) {
|
|
55
|
+
if (!(item instanceof File)) return `id:${item.id}`
|
|
56
|
+
let k = fileKeys.get(item)
|
|
57
|
+
if (!k) fileKeys.set(item, (k = `file:${++counter}`))
|
|
58
|
+
return k
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The icon for a type, by its MIME type or, without one, its extension. */
|
|
62
|
+
function iconOf(item: FileListItem) {
|
|
63
|
+
const type = (item.type ?? '').toLowerCase()
|
|
64
|
+
const ext = item.name.toLowerCase().split('.').pop() ?? ''
|
|
65
|
+
if (type.startsWith('image/') || /^(png|jpe?g|gif|webp|heic|svg|bmp|tiff?)$/.test(ext)) return 'i-lucide-file-image'
|
|
66
|
+
if (type.startsWith('video/') || /^(mp4|mov|webm|avi|mkv)$/.test(ext)) return 'i-lucide-file-video'
|
|
67
|
+
if (type.startsWith('audio/') || /^(mp3|wav|m4a|flac|ogg)$/.test(ext)) return 'i-lucide-file-audio'
|
|
68
|
+
if (/spreadsheet|excel|csv/.test(type) || /^(xlsx?|csv|ods|numbers)$/.test(ext)) return 'i-lucide-file-spreadsheet'
|
|
69
|
+
if (/zip|compressed|tar|gzip|7z|rar/.test(type) || /^(zip|tar|gz|tgz|7z|rar)$/.test(ext)) return 'i-lucide-file-archive'
|
|
70
|
+
if (type === 'application/pdf' || type.startsWith('text/') || /word|document|rtf/.test(type) || /^(pdf|txt|md|docx?|rtf|odt|pages)$/.test(ext)) return 'i-lucide-file-text'
|
|
71
|
+
return 'i-lucide-file'
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Thumbnails. An object URL holds its File in memory until revoked, so one is made only on the client (never during
|
|
75
|
+
// SSR, where it would leak and mismatch), only for an image File in a row that is shown, and revoked as soon as that
|
|
76
|
+
// File leaves `items`, `preview` is turned off, or the list unmounts.
|
|
77
|
+
const onClient = ref(false)
|
|
78
|
+
onMounted(() => { onClient.value = true })
|
|
79
|
+
const objectUrls = new Map<File, string>()
|
|
80
|
+
/** Thumbnails that failed to load (a type the browser can't draw, such as HEIC): those rows show the icon. */
|
|
81
|
+
const broken = ref(new Set<string>())
|
|
82
|
+
|
|
83
|
+
function thumbnailOf(item: FileListItem): string | undefined {
|
|
84
|
+
if (!props.preview || broken.value.has(keyOf(item))) return undefined
|
|
85
|
+
if (!(item instanceof File)) return item.thumbnail || undefined
|
|
86
|
+
if (!onClient.value || !item.type.toLowerCase().startsWith('image/')) return undefined
|
|
87
|
+
let url = objectUrls.get(item)
|
|
88
|
+
if (!url) objectUrls.set(item, (url = URL.createObjectURL(item)))
|
|
89
|
+
return url
|
|
90
|
+
}
|
|
91
|
+
function revoke(keep: ReadonlySet<FileListItem>) {
|
|
92
|
+
for (const [file, url] of objectUrls) {
|
|
93
|
+
if (keep.has(file)) continue
|
|
94
|
+
URL.revokeObjectURL(url)
|
|
95
|
+
objectUrls.delete(file)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
watch([() => props.items, () => props.preview], ([items, preview]) => revoke(preview ? new Set(items) : new Set()), { deep: 1 })
|
|
99
|
+
onBeforeUnmount(() => revoke(new Set()))
|
|
100
|
+
|
|
101
|
+
const removing = ref(new Set<string>())
|
|
102
|
+
async function onRemove(item: FileListItem, index: number) {
|
|
103
|
+
if (!props.remove) return
|
|
104
|
+
const key = keyOf(item)
|
|
105
|
+
if (removing.value.has(key)) return
|
|
106
|
+
removing.value = new Set(removing.value).add(key)
|
|
107
|
+
try {
|
|
108
|
+
await props.remove(item, index)
|
|
109
|
+
} catch (error) {
|
|
110
|
+
emit('removeError', error, item)
|
|
111
|
+
} finally {
|
|
112
|
+
const next = new Set(removing.value)
|
|
113
|
+
next.delete(key)
|
|
114
|
+
removing.value = next
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
</script>
|
|
118
|
+
|
|
119
|
+
<template>
|
|
120
|
+
<ul v-if="items.length" class="flex flex-col gap-1" data-file-list>
|
|
121
|
+
<li
|
|
122
|
+
v-for="(item, index) in items"
|
|
123
|
+
:key="keyOf(item)"
|
|
124
|
+
:aria-busy="removing.has(keyOf(item)) || undefined"
|
|
125
|
+
class="flex items-center gap-2 rounded-md bg-elevated px-3 py-2"
|
|
126
|
+
:data-file-row="item.name"
|
|
127
|
+
:data-stored="isStored(item) ? '' : undefined"
|
|
128
|
+
>
|
|
129
|
+
<slot name="leading" :item="item" :index="index" :removing="removing.has(keyOf(item))" />
|
|
130
|
+
<img
|
|
131
|
+
v-if="thumbnailOf(item)"
|
|
132
|
+
:src="thumbnailOf(item)"
|
|
133
|
+
alt=""
|
|
134
|
+
class="size-8 shrink-0 rounded-sm bg-default object-cover"
|
|
135
|
+
data-file-thumbnail
|
|
136
|
+
@error="broken = new Set(broken).add(keyOf(item))"
|
|
137
|
+
>
|
|
138
|
+
<UIcon v-else :name="iconOf(item)" class="size-4 shrink-0 text-dimmed" aria-hidden="true" />
|
|
139
|
+
<span class="min-w-0 flex-1 truncate text-sm text-default">{{ item.name }}</span>
|
|
140
|
+
<span class="shrink-0 text-xs text-muted tabular-nums">{{ formatFileSize(item.size, lang, { binary }) }}</span>
|
|
141
|
+
<slot name="trailing" :item="item" :index="index" :removing="removing.has(keyOf(item))" />
|
|
142
|
+
<UButton
|
|
143
|
+
v-if="remove"
|
|
144
|
+
type="button"
|
|
145
|
+
size="xs"
|
|
146
|
+
variant="ghost"
|
|
147
|
+
color="error"
|
|
148
|
+
icon="i-lucide-x"
|
|
149
|
+
:loading="removing.has(keyOf(item))"
|
|
150
|
+
:aria-label="`${removeLabel} ${item.name}`"
|
|
151
|
+
@click="onRemove(item, index)"
|
|
152
|
+
/>
|
|
153
|
+
</li>
|
|
154
|
+
</ul>
|
|
155
|
+
</template>
|