@ac-kit/browser 0.3.0 → 0.4.1

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.
Files changed (78) hide show
  1. package/CHANGELOG.md +146 -0
  2. package/README.md +171 -10
  3. package/dist/dom/drag-and-drop/file-drop-zone.d.ts +71 -0
  4. package/dist/dom/drag-and-drop/file-drop-zone.d.ts.map +1 -0
  5. package/dist/dom/drag-and-drop/file-drop-zone.js +145 -0
  6. package/dist/dom/drag-and-drop/file-drop-zone.js.map +1 -0
  7. package/dist/dom/forms/get-form-input.d.ts +17 -0
  8. package/dist/dom/forms/get-form-input.d.ts.map +1 -0
  9. package/dist/dom/forms/get-form-input.js +35 -0
  10. package/dist/dom/forms/get-form-input.js.map +1 -0
  11. package/dist/dom/index.d.ts +3 -0
  12. package/dist/dom/index.d.ts.map +1 -1
  13. package/dist/dom/index.js +3 -0
  14. package/dist/dom/index.js.map +1 -1
  15. package/dist/dom/user-activation/user-activation.d.ts +54 -0
  16. package/dist/dom/user-activation/user-activation.d.ts.map +1 -0
  17. package/dist/dom/user-activation/user-activation.js +93 -0
  18. package/dist/dom/user-activation/user-activation.js.map +1 -0
  19. package/dist/index.d.ts +3 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +3 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/indexeddb/open-indexed-db.d.ts.map +1 -1
  24. package/dist/indexeddb/open-indexed-db.js +1 -0
  25. package/dist/indexeddb/open-indexed-db.js.map +1 -1
  26. package/dist/permissions/_untracked-permission-watch.d.ts +4 -0
  27. package/dist/permissions/_untracked-permission-watch.d.ts.map +1 -0
  28. package/dist/permissions/_untracked-permission-watch.js +5 -0
  29. package/dist/permissions/_untracked-permission-watch.js.map +1 -0
  30. package/dist/permissions/watch-permission.d.ts +38 -0
  31. package/dist/permissions/watch-permission.d.ts.map +1 -0
  32. package/dist/permissions/watch-permission.js +58 -0
  33. package/dist/permissions/watch-permission.js.map +1 -0
  34. package/dist/temporal/enable-temporal.d.ts +44 -0
  35. package/dist/temporal/enable-temporal.d.ts.map +1 -0
  36. package/dist/temporal/enable-temporal.js +70 -0
  37. package/dist/temporal/enable-temporal.js.map +1 -0
  38. package/dist/testing/fake-audio-context.js +2 -2
  39. package/dist/testing/fake-audio-context.js.map +1 -1
  40. package/dist/testing/fake-match-media.js +1 -1
  41. package/dist/testing/fake-match-media.js.map +1 -1
  42. package/dist/testing/fake-media-devices.js +2 -2
  43. package/dist/testing/fake-media-devices.js.map +1 -1
  44. package/dist/testing/fake-permissions.d.ts +48 -0
  45. package/dist/testing/fake-permissions.d.ts.map +1 -0
  46. package/dist/testing/fake-permissions.js +90 -0
  47. package/dist/testing/fake-permissions.js.map +1 -0
  48. package/dist/testing/fake-web-midi.d.ts +26 -3
  49. package/dist/testing/fake-web-midi.d.ts.map +1 -1
  50. package/dist/testing/fake-web-midi.js +62 -2
  51. package/dist/testing/fake-web-midi.js.map +1 -1
  52. package/dist/testing/index.d.ts +1 -0
  53. package/dist/testing/index.d.ts.map +1 -1
  54. package/dist/testing/index.js +1 -0
  55. package/dist/testing/index.js.map +1 -1
  56. package/dist/web-audio/audio-clock-mapping.d.ts +36 -14
  57. package/dist/web-audio/audio-clock-mapping.d.ts.map +1 -1
  58. package/dist/web-audio/audio-clock-mapping.js +147 -35
  59. package/dist/web-audio/audio-clock-mapping.js.map +1 -1
  60. package/dist/web-midi/_midi-port.d.ts +4 -0
  61. package/dist/web-midi/_midi-port.d.ts.map +1 -0
  62. package/dist/web-midi/_midi-port.js +12 -0
  63. package/dist/web-midi/_midi-port.js.map +1 -0
  64. package/dist/web-midi/web-midi-access.d.ts +20 -0
  65. package/dist/web-midi/web-midi-access.d.ts.map +1 -1
  66. package/dist/web-midi/web-midi-access.js +31 -0
  67. package/dist/web-midi/web-midi-access.js.map +1 -1
  68. package/dist/web-midi/web-midi-inputs.d.ts.map +1 -1
  69. package/dist/web-midi/web-midi-inputs.js +1 -11
  70. package/dist/web-midi/web-midi-inputs.js.map +1 -1
  71. package/dist/web-midi/web-midi-pinned-input.d.ts +124 -0
  72. package/dist/web-midi/web-midi-pinned-input.d.ts.map +1 -0
  73. package/dist/web-midi/web-midi-pinned-input.js +177 -0
  74. package/dist/web-midi/web-midi-pinned-input.js.map +1 -0
  75. package/dist/xhr/send-with-progress.d.ts.map +1 -1
  76. package/dist/xhr/send-with-progress.js +3 -0
  77. package/dist/xhr/send-with-progress.js.map +1 -1
  78. package/package.json +19 -8
