@zooui/nuxt 0.1.1 → 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 +14 -2
- package/components/ConfirmButton.vue +124 -0
- package/components/ConfirmDialog.vue +201 -0
- package/components/DateField.vue +176 -42
- 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 +189 -8
- package/utils/file-drop.ts +151 -0
- package/utils/filter-match.ts +62 -0
- package/utils/order-list.ts +42 -0
package/utils/date-entry.ts
CHANGED
|
@@ -13,6 +13,12 @@
|
|
|
13
13
|
// server route, a domain package). Temporal is the polyfill's own `/implementation`, never the bare entry, which
|
|
14
14
|
// resolves to `globalThis.Temporal` where an engine has one: one class wherever this runs, the same one
|
|
15
15
|
// a database client or serializer in the product hands out.
|
|
16
|
+
//
|
|
17
|
+
// THE TWO DAY TYPES. Everything here works in PlainDate. A field may also hold an @internationalized/date
|
|
18
|
+
// CalendarDate, the type Nuxt UI's UCalendar and UInputDate use: toPlainDate and fromPlainDate convert by the fields
|
|
19
|
+
// alone (year, month, day), and only a Gregorian one is accepted, since another calendar's year and month are not the
|
|
20
|
+
// ISO ones and converting them by their fields would land on a different day.
|
|
21
|
+
import { CalendarDate } from '@internationalized/date'
|
|
16
22
|
import { Temporal } from 'temporal-polyfill/implementation'
|
|
17
23
|
|
|
18
24
|
type PlainDate = Temporal.PlainDate
|
|
@@ -123,11 +129,12 @@ function dayFromParts(order: DateEntryOrder, parts: string[], today: PlainDate):
|
|
|
123
129
|
* In MM/DD/YYYY (the default):
|
|
124
130
|
*
|
|
125
131
|
* - `5`, `12` (up to 31) — that day of today's month
|
|
126
|
-
* - `0512` — May 12 this year; `051226` — May 12, 2026
|
|
132
|
+
* - `0512` — May 12 this year; `051226` and `05122026` — May 12, 2026
|
|
127
133
|
* - `5/12` — this year; `5/12/26`, `5/12/2026`
|
|
128
134
|
*
|
|
129
135
|
* Other formats read the same shapes in their own order: in DD/MM/YYYY `1205` and `12/5` are May 12, and
|
|
130
|
-
* `120526`
|
|
136
|
+
* `120526` and `12052026` are May 12, 2026; in YYYY-MM-DD `0512` and `5-12` are May 12 and `260512`, `20260512` and
|
|
137
|
+
* `2026-5-12` carry the year.
|
|
131
138
|
* A bare day of the month reads the same in all of them. Returns null for anything that doesn't name a real day,
|
|
132
139
|
* including blank text and a year of all zeros (`5/12/0`, `5/12/00`, `051200`).
|
|
133
140
|
*/
|
|
@@ -143,6 +150,12 @@ export function parseDateEntry(text: string, today: PlainDate, format: DateEntry
|
|
|
143
150
|
return dayOf(today.year, today.month, Number(value))
|
|
144
151
|
if (value.length === 4) return dayFromParts(order, [value.slice(0, 2), value.slice(2, 4)], today)
|
|
145
152
|
if (value.length === 6) return dayFromParts(order, [value.slice(0, 2), value.slice(2, 4), value.slice(4, 6)], today)
|
|
153
|
+
// Eight digits carry a four-digit year, wherever the format puts it: MMDDYYYY, DDMMYYYY, YYYYMMDD.
|
|
154
|
+
if (value.length === 8) {
|
|
155
|
+
let at = 0
|
|
156
|
+
const parts = [...order].map((field) => value.slice(at, (at += field === 'Y' ? 4 : 2)))
|
|
157
|
+
return dayFromParts(order, parts, today)
|
|
158
|
+
}
|
|
146
159
|
return null
|
|
147
160
|
}
|
|
148
161
|
|
|
@@ -209,14 +222,63 @@ export function commitDateEntry(text: string, current: PlainDate | null, today:
|
|
|
209
222
|
*
|
|
210
223
|
* Pressing m, h, y or r again keeps walking, so "hhh" is the end of the month after next. The walk stops at
|
|
211
224
|
* FIRST_ENTRY_DATE and LAST_ENTRY_DATE, so a shortcut never lands on a day the field can't read back.
|
|
225
|
+
*
|
|
226
|
+
* With `limits`, it also stops at `minValue` and `maxValue`, and skips the days `isDateUnavailable` refuses:
|
|
227
|
+
*
|
|
228
|
+
* - `m` and `y` land on the first available day of the month or year, `h` and `r` on the last; pressed there, they
|
|
229
|
+
* go on to the month or year before (m, y) or after (h, r), as they do from the 1st or the 31st.
|
|
230
|
+
* - `+` and `-` go to the next or previous available day.
|
|
231
|
+
* - `t` is today, or the nearest allowed day to it: `minValue` when today is before it, `maxValue` when today is
|
|
232
|
+
* after it, and the next available day when today is unavailable (the one before, at `maxValue`).
|
|
233
|
+
*
|
|
234
|
+
* Unavailable days are looked through for a year at most. When no allowed day is in reach it returns null, as for a
|
|
235
|
+
* key that isn't a shortcut: isDateShortcut tells the two apart.
|
|
212
236
|
*/
|
|
213
|
-
export function applyDateShortcut(
|
|
237
|
+
export function applyDateShortcut(
|
|
238
|
+
key: string,
|
|
239
|
+
current: PlainDate | null,
|
|
240
|
+
today: PlainDate,
|
|
241
|
+
format: DateEntryFormat | string = US_DATE_ENTRY,
|
|
242
|
+
limits?: DateLimits,
|
|
243
|
+
): PlainDate | null {
|
|
214
244
|
if (key === formatOf(format).separator) return null
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
if (
|
|
219
|
-
|
|
245
|
+
const k = key.toLowerCase()
|
|
246
|
+
const base = current ?? today
|
|
247
|
+
const target = shortcutTarget(k, base, today)
|
|
248
|
+
if (!target) return null
|
|
249
|
+
const { lo, hi } = bounds(limits)
|
|
250
|
+
if (Temporal.PlainDate.compare(lo, hi) > 0) return null
|
|
251
|
+
const unavailable = limits?.isDateUnavailable
|
|
252
|
+
const walk = WALK[k]!
|
|
253
|
+
const inward = INWARD[k]
|
|
254
|
+
const day = nearestAllowed(target, inward ?? walk, lo, hi, unavailable)
|
|
255
|
+
if (!inward || !day || Temporal.PlainDate.compare(day, base) * walk > 0) return day
|
|
256
|
+
// The first available day of the month is where m already is (or after it): the month before's, as from the 1st.
|
|
257
|
+
const again = nearestAllowed(shortcutTarget(k, target.add({ days: walk }), today)!, inward, lo, hi, unavailable)
|
|
258
|
+
return again && Temporal.PlainDate.compare(again, base) * walk > 0 ? again : day
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Which way each key moves, and for the period keys, which way to look for an available day from the period's edge.
|
|
262
|
+
const WALK: Record<string, 1 | -1> = { 't': 1, 'h': 1, 'r': 1, '+': 1, '=': 1, 'm': -1, 'y': -1, '-': -1, '_': -1 }
|
|
263
|
+
const INWARD: Record<string, 1 | -1> = { m: 1, y: 1, h: -1, r: -1 }
|
|
264
|
+
const UNAVAILABLE_SEARCH_DAYS = 366
|
|
265
|
+
|
|
266
|
+
// `target` pulled inside lo..hi, then the nearest available day from it: `step`'s way, or back into the bounds when
|
|
267
|
+
// it was pulled in. Null when there is none within the bounds and the search's year.
|
|
268
|
+
function nearestAllowed(target: PlainDate, step: 1 | -1, lo: PlainDate, hi: PlainDate, unavailable?: ((date: PlainDate) => boolean) | null) {
|
|
269
|
+
const dir = Temporal.PlainDate.compare(target, hi) > 0 ? -1 : Temporal.PlainDate.compare(target, lo) < 0 ? 1 : step
|
|
270
|
+
let day = clamp(target, lo, hi)
|
|
271
|
+
for (let i = 0; unavailable?.(day); i++) {
|
|
272
|
+
day = day.add({ days: dir })
|
|
273
|
+
if (i >= UNAVAILABLE_SEARCH_DAYS || Temporal.PlainDate.compare(day, lo) < 0 || Temporal.PlainDate.compare(day, hi) > 0) return null
|
|
274
|
+
}
|
|
275
|
+
return day
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Whether `key` is one of applyDateShortcut's keys in this format (its separator never is), so a field can take the
|
|
279
|
+
* key even when the shortcut has no allowed day to go to. */
|
|
280
|
+
export function isDateShortcut(key: string, format: DateEntryFormat | string = US_DATE_ENTRY): boolean {
|
|
281
|
+
return key !== formatOf(format).separator && Object.hasOwn(WALK, key.toLowerCase())
|
|
220
282
|
}
|
|
221
283
|
|
|
222
284
|
function shortcutTarget(key: string, base: PlainDate, today: PlainDate): PlainDate | null {
|
|
@@ -243,3 +305,122 @@ function shortcutTarget(key: string, base: PlainDate, today: PlainDate): PlainDa
|
|
|
243
305
|
return null
|
|
244
306
|
}
|
|
245
307
|
}
|
|
308
|
+
|
|
309
|
+
// ── The two day types ───────────────────────────────────────────────────────────────────────────────────────────
|
|
310
|
+
|
|
311
|
+
/** What a field holds and hands back: a Temporal.PlainDate, or an @internationalized/date CalendarDate. */
|
|
312
|
+
export type DateModelType = 'plain-date' | 'calendar-date'
|
|
313
|
+
|
|
314
|
+
/** The part of an @internationalized/date CalendarDate read here. Matched by shape, not by class, so a CalendarDate
|
|
315
|
+
* from the product's own copy of the package is read the same as one from Nuxt UI's. */
|
|
316
|
+
export interface CalendarDateLike {
|
|
317
|
+
readonly calendar: { readonly identifier: string }
|
|
318
|
+
readonly era: string
|
|
319
|
+
readonly year: number
|
|
320
|
+
readonly month: number
|
|
321
|
+
readonly day: number
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** A day in either type. */
|
|
325
|
+
export type EntryDate = PlainDate | CalendarDateLike
|
|
326
|
+
|
|
327
|
+
/** Which of the two types `value` is, or null for anything else (null included). */
|
|
328
|
+
export function dateModelTypeOf(value: unknown): DateModelType | null {
|
|
329
|
+
if (value instanceof Temporal.PlainDate) return 'plain-date'
|
|
330
|
+
if (!value || typeof value !== 'object') return null
|
|
331
|
+
const v = value as Partial<CalendarDateLike>
|
|
332
|
+
return typeof v.calendar?.identifier === 'string' && typeof v.year === 'number' && typeof v.month === 'number' && typeof v.day === 'number'
|
|
333
|
+
? 'calendar-date'
|
|
334
|
+
: null
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The day as a PlainDate: a PlainDate as it is, a Gregorian CalendarDate by its fields, null for null. A CalendarDate
|
|
339
|
+
* in any other calendar system (Japanese, Buddhist, Persian …) throws a RangeError rather than land on another day:
|
|
340
|
+
* convert it first with @internationalized/date's `toCalendar(date, new GregorianCalendar())`.
|
|
341
|
+
*/
|
|
342
|
+
export function toPlainDate(value: EntryDate | null | undefined): PlainDate | null {
|
|
343
|
+
if (value == null) return null
|
|
344
|
+
if (value instanceof Temporal.PlainDate) return value
|
|
345
|
+
if (dateModelTypeOf(value) !== 'calendar-date') throw new TypeError(`Not a day: expected a Temporal.PlainDate or an @internationalized/date CalendarDate, got ${String(value)}.`)
|
|
346
|
+
const { calendar, era, year, month, day } = value
|
|
347
|
+
if (calendar.identifier !== 'gregory') {
|
|
348
|
+
throw new RangeError(`Only a Gregorian CalendarDate can be used as a day here; this one is in the "${calendar.identifier}" calendar. Convert it with toCalendar(date, new GregorianCalendar()) from @internationalized/date.`)
|
|
349
|
+
}
|
|
350
|
+
// A Gregorian year counts within its era: 1 BC is the ISO year 0.
|
|
351
|
+
return Temporal.PlainDate.from({ year: era === 'BC' ? 1 - year : year, month, day }, { overflow: 'reject' })
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
type CalendarDateClass = new (year: number, month: number, day: number) => CalendarDateLike
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* The day in the type asked for: the PlainDate itself, or a Gregorian CalendarDate with its fields. `like`, a
|
|
358
|
+
* CalendarDate the caller already holds, names the class to build with, so the caller's own copy of
|
|
359
|
+
* @internationalized/date gets its own class back; without one it is the layer's.
|
|
360
|
+
*/
|
|
361
|
+
export function fromPlainDate(d: PlainDate, type: 'plain-date', like?: CalendarDateLike | null): PlainDate
|
|
362
|
+
export function fromPlainDate(d: PlainDate, type: 'calendar-date', like?: CalendarDateLike | null): CalendarDate
|
|
363
|
+
export function fromPlainDate(d: PlainDate, type: DateModelType, like?: CalendarDateLike | null): EntryDate
|
|
364
|
+
export function fromPlainDate(d: PlainDate | null, type: DateModelType, like?: CalendarDateLike | null): EntryDate | null
|
|
365
|
+
export function fromPlainDate(d: PlainDate | null, type: DateModelType, like?: CalendarDateLike | null): EntryDate | null {
|
|
366
|
+
if (!d || type === 'plain-date') return d
|
|
367
|
+
const Class = (like?.constructor ?? CalendarDate) as CalendarDateClass
|
|
368
|
+
return new Class(d.year, d.month, d.day)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// ── Limits ──────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
372
|
+
|
|
373
|
+
/** The days a field takes, as Nuxt UI's UCalendar names them: the first and the last, and the days in between it
|
|
374
|
+
* refuses. `minValue` and `maxValue` may be either day type. */
|
|
375
|
+
export interface DateLimits {
|
|
376
|
+
minValue?: EntryDate | null
|
|
377
|
+
maxValue?: EntryDate | null
|
|
378
|
+
isDateUnavailable?: ((date: PlainDate) => boolean) | null
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Why the limits refuse a day. */
|
|
382
|
+
export type DateLimitReason = 'before-min' | 'after-max' | 'unavailable'
|
|
383
|
+
|
|
384
|
+
/** A day the limits refuse: why, the day, and the limit it crossed (`minValue` or `maxValue`; null when unavailable).
|
|
385
|
+
* A PlainDate here; ZDateField's `invalid` hands it back in the model's own type. */
|
|
386
|
+
export interface DateLimitProblem<D = PlainDate> {
|
|
387
|
+
readonly reason: DateLimitReason
|
|
388
|
+
readonly date: D
|
|
389
|
+
readonly limit: D | null
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const earlier = (a: PlainDate, b: PlainDate) => (Temporal.PlainDate.compare(a, b) <= 0 ? a : b)
|
|
393
|
+
const later = (a: PlainDate, b: PlainDate) => (Temporal.PlainDate.compare(a, b) >= 0 ? a : b)
|
|
394
|
+
const clamp = (d: PlainDate, lo: PlainDate, hi: PlainDate) => later(lo, earlier(d, hi))
|
|
395
|
+
|
|
396
|
+
// The first and last days a shortcut may land on: the limits', within the days a field can show and read back.
|
|
397
|
+
function bounds(limits?: DateLimits) {
|
|
398
|
+
const min = toPlainDate(limits?.minValue)
|
|
399
|
+
const max = toPlainDate(limits?.maxValue)
|
|
400
|
+
return { lo: min ? later(min, FIRST_ENTRY_DATE) : FIRST_ENTRY_DATE, hi: max ? earlier(max, LAST_ENTRY_DATE) : LAST_ENTRY_DATE }
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Why the limits refuse `date`, or null when they take it. Checked in that order: before `minValue`, after
|
|
404
|
+
* `maxValue`, then `isDateUnavailable`. */
|
|
405
|
+
export function checkDateLimits(date: PlainDate, limits?: DateLimits): DateLimitProblem | null {
|
|
406
|
+
const min = toPlainDate(limits?.minValue)
|
|
407
|
+
if (min && Temporal.PlainDate.compare(date, min) < 0) return { reason: 'before-min', date, limit: min }
|
|
408
|
+
const max = toPlainDate(limits?.maxValue)
|
|
409
|
+
if (max && Temporal.PlainDate.compare(date, max) > 0) return { reason: 'after-max', date, limit: max }
|
|
410
|
+
if (limits?.isDateUnavailable?.(date)) return { reason: 'unavailable', date, limit: null }
|
|
411
|
+
return null
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** An English sentence for a refused day, its dates in the field's format: "Choose a date on or after 01/01/2026.",
|
|
415
|
+
* "Choose a date on or before 12/31/2026.", "12/25/2026 isn't available." A product in another language builds its
|
|
416
|
+
* own from the problem's `reason`, `date` and `limit`. */
|
|
417
|
+
export function dateLimitMessage(problem: DateLimitProblem, format: DateEntryFormat | string = US_DATE_ENTRY): string {
|
|
418
|
+
switch (problem.reason) {
|
|
419
|
+
case 'before-min':
|
|
420
|
+
return `Choose a date on or after ${formatDateEntry(problem.limit, format)}.`
|
|
421
|
+
case 'after-max':
|
|
422
|
+
return `Choose a date on or before ${formatDateEntry(problem.limit, format)}.`
|
|
423
|
+
case 'unavailable':
|
|
424
|
+
return `${formatDateEntry(problem.date, format)} isn't available.`
|
|
425
|
+
}
|
|
426
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Files dropped on a page, sorted the way a native <input type="file"> would take them: `accept` in the input's own
|
|
2
|
+
// syntax (MIME types, `type/*`, `.ext`, comma-separated), and one file unless `multiple`; then a product's own `filter`
|
|
3
|
+
// (a size or name rule) and, optionally, no name twice. Used by ZFileDropOverlay; a product can hold its own drop zones
|
|
4
|
+
// or inputs to the same rule.
|
|
5
|
+
//
|
|
6
|
+
// Framework-free: Nuxt auto-imports it in a product.
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A file the drop did not take, and why: its type isn't in `accept`, `filter` refused it, its name is already held
|
|
10
|
+
* (`duplicate`), or it came after the first without `multiple`.
|
|
11
|
+
*/
|
|
12
|
+
export interface RejectedFile {
|
|
13
|
+
file: File
|
|
14
|
+
reason: 'accept' | 'filter' | 'duplicate' | 'multiple'
|
|
15
|
+
/** The message `filter` returned for it, when it returned one. */
|
|
16
|
+
message?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A product's own rule for a file: `true` takes it; `false`, or a message saying why, refuses it. */
|
|
20
|
+
export type FileFilter = (file: File) => boolean | string
|
|
21
|
+
|
|
22
|
+
/** A row of ZFileList: a `File` just added, or a file already stored, by its id, with a thumbnail URL if it has one. */
|
|
23
|
+
export type FileListItem = File | { id: string | number; name: string; size: number; type?: string; thumbnail?: string }
|
|
24
|
+
|
|
25
|
+
/** How sortDroppedFiles sorts. */
|
|
26
|
+
export interface SortDroppedFilesOptions {
|
|
27
|
+
/** The native input's `accept`. Unset, every type is taken. */
|
|
28
|
+
accept?: string | readonly string[]
|
|
29
|
+
/** Take every file the rules let through; without it, the first. */
|
|
30
|
+
multiple?: boolean
|
|
31
|
+
/** A product's own rule, run on each file `accept` takes. */
|
|
32
|
+
filter?: FileFilter
|
|
33
|
+
/** Names already held (the list the files join): a file of one of these names is refused as `duplicate`. Implies
|
|
34
|
+
* `dedupe: 'name'`. */
|
|
35
|
+
existing?: Iterable<string>
|
|
36
|
+
/** `'name'`: refuse a file whose name is in `existing` or came earlier in the same drop. */
|
|
37
|
+
dedupe?: 'name'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** What a file needs for matching: a `File`, or anything with its name and MIME type. */
|
|
41
|
+
type Named = Pick<File, 'name' | 'type'>
|
|
42
|
+
|
|
43
|
+
/** The valid tokens of an `accept` value, lower-cased; tokens the input would ignore are dropped. */
|
|
44
|
+
function acceptTokens(accept: string | readonly string[] | undefined): string[] {
|
|
45
|
+
const list = accept === undefined ? [] : typeof accept === 'string' ? accept.split(',') : accept.flatMap((a) => a.split(','))
|
|
46
|
+
return list
|
|
47
|
+
.map((t) => t.trim().toLowerCase())
|
|
48
|
+
.filter((t) => /^\.[^./\s][^/\s]*$/.test(t) || /^[a-z0-9!#$&^_.+-]+\/(\*|[a-z0-9!#$&^_.+-]+)$/.test(t))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether `accept` takes the file, as the native input reads it: `.pdf` matches the name's ending (any case),
|
|
53
|
+
* `image/*` the MIME type's first half, `application/pdf` the whole type. An empty `accept`, or one with no valid
|
|
54
|
+
* token, takes every file.
|
|
55
|
+
*/
|
|
56
|
+
export function fileMatchesAccept(file: Named, accept?: string | readonly string[]): boolean {
|
|
57
|
+
const tokens = acceptTokens(accept)
|
|
58
|
+
if (!tokens.length) return true
|
|
59
|
+
const name = file.name.toLowerCase()
|
|
60
|
+
const type = file.type.toLowerCase().split(';')[0]!.trim()
|
|
61
|
+
return tokens.some((t) => {
|
|
62
|
+
if (t.startsWith('.')) return name.endsWith(t)
|
|
63
|
+
if (t.endsWith('/*')) return type.startsWith(t.slice(0, -1))
|
|
64
|
+
return type === t
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The dropped files, split into the ones taken and the ones refused, in drop order. Each file is checked against
|
|
70
|
+
* `accept`, then `filter`, then (with `existing` or `dedupe`) the names already held, and refused at the first that
|
|
71
|
+
* says no. Without `multiple`, the first file to pass is the one taken, and later ones are refused as `multiple`.
|
|
72
|
+
*/
|
|
73
|
+
export function sortDroppedFiles(
|
|
74
|
+
files: Iterable<File>,
|
|
75
|
+
options: SortDroppedFilesOptions = {},
|
|
76
|
+
): { accepted: File[]; rejected: RejectedFile[] } {
|
|
77
|
+
const accepted: File[] = []
|
|
78
|
+
const rejected: RejectedFile[] = []
|
|
79
|
+
const dedupe = options.dedupe === 'name' || options.existing !== undefined
|
|
80
|
+
const held = new Set(options.existing ?? [])
|
|
81
|
+
for (const file of files) {
|
|
82
|
+
if (!fileMatchesAccept(file, options.accept)) {
|
|
83
|
+
rejected.push({ file, reason: 'accept' })
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
const verdict = options.filter ? options.filter(file) : true
|
|
87
|
+
if (verdict !== true) {
|
|
88
|
+
rejected.push(typeof verdict === 'string' && verdict ? { file, reason: 'filter', message: verdict } : { file, reason: 'filter' })
|
|
89
|
+
} else if (dedupe && held.has(file.name)) {
|
|
90
|
+
rejected.push({ file, reason: 'duplicate' })
|
|
91
|
+
} else if (!options.multiple && accepted.length) {
|
|
92
|
+
rejected.push({ file, reason: 'multiple' })
|
|
93
|
+
} else {
|
|
94
|
+
accepted.push(file)
|
|
95
|
+
held.add(file.name)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return { accepted, rejected }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The files a drop carries: `dataTransfer.files`, or, when a browser leaves that empty (some do, for some sources),
|
|
103
|
+
* the file entries of `dataTransfer.items`.
|
|
104
|
+
*/
|
|
105
|
+
export function droppedFiles(dataTransfer: Pick<DataTransfer, 'files' | 'items'> | null | undefined): File[] {
|
|
106
|
+
if (!dataTransfer) return []
|
|
107
|
+
const files = Array.from(dataTransfer.files ?? [])
|
|
108
|
+
if (files.length) return files
|
|
109
|
+
const fromItems: File[] = []
|
|
110
|
+
for (const item of Array.from(dataTransfer.items ?? [])) {
|
|
111
|
+
if (item.kind !== 'file') continue
|
|
112
|
+
const file = item.getAsFile()
|
|
113
|
+
if (file) fromItems.push(file)
|
|
114
|
+
}
|
|
115
|
+
return fromItems
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Whether a drag carries files from outside the page, rather than text, a link or an element dragged within it. */
|
|
119
|
+
export function dragCarriesFiles(dataTransfer: Pick<DataTransfer, 'types'> | null | undefined): boolean {
|
|
120
|
+
return !!dataTransfer && Array.from(dataTransfer.types).includes('Files')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const SIZE_UNITS = ['kilobyte', 'megabyte', 'gigabyte', 'terabyte'] as const
|
|
124
|
+
const BINARY_UNITS = ['KB', 'MB', 'GB', 'TB'] as const
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A file's size for people, in the locale's digits. By default in steps of 1024, as Windows and most file managers
|
|
128
|
+
* count, with one decimal at most: `512 B`, `1.2 KB`, `239.3 KB`, `1.7 MB` (`1,2 KB` in German). With
|
|
129
|
+
* `binary: false`, decimal units as their names say (1 kB = 1000 bytes), in the locale's own words, one decimal under
|
|
130
|
+
* 10 and none above: `512 bytes`, `1.2 kB`, `245 kB`.
|
|
131
|
+
*/
|
|
132
|
+
export function formatFileSize(bytes: number, locale?: string, options: { binary?: boolean } = {}): string {
|
|
133
|
+
const binary = options.binary ?? true
|
|
134
|
+
const step = binary ? 1024 : 1000
|
|
135
|
+
const size = Number.isFinite(bytes) ? Math.max(0, bytes) : 0
|
|
136
|
+
if (size < step) {
|
|
137
|
+
const whole = Math.round(size)
|
|
138
|
+
if (binary) return `${new Intl.NumberFormat(locale).format(whole)} B`
|
|
139
|
+
return new Intl.NumberFormat(locale, { style: 'unit', unit: 'byte', unitDisplay: 'long' }).format(whole)
|
|
140
|
+
}
|
|
141
|
+
let value = size / step
|
|
142
|
+
let unit = 0
|
|
143
|
+
const digits = (v: number) => (binary || v < 10 ? 1 : 0)
|
|
144
|
+
// 999,960 bytes is 1 MB, not "1,000 kB": step up whenever rounding reaches the next unit.
|
|
145
|
+
while (unit < SIZE_UNITS.length - 1 && Number(value.toFixed(digits(value))) >= step) {
|
|
146
|
+
value /= step
|
|
147
|
+
unit += 1
|
|
148
|
+
}
|
|
149
|
+
if (binary) return `${new Intl.NumberFormat(locale, { maximumFractionDigits: 1 }).format(value)} ${BINARY_UNITS[unit]}`
|
|
150
|
+
return new Intl.NumberFormat(locale, { style: 'unit', unit: SIZE_UNITS[unit], unitDisplay: 'short', maximumFractionDigits: digits(value) }).format(value)
|
|
151
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Search for ZFilterSelect: a literal, case-insensitive substring match, and the label cut into the segments that
|
|
2
|
+
// matched and the ones that didn't, so the matches are drawn as <mark> elements rather than written into v-html.
|
|
3
|
+
// The query is text, never a pattern: `(`, `.` or `\` match themselves and nothing else.
|
|
4
|
+
//
|
|
5
|
+
// Case is folded one character at a time, the same way for the label and the query, so a match found in the folded
|
|
6
|
+
// text always maps back onto whole characters of the label, even where lowercasing changes a character's length
|
|
7
|
+
// ('İ' becomes 'i' and a combining dot).
|
|
8
|
+
|
|
9
|
+
/** A run of a label: matched by the search, or not. Joined in order, the segments are the label. */
|
|
10
|
+
export interface FilterSelectSegment {
|
|
11
|
+
text: string
|
|
12
|
+
match: boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The text with each character lowercased, and the span of the original each folded code unit came from. */
|
|
16
|
+
function fold(text: string) {
|
|
17
|
+
let folded = ''
|
|
18
|
+
const start: number[] = []
|
|
19
|
+
const end: number[] = []
|
|
20
|
+
let at = 0
|
|
21
|
+
for (const ch of text) {
|
|
22
|
+
const lower = ch.toLowerCase()
|
|
23
|
+
for (let k = 0; k < lower.length; k++) {
|
|
24
|
+
start.push(at)
|
|
25
|
+
end.push(at + ch.length)
|
|
26
|
+
}
|
|
27
|
+
folded += lower
|
|
28
|
+
at += ch.length
|
|
29
|
+
}
|
|
30
|
+
return { folded, start, end }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Whether `text` contains `query`, ignoring case. An empty query matches everything. */
|
|
34
|
+
export function filterSelectMatches(text: string, query: string): boolean {
|
|
35
|
+
if (!query) return true
|
|
36
|
+
return fold(text).folded.includes(fold(query).folded)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** `text` cut into matched and unmatched runs for `query`, left to right, matches not overlapping. */
|
|
40
|
+
export function filterSelectSegments(text: string, query: string): FilterSelectSegment[] {
|
|
41
|
+
if (!text) return []
|
|
42
|
+
const q = query ? fold(query).folded : ''
|
|
43
|
+
if (!q) return [{ text, match: false }]
|
|
44
|
+
const { folded, start, end } = fold(text)
|
|
45
|
+
const out: FilterSelectSegment[] = []
|
|
46
|
+
const push = (from: number, to: number, match: boolean) => {
|
|
47
|
+
if (to <= from) return
|
|
48
|
+
const last = out.at(-1)
|
|
49
|
+
if (last && last.match === match) last.text += text.slice(from, to)
|
|
50
|
+
else out.push({ text: text.slice(from, to), match })
|
|
51
|
+
}
|
|
52
|
+
let done = 0
|
|
53
|
+
for (let at = folded.indexOf(q); at !== -1; at = folded.indexOf(q, at + q.length)) {
|
|
54
|
+
const from = Math.max(start[at]!, done)
|
|
55
|
+
const to = end[at + q.length - 1]!
|
|
56
|
+
push(done, from, false)
|
|
57
|
+
push(from, to, true)
|
|
58
|
+
done = Math.max(done, to)
|
|
59
|
+
}
|
|
60
|
+
push(done, text.length, false)
|
|
61
|
+
return out
|
|
62
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// ZOrderList's ordering, framework-free: where a move takes an item, the array after it, and which row a typed
|
|
2
|
+
// prefix lands on. components/OrderList.vue is the only caller; scripts/order-list.test.mjs tests it.
|
|
3
|
+
|
|
4
|
+
/** The four moves: to the top, one up, one down, to the bottom. */
|
|
5
|
+
export type OrderMove = 'top' | 'up' | 'down' | 'bottom'
|
|
6
|
+
|
|
7
|
+
/** Where `move` takes the item at `index` in a list of `length`, or null when it has nowhere to go (already at that
|
|
8
|
+
* end, or `index` is not a row). */
|
|
9
|
+
export function moveTarget(index: number, length: number, move: OrderMove): number | null {
|
|
10
|
+
if (!Number.isInteger(index) || index < 0 || index >= length) return null
|
|
11
|
+
const to = move === 'top' ? 0 : move === 'up' ? index - 1 : move === 'down' ? index + 1 : length - 1
|
|
12
|
+
return to < 0 || to >= length || to === index ? null : to
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A new array with the item at `from` taken out and put back at `to`; the items themselves are the same objects. */
|
|
16
|
+
export function moveItem<T>(items: readonly T[], from: number, to: number): T[] {
|
|
17
|
+
const next = [...items]
|
|
18
|
+
const [item] = next.splice(from, 1)
|
|
19
|
+
next.splice(to, 0, item as T)
|
|
20
|
+
return next
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The row a typed prefix selects, given the row `start` it is typed on (-1 for none): the first title, searching
|
|
25
|
+
* forward and wrapping, that starts with `query` (case- and accent-insensitive, leading space ignored). One letter
|
|
26
|
+
* searches from the row after `start`, so the same letter typed over and over ("sss") cycles through the rows starting
|
|
27
|
+
* with it, as a native select does; a longer query includes `start`, so the row it is on stays while it still matches.
|
|
28
|
+
* -1 when nothing matches.
|
|
29
|
+
*/
|
|
30
|
+
export function typeaheadIndex(titles: readonly string[], query: string, start: number): number {
|
|
31
|
+
const fold = (s: string) => s.normalize('NFD').replace(/\p{M}/gu, '').trimStart().toLocaleLowerCase()
|
|
32
|
+
let q = fold(query)
|
|
33
|
+
if (!q) return -1
|
|
34
|
+
if ([...q].every((c) => c === q[0])) q = q[0]!
|
|
35
|
+
const n = titles.length
|
|
36
|
+
const from = start < 0 || start >= n ? 0 : q.length > 1 ? start : start + 1
|
|
37
|
+
for (let k = 0; k < n; k++) {
|
|
38
|
+
const i = (from + k) % n
|
|
39
|
+
if (fold(titles[i]!).startsWith(q)) return i
|
|
40
|
+
}
|
|
41
|
+
return -1
|
|
42
|
+
}
|