@dasl/tile-loader 3.0.0 → 3.2.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.
Files changed (87) hide show
  1. package/README.md +841 -26
  2. package/dist/at.d.ts +126 -12
  3. package/dist/at.d.ts.map +1 -1
  4. package/dist/at.js +574 -90
  5. package/dist/at.js.map +1 -1
  6. package/dist/car.d.ts +21 -2
  7. package/dist/car.d.ts.map +1 -1
  8. package/dist/car.js +78 -15
  9. package/dist/car.js.map +1 -1
  10. package/dist/content-scheme.d.ts +17 -3
  11. package/dist/content-scheme.d.ts.map +1 -1
  12. package/dist/content-scheme.js +32 -6
  13. package/dist/content-scheme.js.map +1 -1
  14. package/dist/guard.d.ts +242 -0
  15. package/dist/guard.d.ts.map +1 -0
  16. package/dist/guard.js +956 -0
  17. package/dist/guard.js.map +1 -0
  18. package/dist/index.d.ts +297 -13
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +917 -78
  21. package/dist/index.js.map +1 -1
  22. package/dist/lib/cid.d.ts +25 -0
  23. package/dist/lib/cid.d.ts.map +1 -0
  24. package/dist/lib/cid.js +65 -0
  25. package/dist/lib/cid.js.map +1 -0
  26. package/dist/lib/defaults.d.ts +8 -0
  27. package/dist/lib/defaults.d.ts.map +1 -0
  28. package/dist/lib/defaults.js +14 -0
  29. package/dist/lib/defaults.js.map +1 -0
  30. package/dist/lib/destinations.d.ts +25 -0
  31. package/dist/lib/destinations.d.ts.map +1 -0
  32. package/dist/lib/destinations.js +94 -0
  33. package/dist/lib/destinations.js.map +1 -0
  34. package/dist/lib/drisl.d.ts +10 -0
  35. package/dist/lib/drisl.d.ts.map +1 -0
  36. package/dist/lib/drisl.js +169 -0
  37. package/dist/lib/drisl.js.map +1 -0
  38. package/dist/lib/errors.d.ts +80 -0
  39. package/dist/lib/errors.d.ts.map +1 -0
  40. package/dist/lib/errors.js +44 -0
  41. package/dist/lib/errors.js.map +1 -0
  42. package/dist/lib/fetching.d.ts +24 -0
  43. package/dist/lib/fetching.d.ts.map +1 -0
  44. package/dist/lib/fetching.js +135 -0
  45. package/dist/lib/fetching.js.map +1 -0
  46. package/dist/lib/limits.d.ts +34 -0
  47. package/dist/lib/limits.d.ts.map +1 -0
  48. package/dist/lib/limits.js +33 -0
  49. package/dist/lib/limits.js.map +1 -0
  50. package/dist/lib/pointer.d.ts +83 -0
  51. package/dist/lib/pointer.d.ts.map +1 -0
  52. package/dist/lib/pointer.js +244 -0
  53. package/dist/lib/pointer.js.map +1 -0
  54. package/dist/link.d.ts +110 -13
  55. package/dist/link.d.ts.map +1 -1
  56. package/dist/link.js +336 -43
  57. package/dist/link.js.map +1 -1
  58. package/dist/memory.d.ts +1 -0
  59. package/dist/memory.d.ts.map +1 -1
  60. package/dist/memory.js +5 -0
  61. package/dist/memory.js.map +1 -1
  62. package/dist/mount.d.ts +54 -0
  63. package/dist/mount.d.ts.map +1 -0
  64. package/dist/mount.js +286 -0
  65. package/dist/mount.js.map +1 -0
  66. package/dist/size.d.ts +43 -2
  67. package/dist/size.d.ts.map +1 -1
  68. package/dist/size.js +296 -15
  69. package/dist/size.js.map +1 -1
  70. package/package.json +13 -3
  71. package/src/at.ts +582 -84
  72. package/src/car.ts +75 -16
  73. package/src/content-scheme.ts +38 -7
  74. package/src/guard.ts +1013 -0
  75. package/src/index.ts +1011 -80
  76. package/src/lib/cid.ts +65 -0
  77. package/src/lib/defaults.ts +12 -0
  78. package/src/lib/destinations.ts +79 -0
  79. package/src/lib/drisl.ts +160 -0
  80. package/src/lib/errors.ts +111 -0
  81. package/src/lib/fetching.ts +138 -0
  82. package/src/lib/limits.ts +61 -0
  83. package/src/lib/pointer.ts +290 -0
  84. package/src/link.ts +365 -48
  85. package/src/memory.ts +5 -0
  86. package/src/mount.ts +356 -0
  87. package/src/size.ts +304 -16