package/CHANGELOG.md CHANGED
@@ -1,5 +1,151 @@
1
1
  # @ac-kit/browser
2
2
 
3
+ ## 0.4.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [5c203e7]
8
+ - Updated dependencies [5c203e7]
9
+ - Updated dependencies [5c203e7]
10
+ - Updated dependencies [5c203e7]
11
+ - Updated dependencies [5c203e7]
12
+ - Updated dependencies [5c203e7]
13
+ - @ac-kit/async@0.9.0
14
+ - @ac-kit/core@0.14.0
15
+
16
+ ## 0.4.0
17
+
18
+ ### Minor Changes
19
+
20
+ - 0b4fe43: `AudioClockMapping` takes a step up in the offset under jitter. A step up is
21
+ now taken from a steady run of any length of at least `readyTicks` ticks, each
22
+ more than `jumpMs` above the least offset of the ticks before it, whose floor
23
+ is flat: the lower convex hull of the run's offsets falls by at most
24
+ `steadyMs / 4` per `readyTicks − 1` ticks over the run's middle half, and the
25
+ least offset of its later half is at most `steadyMs` above its earlier half's.
26
+ Ticks may jitter above that floor: the run then has to be long enough that the
27
+ least offset of half of it, for the jitter width the run and the ticks before it
28
+ show, lies on average within half the allowed fall. Clean ticks still give a
29
+ step in `readyTicks` ticks; ticks jittering by 8 ms or more, which never made a
30
+ step before and kept the old minimum until it left the window, now give one —
31
+ about 15 ticks at 8 ms, a tick every 50 ms and `steadyMs` 5. A backlog draining
32
+ after a freeze and jank are still refused.
33
+
34
+ Migration: `steadyMs` no longer bounds the spread of the run's offsets; it bounds
35
+ how far the floor of the run may move. A step whose ticks spread by more than
36
+ `steadyMs` above a flat floor, refused before, is taken now; a backlog whose
37
+ floor falls between `steadyMs / 4` and `steadyMs / 2` per `readyTicks − 1`
38
+ ticks, taken before when clean, is refused.
39
+ - 0b4fe43: New in `@ac-kit/browser/dom`:
40
+
41
+ - `bindFileDropZone(element, { onDrop, onActiveChange?, dropEffect?, signal? })`
42
+ makes an element take files dropped on it. A drag carrying files
43
+ (`dataTransfer.types` includes `"Files"`) over the element or any node inside
44
+ it is accepted with the cursor showing `dropEffect` (`"copy"` by default),
45
+ and the drop calls `onDrop(files, event)` (never with no file) with the page
46
+ kept; other drags are left alone. `onActiveChange(isActive)` tells when a
47
+ drag of files comes over the zone and when it leaves, is dropped or is given
48
+ up; moving over the zone's own nodes keeps it active, whether or not
49
+ `dragleave` names a `relatedTarget` (Safari does not) and when the node under
50
+ the drag leaves the document. The returned `FileDropZone` reads `isActive`;
51
+ disposing it, or aborting `signal`, stops the binding without a call.
52
+ - `preventFileDropNavigation(window, { signal? })` keeps a file dropped where
53
+ no zone takes it from replacing the page: such a drag shows the "no drop"
54
+ cursor and its drop is cancelled, while a zone's own drop effect is kept.
55
+ - `isFileDrag(event)` tells whether a drag event carries files, narrowing it
56
+ to `FileDragEvent`.
57
+ - `bindUserActivation(target, { onActivation, once?, navigator?, signal? })`
58
+ calls `onActivation(event)` synchronously inside each event that gives the
59
+ page a user activation, listening in the capture phase and passively, so a
60
+ handler stopping or cancelling the event hides nothing. Given `navigator`, it
61
+ calls `onActivation(undefined)` at once when `navigator.userActivation`
62
+ reports a transient activation. `isActivationTriggeringEvent(event)` is the
63
+ HTML standard's definition: a trusted `keydown` other than Escape,
64
+ `mousedown`, a mouse's `pointerdown`, another pointer's `pointerup`, or
65
+ `touchend`.
66
+ - `getFormInput(form.elements, name)` returns the named `<input>` of a form's
67
+ controls and throws a `TypeError` for a missing name, a name several controls
68
+ share, or another kind of control; it recognises an `<input>` by tag and
69
+ namespace, so forms of other windows work too.
70
+
71
+ New at the root:
72
+
73
+ - `enableTemporal(scope, { load, onWarning })` makes `scope.Temporal`
74
+ (`globalThis`, or a worker's `self`) a complete Temporal: a native one is
75
+ kept; when it is missing or lacks `ZonedDateTime.prototype.getTimeZoneTransition`,
76
+ the `Temporal` of the module `load()` resolves to (such as
77
+ `import("temporal-polyfill/implementation")`) is installed as a writable,
78
+ configurable, non-enumerable property. `load` runs only then. It resolves to
79
+ whether Temporal is complete afterwards; when not, `onWarning(message, cause)`
80
+ was told why (the module failed to load, is not a complete Temporal, or the
81
+ scope refused it). Overlapping calls install a single fallback.
82
+
83
+ The package gains `yarn test:browser`, its real-browser tests.
84
+ - 0b4fe43: Permissions followed, and a Web MIDI input opened and pinned.
85
+
86
+ - `watchPermission(navigator.permissions, descriptor, { onChange?, signal? })`
87
+ follows a permission through the browser's `PermissionStatus` and its
88
+ `change` events: `state` is the current one (the last known once disposed),
89
+ `onChange(state)` hears each change, and disposing the watch, or aborting
90
+ `signal`, stops it. Where the browser cannot query the permission the state
91
+ is `"prompt"` and `isTracked` is `false`. Aborting `signal` before the
92
+ browser answers rejects with its reason.
93
+ - `watchWebMidiPermission(navigator, { sysex?, onChange?, signal? })` is that
94
+ watch for the MIDI permission `queryWebMidiPermission` reads, `"unsupported"`
95
+ (untracked) without Web MIDI.
96
+ - `findWebMidiInput(access, selector)` finds the one connected input of
97
+ `{ id }` or `{ name, manufacturer? }` (any manufacturer when left out):
98
+ `{ status: "found", port }`, `{ status: "missing" }`, or
99
+ `{ status: "ambiguous", ports }`.
100
+ - `openWebMidiInput(access, selector, { signal? })` opens that input with
101
+ `MIDIPort.open()` and resolves to a `WebMidiPinnedInput` bound to that port
102
+ object: `message` events carry each `midimessage`'s bytes and `timeStamp`;
103
+ when the port is unplugged, leaves the list or is closed elsewhere, `lost`
104
+ fires once, `isOpen` becomes `false` and the port is closed — another port
105
+ of the same name is never taken up (`WebMidiInputs` keeps following by
106
+ identity). It rejects, without opening anything it refuses, with
107
+ `WebMidiInputNotFoundError` (no connected match, or the port went while
108
+ opening), `WebMidiInputAmbiguousError` (several matches, in `ports`), or
109
+ `WebMidiInputUnavailableError` (the browser refused to open it; its
110
+ `DOMException` as `cause`). Aborting `signal` rejects at once with its reason
111
+ and closes the port once the browser has opened it; after opening, it
112
+ disposes the input. Disposing closes the port without a `lost` event. It
113
+ takes the browser's `MIDIAccess` (`WebMidiOpenableAccess`).
114
+
115
+ `@ac-kit/browser/testing`:
116
+
117
+ - `FakePermissions` and `FakePermissionStatus`: `navigator.permissions` for the
118
+ names given, each `"prompt"` until `set(descriptor, state)` changes it and
119
+ fires `change` on every status queried for it; other names are refused with a
120
+ `TypeError`; `queries` records the descriptors.
121
+ - `FakeMidiInput` gains `connection`, `open()` (`"open"`, or `"pending"` while
122
+ unplugged), `close()` now setting `"closed"`, `openCount`, `holdOpen()` and a
123
+ one-shot `openFailure`; each connection change fires `statechange` on the
124
+ port and on its `FakeMidiAccess`, as a browser does. `FakeMidiAccess` moves
125
+ an open port to `"pending"` on `unplug` and back to `"open"` on `plug`.
126
+
127
+ ### Patch Changes
128
+
129
+ - Updated dependencies [0b4fe43]
130
+ - Updated dependencies [0b4fe43]
131
+ - Updated dependencies [0b4fe43]
132
+ - Updated dependencies [0b4fe43]
133
+ - Updated dependencies [0b4fe43]
134
+ - Updated dependencies [0b4fe43]
135
+ - Updated dependencies [0b4fe43]
136
+ - Updated dependencies [0b4fe43]
137
+ - Updated dependencies [0b4fe43]
138
+ - Updated dependencies [0b4fe43]
139
+ - Updated dependencies [0b4fe43]
140
+ - Updated dependencies [0b4fe43]
141
+ - Updated dependencies [0b4fe43]
142
+ - Updated dependencies [0b4fe43]
143
+ - Updated dependencies [0b4fe43]
144
+ - Updated dependencies [0b4fe43]
145
+ - Updated dependencies [0b4fe43]
146
+ - @ac-kit/core@0.13.0
147
+ - @ac-kit/async@0.8.1
148
+
3
149
  ## 0.3.0
4
150
 
5
151
  ### Minor Changes
package/README.md CHANGED
@@ -2,14 +2,18 @@
2
2
 
3
3
  Browser extensions and zero-dependency wrappers over browser APIs, the
4
4
  browser's counterpart of `@ac-kit/node`: IndexedDB as promises, with versioned
5
- upgrades; Web MIDI, with access asked for and the input port a person chose
6
- followed by its name across reloads and hot-plugs; the microphone opened for
5
+ upgrades; Web MIDI, with access asked for, its permission followed, the input
6
+ port a person chose followed by its name across reloads and hot-plugs, or one
7
+ port opened and pinned; the microphone opened for
7
8
  analysis, its permission and its devices; an audio clock mapped onto the
8
9
  page's; requests over
9
10
  `XMLHttpRequest` that report their upload progress, which `fetch` cannot; an
10
11
  image's pixels read through a canvas, scaled down; a canvas's backing store
11
12
  kept at its displayed size in device pixels; rendering on animation frames at
12
- most once each; and the wait for an element's CSS transitions to end.
13
+ most once each; the wait for an element's CSS transitions to end; files
14
+ dropped on an element, with the page guarded; what a user activation allows,
15
+ called inside it; a form's inputs read checked; and a Temporal fallback
16
+ installed where the browser lacks one.
13
17
 
14
18
  Every function takes the browser object it works through — `indexedDB`,
15
19
  `navigator`, a `MIDIAccess`, `XMLHttpRequest`, a canvas factory, an element —
@@ -140,6 +144,54 @@ if ((await queryWebMidiPermission(navigator)) !== "unsupported") {
140
144
  - `isSameMidiPort` — whether two port identities (name and manufacturer) are
141
145
  the same device; a port's id changes between sessions, its identity does not.
142
146
 
147
+ ```ts
148
+ import {
149
+ openWebMidiInput,
150
+ requestWebMidiAccess,
151
+ watchWebMidiPermission,
152
+ } from "@ac-kit/browser";
153
+
154
+ // Follow the permission: no snapshot, the user may answer or change it later.
155
+ using permission = await watchWebMidiPermission(navigator, {
156
+ onChange: (state) => showAccessButton(state === "prompt"),
157
+ signal,
158
+ });
159
+ const access = await requestWebMidiAccess(navigator, { software: true });
160
+ // Pin the one port of that name: refused when several match, lost for good
161
+ // when it goes.
162
+ using input = await openWebMidiInput(access, { name: "Bridge" }, { signal });
163
+ input.subscribe("message", (data, time) => relay(data, time));
164
+ input.subscribe("lost", () => fallBackToTheServer());
165
+ ```
166
+
167
+ - `watchWebMidiPermission(navigator, { sysex?, onChange?, signal? })` — the
168
+ permission `queryWebMidiPermission` reads, followed through the browser's
169
+ `PermissionStatus` and its `change` events: `state` is the current one,
170
+ `onChange(state)` hears each change, disposing it (or aborting `signal`)
171
+ stops. `"unsupported"` without Web MIDI and `"prompt"` where the browser
172
+ cannot query it, both with `isTracked: false`, never change. Aborting
173
+ `signal` before the browser answers rejects with its reason.
174
+ - `findWebMidiInput(access, selector)` — the one connected input a selector
175
+ names (`{ id }`, or `{ name, manufacturer? }` with any manufacturer when
176
+ left out): `{ status: "found", port }`, `"missing"`, or `"ambiguous"` with
177
+ every match in `ports`. Disconnected ports never match.
178
+ - `openWebMidiInput(access, selector, { signal? })` — opens that input
179
+ explicitly (`MIDIPort.open()`) and pins it: `message` hands over its bytes
180
+ with their time of arrival, until the port is unplugged, leaves the list or
181
+ is closed by other code, when `lost` is told once and the input is closed
182
+ for good — a port of the same name plugged in later is never taken up, as
183
+ `WebMidiInputs` would. It rejects with `WebMidiInputNotFoundError` (none
184
+ connected, or gone while opening), `WebMidiInputAmbiguousError` (several,
185
+ listed in `ports`: the choice is the person's) or
186
+ `WebMidiInputUnavailableError` (the browser refused to open it, as when
187
+ another application holds it; its `DOMException` as `cause`), without
188
+ opening a port it refuses. Aborting `signal` rejects at once with its reason
189
+ (the port is closed once the browser opened it), or disposes the input once
190
+ open. Disposing it closes the port, telling no loss.
191
+ - `watchPermission(permissions, descriptor, { onChange?, signal? })` — the same
192
+ watch for any permission (`{ name: "microphone" }`, …), from
193
+ `navigator.permissions`.
194
+
143
195
  ## Microphone
144
196
 
145
197
  ```ts
@@ -218,14 +270,20 @@ if (mapping.isReady) {
218
270
  slow drift is followed. It starts again, and is not ready until
219
271
  `readyTicks` more ticks, on `reset()` (a context suspended and resumed, a
220
272
  worklet restarted), when the audio clock goes backwards, on a tick more
221
- than `jumpMs` below the window's least `page − audio`, or on `readyTicks`
222
- ticks in a row more than `jumpMs` above it whose offsets differ by at most `steadyMs` and do not keep
223
- falling (a backlog draining after a freeze does). `toPageMs` and
273
+ than `jumpMs` below the window's least `page − audio`, or on a steady run
274
+ of ticks more than `jumpMs` above it: at least `readyTicks` of them, whose
275
+ floor (the lower convex hull of their offsets) does not fall by
276
+ `steadyMs / 4` per `readyTicks − 1` ticks (a backlog draining after a
277
+ freeze does), and whose later half does not rise `steadyMs` above the
278
+ earlier one. Jitter above the floor is
279
+ tolerated: it lengthens the run needed, from the jitter the run and the
280
+ ticks before it show, so ticks jittering by 8 ms every 50 ms take a step
281
+ in about 15 ticks rather than when the old minimum leaves the window, while
282
+ jank, spread far above its floor, never makes a run. `toPageMs` and
224
283
  `toAudioMs` convert either way once ready, and throw before; `resetCount`
225
284
  counts the restarts. A tick arriving before the previous one, or a time not
226
- finite, throws `RangeError`. The ticks of a step up must all lie in the
227
- window: when `readyTicks` of them do not fit, the window takes the new
228
- offset on its own once the older ticks leave it. It reads no clock: it runs
285
+ finite, throws `RangeError`. A step whose run never gets steady is taken
286
+ once the older ticks leave the window. It reads no clock: it runs
229
287
  in a worker or under Node as well. O(n) per tick and per conversion, n the
230
288
  ticks in the window, held in a ring buffer: a tick allocates nothing.
231
289
 
@@ -429,6 +487,97 @@ signal? })` — renders on animation frames, at most once each: after
429
487
  For a simulation stepped at a fixed rate under the frame rate, see
430
488
  `FixedTimestep` in `@ac-kit/async`.
431
489
 
490
+ ## File drop
491
+
492
+ ```ts
493
+ import {
494
+ bindFileDropZone,
495
+ preventFileDropNavigation,
496
+ } from "@ac-kit/browser/dom";
497
+
498
+ using guard = preventFileDropNavigation(window, { signal });
499
+ using zone = bindFileDropZone(list, {
500
+ onDrop: (files) => importSongs(files),
501
+ onActiveChange: (isActive) => list.classList.toggle("drop-target", isActive),
502
+ signal,
503
+ });
504
+ ```
505
+
506
+ - `bindFileDropZone(element, { onDrop, onActiveChange?, dropEffect?, signal? })`
507
+ (from `/dom`) — makes an element take files dropped on it: a drag of files
508
+ (`dataTransfer.types` has `"Files"`) over it or any node inside it is
509
+ accepted, the cursor showing `dropEffect` (`"copy"` by default), and the drop
510
+ hands `onDrop` its files (never none) with the page kept. A drag of text, a
511
+ link or an element of the page is left alone. `onActiveChange(isActive)`
512
+ tells when the drag comes over the zone and when it leaves, is dropped or is
513
+ given up; moving over the zone's own nodes keeps it active, whichever node
514
+ `dragleave` names (Safari gives it no `relatedTarget`) and even when the node
515
+ under the drag leaves the document. `isActive` reads it. Disposing (or
516
+ aborting `signal`) stops the binding without telling.
517
+ - `preventFileDropNavigation(window, { signal? })` (from `/dom`) — keeps a file
518
+ dropped where no zone takes it from replacing the page, as a browser does by
519
+ opening it: such a drag shows the "no drop" cursor and its drop is
520
+ cancelled; a zone's own drop effect is kept.
521
+ - `isFileDrag(event)` (from `/dom`) — whether a drag event carries files.
522
+
523
+ ## User activation
524
+
525
+ ```ts
526
+ import { bindUserActivation } from "@ac-kit/browser/dom";
527
+
528
+ using unlock = bindUserActivation(window, {
529
+ onActivation: () => void audioContext.resume(),
530
+ navigator,
531
+ signal,
532
+ });
533
+ ```
534
+
535
+ - `bindUserActivation(target, { onActivation, once?, navigator?, signal? })`
536
+ (from `/dom`) — calls `onActivation(event)` synchronously inside each event
537
+ that gives the page a user activation, so it may start audio, enter full
538
+ screen or open a window even where the browser allows it only during the
539
+ event (Safari). It listens on `target` (the page's `window`, or an element)
540
+ in the capture phase and passively, so a handler stopping or cancelling the
541
+ event hides nothing. With `navigator`, it is called at once (with
542
+ `undefined`) when `navigator.userActivation.isActive` says the page has a
543
+ transient activation already, as when bound from a click's handler. One
544
+ press can be two activating events (a tap's `pointerup` and `touchend`):
545
+ make it idempotent, or pass `once`.
546
+ - `isActivationTriggeringEvent(event)` (from `/dom`) — the HTML standard's
547
+ definition: a trusted `keydown` but Escape, `mousedown`, a mouse's
548
+ `pointerdown`, a touch's or a pen's `pointerup`, `touchend`. A `click`
549
+ follows one of those; a script's event is none.
550
+
551
+ ## Form inputs
552
+
553
+ - `getFormInput(form.elements, name)` (from `/dom`) — the `<input>` a form's
554
+ controls name, checked rather than cast: a missing name, one several
555
+ controls share (a `RadioNodeList`, as a radio group is), or another control
556
+ (`<select>`, `<textarea>`, …) throw a `TypeError`. It reads the tag and
557
+ namespace, not `instanceof`, so a form of an `<iframe>` is read alike.
558
+
559
+ ## Temporal
560
+
561
+ ```ts
562
+ import { enableTemporal } from "@ac-kit/browser";
563
+
564
+ const hasTemporal = await enableTemporal(globalThis, {
565
+ load: () => import("temporal-polyfill/implementation"),
566
+ onWarning: (message, cause) => console.warn(`Heliotrope: ${message}`, cause),
567
+ });
568
+ ```
569
+
570
+ - `enableTemporal(scope, { load, onWarning })` — makes sure `scope.Temporal`
571
+ (`globalThis`, or a worker's `self`) is a complete Temporal: a native one is
572
+ kept, and where it is missing or of the shape before the finished proposal
573
+ (no `ZonedDateTime.prototype.getTimeZoneTransition`), the one `load` gives is
574
+ installed as a built-in would be. `load` runs only then, so the bundler puts
575
+ the fallback in a chunk only those browsers fetch; this package depends on no
576
+ polyfill and reimplements nothing. It answers whether Temporal is complete
577
+ now; when not — the fallback failed to load, is not complete, or the scope
578
+ refused it — `onWarning(message, cause)` was told why. Overlapping calls
579
+ install one fallback.
580
+
432
581
  ## Testing
433
582
 
434
583
  ```ts
@@ -476,7 +625,15 @@ helpers, and code built on them, take — for tests under Node, not for a page:
476
625
  hands it to `port.onPostMessage` where the test runs the processor's logic,
477
626
  and delivers what the test `emit`s.
478
627
  - `FakeMidiAccess` and `FakeMidiInput` — ports plugged in and out, messages
479
- sent with their time stamps.
628
+ sent with their time stamps; `open()` and `close()` move `connection`
629
+ (`"open"`, `"pending"` while unplugged, `"closed"`) with a `statechange`,
630
+ counted (`openCount`, `closeCount`), held (`holdOpen`) or refused once
631
+ (`openFailure`).
632
+ - `FakePermissions` — `navigator.permissions` for the names it is given, each
633
+ `"prompt"` until `set(descriptor, state)` changes it and fires `change` on
634
+ every `FakePermissionStatus` queried for it; another name is refused with a
635
+ `TypeError`, as a browser refuses one it cannot query. `queries` records
636
+ each descriptor.
480
637
  - `FakeAnimationFrames` — `requestAnimationFrame` queueing, `runFrame(time)`
481
638
  running what was queued before the call.
482
639
  - `FakeResizeObserver` — records what is observed, delivers `resize(size)`
@@ -507,3 +664,7 @@ for the full reference.
507
664
  - [HTML Standard: the 2D rendering context](https://html.spec.whatwg.org/multipage/canvas.html#2dcontext)
508
665
  - [Web Animations](https://www.w3.org/TR/web-animations-1/)
509
666
  - [CSS Transitions](https://www.w3.org/TR/css-transitions-1/)
667
+ - [HTML Standard: drag and drop](https://html.spec.whatwg.org/multipage/dnd.html)
668
+ - [HTML Standard: user activation](https://html.spec.whatwg.org/multipage/interaction.html#tracking-user-activation)
669
+ - [Permissions](https://www.w3.org/TR/permissions/)
670
+ - [Temporal](https://tc39.es/proposal-temporal/)
@@ -0,0 +1,71 @@
1
+ import type { Callable } from "@ac-kit/core";
2
+ /** A drag event that carries files. */
3
+ export type FileDragEvent = DragEvent & {
4
+ readonly dataTransfer: DataTransfer;
5
+ };
6
+ /** Whether a drag carries files — from the desktop or another application. */
7
+ export declare function isFileDrag(event: Event): event is FileDragEvent;
8
+ export type FileDropZoneOptions = {
9
+ /**
10
+ * Called on a drop of files on the zone, with them in the browser's order and
11
+ * the `drop` event; never for a drop of no file (a drag from an application
12
+ * that gives none).
13
+ */
14
+ readonly onDrop: Callable<[files: readonly File[], event: FileDragEvent]>;
15
+ /**
16
+ * Called with `true` when a drag of files comes over the zone, and with
17
+ * `false` when it leaves, is dropped or is given up: for the zone to show it
18
+ * takes the drop.
19
+ */
20
+ readonly onActiveChange?: Callable<[isActive: boolean]>;
21
+ /** The operation the cursor shows over the zone. Default `"copy"`. */
22
+ readonly dropEffect?: "copy" | "link" | "move";
23
+ readonly signal?: AbortSignal;
24
+ };
25
+ /** A file drop zone bound until disposed. */
26
+ export type FileDropZone = Disposable & {
27
+ /** Whether a drag of files is over the zone now. */
28
+ readonly isActive: boolean;
29
+ };
30
+ /**
31
+ * Makes an element take files dropped on it. A drag of files over it, or over
32
+ * any element inside it, is accepted (`dragenter` and `dragover` cancelled, the
33
+ * cursor showing `dropEffect`), and the drop is handed to `onDrop` with the
34
+ * page kept: a browser opens a file dropped where no one takes it. A drag of
35
+ * anything else — text, a link, an element of the page — is left to the page.
36
+ *
37
+ * Moving over the zone's own elements fires `dragleave` on the one left: the
38
+ * zone stays active until the drag is over no node inside it, whichever node
39
+ * the browser names (Safari gives `dragleave` no `relatedTarget`), and even
40
+ * when the node under the drag leaves the document. Pair it with
41
+ * {@link preventFileDropNavigation} so that a file dropped beside the zone does
42
+ * not replace the page.
43
+ *
44
+ * @example
45
+ * using zone = bindFileDropZone(list, {
46
+ * onDrop: (files) => importSongs(files),
47
+ * onActiveChange: (isActive) =>
48
+ * list.classList.toggle("drop-target", isActive),
49
+ * signal,
50
+ * });
51
+ *
52
+ * @returns Stops the binding, telling nothing; disposing it again does nothing.
53
+ */
54
+ export declare function bindFileDropZone(zone: Element, options: FileDropZoneOptions): FileDropZone;
55
+ export type PreventFileDropNavigationOptions = {
56
+ readonly signal?: AbortSignal;
57
+ };
58
+ /**
59
+ * Keeps a file dropped where nothing takes it from replacing the page, as a
60
+ * browser does by opening it: on `target` (the page's `window`), a drag of
61
+ * files no drop zone accepted shows the "no drop" cursor (`dropEffect`
62
+ * `"none"`), and its drop is cancelled. A zone accepting the drag cancels it
63
+ * first, and its drop effect is kept.
64
+ *
65
+ * @example
66
+ * using guard = preventFileDropNavigation(window);
67
+ *
68
+ * @returns Stops the guard; disposing it again does nothing.
69
+ */
70
+ export declare function preventFileDropNavigation(target: Pick<EventTarget, "addEventListener">, options?: PreventFileDropNavigationOptions): Disposable;
71
+ //# sourceMappingURL=file-drop-zone.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-drop-zone.d.ts","sourceRoot":"","sources":["../../../src/dom/drag-and-drop/file-drop-zone.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C,uCAAuC;AACvC,MAAM,MAAM,aAAa,GAAG,SAAS,GAAG;IAAE,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAA;CAAE,CAAC;AAEhF,8EAA8E;AAC9E,wBAAgB,UAAU,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,IAAI,aAAa,CAK/D;AAED,MAAM,MAAM,mBAAmB,GAAG;IACjC;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC;IAC1E;;;;OAIG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;IACxD,sEAAsE;IACtE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;IAC/C,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B,CAAC;AAEF,6CAA6C;AAC7C,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG;IACvC,oDAAoD;IACpD,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC3B,CAAC;AAUF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,gBAAgB,CAC/B,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,mBAAmB,GAC1B,YAAY,CA4Ed;AAED,MAAM,MAAM,gCAAgC,GAAG;IAC9C,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,yBAAyB,CACxC,MAAM,EAAE,IAAI,CAAC,WAAW,EAAE,kBAAkB,CAAC,EAC7C,OAAO,GAAE,gCAAqC,GAC5C,UAAU,CAsBZ"}
@@ -0,0 +1,145 @@
1
+ /** Whether a drag carries files — from the desktop or another application. */
2
+ export function isFileDrag(event) {
3
+ return ("dataTransfer" in event &&
4
+ event.dataTransfer?.types.includes("Files") === true // a drag event's only member read
5
+ );
6
+ }
7
+ // What an event heard on an element was fired at: one of the nodes of its
8
+ // tree, or none.
9
+ function targetNode_(event) {
10
+ const { target } = event;
11
+ return target !== null && "nodeType" in target ? target : undefined; // an event target with a node type is a node
12
+ }
13
+ /**
14
+ * Makes an element take files dropped on it. A drag of files over it, or over
15
+ * any element inside it, is accepted (`dragenter` and `dragover` cancelled, the
16
+ * cursor showing `dropEffect`), and the drop is handed to `onDrop` with the
17
+ * page kept: a browser opens a file dropped where no one takes it. A drag of
18
+ * anything else — text, a link, an element of the page — is left to the page.
19
+ *
20
+ * Moving over the zone's own elements fires `dragleave` on the one left: the
21
+ * zone stays active until the drag is over no node inside it, whichever node
22
+ * the browser names (Safari gives `dragleave` no `relatedTarget`), and even
23
+ * when the node under the drag leaves the document. Pair it with
24
+ * {@link preventFileDropNavigation} so that a file dropped beside the zone does
25
+ * not replace the page.
26
+ *
27
+ * @example
28
+ * using zone = bindFileDropZone(list, {
29
+ * onDrop: (files) => importSongs(files),
30
+ * onActiveChange: (isActive) =>
31
+ * list.classList.toggle("drop-target", isActive),
32
+ * signal,
33
+ * });
34
+ *
35
+ * @returns Stops the binding, telling nothing; disposing it again does nothing.
36
+ */
37
+ export function bindFileDropZone(zone, options) {
38
+ const { onDrop, onActiveChange, dropEffect = "copy", signal } = options;
39
+ const lifetime = new AbortController();
40
+ const stop = signal === undefined
41
+ ? lifetime.signal
42
+ : AbortSignal.any([signal, lifetime.signal]);
43
+ // The nodes the drag entered and has not left: the browser enters the next
44
+ // one before it leaves the last, so this holds the node under the drag.
45
+ const entered = new Set();
46
+ let isActive = false;
47
+ const setActive = (active) => {
48
+ if (isActive !== active) {
49
+ isActive = active;
50
+ onActiveChange?.(active);
51
+ }
52
+ };
53
+ const accept = (event) => {
54
+ if (!isFileDrag(event)) {
55
+ return;
56
+ }
57
+ event.preventDefault();
58
+ event.dataTransfer.dropEffect = dropEffect;
59
+ const node = targetNode_(event);
60
+ if (node !== undefined) {
61
+ entered.add(node);
62
+ }
63
+ setActive(true);
64
+ };
65
+ const leave = (event) => {
66
+ if (!isFileDrag(event)) {
67
+ return;
68
+ }
69
+ const node = targetNode_(event);
70
+ if (node !== undefined) {
71
+ entered.delete(node);
72
+ }
73
+ // A node removed under the drag is never left.
74
+ for (const other of entered) {
75
+ if (!zone.contains(other)) {
76
+ entered.delete(other);
77
+ }
78
+ }
79
+ if (entered.size === 0) {
80
+ setActive(false);
81
+ }
82
+ };
83
+ const drop = (event) => {
84
+ if (!isFileDrag(event)) {
85
+ return;
86
+ }
87
+ event.preventDefault();
88
+ entered.clear();
89
+ setActive(false);
90
+ const files = Array.from(event.dataTransfer.files);
91
+ if (files.length > 0) {
92
+ onDrop(files, event);
93
+ }
94
+ };
95
+ if (!stop.aborted) {
96
+ zone.addEventListener("dragenter", accept, { signal: stop });
97
+ zone.addEventListener("dragover", accept, { signal: stop });
98
+ zone.addEventListener("dragleave", leave, { signal: stop });
99
+ zone.addEventListener("drop", drop, { signal: stop });
100
+ stop.addEventListener("abort", () => {
101
+ entered.clear();
102
+ isActive = false;
103
+ });
104
+ }
105
+ return {
106
+ get isActive() {
107
+ return isActive;
108
+ },
109
+ [Symbol.dispose]: () => lifetime.abort(),
110
+ };
111
+ }
112
+ /**
113
+ * Keeps a file dropped where nothing takes it from replacing the page, as a
114
+ * browser does by opening it: on `target` (the page's `window`), a drag of
115
+ * files no drop zone accepted shows the "no drop" cursor (`dropEffect`
116
+ * `"none"`), and its drop is cancelled. A zone accepting the drag cancels it
117
+ * first, and its drop effect is kept.
118
+ *
119
+ * @example
120
+ * using guard = preventFileDropNavigation(window);
121
+ *
122
+ * @returns Stops the guard; disposing it again does nothing.
123
+ */
124
+ export function preventFileDropNavigation(target, options = {}) {
125
+ const { signal } = options;
126
+ const lifetime = new AbortController();
127
+ const stop = signal === undefined
128
+ ? lifetime.signal
129
+ : AbortSignal.any([signal, lifetime.signal]);
130
+ const guard = (event) => {
131
+ if (!isFileDrag(event) || event.defaultPrevented) {
132
+ return;
133
+ }
134
+ event.preventDefault();
135
+ if (event.type === "dragover") {
136
+ event.dataTransfer.dropEffect = "none";
137
+ }
138
+ };
139
+ if (!stop.aborted) {
140
+ target.addEventListener("dragover", guard, { signal: stop });
141
+ target.addEventListener("drop", guard, { signal: stop });
142
+ }
143
+ return { [Symbol.dispose]: () => lifetime.abort() };
144
+ }
145
+ //# sourceMappingURL=file-drop-zone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-drop-zone.js","sourceRoot":"","sources":["../../../src/dom/drag-and-drop/file-drop-zone.ts"],"names":[],"mappings":"AAKA,8EAA8E;AAC9E,MAAM,UAAU,UAAU,CAAC,KAAY;IACtC,OAAO,CACN,cAAc,IAAI,KAAK;QACtB,KAAmB,CAAC,YAAY,EAAE,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,kCAAkC;KACtG,CAAC;AACH,CAAC;AA0BD,0EAA0E;AAC1E,iBAAiB;AACjB,SAAS,WAAW,CAAC,KAAY;IAChC,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;IAEzB,OAAO,MAAM,KAAK,IAAI,IAAI,UAAU,IAAI,MAAM,CAAC,CAAC,CAAE,MAAe,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,6CAA6C;AAC7H,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,gBAAgB,CAC/B,IAAa,EACb,OAA4B;IAE5B,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,UAAU,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IACxE,MAAM,QAAQ,GAAG,IAAI,eAAe,EAAE,CAAC;IACvC,MAAM,IAAI,GACT,MAAM,KAAK,SAAS;QACnB,CAAC,CAAC,QAAQ,CAAC,MAAM;QACjB,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/C,2EAA2E;IAC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 * missing name, one several controls share (a `RadioNodeList`, as a radio group
4
+ * is: read it from `namedItem` itself), or another control (`<select>`,
5
+ * `<textarea>`, `<button>`, …) are refused. It tells an `<input>` by its tag
6
+ * and namespace, so a form of another window (an `<iframe>`'s) is read alike.
7
+ *
8
+ * @example
9
+ * form.addEventListener("submit", (event) => {
10
+ * const latitude = getFormInput(form.elements, "latitude").valueAsNumber;
11
+ * });
12
+ *
13
+ * @param elements A form's `elements`.
14
+ * @throws {TypeError} When `name` is not that of one `<input>`.
15
+ */
16
+ export declare function getFormInput(elements: Pick<HTMLFormControlsCollection, "namedItem">, name: string): HTMLInputElement;
17
+ //# sourceMappingURL=get-form-input.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-form-input.d.ts","sourceRoot":"","sources":["../../../src/dom/forms/get-form-input.ts"],"names":[],"mappings":"AAYA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAC3B,QAAQ,EAAE,IAAI,CAAC,0BAA0B,EAAE,WAAW,CAAC,EACvD,IAAI,EAAE,MAAM,GACV,gBAAgB,CAelB"}
@@ -0,0 +1,35 @@
1
+ const HTML_NAMESPACE_ = "http://www.w3.org/1999/xhtml";
2
+ function isInput_(control) {
3
+ return ("localName" in control &&
4
+ control.localName === "input" &&
5
+ control.namespaceURI === HTML_NAMESPACE_);
6
+ }
7
+ /**
8
+ * The `<input>` a form's controls name `name`, checked rather than cast: a
9
+ * missing name, one several controls share (a `RadioNodeList`, as a radio group
10
+ * is: read it from `namedItem` itself), or another control (`<select>`,
11
+ * `<textarea>`, `<button>`, …) are refused. It tells an `<input>` by its tag
12
+ * and namespace, so a form of another window (an `<iframe>`'s) is read alike.
13
+ *
14
+ * @example
15
+ * form.addEventListener("submit", (event) => {
16
+ * const latitude = getFormInput(form.elements, "latitude").valueAsNumber;
17
+ * });
18
+ *
19
+ * @param elements A form's `elements`.
20
+ * @throws {TypeError} When `name` is not that of one `<input>`.
21
+ */
22
+ export function getFormInput(elements, name) {
23
+ const control = elements.namedItem(name);
24
+ if (control === null) {
25
+ throw new TypeError(`The form has no control named "${name}"`);
26
+ }
27
+ if (!("localName" in control)) {
28
+ throw new TypeError(`The form has several controls named "${name}"`);
29
+ }
30
+ if (!isInput_(control)) {
31
+ throw new TypeError(`The control named "${name}" is a <${control.localName}>, not an <input>`);
32
+ }
33
+ return control;
34
+ }
35
+ //# sourceMappingURL=get-form-input.js.map