@getcortexlabs/mavis-core 0.1.0-alpha.4 → 0.1.0-alpha.5
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 +2 -2
- package/dist/capabilities.d.ts +119 -0
- package/dist/capabilities.d.ts.map +1 -0
- package/dist/capabilities.js +142 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/combobox.d.ts +42 -0
- package/dist/combobox.d.ts.map +1 -0
- package/dist/combobox.js +159 -0
- package/dist/combobox.js.map +1 -0
- package/dist/contracts.d.ts +1201 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/contracts.js +242 -0
- package/dist/contracts.js.map +1 -0
- package/dist/date.d.ts +96 -0
- package/dist/date.d.ts.map +1 -0
- package/dist/date.js +235 -0
- package/dist/date.js.map +1 -0
- package/dist/experimental-regions.d.ts +142 -0
- package/dist/experimental-regions.d.ts.map +1 -0
- package/dist/experimental-regions.js +315 -0
- package/dist/experimental-regions.js.map +1 -0
- package/dist/experimental.d.ts +13 -1
- package/dist/experimental.d.ts.map +1 -1
- package/dist/experimental.js +153 -23
- package/dist/experimental.js.map +1 -1
- package/dist/files.d.ts +87 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +328 -0
- package/dist/files.js.map +1 -0
- package/dist/form.d.ts +39 -0
- package/dist/form.d.ts.map +1 -0
- package/dist/form.js +170 -0
- package/dist/form.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/navigation.d.ts +22 -0
- package/dist/navigation.d.ts.map +1 -0
- package/dist/navigation.js +54 -0
- package/dist/navigation.js.map +1 -0
- package/dist/vue/combobox.d.ts +155 -0
- package/dist/vue/combobox.d.ts.map +1 -0
- package/dist/vue/combobox.js +195 -0
- package/dist/vue/combobox.js.map +1 -0
- package/dist/vue/component-props.d.ts +1989 -0
- package/dist/vue/component-props.d.ts.map +1 -0
- package/dist/vue/component-props.js +18 -0
- package/dist/vue/component-props.js.map +1 -0
- package/dist/vue/contracts.d.ts +18 -0
- package/dist/vue/contracts.d.ts.map +1 -0
- package/dist/vue/contracts.js +18 -0
- package/dist/vue/contracts.js.map +1 -0
- package/dist/vue/date-calendar.d.ts +64 -0
- package/dist/vue/date-calendar.d.ts.map +1 -0
- package/dist/vue/date-calendar.js +65 -0
- package/dist/vue/date-calendar.js.map +1 -0
- package/dist/vue/date-picker.d.ts +927 -0
- package/dist/vue/date-picker.d.ts.map +1 -0
- package/dist/vue/date-picker.js +319 -0
- package/dist/vue/date-picker.js.map +1 -0
- package/dist/vue/date-props.d.ts +254 -0
- package/dist/vue/date-props.d.ts.map +1 -0
- package/dist/vue/date-props.js +74 -0
- package/dist/vue/date-props.js.map +1 -0
- package/dist/vue/files.d.ts +53 -0
- package/dist/vue/files.d.ts.map +1 -0
- package/dist/vue/files.js +40 -0
- package/dist/vue/files.js.map +1 -0
- package/dist/vue/forms.d.ts +4 -0
- package/dist/vue/forms.d.ts.map +1 -0
- package/dist/vue/forms.js +15 -0
- package/dist/vue/forms.js.map +1 -0
- package/dist/vue/global-components.d.ts +35 -2
- package/dist/vue/global-components.d.ts.map +1 -1
- package/dist/vue/index.d.ts +12 -2
- package/dist/vue/index.d.ts.map +1 -1
- package/dist/vue/index.js +10 -1
- package/dist/vue/index.js.map +1 -1
- package/dist/vue/inline-content.d.ts +3 -1
- package/dist/vue/inline-content.d.ts.map +1 -1
- package/dist/vue/props.d.ts +1026 -495
- package/dist/vue/props.d.ts.map +1 -1
- package/dist/vue/props.js +17 -118
- package/dist/vue/props.js.map +1 -1
- package/dist/vue/workflow-props.d.ts +110 -0
- package/dist/vue/workflow-props.d.ts.map +1 -0
- package/dist/vue/workflow-props.js +37 -0
- package/dist/vue/workflow-props.js.map +1 -0
- package/dist/vue/workflows.d.ts +267 -0
- package/dist/vue/workflows.d.ts.map +1 -0
- package/dist/vue/workflows.js +200 -0
- package/dist/vue/workflows.js.map +1 -0
- package/package.json +1 -1
- package/src/capabilities.ts +145 -0
- package/src/combobox.ts +198 -0
- package/src/contracts.ts +295 -0
- package/src/date.ts +280 -0
- package/src/experimental-regions.ts +463 -0
- package/src/experimental.ts +158 -16
- package/src/files.ts +408 -0
- package/src/form.ts +190 -0
- package/src/index.ts +17 -0
- package/src/navigation.ts +61 -0
- package/src/vue/combobox.ts +238 -0
- package/src/vue/component-props.ts +24 -0
- package/src/vue/contracts.ts +33 -0
- package/src/vue/date-calendar.ts +76 -0
- package/src/vue/date-picker.ts +387 -0
- package/src/vue/date-props.ts +81 -0
- package/src/vue/files.ts +53 -0
- package/src/vue/forms.ts +20 -0
- package/src/vue/global-components.ts +44 -2
- package/src/vue/index.ts +13 -2
- package/src/vue/props.ts +34 -130
- package/src/vue/workflow-props.ts +41 -0
- package/src/vue/workflows.ts +257 -0
package/src/experimental.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
/** Experimental, single-surface motion contract. No platform objects cross this boundary. */
|
|
2
|
+
import {
|
|
3
|
+
regionState,
|
|
4
|
+
validatePointer,
|
|
5
|
+
type MotionRegion,
|
|
6
|
+
type MotionRegionAdapter,
|
|
7
|
+
type MotionPointerOptions,
|
|
8
|
+
type MotionPointer,
|
|
9
|
+
type MotionPointerAdapter,
|
|
10
|
+
} from './experimental-regions.js'
|
|
11
|
+
export * from './experimental-regions.js'
|
|
2
12
|
export type MotionValues = Partial<
|
|
3
13
|
Record<'scale' | 'scaleX' | 'scaleY' | 'x' | 'y' | 'rotate' | 'opacity', number>
|
|
4
14
|
>
|
|
@@ -8,6 +18,7 @@ export interface MotionOptions {
|
|
|
8
18
|
duration?: number
|
|
9
19
|
bounce?: number
|
|
10
20
|
ease?: 'linear' | 'easeIn' | 'easeOut' | 'easeInOut'
|
|
21
|
+
delay?: number
|
|
11
22
|
}
|
|
12
23
|
export interface MotionPlayback {
|
|
13
24
|
readonly finished: Promise<boolean>
|
|
@@ -19,6 +30,9 @@ export interface MotionSurface {
|
|
|
19
30
|
set(values: MotionValues): MotionPlayback
|
|
20
31
|
/** Runs once per mount/load. Its resources belong to that presentation. */
|
|
21
32
|
onAvailable(setup: () => void | (() => void)): () => void
|
|
33
|
+
/** A child owner, recreated on mount/load. Late work must use the supplied child. */
|
|
34
|
+
use(setup: (motion: MotionSurface) => void | (() => void)): () => void
|
|
35
|
+
pointerFollow(region: MotionRegion, options?: MotionPointerOptions): MotionPointer
|
|
22
36
|
motionValue(initial: number): MotionValue
|
|
23
37
|
springValue(source: MotionValue, options?: MotionSpringOptions): MotionSpring
|
|
24
38
|
bind(values: MotionBindings): MotionBinding
|
|
@@ -79,6 +93,9 @@ export type MotionValueAdapter = Pick<MotionValue, 'get' | 'set' | 'dispose'>
|
|
|
79
93
|
export type MotionSpringAdapter = Omit<MotionSpring, 'disposed' | 'to'>
|
|
80
94
|
export type MotionDragAdapter = Omit<MotionDrag, 'disposed'>
|
|
81
95
|
export interface MotionSurfaceAdapter {
|
|
96
|
+
/** The renderer borrows its host runtime and owns only this child's resources. */
|
|
97
|
+
use?(setup: (adapter: MotionSurfaceAdapter) => () => void): () => void
|
|
98
|
+
pointerFollow?(region: MotionRegionAdapter, options: MotionPointerOptions): MotionPointerAdapter
|
|
82
99
|
animate(values: MotionFrames, options?: MotionOptions): MotionPlayback
|
|
83
100
|
set(values: MotionValues): MotionPlayback
|
|
84
101
|
motionValue?(initial: number): MotionValueAdapter
|
|
@@ -128,6 +145,11 @@ type ScrollState = {
|
|
|
128
145
|
}
|
|
129
146
|
const scrollSources = new WeakMap<MotionScrollSource, ScrollState | undefined>()
|
|
130
147
|
const scrollSetups = new WeakMap<MotionScrollSource, Set<() => void | (() => void)>>()
|
|
148
|
+
export function motionScrollAdapter(source: MotionScrollSource): MotionScrollAdapter {
|
|
149
|
+
const state = scrollSources.get(source)
|
|
150
|
+
if (!state) throw new Error('Scroll source is unavailable.')
|
|
151
|
+
return state.adapter
|
|
152
|
+
}
|
|
131
153
|
|
|
132
154
|
export function createMotionScrollSource(): MotionScrollSource {
|
|
133
155
|
const source: MotionScrollSource = {
|
|
@@ -233,9 +255,17 @@ type Binding = {
|
|
|
233
255
|
adapter: MotionSurfaceAdapter
|
|
234
256
|
active: Set<MotionPlayback>
|
|
235
257
|
resources: Set<Resource>
|
|
258
|
+
cleanup: Set<() => void>
|
|
236
259
|
valid: boolean
|
|
260
|
+
context: object
|
|
237
261
|
}
|
|
238
262
|
const bindings = new WeakMap<MotionSurface, Binding | undefined>()
|
|
263
|
+
/** Renderer bridge; the identity has no platform object in it. */
|
|
264
|
+
export function motionSurfaceAdapter(surface: MotionSurface): MotionSurfaceAdapter {
|
|
265
|
+
const binding = bindings.get(surface)
|
|
266
|
+
if (!binding?.valid) throw new Error('Motion surface is unavailable.')
|
|
267
|
+
return binding.adapter
|
|
268
|
+
}
|
|
239
269
|
const setups = new WeakMap<MotionSurface, Set<() => void | (() => void)>>()
|
|
240
270
|
type ValueState = {
|
|
241
271
|
binding: Binding
|
|
@@ -266,9 +296,21 @@ function own<T extends Resource>(binding: Binding, raw: T): T & MotionBinding {
|
|
|
266
296
|
for (const group of dependencies.get(handle) ?? []) group.delete(handle)
|
|
267
297
|
dependencies.delete(handle)
|
|
268
298
|
const state = values.get(handle as unknown as MotionReadable)
|
|
269
|
-
|
|
299
|
+
const errors: unknown[] = []
|
|
300
|
+
for (const dependent of state?.dependents ?? []) {
|
|
301
|
+
try {
|
|
302
|
+
dependent.dispose()
|
|
303
|
+
} catch (error) {
|
|
304
|
+
errors.push(error)
|
|
305
|
+
}
|
|
306
|
+
}
|
|
270
307
|
state?.dependents.clear()
|
|
271
|
-
|
|
308
|
+
try {
|
|
309
|
+
target.dispose()
|
|
310
|
+
} catch (error) {
|
|
311
|
+
errors.push(error)
|
|
312
|
+
}
|
|
313
|
+
if (errors.length) throw new AggregateError(errors, 'Motion resource cleanup failed.')
|
|
272
314
|
}
|
|
273
315
|
if (disposed || !binding.valid) throw new Error('This motion resource is disposed.')
|
|
274
316
|
const value = Reflect.get(target, key, target)
|
|
@@ -339,7 +381,12 @@ export function createMotionSurface(): MotionSurface {
|
|
|
339
381
|
}
|
|
340
382
|
const source = (value: MotionReadable, binding: Binding, input = false) => {
|
|
341
383
|
const state = values.get(value)
|
|
342
|
-
if (
|
|
384
|
+
if (
|
|
385
|
+
!state ||
|
|
386
|
+
state.binding.context !== binding.context ||
|
|
387
|
+
value.disposed ||
|
|
388
|
+
(input && !state.input)
|
|
389
|
+
)
|
|
343
390
|
throw new TypeError('Use a live value from this surface and presentation.')
|
|
344
391
|
return state
|
|
345
392
|
}
|
|
@@ -361,6 +408,59 @@ export function createMotionSurface(): MotionSurface {
|
|
|
361
408
|
},
|
|
362
409
|
animate: (values, options) => run((adapter) => adapter.animate(values, options)),
|
|
363
410
|
set: (values) => run((adapter) => adapter.set(values)),
|
|
411
|
+
use(setup) {
|
|
412
|
+
return surface.onAvailable(() => {
|
|
413
|
+
const binding = current()
|
|
414
|
+
const start = (adapter: MotionSurfaceAdapter) => {
|
|
415
|
+
const child = createMotionSurface()
|
|
416
|
+
const unbind = bindMotionSurface(child, adapter, binding.context)
|
|
417
|
+
let cleanup: void | (() => void)
|
|
418
|
+
try {
|
|
419
|
+
cleanup = setup(child)
|
|
420
|
+
if (cleanup !== undefined && typeof cleanup !== 'function')
|
|
421
|
+
throw new TypeError('Motion setup must return a cleanup function, not a promise.')
|
|
422
|
+
} catch (error) {
|
|
423
|
+
unbind()
|
|
424
|
+
throw error
|
|
425
|
+
}
|
|
426
|
+
return () => {
|
|
427
|
+
try {
|
|
428
|
+
cleanup?.()
|
|
429
|
+
} finally {
|
|
430
|
+
unbind()
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return binding.adapter.use?.(start) ?? start(binding.adapter)
|
|
435
|
+
})
|
|
436
|
+
},
|
|
437
|
+
pointerFollow(region, options = {}) {
|
|
438
|
+
const binding = current()
|
|
439
|
+
validatePointer(options)
|
|
440
|
+
const state = regionState(region)
|
|
441
|
+
if (!binding.adapter.pointerFollow) throw new Error('Pointer input is unavailable.')
|
|
442
|
+
const raw = binding.adapter.pointerFollow(state.adapter, options)
|
|
443
|
+
const x = ownValue(binding, raw.x, false)
|
|
444
|
+
const y = ownValue(binding, raw.y, false)
|
|
445
|
+
const control = own(binding, {
|
|
446
|
+
x,
|
|
447
|
+
y,
|
|
448
|
+
execution: raw.execution,
|
|
449
|
+
get active() {
|
|
450
|
+
return raw.active
|
|
451
|
+
},
|
|
452
|
+
update: (state: { disabled: boolean }) => raw.update(state),
|
|
453
|
+
refresh: () => raw.refresh(),
|
|
454
|
+
dispose() {
|
|
455
|
+
state.resources.delete(control)
|
|
456
|
+
x.dispose()
|
|
457
|
+
y.dispose()
|
|
458
|
+
raw.dispose()
|
|
459
|
+
},
|
|
460
|
+
})
|
|
461
|
+
state.resources.add(control)
|
|
462
|
+
return control
|
|
463
|
+
},
|
|
364
464
|
onAvailable(setup) {
|
|
365
465
|
let cleanup: (() => void) | undefined
|
|
366
466
|
const invoke = () => {
|
|
@@ -382,20 +482,31 @@ export function createMotionSurface(): MotionSurface {
|
|
|
382
482
|
cleanup = () => {
|
|
383
483
|
if (disposed) return
|
|
384
484
|
disposed = true
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
485
|
+
binding.cleanup.delete(cleanup!)
|
|
486
|
+
const errors: unknown[] = []
|
|
487
|
+
for (const dispose of [
|
|
488
|
+
() => release?.(),
|
|
489
|
+
...resources.reverse().map((resource) => () => resource.dispose()),
|
|
490
|
+
...playback.map((handle) => () => {
|
|
491
|
+
if (binding.active.has(handle)) handle.stop()
|
|
492
|
+
}),
|
|
493
|
+
]) {
|
|
494
|
+
try {
|
|
495
|
+
dispose()
|
|
496
|
+
} catch (error) {
|
|
497
|
+
errors.push(error)
|
|
498
|
+
}
|
|
391
499
|
}
|
|
500
|
+
cleanup = undefined
|
|
501
|
+
if (errors.length) throw new AggregateError(errors, 'Motion setup cleanup failed.')
|
|
392
502
|
}
|
|
503
|
+
binding.cleanup.add(cleanup)
|
|
393
504
|
return cleanup
|
|
394
505
|
}
|
|
395
506
|
setups.get(surface)!.add(invoke)
|
|
396
507
|
if (surface.available) {
|
|
397
508
|
try {
|
|
398
|
-
|
|
509
|
+
invoke()
|
|
399
510
|
} catch (error) {
|
|
400
511
|
setups.get(surface)!.delete(invoke)
|
|
401
512
|
throw error
|
|
@@ -471,23 +582,54 @@ export function createMotionSurface(): MotionSurface {
|
|
|
471
582
|
export function bindMotionSurface(
|
|
472
583
|
surface: MotionSurface,
|
|
473
584
|
adapter: MotionSurfaceAdapter,
|
|
585
|
+
context: object = {},
|
|
474
586
|
): () => void {
|
|
475
587
|
if (!bindings.has(surface)) throw new TypeError('Use createMotionSurface() to declare a surface.')
|
|
476
588
|
const invalidate = (binding: Binding) => {
|
|
477
589
|
binding.valid = false
|
|
478
|
-
|
|
590
|
+
const errors: unknown[] = []
|
|
591
|
+
for (const cleanup of [...binding.cleanup].reverse()) {
|
|
592
|
+
try {
|
|
593
|
+
cleanup()
|
|
594
|
+
} catch (error) {
|
|
595
|
+
errors.push(error)
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
binding.cleanup.clear()
|
|
599
|
+
for (const playback of binding.active) {
|
|
600
|
+
try {
|
|
601
|
+
playback.stop()
|
|
602
|
+
} catch (error) {
|
|
603
|
+
errors.push(error)
|
|
604
|
+
}
|
|
605
|
+
}
|
|
479
606
|
binding.active.clear()
|
|
480
|
-
for (const resource of [...binding.resources].reverse())
|
|
607
|
+
for (const resource of [...binding.resources].reverse()) {
|
|
608
|
+
try {
|
|
609
|
+
resource.dispose()
|
|
610
|
+
} catch (error) {
|
|
611
|
+
errors.push(error)
|
|
612
|
+
}
|
|
613
|
+
}
|
|
481
614
|
binding.resources.clear()
|
|
615
|
+
if (errors.length) throw new AggregateError(errors, 'Motion cleanup failed.')
|
|
482
616
|
}
|
|
483
617
|
const previous = bindings.get(surface)
|
|
484
618
|
if (previous) invalidate(previous)
|
|
485
|
-
const binding: Binding = {
|
|
619
|
+
const binding: Binding = {
|
|
620
|
+
adapter,
|
|
621
|
+
active: new Set(),
|
|
622
|
+
resources: new Set(),
|
|
623
|
+
cleanup: new Set(),
|
|
624
|
+
valid: true,
|
|
625
|
+
context,
|
|
626
|
+
}
|
|
486
627
|
bindings.set(surface, binding)
|
|
487
628
|
try {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
629
|
+
// A setup can register a child while the surface is already available. That
|
|
630
|
+
// child starts immediately; a live Set iteration would run it a second time.
|
|
631
|
+
for (const setup of [...setups.get(surface)!]) {
|
|
632
|
+
setup()
|
|
491
633
|
}
|
|
492
634
|
} catch (error) {
|
|
493
635
|
invalidate(binding)
|
package/src/files.ts
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/** Serializable metadata only. Local handles and previews never enter this descriptor. */
|
|
2
|
+
export interface FileItem {
|
|
3
|
+
readonly id: string
|
|
4
|
+
readonly name: string
|
|
5
|
+
readonly size?: number
|
|
6
|
+
readonly type?: string
|
|
7
|
+
readonly status: 'selected' | 'uploading' | 'uploaded' | 'failed' | 'cancelled'
|
|
8
|
+
/** A fraction from zero to one; absent means the total is unknown. */
|
|
9
|
+
readonly progress?: number
|
|
10
|
+
readonly error?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Platform-owned, session-only identity. Use a platform accessor inside host transport. */
|
|
14
|
+
export interface FileHandle {
|
|
15
|
+
readonly __mavisFileHandle: never
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface FileCancellation {
|
|
19
|
+
readonly cancelled: boolean
|
|
20
|
+
onCancel(callback: () => void): () => void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SelectedFile {
|
|
24
|
+
readonly name: string
|
|
25
|
+
readonly size?: number
|
|
26
|
+
readonly type?: string
|
|
27
|
+
readonly handle: FileHandle
|
|
28
|
+
/** Renderer-specific image source, owned by this resource. Failure keeps the file. */
|
|
29
|
+
preview?(): Promise<unknown>
|
|
30
|
+
release(): void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface FileSelectionRequest {
|
|
34
|
+
readonly multiple: boolean
|
|
35
|
+
readonly accept: readonly string[]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface FilePickerAdapter {
|
|
39
|
+
/** Must be called directly from user activation, before any asynchronous work. */
|
|
40
|
+
pick(
|
|
41
|
+
request: FileSelectionRequest,
|
|
42
|
+
cancellation: FileCancellation,
|
|
43
|
+
): Promise<readonly SelectedFile[] | null>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface FileUploadContext {
|
|
47
|
+
readonly item: FileItem
|
|
48
|
+
readonly handle: FileHandle
|
|
49
|
+
readonly cancellation: FileCancellation
|
|
50
|
+
/** Omit total when unknown; a reported fraction never completes an upload. */
|
|
51
|
+
progress(loaded: number, total?: number): void
|
|
52
|
+
}
|
|
53
|
+
export type FileUpload = (context: FileUploadContext) => Promise<void>
|
|
54
|
+
|
|
55
|
+
export interface FileSelectionOptions {
|
|
56
|
+
/** Metadata from the host, including SSR. Initial entries have no local resource. */
|
|
57
|
+
initial?: readonly FileItem[]
|
|
58
|
+
multiple?: boolean
|
|
59
|
+
/** Extensions (.pdf), exact MIME types, or MIME families (image/*). */
|
|
60
|
+
accept?: readonly string[]
|
|
61
|
+
maxCount?: number
|
|
62
|
+
/** Per-file limit in bytes. Unknown sizes cannot be checked locally. */
|
|
63
|
+
maxSize?: number
|
|
64
|
+
upload?: FileUpload
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type FileSelectionErrorCode = 'picker' | 'type' | 'size' | 'count'
|
|
68
|
+
export interface FileSelectionState {
|
|
69
|
+
readonly items: readonly FileItem[]
|
|
70
|
+
readonly picking: boolean
|
|
71
|
+
readonly error?: { readonly code: FileSelectionErrorCode; readonly message: string }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface FileSelectionController {
|
|
75
|
+
readonly state: FileSelectionState
|
|
76
|
+
readonly canUpload: boolean
|
|
77
|
+
subscribe(listener: (state: FileSelectionState) => void): () => void
|
|
78
|
+
select(picker: FilePickerAdapter): Promise<void>
|
|
79
|
+
cancelSelection(): void
|
|
80
|
+
hasFile(id: string): boolean
|
|
81
|
+
preview(id: string): unknown
|
|
82
|
+
discardPreview(id: string): void
|
|
83
|
+
upload(id: string): Promise<void>
|
|
84
|
+
retry(id: string): Promise<void>
|
|
85
|
+
cancel(id: string): void
|
|
86
|
+
remove(id: string): void
|
|
87
|
+
clear(): void
|
|
88
|
+
/** Cancels active operations, retaining selected files for an explicit retry. */
|
|
89
|
+
pause(): void
|
|
90
|
+
/** Idempotent. Cancels work and releases every locally owned resource. */
|
|
91
|
+
dispose(): void
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function cancellation() {
|
|
95
|
+
let cancelled = false
|
|
96
|
+
const callbacks = new Set<() => void>()
|
|
97
|
+
const token: FileCancellation = {
|
|
98
|
+
get cancelled() {
|
|
99
|
+
return cancelled
|
|
100
|
+
},
|
|
101
|
+
onCancel(callback) {
|
|
102
|
+
if (cancelled) callback()
|
|
103
|
+
else callbacks.add(callback)
|
|
104
|
+
return () => {
|
|
105
|
+
callbacks.delete(callback)
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
token,
|
|
111
|
+
cancel() {
|
|
112
|
+
if (cancelled) return
|
|
113
|
+
cancelled = true
|
|
114
|
+
const pending = [...callbacks]
|
|
115
|
+
callbacks.clear()
|
|
116
|
+
for (const callback of pending) {
|
|
117
|
+
// A broken host abort callback must not retain the other resources.
|
|
118
|
+
try {
|
|
119
|
+
callback()
|
|
120
|
+
} catch {
|
|
121
|
+
/* cancellation remains authoritative */
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function release(file: SelectedFile) {
|
|
129
|
+
try {
|
|
130
|
+
file.release()
|
|
131
|
+
} catch {
|
|
132
|
+
/* release the remaining resources as well */
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function acceptsFile(file: Pick<FileItem, 'name' | 'type'>, accept: readonly string[]) {
|
|
137
|
+
if (!accept.length) return true
|
|
138
|
+
const name = file.name.toLowerCase()
|
|
139
|
+
const type = file.type?.toLowerCase()
|
|
140
|
+
return accept.some((hint) => {
|
|
141
|
+
const value = hint.toLowerCase()
|
|
142
|
+
if (value.startsWith('.')) return name.endsWith(value)
|
|
143
|
+
if (value.endsWith('/*')) return Boolean(type?.startsWith(value.slice(0, -1)))
|
|
144
|
+
return type === value
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Pure controller: no DOM, native APIs, timers, request globals, or automatic transfers. */
|
|
149
|
+
export function createFileSelection(options: FileSelectionOptions = {}): FileSelectionController {
|
|
150
|
+
const multiple = options.multiple ?? false
|
|
151
|
+
const accept = Object.freeze([...(options.accept ?? [])])
|
|
152
|
+
const maxCount = options.maxCount ?? (multiple ? 10 : 1)
|
|
153
|
+
const maxSize = options.maxSize
|
|
154
|
+
if (!Number.isInteger(maxCount) || maxCount < 1)
|
|
155
|
+
throw new RangeError('File maxCount must be a positive integer')
|
|
156
|
+
if (maxSize !== undefined && (!Number.isFinite(maxSize) || maxSize < 0))
|
|
157
|
+
throw new RangeError('File maxSize must be finite and nonnegative')
|
|
158
|
+
if (accept.some((value) => !/^(\.[\w.-]+|[\w!#$&^.+-]+\/(?:[\w!#$&^.+-]+|\*))$/.test(value)))
|
|
159
|
+
throw new TypeError('File accept must contain extensions or MIME types')
|
|
160
|
+
let nextId = 0
|
|
161
|
+
let disposed = false
|
|
162
|
+
let picking: ReturnType<typeof cancellation> | undefined
|
|
163
|
+
const listeners = new Set<(state: FileSelectionState) => void>()
|
|
164
|
+
const resources = new Map<string, SelectedFile>()
|
|
165
|
+
const previews = new Map<string, unknown>()
|
|
166
|
+
const operations = new Map<string, ReturnType<typeof cancellation>>()
|
|
167
|
+
const ids = new Set<string>()
|
|
168
|
+
const initial = (options.initial ?? []).map((item) => {
|
|
169
|
+
if (!item.id || ids.has(item.id) || !item.name.trim())
|
|
170
|
+
throw new TypeError('File descriptors need unique IDs and readable names')
|
|
171
|
+
if (item.size !== undefined && (!Number.isFinite(item.size) || item.size < 0))
|
|
172
|
+
throw new RangeError('File size must be finite and nonnegative')
|
|
173
|
+
if (!['selected', 'uploading', 'uploaded', 'failed', 'cancelled'].includes(item.status))
|
|
174
|
+
throw new TypeError('Invalid file status')
|
|
175
|
+
if (
|
|
176
|
+
item.progress !== undefined &&
|
|
177
|
+
(!Number.isFinite(item.progress) || item.progress < 0 || item.progress > 1)
|
|
178
|
+
)
|
|
179
|
+
throw new RangeError('File progress must be between zero and one')
|
|
180
|
+
ids.add(item.id)
|
|
181
|
+
// Copy only portable fields; do not retain extra properties from a host object.
|
|
182
|
+
return Object.freeze({
|
|
183
|
+
id: item.id,
|
|
184
|
+
name: item.name,
|
|
185
|
+
size: item.size,
|
|
186
|
+
type: item.type,
|
|
187
|
+
status: item.status,
|
|
188
|
+
progress: item.progress,
|
|
189
|
+
error: item.error,
|
|
190
|
+
})
|
|
191
|
+
})
|
|
192
|
+
if (initial.length > (multiple ? maxCount : 1))
|
|
193
|
+
throw new RangeError('Initial files exceed the selection count')
|
|
194
|
+
let state: FileSelectionState = Object.freeze({ items: Object.freeze(initial), picking: false })
|
|
195
|
+
|
|
196
|
+
function publish(patch: Partial<FileSelectionState> = {}) {
|
|
197
|
+
if (disposed) return
|
|
198
|
+
state = Object.freeze({ ...state, ...patch })
|
|
199
|
+
for (const listener of [...listeners]) listener(state)
|
|
200
|
+
}
|
|
201
|
+
function update(id: string, patch: Partial<FileItem>) {
|
|
202
|
+
publish({
|
|
203
|
+
items: Object.freeze(
|
|
204
|
+
state.items.map((item) => (item.id === id ? Object.freeze({ ...item, ...patch }) : item)),
|
|
205
|
+
),
|
|
206
|
+
})
|
|
207
|
+
}
|
|
208
|
+
function cancel(id: string) {
|
|
209
|
+
const operation = operations.get(id)
|
|
210
|
+
if (!operation) return
|
|
211
|
+
operations.delete(id)
|
|
212
|
+
operation.cancel()
|
|
213
|
+
update(id, { status: 'cancelled', progress: undefined, error: undefined })
|
|
214
|
+
}
|
|
215
|
+
function forget(id: string) {
|
|
216
|
+
cancel(id)
|
|
217
|
+
const file = resources.get(id)
|
|
218
|
+
resources.delete(id)
|
|
219
|
+
previews.delete(id)
|
|
220
|
+
if (file) release(file)
|
|
221
|
+
}
|
|
222
|
+
function cancelSelection() {
|
|
223
|
+
const previous = picking
|
|
224
|
+
picking = undefined
|
|
225
|
+
previous?.cancel()
|
|
226
|
+
if (previous) publish({ picking: false })
|
|
227
|
+
}
|
|
228
|
+
async function upload(id: string) {
|
|
229
|
+
const file = resources.get(id)
|
|
230
|
+
const item = state.items.find((entry) => entry.id === id)
|
|
231
|
+
if (
|
|
232
|
+
disposed ||
|
|
233
|
+
!file ||
|
|
234
|
+
!item ||
|
|
235
|
+
!options.upload ||
|
|
236
|
+
operations.has(id) ||
|
|
237
|
+
item.status === 'uploaded'
|
|
238
|
+
)
|
|
239
|
+
return
|
|
240
|
+
const operation = cancellation()
|
|
241
|
+
operations.set(id, operation)
|
|
242
|
+
const current = () =>
|
|
243
|
+
!disposed && operations.get(id) === operation && resources.get(id) === file
|
|
244
|
+
update(id, { status: 'uploading', progress: undefined, error: undefined })
|
|
245
|
+
try {
|
|
246
|
+
await options.upload({
|
|
247
|
+
item,
|
|
248
|
+
handle: file.handle,
|
|
249
|
+
cancellation: operation.token,
|
|
250
|
+
progress(loaded, total) {
|
|
251
|
+
if (!current()) return
|
|
252
|
+
if (
|
|
253
|
+
!Number.isFinite(loaded) ||
|
|
254
|
+
loaded < 0 ||
|
|
255
|
+
(total !== undefined && (!Number.isFinite(total) || total <= 0))
|
|
256
|
+
)
|
|
257
|
+
throw new RangeError(
|
|
258
|
+
'Upload progress needs nonnegative loaded bytes and a positive total',
|
|
259
|
+
)
|
|
260
|
+
update(id, { progress: total === undefined ? undefined : Math.min(1, loaded / total) })
|
|
261
|
+
},
|
|
262
|
+
})
|
|
263
|
+
if (current()) update(id, { status: 'uploaded', progress: 1, error: undefined })
|
|
264
|
+
} catch (error) {
|
|
265
|
+
if (current())
|
|
266
|
+
update(id, {
|
|
267
|
+
status: 'failed',
|
|
268
|
+
error: error instanceof Error ? error.message : 'Upload failed',
|
|
269
|
+
})
|
|
270
|
+
} finally {
|
|
271
|
+
if (operations.get(id) === operation) operations.delete(id)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const controller: FileSelectionController = {
|
|
275
|
+
get state() {
|
|
276
|
+
return state
|
|
277
|
+
},
|
|
278
|
+
get canUpload() {
|
|
279
|
+
return Boolean(options.upload)
|
|
280
|
+
},
|
|
281
|
+
subscribe(listener) {
|
|
282
|
+
if (!disposed) listeners.add(listener)
|
|
283
|
+
return () => {
|
|
284
|
+
listeners.delete(listener)
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
async select(picker) {
|
|
288
|
+
if (disposed) return
|
|
289
|
+
cancelSelection()
|
|
290
|
+
const operation = cancellation()
|
|
291
|
+
picking = operation
|
|
292
|
+
publish({ picking: true, error: undefined })
|
|
293
|
+
let files: readonly SelectedFile[] | null = null
|
|
294
|
+
let retained = false
|
|
295
|
+
try {
|
|
296
|
+
// Keep the actual picker launch in the same user activation stack.
|
|
297
|
+
files = await picker.pick({ multiple, accept }, operation.token)
|
|
298
|
+
if (disposed || picking !== operation || operation.token.cancelled || !files?.length) return
|
|
299
|
+
const count = multiple ? state.items.length + files.length : files.length
|
|
300
|
+
let error: FileSelectionState['error']
|
|
301
|
+
if (count > (multiple ? maxCount : 1))
|
|
302
|
+
error = { code: 'count', message: `Choose at most ${multiple ? maxCount : 1} files` }
|
|
303
|
+
else if (files.some((file) => !file.name.trim() || !acceptsFile(file, accept)))
|
|
304
|
+
error = { code: 'type', message: 'This file type is not accepted' }
|
|
305
|
+
else if (
|
|
306
|
+
files.some(
|
|
307
|
+
(file) =>
|
|
308
|
+
file.size !== undefined &&
|
|
309
|
+
(!Number.isFinite(file.size) ||
|
|
310
|
+
file.size < 0 ||
|
|
311
|
+
(maxSize !== undefined && file.size > maxSize)),
|
|
312
|
+
)
|
|
313
|
+
)
|
|
314
|
+
error = { code: 'size', message: 'A file exceeds the size limit' }
|
|
315
|
+
if (error) {
|
|
316
|
+
publish({ error })
|
|
317
|
+
return
|
|
318
|
+
}
|
|
319
|
+
if (!multiple) for (const item of state.items) forget(item.id)
|
|
320
|
+
const additions = files.map((file) => {
|
|
321
|
+
let id: string
|
|
322
|
+
do {
|
|
323
|
+
id = `file-${++nextId}`
|
|
324
|
+
} while (ids.has(id))
|
|
325
|
+
ids.add(id)
|
|
326
|
+
resources.set(id, file)
|
|
327
|
+
return Object.freeze({
|
|
328
|
+
id,
|
|
329
|
+
name: file.name,
|
|
330
|
+
size: file.size,
|
|
331
|
+
type: file.type,
|
|
332
|
+
status: 'selected' as const,
|
|
333
|
+
})
|
|
334
|
+
})
|
|
335
|
+
retained = true
|
|
336
|
+
publish({ items: Object.freeze(multiple ? [...state.items, ...additions] : additions) })
|
|
337
|
+
for (const item of additions) {
|
|
338
|
+
const file = resources.get(item.id)!
|
|
339
|
+
if (file.preview)
|
|
340
|
+
void Promise.resolve()
|
|
341
|
+
.then(() =>
|
|
342
|
+
!disposed && resources.get(item.id) === file ? file.preview!() : undefined,
|
|
343
|
+
)
|
|
344
|
+
.then((preview) => {
|
|
345
|
+
if (!disposed && resources.get(item.id) === file && preview != null) {
|
|
346
|
+
previews.set(item.id, preview)
|
|
347
|
+
publish()
|
|
348
|
+
}
|
|
349
|
+
})
|
|
350
|
+
.catch(() => {
|
|
351
|
+
/* Preview failure never rejects an otherwise usable file. */
|
|
352
|
+
})
|
|
353
|
+
}
|
|
354
|
+
} catch (error) {
|
|
355
|
+
if (!disposed && picking === operation && !operation.token.cancelled)
|
|
356
|
+
publish({
|
|
357
|
+
error: {
|
|
358
|
+
code: 'picker',
|
|
359
|
+
message: error instanceof Error ? error.message : 'Unable to select files',
|
|
360
|
+
},
|
|
361
|
+
})
|
|
362
|
+
} finally {
|
|
363
|
+
if (!retained && files) for (const file of files) release(file)
|
|
364
|
+
if (picking === operation) {
|
|
365
|
+
picking = undefined
|
|
366
|
+
publish({ picking: false })
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
},
|
|
370
|
+
cancelSelection,
|
|
371
|
+
hasFile: (id) => resources.has(id),
|
|
372
|
+
preview: (id) => previews.get(id),
|
|
373
|
+
discardPreview(id) {
|
|
374
|
+
if (previews.delete(id)) publish()
|
|
375
|
+
},
|
|
376
|
+
upload,
|
|
377
|
+
retry: upload,
|
|
378
|
+
cancel,
|
|
379
|
+
remove(id) {
|
|
380
|
+
if (disposed) return
|
|
381
|
+
forget(id)
|
|
382
|
+
publish({
|
|
383
|
+
items: Object.freeze(state.items.filter((item) => item.id !== id)),
|
|
384
|
+
error: undefined,
|
|
385
|
+
})
|
|
386
|
+
},
|
|
387
|
+
clear() {
|
|
388
|
+
cancelSelection()
|
|
389
|
+
for (const item of state.items) forget(item.id)
|
|
390
|
+
publish({ items: Object.freeze([]), error: undefined })
|
|
391
|
+
},
|
|
392
|
+
pause() {
|
|
393
|
+
cancelSelection()
|
|
394
|
+
for (const id of [...operations.keys()]) cancel(id)
|
|
395
|
+
},
|
|
396
|
+
dispose() {
|
|
397
|
+
if (disposed) return
|
|
398
|
+
disposed = true
|
|
399
|
+
cancelSelection()
|
|
400
|
+
for (const id of [...operations.keys()]) cancel(id)
|
|
401
|
+
for (const file of resources.values()) release(file)
|
|
402
|
+
resources.clear()
|
|
403
|
+
previews.clear()
|
|
404
|
+
listeners.clear()
|
|
405
|
+
},
|
|
406
|
+
}
|
|
407
|
+
return Object.freeze(controller)
|
|
408
|
+
}
|