@@ -0,0 +1,290 @@
1
+ // Where the user's pointer is, as far as a page that frames a tile can know,
2
+ // and a way to keep presses off a piece of that page for a moment. For the
3
+ // modules that hold a tile to one rule: it does not get to move what the
4
+ // user is about to press.
5
+ //
6
+ // What the page can and cannot know decides everything here, so, once:
7
+ //
8
+ // - A mouse over a tile's frame is invisible to the page, except that it is
9
+ // there: the frame element gets a pointerover when the mouse goes in, with
10
+ // coordinates, the page gets a pointermove when it comes out, and nothing
11
+ // in between. A press inside the frame is never seen.
12
+ // - A finger gives no warning at all: nothing is seen of a touch that lands
13
+ // on the frame. And where a screen takes fingers as well as a mouse, one
14
+ // may come down in the frame whatever the page knows of the mouse.
15
+
16
+ const now = () => performance.now();
17
+
18
+ export interface PointerState {
19
+ /**
20
+ * `unknown`: nothing seen yet. `host`: over the page itself, at (x, y).
21
+ * `frame`: inside a frame, somewhere (it went in at x, y). `away`: out of
22
+ * the window. `none`: the last pointer was a finger, and it is up.
23
+ */
24
+ kind: 'unknown' | 'host' | 'frame' | 'away' | 'none';
25
+ /** `mouse`, `pen` or `touch`: the last pointer seen. Empty before any. */
26
+ type: string;
27
+ /** Where it was last seen, in client coordinates. */
28
+ x: number;
29
+ y: number;
30
+ /** When that was (performance.now()). */
31
+ at: number;
32
+ /**
33
+ * Whether it got there by moving: it was last seen somewhere else. A
34
+ * pointer that shows up in a frame at the very place the page last saw it
35
+ * has not moved: the frame was put under it (the page scrolled, or was laid
36
+ * out again), and the browser is only bringing hover up to date.
37
+ */
38
+ moved: boolean;
39
+ }
40
+
41
+ export interface PointerWatch {
42
+ readonly state: PointerState;
43
+ /** The frame the pointer is in, when it is in one. */
44
+ readonly frame: Element | undefined;
45
+ /** Whether what this user points with hovers (a mouse, a pen) or not (a finger). */
46
+ readonly hovers: boolean;
47
+ /**
48
+ * Whether this screen takes fingers at all. Where it does, a finger may
49
+ * land in a frame at any moment, unseen, wherever the mouse is known to be.
50
+ */
51
+ readonly fingers: boolean;
52
+ /** Whether the pointer is, or may be, inside `frame`. */
53
+ inside (frame: Element): boolean;
54
+ /**
55
+ * Has `fn` called after the pointer was seen somewhere else. `fn` is held
56
+ * weakly: whoever asks keeps it, and it goes when they do. Returns a
57
+ * function that stops it.
58
+ */
59
+ onChange (fn: () => void): () => void;
60
+ }
61
+
62
+ const watches = new WeakMap<Document, PointerWatch>();
63
+ const isFrame = (el: EventTarget | null): el is Element => (el as Element | null)?.localName === 'iframe' || (el as Element | null)?.localName === 'frame';
64
+ // What an event was dispatched at. For a frame inside a shadow tree the
65
+ // document is told the tree's host instead; the event's path still starts
66
+ // at the frame (where the tree is closed it does not: `inside()` then goes
67
+ // by whether the frame is hovered).
68
+ const targetOf = (e: Event): EventTarget | null => e.composedPath?.()[0] ?? e.target;
69
+
70
+ /** The pointer in `doc`, followed from now on. One watch per document. */
71
+ export function watchPointer (doc: Document): PointerWatch {
72
+ const known = watches.get(doc);
73
+ if (known) return known;
74
+ const win = doc.defaultView as Window;
75
+ const state: PointerState = { kind: 'unknown', type: '', x: 0, y: 0, at: 0, moved: false };
76
+ // Weakly: the frame is the host's, and may be gone before the pointer moves again.
77
+ let frame: WeakRef<Element> | undefined;
78
+ const listeners = new Set<WeakRef<() => void>>();
79
+ let queued = false;
80
+ const tell = () => {
81
+ if (queued) return;
82
+ queued = true;
83
+ queueMicrotask(() => {
84
+ queued = false;
85
+ for (const ref of [...listeners]) {
86
+ const fn = ref.deref();
87
+ if (fn) fn();
88
+ else listeners.delete(ref);
89
+ }
90
+ });
91
+ };
92
+ const on = (ev: Event) => {
93
+ // Only the user's own pointer counts, not an event a script made up.
94
+ if (!ev.isTrusted) return;
95
+ const e = ev as PointerEvent;
96
+ const type = e.pointerType || 'mouse';
97
+ // A pointer that went into a frame comes out of it with a move the page
98
+ // gets to see. A bare pointerover on something of the page's, with no
99
+ // move, is the browser bringing hover up to date (a dialog opened over
100
+ // the frame, the frame was hidden): it is dispatched where the PAGE last
101
+ // saw the pointer, which is where it went into the frame, not where it
102
+ // is. It says nothing about the pointer having left.
103
+ const target = targetOf(e);
104
+ if (e.type === 'pointerover' && type !== 'touch' && state.kind === 'frame' && !isFrame(target)) return;
105
+ const here = e.clientX === state.x && e.clientY === state.y;
106
+ const again = here && state.kind === 'frame' && frame?.deref() === target;
107
+ // (Told twice of the same arrival, it is still the one arrival.)
108
+ if (!isFrame(target) || type === 'touch') state.moved = true;
109
+ else if (!again) state.moved = state.kind === 'away' || ((state.kind === 'host' || state.kind === 'frame') && !here);
110
+ state.type = type;
111
+ state.at = now();
112
+ state.x = e.clientX;
113
+ state.y = e.clientY;
114
+ if (type === 'touch') {
115
+ state.kind = e.type === 'pointerup' || e.type === 'pointercancel' ? 'none' : 'host';
116
+ frame = undefined;
117
+ }
118
+ else if (isFrame(target)) {
119
+ state.kind = 'frame';
120
+ frame = new WeakRef(target);
121
+ }
122
+ else {
123
+ state.kind = 'host';
124
+ frame = undefined;
125
+ }
126
+ tell();
127
+ };
128
+ for (const type of ['pointerover', 'pointermove', 'pointerdown', 'pointerup', 'pointercancel']) {
129
+ doc.addEventListener(type, on, { capture: true, passive: true });
130
+ }
131
+ // Out of the window altogether. (The root element is also "left" when a
132
+ // modal dialog opens under a pointer that has not moved, since everything
133
+ // but the dialog goes inert: that is not the pointer going anywhere, and it
134
+ // shows in where the event says the pointer is.)
135
+ doc.documentElement.addEventListener('pointerleave', (ev) => {
136
+ const e = ev as PointerEvent;
137
+ if (!e.isTrusted || e.pointerType === 'touch') return;
138
+ if (e.clientX > 1 && e.clientY > 1 && e.clientX < win.innerWidth - 1 && e.clientY < win.innerHeight - 1) return;
139
+ state.kind = 'away';
140
+ state.moved = true;
141
+ frame = undefined;
142
+ state.at = now();
143
+ tell();
144
+ });
145
+ const watch: PointerWatch = {
146
+ state,
147
+ get frame () { return frame?.deref(); },
148
+ get hovers () {
149
+ if (state.type) return state.type !== 'touch';
150
+ return win.matchMedia?.('(hover: hover)').matches ?? true;
151
+ },
152
+ get fingers () { return (win.navigator?.maxTouchPoints ?? 0) > 0; },
153
+ inside (el) {
154
+ return (state.kind === 'frame' && frame?.deref() === el) || (state.type !== 'touch' && el.matches(':hover'));
155
+ },
156
+ onChange (fn) {
157
+ const ref = new WeakRef(fn);
158
+ listeners.add(ref);
159
+ return () => { listeners.delete(ref); };
160
+ },
161
+ };
162
+ watches.set(doc, watch);
163
+ return watch;
164
+ }
165
+
166
+ /** A rectangle in client coordinates. */
167
+ export interface Box {
168
+ left: number;
169
+ top: number;
170
+ width: number;
171
+ height: number;
172
+ }
173
+
174
+ export interface ShieldOptions {
175
+ /** How long it stays after the last press on it, in milliseconds. */
176
+ ms: number;
177
+ /** How long it stays while a mouse rests on it without pressing, in milliseconds. */
178
+ dwellMs: number;
179
+ /** Stay until a press has been taken (then `ms`), or for `maxMs`. */
180
+ sticky?: boolean;
181
+ maxMs?: number;
182
+ /** An element whose box the rectangle follows: it is placed again whenever that box changes. */
183
+ follow?: Element;
184
+ /** Called with each press it takes. */
185
+ onAbsorb?: () => void;
186
+ }
187
+
188
+ export interface Shield {
189
+ /** Whether it has gone. */
190
+ readonly gone: boolean;
191
+ remove (): void;
192
+ }
193
+
194
+ /** The class of a shield's element, for a host's own rules to know it by. */
195
+ export const SHIELD_CLASS = 'tile-shield';
196
+ // Not quite transparent, on purpose. Which process gets a press, the page's
197
+ // or a frame's under it, is the browser's to decide, from what the page last
198
+ // drew. A block that paints nothing changes nothing in what was drawn, and a
199
+ // press on a pointer that has not moved then goes straight to the frame
200
+ // underneath. A tint too faint to see is enough.
201
+ const SHIELD_TINT = 'rgba(127,127,127,0.03)';
202
+ const SWALLOWED = ['pointerdown', 'pointerup', 'mousedown', 'mouseup', 'click', 'dblclick', 'auxclick', 'contextmenu'];
203
+
204
+ // Where a block has to be put to lie over `el`: in the page's body, unless
205
+ // `el` is shown in the browser's top layer (a modal dialog, a popover, an
206
+ // element gone fullscreen), which is drawn over everything in the body.
207
+ function layerOf (el: Element | undefined, doc: Document): Element {
208
+ for (let node: Node | null = el ?? null; node; node = node.parentNode ?? (node as ShadowRoot).host ?? null) {
209
+ if (node.nodeType !== 1) continue;
210
+ const e = node as HTMLElement;
211
+ if (e === doc.fullscreenElement || (e.localName === 'dialog' && (e as HTMLDialogElement).open)) return e;
212
+ try { if (e.hasAttribute('popover') && e.matches(':popover-open')) return e; }
213
+ catch { /* a browser without popovers */ }
214
+ }
215
+ return doc.body;
216
+ }
217
+
218
+ /**
219
+ * A piece of the page that takes no press for a moment: a block of the
220
+ * host's, all but invisible, on top of everything, where `rect()` says (and
221
+ * it is asked again as the page scrolls). It goes `ms` after the last press
222
+ * on it; a press on it is swallowed and starts the wait again; and while a
223
+ * mouse rests on it, it stays until the mouse has left, or has rested there
224
+ * `dwellMs` without pressing. A `sticky` one stays until it has swallowed a
225
+ * press (then `ms`), or for `maxMs`. A wheel or a finger still scrolls the
226
+ * page through it.
227
+ */
228
+ export function shield (doc: Document, rect: () => Box | undefined, options: ShieldOptions): Shield {
229
+ const win = doc.defaultView as Window;
230
+ const { ms, dwellMs, sticky = false, maxMs = ms } = options;
231
+ const el = doc.createElement('div');
232
+ el.className = SHIELD_CLASS;
233
+ el.setAttribute('aria-hidden', 'true');
234
+ el.style.cssText = `position:fixed;z-index:2147483647;margin:0;padding:0;border:0;background:${SHIELD_TINT};`;
235
+ const born = now();
236
+ let armed = born;
237
+ let absorbed = 0;
238
+ let over = false;
239
+ let overSince = 0;
240
+ let gone = false;
241
+ const place = () => {
242
+ const r = rect();
243
+ if (!r || r.width <= 0 || r.height <= 0) { el.style.display = 'none'; return; }
244
+ el.style.display = 'block';
245
+ el.style.left = `${r.left}px`;
246
+ el.style.top = `${r.top}px`;
247
+ el.style.width = `${r.width}px`;
248
+ el.style.height = `${r.height}px`;
249
+ };
250
+ const swallow = (ev: Event) => {
251
+ const e = ev as PointerEvent;
252
+ e.stopPropagation();
253
+ // A finger going down may be the start of a scroll: that is let be, and the tap it may become is what is refused.
254
+ if (e.type !== 'pointerdown' || e.pointerType !== 'touch') e.preventDefault();
255
+ if (e.type === 'pointerdown' || e.type === 'click') {
256
+ armed = now();
257
+ overSince = armed;
258
+ if (e.type === 'pointerdown') { absorbed++; options.onAbsorb?.(); }
259
+ }
260
+ };
261
+ for (const type of SWALLOWED) el.addEventListener(type, swallow);
262
+ el.addEventListener('pointerenter', (e) => { if ((e as PointerEvent).pointerType !== 'touch') { over = true; overSince = now(); } });
263
+ el.addEventListener('pointerleave', () => { over = false; });
264
+ const observer = options.follow && typeof ResizeObserver === 'function' ? new ResizeObserver(place) : undefined;
265
+ const remove = () => {
266
+ if (gone) return;
267
+ gone = true;
268
+ el.remove();
269
+ observer?.disconnect();
270
+ win.removeEventListener('scroll', place, true);
271
+ win.removeEventListener('resize', place);
272
+ };
273
+ const tick = () => {
274
+ if (gone) return;
275
+ const t = now();
276
+ // A sticky one waits for the press it is there to take: a tile that has
277
+ // its user pressing to a beat slower than `ms` gets past one that only waits.
278
+ const waited = sticky && !absorbed ? t - born >= maxMs : t - armed >= ms;
279
+ if (waited && (!over || t - overSince >= dwellMs)) return remove();
280
+ place();
281
+ win.setTimeout(tick, 50);
282
+ };
283
+ win.addEventListener('scroll', place, true);
284
+ win.addEventListener('resize', place);
285
+ if (options.follow) observer?.observe(options.follow);
286
+ place();
287
+ layerOf(options.follow, doc).append(el);
288
+ win.setTimeout(tick, 50);
289
+ return { get gone () { return gone; }, remove };
290
+ }