@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.
- package/README.md +841 -26
- package/dist/at.d.ts +126 -12
- package/dist/at.d.ts.map +1 -1
- package/dist/at.js +574 -90
- package/dist/at.js.map +1 -1
- package/dist/car.d.ts +21 -2
- package/dist/car.d.ts.map +1 -1
- package/dist/car.js +78 -15
- package/dist/car.js.map +1 -1
- package/dist/content-scheme.d.ts +17 -3
- package/dist/content-scheme.d.ts.map +1 -1
- package/dist/content-scheme.js +32 -6
- package/dist/content-scheme.js.map +1 -1
- package/dist/guard.d.ts +242 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/guard.js +956 -0
- package/dist/guard.js.map +1 -0
- package/dist/index.d.ts +297 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +917 -78
- package/dist/index.js.map +1 -1
- package/dist/lib/cid.d.ts +25 -0
- package/dist/lib/cid.d.ts.map +1 -0
- package/dist/lib/cid.js +65 -0
- package/dist/lib/cid.js.map +1 -0
- package/dist/lib/defaults.d.ts +8 -0
- package/dist/lib/defaults.d.ts.map +1 -0
- package/dist/lib/defaults.js +14 -0
- package/dist/lib/defaults.js.map +1 -0
- package/dist/lib/destinations.d.ts +25 -0
- package/dist/lib/destinations.d.ts.map +1 -0
- package/dist/lib/destinations.js +94 -0
- package/dist/lib/destinations.js.map +1 -0
- package/dist/lib/drisl.d.ts +10 -0
- package/dist/lib/drisl.d.ts.map +1 -0
- package/dist/lib/drisl.js +169 -0
- package/dist/lib/drisl.js.map +1 -0
- package/dist/lib/errors.d.ts +80 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +44 -0
- package/dist/lib/errors.js.map +1 -0
- package/dist/lib/fetching.d.ts +24 -0
- package/dist/lib/fetching.d.ts.map +1 -0
- package/dist/lib/fetching.js +135 -0
- package/dist/lib/fetching.js.map +1 -0
- package/dist/lib/limits.d.ts +34 -0
- package/dist/lib/limits.d.ts.map +1 -0
- package/dist/lib/limits.js +33 -0
- package/dist/lib/limits.js.map +1 -0
- package/dist/lib/pointer.d.ts +83 -0
- package/dist/lib/pointer.d.ts.map +1 -0
- package/dist/lib/pointer.js +244 -0
- package/dist/lib/pointer.js.map +1 -0
- package/dist/link.d.ts +110 -13
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +336 -43
- package/dist/link.js.map +1 -1
- package/dist/memory.d.ts +1 -0
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +5 -0
- package/dist/memory.js.map +1 -1
- package/dist/mount.d.ts +54 -0
- package/dist/mount.d.ts.map +1 -0
- package/dist/mount.js +286 -0
- package/dist/mount.js.map +1 -0
- package/dist/size.d.ts +43 -2
- package/dist/size.d.ts.map +1 -1
- package/dist/size.js +296 -15
- package/dist/size.js.map +1 -1
- package/package.json +13 -3
- package/src/at.ts +582 -84
- package/src/car.ts +75 -16
- package/src/content-scheme.ts +38 -7
- package/src/guard.ts +1013 -0
- package/src/index.ts +1011 -80
- package/src/lib/cid.ts +65 -0
- package/src/lib/defaults.ts +12 -0
- package/src/lib/destinations.ts +79 -0
- package/src/lib/drisl.ts +160 -0
- package/src/lib/errors.ts +111 -0
- package/src/lib/fetching.ts +138 -0
- package/src/lib/limits.ts +61 -0
- package/src/lib/pointer.ts +290 -0
- package/src/link.ts +365 -48
- package/src/memory.ts +5 -0
- package/src/mount.ts +356 -0
- package/src/size.ts +304 -16
package/dist/guard.js
ADDED
|
@@ -0,0 +1,956 @@
|
|
|
1
|
+
// Guards for a page that runs a tile among things of its own: its fields, its
|
|
2
|
+
// buttons, the rest of a feed.
|
|
3
|
+
//
|
|
4
|
+
// A tile cannot reach the page that frames it. But it runs in the middle of
|
|
5
|
+
// it, and what it does to itself can land on the user of that page: a tile's
|
|
6
|
+
// script can put the keyboard into the tile while the user types in a field
|
|
7
|
+
// of the page's. Nothing in the frame's sandbox governs that, so a host that
|
|
8
|
+
// has fields of its own turns a guard on:
|
|
9
|
+
//
|
|
10
|
+
// import { guardFocus } from '@dasl/tile-loader/guard';
|
|
11
|
+
// const frame = tile.renderContent();
|
|
12
|
+
// guardFocus(tile, frame);
|
|
13
|
+
//
|
|
14
|
+
// A tile can also scroll the page it is in: asking the browser to bring an
|
|
15
|
+
// element of its own into view scrolls every scroller above it, the page's
|
|
16
|
+
// document included. That takes the page to the tile, holds it there, or
|
|
17
|
+
// slides a control of the page's under a pointer that has not moved.
|
|
18
|
+
// `guardScroll` accounts for every move of the page and takes back the
|
|
19
|
+
// tile's; `clickGate` keeps what an unexplained move slid under the pointer
|
|
20
|
+
// from taking a press for a moment.
|
|
21
|
+
//
|
|
22
|
+
// Giving back what a tile took is a contest with one that takes it again, and
|
|
23
|
+
// the tile is the faster. So a tile that keeps at it is stopped: hidden at
|
|
24
|
+
// once, closed, and the host told why (a `stopped` event on the tile, with a
|
|
25
|
+
// `reason` of `focus` or `scroll`). `stop: false` has a guard only report.
|
|
26
|
+
//
|
|
27
|
+
// And a tile takes whatever input lands on it. On a touch screen that is the
|
|
28
|
+
// finger that was scrolling the feed: a tile can keep the page from moving
|
|
29
|
+
// for as long as the finger started on it. `engage` puts a layer of the
|
|
30
|
+
// host's over the tile until the user picks it, so that until then the tile
|
|
31
|
+
// runs and shows, and gets no input at all.
|
|
32
|
+
//
|
|
33
|
+
// A guard listens to the page, so unlike the modules that only listen to the
|
|
34
|
+
// tile it does not go when the tile is dropped: it stops when the tile is
|
|
35
|
+
// closed (`tile.close()`), or when it is told to (`stop()`).
|
|
36
|
+
import { withDefaults } from './lib/defaults.js';
|
|
37
|
+
import { SHIELD_CLASS, watchPointer } from './lib/pointer.js';
|
|
38
|
+
const now = () => performance.now();
|
|
39
|
+
const editable = (el) => Boolean(el) && (el.isContentEditable || /^(input|textarea|select)$/.test(el?.localName || ''));
|
|
40
|
+
// The element that has the keyboard, looked for through shadow trees.
|
|
41
|
+
const focused = (doc) => {
|
|
42
|
+
let el = doc.activeElement;
|
|
43
|
+
while (el?.shadowRoot?.activeElement)
|
|
44
|
+
el = el.shadowRoot.activeElement;
|
|
45
|
+
return el;
|
|
46
|
+
};
|
|
47
|
+
// Whether the keyboard is in `frame`. Asked of the tree the frame is in, so
|
|
48
|
+
// that a frame in a shadow tree is found as well.
|
|
49
|
+
const holds = (frame) => frame.getRootNode().activeElement === frame;
|
|
50
|
+
// The tiles a guard is in the middle of stopping.
|
|
51
|
+
const halting = new WeakSet();
|
|
52
|
+
// Stops a tile that would not stop. It is out of the way at once: a frame
|
|
53
|
+
// that is not rendered takes no focus and scrolls nothing. Its frame stays in
|
|
54
|
+
// the document for the moment closing takes, since a tile's runtime needs it
|
|
55
|
+
// there to clean up after itself; the guards go on until it is gone.
|
|
56
|
+
function halt(tile, frame, reason) {
|
|
57
|
+
if (halting.has(tile))
|
|
58
|
+
return;
|
|
59
|
+
halting.add(tile);
|
|
60
|
+
frame.style.display = 'none';
|
|
61
|
+
tile.dispatchEvent(Object.assign(new Event('stopped'), { reason }));
|
|
62
|
+
// Rendered again, it is a tile like any other.
|
|
63
|
+
tile.close().finally(() => halting.delete(tile));
|
|
64
|
+
}
|
|
65
|
+
const DOCK_STATUS = 'tiles-dock-status';
|
|
66
|
+
// What the tile's dock says of the user and that tile, asked for when it
|
|
67
|
+
// matters (see `DockStamp`): whether the user has just pressed or typed
|
|
68
|
+
// inside the tile. `ask()` resolves `undefined` when no answer comes within
|
|
69
|
+
// `ms`. A loading server from before its dock knew the question never
|
|
70
|
+
// answers; `answers` says whether this one ever has. The answer is only ever
|
|
71
|
+
// used to refuse, so that one that is not the dock's own gains nothing.
|
|
72
|
+
function dockStatus(tile, frame, ms) {
|
|
73
|
+
const win = frame.ownerDocument.defaultView;
|
|
74
|
+
const origin = new URL(tile.getLoadSource()).origin;
|
|
75
|
+
const waiting = new Map();
|
|
76
|
+
let n = 0;
|
|
77
|
+
let answers = false;
|
|
78
|
+
const onMessage = (ev) => {
|
|
79
|
+
if (ev.source !== frame.contentWindow || ev.origin !== origin || ev.data?.action !== DOCK_STATUS)
|
|
80
|
+
return;
|
|
81
|
+
const { id, activated } = ev.data.payload ?? {};
|
|
82
|
+
if (typeof activated !== 'boolean')
|
|
83
|
+
return;
|
|
84
|
+
answers = true;
|
|
85
|
+
const resolve = waiting.get(id);
|
|
86
|
+
if (!resolve)
|
|
87
|
+
return;
|
|
88
|
+
waiting.delete(id);
|
|
89
|
+
resolve(activated);
|
|
90
|
+
};
|
|
91
|
+
win.addEventListener('message', onMessage);
|
|
92
|
+
return {
|
|
93
|
+
get answers() { return answers; },
|
|
94
|
+
ask() {
|
|
95
|
+
const id = `d${++n}`;
|
|
96
|
+
return new Promise((resolve) => {
|
|
97
|
+
waiting.set(id, resolve);
|
|
98
|
+
win.setTimeout(() => { if (waiting.delete(id))
|
|
99
|
+
resolve(undefined); }, ms);
|
|
100
|
+
tile.post(DOCK_STATUS, { id });
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
stop() { win.removeEventListener('message', onMessage); },
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** `guardFocus`'s numbers where a host gives none. */
|
|
107
|
+
export const FOCUS_GUARD = Object.freeze({
|
|
108
|
+
tabMs: 400,
|
|
109
|
+
typingMs: 1000,
|
|
110
|
+
grabs: 3,
|
|
111
|
+
withinMs: 10000,
|
|
112
|
+
dockMs: 150,
|
|
113
|
+
pollMs: 100,
|
|
114
|
+
});
|
|
115
|
+
/**
|
|
116
|
+
* Gives back keyboard focus that a tile's script took, and leaves it with a
|
|
117
|
+
* user who went into the tile.
|
|
118
|
+
*
|
|
119
|
+
* Focus arriving in the frame is the user's doing when they tabbed into it,
|
|
120
|
+
* or pressed on it. The page sees the Tab key. It never sees the press: only
|
|
121
|
+
* that a mouse is over the frame, and not even that for a finger. So focus
|
|
122
|
+
* stays in the tile when a Tab was just pressed on the page; otherwise, when
|
|
123
|
+
* a mouse is in the frame (or the user has none) and they were not typing in
|
|
124
|
+
* a field of the page a moment ago, the tile's dock is asked, which knows
|
|
125
|
+
* whether the user pressed or typed inside this tile. In every other case,
|
|
126
|
+
* and when the dock says no, focus goes back to what had it.
|
|
127
|
+
*
|
|
128
|
+
* A dock that has answered before and now does not is taken to say no: its
|
|
129
|
+
* silence is not to be had by keeping it busy. One that has never answered
|
|
130
|
+
* (an older loading server's) leaves the guard with what the page can see:
|
|
131
|
+
* a mouse resting on the tile, or a finger, cannot be told from a press.
|
|
132
|
+
*
|
|
133
|
+
* Giving it back does not win against a tile that keeps taking it: a tile
|
|
134
|
+
* that does so `grabs` times within `withinMs` is reported (`onAbuse`) and
|
|
135
|
+
* stopped (see `GuardStoppedEvent`), unless told otherwise (`stop: false`).
|
|
136
|
+
*/
|
|
137
|
+
export function guardFocus(tile, frame, options = {}) {
|
|
138
|
+
const doc = frame.ownerDocument;
|
|
139
|
+
const win = doc.defaultView;
|
|
140
|
+
const pointer = watchPointer(doc);
|
|
141
|
+
const cfg = withDefaults(FOCUS_GUARD, options);
|
|
142
|
+
const dock = dockStatus(tile, frame, cfg.dockMs);
|
|
143
|
+
// Whether this dock answers at all is learnt while nothing is at stake:
|
|
144
|
+
// now, and when the tile's runtime says it is up, before the tile runs.
|
|
145
|
+
const probe = () => { dock.ask(); };
|
|
146
|
+
probe();
|
|
147
|
+
tile.addEventListener('load', probe);
|
|
148
|
+
// What had the keyboard before the tile did.
|
|
149
|
+
let holder = focused(doc);
|
|
150
|
+
if (holder === frame || holder === doc.body)
|
|
151
|
+
holder = null;
|
|
152
|
+
// Whether this window has the keyboard. Which element is the active one is
|
|
153
|
+
// not enough to go by: that says which element WOULD have it. The window's
|
|
154
|
+
// own focus and blur events say whether a frame inside it has taken it.
|
|
155
|
+
let winFocused = doc.hasFocus() && !holds(frame);
|
|
156
|
+
let inTile = false;
|
|
157
|
+
let accepted = false;
|
|
158
|
+
let lastTab = -Infinity;
|
|
159
|
+
let lastTyped = -Infinity;
|
|
160
|
+
let returned = 0;
|
|
161
|
+
let round = 0; // which giving back is under way: one that was overtaken does nothing more
|
|
162
|
+
let grabs = [];
|
|
163
|
+
let stopped = false;
|
|
164
|
+
// Whether `el` is the frame, or something the frame is in (the element
|
|
165
|
+
// whose shadow tree it is in is all the page is told of it, where that
|
|
166
|
+
// tree is closed).
|
|
167
|
+
const around = (el) => {
|
|
168
|
+
for (let node = frame; node; node = node.parentNode ?? node.host ?? null)
|
|
169
|
+
if (node === el)
|
|
170
|
+
return true;
|
|
171
|
+
return false;
|
|
172
|
+
};
|
|
173
|
+
const onFocusIn = (ev) => {
|
|
174
|
+
const target = (ev.composedPath?.()[0] ?? ev.target);
|
|
175
|
+
if (target && target !== frame)
|
|
176
|
+
holder = target;
|
|
177
|
+
// The Tab that brought the user here took them to something of the
|
|
178
|
+
// page's, not into the tile: it explains nothing that arrives there next.
|
|
179
|
+
if (target && !around(target))
|
|
180
|
+
lastTab = -Infinity;
|
|
181
|
+
};
|
|
182
|
+
// Whether a key picks the tile, for a host that has it wait to be picked:
|
|
183
|
+
// Enter or Space on the layer over it (see `engage`), which then puts the
|
|
184
|
+
// keyboard into the tile itself.
|
|
185
|
+
const picks = (ev) => {
|
|
186
|
+
if (ev.key !== 'Enter' && ev.key !== ' ')
|
|
187
|
+
return false;
|
|
188
|
+
const target = (ev.composedPath?.()[0] ?? ev.target);
|
|
189
|
+
return Boolean(target?.classList?.contains(ENGAGE_CLASS) && target.parentNode?.contains(frame));
|
|
190
|
+
};
|
|
191
|
+
const onKey = (ev) => {
|
|
192
|
+
if (!ev.isTrusted)
|
|
193
|
+
return;
|
|
194
|
+
// (Picking the tile from the keyboard is going into it by the keyboard, as tabbing into it is.)
|
|
195
|
+
if (ev.key === 'Tab' || picks(ev))
|
|
196
|
+
lastTab = now();
|
|
197
|
+
else if (editable(focused(doc)))
|
|
198
|
+
lastTyped = now();
|
|
199
|
+
};
|
|
200
|
+
// Whether focus that has just arrived in the frame can be the user's
|
|
201
|
+
// doing: `tab` when the page saw them tab, `ask` when only the dock can
|
|
202
|
+
// tell, `false` when it cannot be.
|
|
203
|
+
const judge = () => {
|
|
204
|
+
const t = now();
|
|
205
|
+
if (options.engaged && !options.engaged())
|
|
206
|
+
return false;
|
|
207
|
+
if (t - lastTab < cfg.tabMs)
|
|
208
|
+
return 'tab';
|
|
209
|
+
if (t - lastTyped < cfg.typingMs)
|
|
210
|
+
return false;
|
|
211
|
+
// A finger gives the page nothing to go by.
|
|
212
|
+
if (!pointer.hovers || pointer.state.kind === 'none')
|
|
213
|
+
return 'ask';
|
|
214
|
+
return pointer.inside(frame) ? 'ask' : false;
|
|
215
|
+
};
|
|
216
|
+
// Never from inside the blur that tells us: the browser is still moving
|
|
217
|
+
// focus into the frame then, and finishes the move after the handler
|
|
218
|
+
// returns, leaving the element "focused" in a window that has no keyboard.
|
|
219
|
+
// So it is done from a later task, checked by the window's own focus event,
|
|
220
|
+
// and done again if it did not take.
|
|
221
|
+
const takeBack = (tries, mine) => {
|
|
222
|
+
if (stopped || winFocused || mine !== round)
|
|
223
|
+
return;
|
|
224
|
+
if (tries === 0 && holder?.isConnected)
|
|
225
|
+
holder.focus({ preventScroll: true });
|
|
226
|
+
else {
|
|
227
|
+
frame.blur();
|
|
228
|
+
win.focus();
|
|
229
|
+
if (holder?.isConnected)
|
|
230
|
+
holder.focus({ preventScroll: true });
|
|
231
|
+
}
|
|
232
|
+
if (tries < 4)
|
|
233
|
+
win.setTimeout(() => takeBack(tries + 1, mine), 30);
|
|
234
|
+
};
|
|
235
|
+
const giveBack = (why) => {
|
|
236
|
+
returned++;
|
|
237
|
+
const t = now();
|
|
238
|
+
grabs = grabs.filter(g => t - g < cfg.withinMs);
|
|
239
|
+
grabs.push(t);
|
|
240
|
+
takeBack(0, ++round);
|
|
241
|
+
options.onReturn?.(why);
|
|
242
|
+
if (grabs.length < cfg.grabs)
|
|
243
|
+
return;
|
|
244
|
+
grabs = [];
|
|
245
|
+
options.onAbuse?.('focus', returned);
|
|
246
|
+
if (options.stop !== false)
|
|
247
|
+
halt(tile, frame, 'focus');
|
|
248
|
+
};
|
|
249
|
+
const check = () => {
|
|
250
|
+
if (stopped || winFocused || !doc.hasFocus())
|
|
251
|
+
return;
|
|
252
|
+
if (holds(frame))
|
|
253
|
+
inTile = true;
|
|
254
|
+
// The keyboard is in another frame of the page's. It left the tile, and
|
|
255
|
+
// what was decided when it came no longer stands: should it come back,
|
|
256
|
+
// that is judged afresh. (The window hears nothing of focus going from
|
|
257
|
+
// one frame to another.)
|
|
258
|
+
else
|
|
259
|
+
inTile = accepted = false;
|
|
260
|
+
if (!inTile || accepted)
|
|
261
|
+
return;
|
|
262
|
+
const how = judge();
|
|
263
|
+
if (!how) {
|
|
264
|
+
inTile = false;
|
|
265
|
+
return giveBack('script');
|
|
266
|
+
}
|
|
267
|
+
accepted = true;
|
|
268
|
+
round++; // nothing still on its way from an earlier giving back takes this one away
|
|
269
|
+
if (how === 'tab')
|
|
270
|
+
return;
|
|
271
|
+
// A second opinion, which takes a moment: the dock's.
|
|
272
|
+
dock.ask().then((acted) => {
|
|
273
|
+
if (acted === true || !accepted || winFocused || stopped)
|
|
274
|
+
return;
|
|
275
|
+
// No answer, from a dock that never gave one: nothing more to go by.
|
|
276
|
+
if (acted === undefined && !dock.answers)
|
|
277
|
+
return;
|
|
278
|
+
accepted = false;
|
|
279
|
+
inTile = false;
|
|
280
|
+
giveBack('dock');
|
|
281
|
+
});
|
|
282
|
+
};
|
|
283
|
+
const onWinBlur = () => {
|
|
284
|
+
winFocused = false;
|
|
285
|
+
// The keyboard has gone again, and that is for `check` to judge and to
|
|
286
|
+
// count. A giving back still making sure of an earlier time is over: it
|
|
287
|
+
// would take this one back as well, before it had been counted.
|
|
288
|
+
round++;
|
|
289
|
+
win.setTimeout(check, 0);
|
|
290
|
+
};
|
|
291
|
+
const onWinFocus = () => {
|
|
292
|
+
winFocused = true;
|
|
293
|
+
inTile = false;
|
|
294
|
+
accepted = false;
|
|
295
|
+
};
|
|
296
|
+
win.addEventListener('blur', onWinBlur);
|
|
297
|
+
win.addEventListener('focus', onWinFocus);
|
|
298
|
+
doc.addEventListener('focusin', onFocusIn, true);
|
|
299
|
+
doc.addEventListener('keydown', onKey, true);
|
|
300
|
+
// (The page's own timers throughout: they go with the page.)
|
|
301
|
+
const poll = win.setInterval(check, cfg.pollMs);
|
|
302
|
+
const stop = () => {
|
|
303
|
+
if (stopped)
|
|
304
|
+
return;
|
|
305
|
+
stopped = true;
|
|
306
|
+
win.clearInterval(poll);
|
|
307
|
+
dock.stop();
|
|
308
|
+
win.removeEventListener('blur', onWinBlur);
|
|
309
|
+
win.removeEventListener('focus', onWinFocus);
|
|
310
|
+
doc.removeEventListener('focusin', onFocusIn, true);
|
|
311
|
+
doc.removeEventListener('keydown', onKey, true);
|
|
312
|
+
tile.removeEventListener('close', stop);
|
|
313
|
+
tile.removeEventListener('load', probe);
|
|
314
|
+
};
|
|
315
|
+
// A tile that is closed has no frame left to guard.
|
|
316
|
+
tile.addEventListener('close', stop);
|
|
317
|
+
return { stop };
|
|
318
|
+
}
|
|
319
|
+
/** The numbers `clickGate` goes by where it is given none. First guesses. */
|
|
320
|
+
export const CLICK_GATE = Object.freeze({
|
|
321
|
+
/** How long presses are ignored, in milliseconds. */
|
|
322
|
+
ms: 600,
|
|
323
|
+
/** How far the pointer has to really move for them to be taken again sooner, in pixels. */
|
|
324
|
+
travel: 24,
|
|
325
|
+
});
|
|
326
|
+
const gates = new WeakMap();
|
|
327
|
+
const PRESSES = ['pointerdown', 'pointerup', 'mousedown', 'mouseup', 'click', 'dblclick', 'auxclick', 'contextmenu'];
|
|
328
|
+
// What keeps its own counsel about presses: the blocks the loader itself
|
|
329
|
+
// puts up, its link prompt, and anything the host marks.
|
|
330
|
+
const PASSES = `.${SHIELD_CLASS}, .tile-link-dialog, [data-tile-gate="pass"]`;
|
|
331
|
+
/**
|
|
332
|
+
* The page's own controls ignore presses and clicks for a moment after
|
|
333
|
+
* something moved them that neither the page nor, as far as it can see, the
|
|
334
|
+
* user did. One per document: `arm(ms)` starts the moment, a press that is
|
|
335
|
+
* ignored starts it again, and a real movement of the pointer across the
|
|
336
|
+
* page, which is the user aiming afresh at what is there now, ends it.
|
|
337
|
+
*
|
|
338
|
+
* A key is not a press: what the keyboard activates is where the focus is,
|
|
339
|
+
* and nothing that moved the page moved that. An element of the host's that
|
|
340
|
+
* must take presses regardless is marked `data-tile-gate="pass"`.
|
|
341
|
+
*/
|
|
342
|
+
export function clickGate(doc = document, options = {}) {
|
|
343
|
+
const known = gates.get(doc);
|
|
344
|
+
if (known)
|
|
345
|
+
return known;
|
|
346
|
+
const win = doc.defaultView;
|
|
347
|
+
const travel = options.travel ?? CLICK_GATE.travel;
|
|
348
|
+
let until = 0;
|
|
349
|
+
let ms = CLICK_GATE.ms;
|
|
350
|
+
let anchor;
|
|
351
|
+
const gate = {
|
|
352
|
+
get closed() { return now() < until; },
|
|
353
|
+
arm(forMs = CLICK_GATE.ms) {
|
|
354
|
+
ms = forMs;
|
|
355
|
+
until = Math.max(until, now() + forMs);
|
|
356
|
+
anchor = undefined;
|
|
357
|
+
},
|
|
358
|
+
open() { until = 0; },
|
|
359
|
+
};
|
|
360
|
+
const swallow = (ev) => {
|
|
361
|
+
const e = ev;
|
|
362
|
+
if (!e.isTrusted || now() >= until)
|
|
363
|
+
return;
|
|
364
|
+
if (e.type === 'click' && e.detail === 0)
|
|
365
|
+
return; // the keyboard's
|
|
366
|
+
if (e.target?.closest?.(PASSES))
|
|
367
|
+
return;
|
|
368
|
+
e.stopImmediatePropagation();
|
|
369
|
+
// 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.
|
|
370
|
+
if (!(e.pointerType === 'touch' && e.type !== 'click'))
|
|
371
|
+
e.preventDefault();
|
|
372
|
+
if (e.type === 'pointerdown' || e.type === 'click')
|
|
373
|
+
until = now() + ms;
|
|
374
|
+
};
|
|
375
|
+
for (const type of PRESSES)
|
|
376
|
+
win.addEventListener(type, swallow, true);
|
|
377
|
+
win.addEventListener('pointermove', (ev) => {
|
|
378
|
+
const e = ev;
|
|
379
|
+
if (!e.isTrusted || now() >= until || e.pointerType === 'touch')
|
|
380
|
+
return;
|
|
381
|
+
if (!anchor) {
|
|
382
|
+
anchor = { x: e.clientX, y: e.clientY };
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (Math.hypot(e.clientX - anchor.x, e.clientY - anchor.y) >= travel)
|
|
386
|
+
until = 0;
|
|
387
|
+
}, { capture: true, passive: true });
|
|
388
|
+
gates.set(doc, gate);
|
|
389
|
+
return gate;
|
|
390
|
+
}
|
|
391
|
+
/** `guardScroll`'s numbers where a host gives none. */
|
|
392
|
+
export const SCROLL_GUARD = Object.freeze({
|
|
393
|
+
inputMs: 1200,
|
|
394
|
+
flingMs: 2500,
|
|
395
|
+
slack: 60,
|
|
396
|
+
pulls: 3,
|
|
397
|
+
withinMs: 10000,
|
|
398
|
+
gateMs: CLICK_GATE.ms,
|
|
399
|
+
jumpPx: 150,
|
|
400
|
+
jumpGateMs: 2500,
|
|
401
|
+
});
|
|
402
|
+
// What a key scrolls by: pixels, pages (viewport heights), or all the way.
|
|
403
|
+
const SCROLL_KEYS = { ArrowDown: ['px', 40], ArrowUp: ['px', -40], PageDown: ['page', 1], PageUp: ['page', -1], ' ': ['page', 1], End: ['all', 1], Home: ['all', -1] };
|
|
404
|
+
const framed = (el) => el?.localName === 'iframe' || el?.localName === 'frame';
|
|
405
|
+
// The frames of a page that have a scroll guard on them, each with whether
|
|
406
|
+
// input can reach it: always, unless it waits to be picked. A guard is one
|
|
407
|
+
// tile's, and the page it watches is everyone's. So each has to know of the
|
|
408
|
+
// others: which of two tiles at the viewport moved the page cannot be told.
|
|
409
|
+
const scrollGuarded = new WeakMap();
|
|
410
|
+
// The calls a page scrolls itself with, wrapped so that each says so first:
|
|
411
|
+
// once for a page, however many of its guards asked, and put back as they
|
|
412
|
+
// were when the last of those stops.
|
|
413
|
+
const declaring = new WeakMap();
|
|
414
|
+
function declareOwn(win, tell) {
|
|
415
|
+
let shared = declaring.get(win);
|
|
416
|
+
if (!shared) {
|
|
417
|
+
const made = { tell: new Set(), restore: [] };
|
|
418
|
+
const wrap = (obj, name) => {
|
|
419
|
+
const original = obj?.[name];
|
|
420
|
+
if (!obj || typeof original !== 'function')
|
|
421
|
+
return;
|
|
422
|
+
obj[name] = function (...args) {
|
|
423
|
+
// (A focus() that says it will not scroll explains no scroll: the
|
|
424
|
+
// focus guard gives the keyboard back with one, whenever a tile
|
|
425
|
+
// takes it.)
|
|
426
|
+
if (!(name === 'focus' && args[0]?.preventScroll))
|
|
427
|
+
for (const fn of made.tell)
|
|
428
|
+
fn();
|
|
429
|
+
return original.apply(this, args);
|
|
430
|
+
};
|
|
431
|
+
made.restore.push(() => { obj[name] = original; });
|
|
432
|
+
};
|
|
433
|
+
const w = win;
|
|
434
|
+
for (const name of ['scrollTo', 'scrollBy', 'scroll']) {
|
|
435
|
+
wrap(w, name);
|
|
436
|
+
wrap(w.Element.prototype, name);
|
|
437
|
+
}
|
|
438
|
+
wrap(w.Element.prototype, 'scrollIntoView');
|
|
439
|
+
wrap(w.HTMLElement.prototype, 'focus');
|
|
440
|
+
declaring.set(win, made);
|
|
441
|
+
shared = made;
|
|
442
|
+
}
|
|
443
|
+
const mine = shared;
|
|
444
|
+
mine.tell.add(tell);
|
|
445
|
+
return () => {
|
|
446
|
+
mine.tell.delete(tell);
|
|
447
|
+
if (mine.tell.size || declaring.get(win) !== mine)
|
|
448
|
+
return;
|
|
449
|
+
mine.restore.forEach(fn => fn());
|
|
450
|
+
declaring.delete(win);
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Accounts for every move of the page a tile is in, and takes back the
|
|
455
|
+
* tile's own.
|
|
456
|
+
*
|
|
457
|
+
* The page's vertical scroll position is followed, and each move is one of:
|
|
458
|
+
*
|
|
459
|
+
* - `user`: the page saw the input for it (a wheel, a key, a finger, a
|
|
460
|
+
* mouse button held down, a pointer on the scrollbar), and the move goes
|
|
461
|
+
* that way and no further than the input asked: a wheel turned by 100
|
|
462
|
+
* does not explain a jump of 1500.
|
|
463
|
+
* - `own`: the page said it would (`expect()`, or `own: 'patch'`), or it
|
|
464
|
+
* navigated, or the user tabbed on.
|
|
465
|
+
* - `layout`: the document's height changed, by as much as the move or
|
|
466
|
+
* more. It stands. Where the tile's own box changed with it, the gate is
|
|
467
|
+
* armed: the move may be the tile's doing after all.
|
|
468
|
+
* - `ambiguous`: none of those, but the page cannot rule the user out. A
|
|
469
|
+
* mouse was inside the tile and may have been wheeling there, the
|
|
470
|
+
* keyboard is in the tile, or a finger may be on it. The same goes for
|
|
471
|
+
* any other frame of the page's: what the user does in one scrolls the
|
|
472
|
+
* page too, and the page sees none of it. The move stands, and the gate
|
|
473
|
+
* is armed: what slid under the pointer takes no press for a moment
|
|
474
|
+
* (`gateMs`; `jumpGateMs` after a jump), or until the pointer has really
|
|
475
|
+
* moved.
|
|
476
|
+
* - `tile`: none of those, and the tile's frame is at or in the viewport
|
|
477
|
+
* once it is done, which is where its own scrolling leaves it. It is
|
|
478
|
+
* taken back before it is painted. (The gate is armed only where the page
|
|
479
|
+
* could not be put back whole: a move that is undone slid nothing under
|
|
480
|
+
* the pointer.)
|
|
481
|
+
* - `elsewhere`: unexplained, and nowhere near the tile. Left alone (the
|
|
482
|
+
* browser and assistive tools scroll pages too), gate armed.
|
|
483
|
+
*
|
|
484
|
+
* A tile that pulls the page back against the way the user has just
|
|
485
|
+
* scrolled is doing it on purpose: `pulls` of those within `withinMs` are
|
|
486
|
+
* reported (`onAbuse`), and the tile is stopped (see `GuardStoppedEvent`)
|
|
487
|
+
* unless told otherwise (`stop: false`). Where another tile with a guard on
|
|
488
|
+
* it is at the viewport as well, which of them it was cannot be told: the
|
|
489
|
+
* move is taken back all the same, and neither is held to it.
|
|
490
|
+
*
|
|
491
|
+
* The page has to say when it scrolls itself, or let the guard wrap its
|
|
492
|
+
* calls: an unexplained move of the page's own that ends at the tile is
|
|
493
|
+
* taken back like the tile's. Only the document's own scrolling is
|
|
494
|
+
* followed, not that of a box inside it.
|
|
495
|
+
*/
|
|
496
|
+
export function guardScroll(tile, frame, options = {}) {
|
|
497
|
+
const doc = frame.ownerDocument;
|
|
498
|
+
const win = doc.defaultView;
|
|
499
|
+
const pointer = watchPointer(doc);
|
|
500
|
+
const cfg = { ...withDefaults(SCROLL_GUARD, options), undo: options.undo !== false };
|
|
501
|
+
const gate = options.gate === false ? undefined : (options.gate ?? clickGate(doc));
|
|
502
|
+
const root = () => doc.scrollingElement || doc.documentElement;
|
|
503
|
+
// How much of the frame is in the viewport, in pixels of height.
|
|
504
|
+
const visible = () => {
|
|
505
|
+
const r = frame.getBoundingClientRect();
|
|
506
|
+
return Math.max(0, Math.min(r.bottom, win.innerHeight) - Math.max(r.top, 0));
|
|
507
|
+
};
|
|
508
|
+
// Whether an element is at or in the viewport.
|
|
509
|
+
const at = (el) => {
|
|
510
|
+
const r = el.getBoundingClientRect();
|
|
511
|
+
return r.height > 0 && r.bottom >= -2 && r.top <= win.innerHeight + 2;
|
|
512
|
+
};
|
|
513
|
+
const near = () => at(frame);
|
|
514
|
+
// Whether a point was in a frame's box before the page moved by `d`.
|
|
515
|
+
const within = (el, p, d) => {
|
|
516
|
+
const r = el.getBoundingClientRect();
|
|
517
|
+
const top = r.top + d;
|
|
518
|
+
return p.x >= r.left - 2 && p.x <= r.right + 2 && p.y >= top - 2 && p.y <= top + r.height + 2;
|
|
519
|
+
};
|
|
520
|
+
const others = scrollGuarded.get(doc) ?? new Map();
|
|
521
|
+
scrollGuarded.set(doc, others);
|
|
522
|
+
others.set(frame, () => !options.engaged || options.engaged());
|
|
523
|
+
// Whether ours is the only guarded tile at the viewport: the only one
|
|
524
|
+
// that can have brought the page to where it is.
|
|
525
|
+
const alone = () => {
|
|
526
|
+
for (const el of others.keys())
|
|
527
|
+
if (el !== frame && el.isConnected && at(el))
|
|
528
|
+
return false;
|
|
529
|
+
return true;
|
|
530
|
+
};
|
|
531
|
+
// Whether a finger may have been on another frame of the page's: one that
|
|
532
|
+
// was in view before the page moved by `d`, and that input reaches. (A
|
|
533
|
+
// frame in a shadow tree is only known of here if it has a guard.)
|
|
534
|
+
const fingerElsewhere = (d) => {
|
|
535
|
+
for (const el of new Set([...doc.getElementsByTagName('iframe'), ...others.keys()])) {
|
|
536
|
+
if (el === frame || !el.isConnected || others.get(el)?.() === false)
|
|
537
|
+
continue;
|
|
538
|
+
const r = el.getBoundingClientRect();
|
|
539
|
+
const top = r.top + d;
|
|
540
|
+
if (r.width > 0 && r.height > 0 && top < win.innerHeight && top + r.height > 0)
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
return false;
|
|
544
|
+
};
|
|
545
|
+
let y = win.scrollY;
|
|
546
|
+
let docHeight = root().scrollHeight;
|
|
547
|
+
let frameHeight = frame.getBoundingClientRect().height;
|
|
548
|
+
let seen = visible();
|
|
549
|
+
// What the user has asked for and the page has not scrolled yet, in pixels (signed).
|
|
550
|
+
let credit = 0;
|
|
551
|
+
let inputAt = -Infinity;
|
|
552
|
+
let lastDir = 0; // the way the user last scrolled
|
|
553
|
+
let lastDirAt = -Infinity;
|
|
554
|
+
let down = false; // a mouse button is down on the page: a scrollbar, a drag, autoscroll
|
|
555
|
+
let downAt = -Infinity;
|
|
556
|
+
let flingUntil = -Infinity;
|
|
557
|
+
let flingDir = 0;
|
|
558
|
+
let expected = -Infinity;
|
|
559
|
+
let restoring; // where the page was put back to, until that move is heard of
|
|
560
|
+
let undone = 0;
|
|
561
|
+
let tookBackAt = -Infinity; // when a move of the tile's was last taken back
|
|
562
|
+
let typedAt = -Infinity; // when the user last typed into a field of the page's
|
|
563
|
+
let pulls = [];
|
|
564
|
+
let stopped = false;
|
|
565
|
+
let touchY;
|
|
566
|
+
// Where the page was when the pointer was last seen going into a frame.
|
|
567
|
+
let entered;
|
|
568
|
+
const onPointer = () => {
|
|
569
|
+
const p = pointer.state;
|
|
570
|
+
if (p.kind !== 'frame')
|
|
571
|
+
entered = undefined;
|
|
572
|
+
else if (entered?.at !== p.at)
|
|
573
|
+
entered = { at: p.at, y: win.scrollY };
|
|
574
|
+
};
|
|
575
|
+
onPointer();
|
|
576
|
+
const offPointer = pointer.onChange(onPointer);
|
|
577
|
+
const ask = (px) => {
|
|
578
|
+
const t = now();
|
|
579
|
+
if (t - inputAt > cfg.inputMs || Math.sign(px) !== Math.sign(credit))
|
|
580
|
+
credit = 0; // stale, or the user turned round
|
|
581
|
+
credit += px;
|
|
582
|
+
inputAt = t;
|
|
583
|
+
lastDir = Math.sign(px);
|
|
584
|
+
lastDirAt = t;
|
|
585
|
+
};
|
|
586
|
+
const onWheel = (ev) => {
|
|
587
|
+
const e = ev;
|
|
588
|
+
if (e.isTrusted && e.deltaY)
|
|
589
|
+
ask(e.deltaY * (e.deltaMode === 1 ? 40 : e.deltaMode === 2 ? win.innerHeight : 1));
|
|
590
|
+
};
|
|
591
|
+
const onKey = (ev) => {
|
|
592
|
+
const e = ev;
|
|
593
|
+
if (!e.isTrusted || holds(frame))
|
|
594
|
+
return;
|
|
595
|
+
if (e.key === 'Tab') {
|
|
596
|
+
expected = Math.max(expected, now() + 300);
|
|
597
|
+
return;
|
|
598
|
+
} // focus moving on may scroll to it
|
|
599
|
+
// A key typed into a field of the page's scrolls nothing itself, but the
|
|
600
|
+
// browser may move the page to keep the caret in view.
|
|
601
|
+
if (editable(focused(doc))) {
|
|
602
|
+
typedAt = now();
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
const k = SCROLL_KEYS[e.key];
|
|
606
|
+
if (!k)
|
|
607
|
+
return;
|
|
608
|
+
const sign = e.key === ' ' && e.shiftKey ? -1 : 1;
|
|
609
|
+
ask(sign * (k[0] === 'px' ? k[1] : k[0] === 'page' ? k[1] * win.innerHeight : k[1] * 1e9));
|
|
610
|
+
};
|
|
611
|
+
const onTouchStart = (ev) => {
|
|
612
|
+
const e = ev;
|
|
613
|
+
if (!e.isTrusted)
|
|
614
|
+
return;
|
|
615
|
+
touchY = e.touches[0]?.clientY;
|
|
616
|
+
flingUntil = -Infinity;
|
|
617
|
+
};
|
|
618
|
+
const onTouchMove = (ev) => {
|
|
619
|
+
const e = ev;
|
|
620
|
+
if (!e.isTrusted)
|
|
621
|
+
return;
|
|
622
|
+
const ty = e.touches[0]?.clientY;
|
|
623
|
+
if (touchY !== undefined && ty !== undefined && ty !== touchY)
|
|
624
|
+
ask(touchY - ty);
|
|
625
|
+
touchY = ty;
|
|
626
|
+
};
|
|
627
|
+
// The finger is up; the page may coast on, the way it was going.
|
|
628
|
+
const onTouchEnd = (ev) => {
|
|
629
|
+
if (!ev.isTrusted || !lastDir || now() - lastDirAt >= 300)
|
|
630
|
+
return;
|
|
631
|
+
flingUntil = now() + cfg.flingMs;
|
|
632
|
+
flingDir = lastDir;
|
|
633
|
+
};
|
|
634
|
+
const onDown = (ev) => { if (ev.isTrusted && ev.pointerType !== 'touch') {
|
|
635
|
+
down = true;
|
|
636
|
+
downAt = now();
|
|
637
|
+
} };
|
|
638
|
+
const onUp = (ev) => { if (ev.isTrusted && ev.pointerType !== 'touch') {
|
|
639
|
+
down = false;
|
|
640
|
+
downAt = now();
|
|
641
|
+
} };
|
|
642
|
+
const onNav = () => { expected = now() + 1500; };
|
|
643
|
+
const byUser = (d, t) => {
|
|
644
|
+
// A button held down on the page, or just let go: the page may scroll
|
|
645
|
+
// for it (a drag, a click on something that scrolls). Not while the tile
|
|
646
|
+
// has just been trying: its next try would pass for the page's answer to
|
|
647
|
+
// a press that had nothing to do with it.
|
|
648
|
+
if ((down || t - downAt < 300) && t - tookBackAt >= cfg.gateMs)
|
|
649
|
+
return true;
|
|
650
|
+
// The user is typing in a field of the page's, and the browser keeps the
|
|
651
|
+
// caret in view: a line or two at a time, with the field still there to
|
|
652
|
+
// see afterwards. (Under the same condition: a tile that keeps trying
|
|
653
|
+
// does not get to creep the page along under the typing.)
|
|
654
|
+
if (t - typedAt < 300 && Math.abs(d) <= cfg.slack && t - tookBackAt >= cfg.gateMs) {
|
|
655
|
+
const field = focused(doc);
|
|
656
|
+
if (field && editable(field) && at(field))
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
659
|
+
// The viewport's own scrollbar gives the page no events: a pointer last seen at the right edge may be on it.
|
|
660
|
+
const p = pointer.state;
|
|
661
|
+
if (p.kind === 'host' && p.type !== 'touch' && p.x >= Math.min(root().clientWidth, win.innerWidth - 18))
|
|
662
|
+
return true;
|
|
663
|
+
if (t < flingUntil && Math.sign(d) === flingDir)
|
|
664
|
+
return true;
|
|
665
|
+
if (t - inputAt > cfg.inputMs || Math.sign(d) !== Math.sign(credit))
|
|
666
|
+
return false;
|
|
667
|
+
if (Math.abs(d) > Math.abs(credit) + cfg.slack)
|
|
668
|
+
return false;
|
|
669
|
+
credit -= d;
|
|
670
|
+
if (Math.sign(credit) !== Math.sign(d))
|
|
671
|
+
credit = 0; // spent (the slack is not carried over)
|
|
672
|
+
return true;
|
|
673
|
+
};
|
|
674
|
+
// Whether a mouse the page last saw going into `el` can still have been
|
|
675
|
+
// in it before the page moved by `d`. The page is told when a mouse goes
|
|
676
|
+
// into a frame, and where; nothing of where it goes from there. So it may
|
|
677
|
+
// be resting anywhere in the frame, and is still in it for as long as the
|
|
678
|
+
// page has not moved by the frame's own height since. One that did not
|
|
679
|
+
// move there is another matter: the frame came to it, and it is where it
|
|
680
|
+
// was, which is all the frame's box before the move is asked.
|
|
681
|
+
const mouseIn = (el, d) => {
|
|
682
|
+
const p = pointer.state;
|
|
683
|
+
if (!p.moved)
|
|
684
|
+
return within(el, p, d);
|
|
685
|
+
const r = el.getBoundingClientRect();
|
|
686
|
+
const top = r.top + d;
|
|
687
|
+
const shifted = Math.abs(y - (entered?.y ?? y));
|
|
688
|
+
return r.width > 0 && shifted < r.height && top < win.innerHeight && top + r.height > 0;
|
|
689
|
+
};
|
|
690
|
+
// Could the user have scrolled through a frame, where the page sees no
|
|
691
|
+
// input? Judged by where things were BEFORE the move: a tile that scrolls
|
|
692
|
+
// itself under a pointer resting on the page is then "hovered", and that
|
|
693
|
+
// must not count for it. `d` is how far the page has just moved.
|
|
694
|
+
const cannotTell = (d) => {
|
|
695
|
+
const p = pointer.state;
|
|
696
|
+
const fingers = !pointer.hovers || p.kind === 'none';
|
|
697
|
+
const into = !fingers && p.kind === 'frame' ? pointer.frame : undefined;
|
|
698
|
+
// The page's other frames first. What the user does in one scrolls the
|
|
699
|
+
// page as well, and is no more the page's to see than what they do in
|
|
700
|
+
// the tile: the keyboard in one, a mouse that went into one, a finger
|
|
701
|
+
// that may be on one.
|
|
702
|
+
const active = focused(doc);
|
|
703
|
+
if (framed(active) && active !== frame)
|
|
704
|
+
return true;
|
|
705
|
+
if (into && into !== frame && mouseIn(into, d))
|
|
706
|
+
return true;
|
|
707
|
+
if (fingers && fingerElsewhere(d))
|
|
708
|
+
return true;
|
|
709
|
+
if (options.engaged && !options.engaged())
|
|
710
|
+
return false; // no input reaches a tile that is not picked
|
|
711
|
+
if (holds(frame))
|
|
712
|
+
return true; // keys pressed in the tile scroll the page too
|
|
713
|
+
if (fingers)
|
|
714
|
+
return seen > 0; // a finger may be on it, if it was in view
|
|
715
|
+
if (into === frame)
|
|
716
|
+
return mouseIn(frame, d); // a mouse that went into it
|
|
717
|
+
return p.kind === 'host' && within(frame, p, d); // or one the page sees over it (it lies under something of the page's)
|
|
718
|
+
};
|
|
719
|
+
const onScroll = (ev) => {
|
|
720
|
+
if (stopped || (ev.target !== doc && ev.target !== root()))
|
|
721
|
+
return;
|
|
722
|
+
const to = win.scrollY;
|
|
723
|
+
const h = root().scrollHeight;
|
|
724
|
+
const fh = frame.getBoundingClientRect().height;
|
|
725
|
+
// Our own move, putting the page back. Unless it has been moved again
|
|
726
|
+
// before this was heard of: that is accounted for like any other move,
|
|
727
|
+
// from where we had put it.
|
|
728
|
+
const ours = restoring !== undefined && Math.abs(to - restoring) < 1;
|
|
729
|
+
if (restoring !== undefined)
|
|
730
|
+
y = restoring;
|
|
731
|
+
restoring = undefined;
|
|
732
|
+
if (ours || to === y) {
|
|
733
|
+
y = to;
|
|
734
|
+
docHeight = h;
|
|
735
|
+
frameHeight = fh;
|
|
736
|
+
seen = visible();
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
const t = now();
|
|
740
|
+
const from = y;
|
|
741
|
+
const d = to - from;
|
|
742
|
+
const against = lastDir !== 0 && t - lastDirAt < cfg.inputMs && Math.sign(d) !== lastDir;
|
|
743
|
+
// A jump in one go is not how a wheel or a finger moves a page: what it
|
|
744
|
+
// put under the pointer stays untouchable for longer.
|
|
745
|
+
const hush = Math.abs(d) > cfg.jumpPx ? cfg.jumpGateMs : cfg.gateMs;
|
|
746
|
+
let verdict;
|
|
747
|
+
if (t < expected)
|
|
748
|
+
verdict = 'own';
|
|
749
|
+
else if (h !== docHeight && Math.abs(d) <= Math.abs(h - docHeight) + cfg.slack) {
|
|
750
|
+
verdict = 'layout';
|
|
751
|
+
// The tile's own box changed with it: this may be its doing after all.
|
|
752
|
+
if (fh !== frameHeight)
|
|
753
|
+
gate?.arm(hush);
|
|
754
|
+
}
|
|
755
|
+
else if (byUser(d, t))
|
|
756
|
+
verdict = 'user';
|
|
757
|
+
else if (cannotTell(d))
|
|
758
|
+
verdict = 'ambiguous';
|
|
759
|
+
else if (near())
|
|
760
|
+
verdict = 'tile';
|
|
761
|
+
else
|
|
762
|
+
verdict = 'elsewhere';
|
|
763
|
+
docHeight = h;
|
|
764
|
+
frameHeight = fh;
|
|
765
|
+
const take = verdict === 'tile' && cfg.undo;
|
|
766
|
+
// Asked before the page is put back: it is where the move left things
|
|
767
|
+
// that says which tiles can have made it.
|
|
768
|
+
const mine = take && alone();
|
|
769
|
+
// Whether the page is back where it was, to the pixel: nothing has then
|
|
770
|
+
// moved under the pointer.
|
|
771
|
+
let whole = false;
|
|
772
|
+
if (take) {
|
|
773
|
+
undone++;
|
|
774
|
+
credit = 0;
|
|
775
|
+
tookBackAt = t;
|
|
776
|
+
win.scrollTo({ top: Math.max(0, Math.min(from, h - win.innerHeight)), left: win.scrollX, behavior: 'instant' });
|
|
777
|
+
restoring = win.scrollY;
|
|
778
|
+
whole = Math.abs(restoring - from) < 1;
|
|
779
|
+
}
|
|
780
|
+
else {
|
|
781
|
+
y = to;
|
|
782
|
+
seen = visible();
|
|
783
|
+
}
|
|
784
|
+
// What a move left standing slid under the pointer takes no press for a
|
|
785
|
+
// moment. One that was put back whole slid nothing anywhere: were it to
|
|
786
|
+
// close the gate, a tile that kept asking would keep the page from
|
|
787
|
+
// taking any press at all.
|
|
788
|
+
if (verdict === 'ambiguous' || verdict === 'elsewhere' || (verdict === 'tile' && !take))
|
|
789
|
+
gate?.arm(hush);
|
|
790
|
+
else if (take && !whole)
|
|
791
|
+
gate?.arm(cfg.gateMs);
|
|
792
|
+
options.onMove?.({ from, to, verdict, undone: take });
|
|
793
|
+
// Taken back whoever made it. Held against this tile only where no
|
|
794
|
+
// other can have: a tile is not stopped for what its neighbour did.
|
|
795
|
+
if (!mine || !against)
|
|
796
|
+
return;
|
|
797
|
+
pulls = pulls.filter(p => t - p < cfg.withinMs);
|
|
798
|
+
pulls.push(t);
|
|
799
|
+
if (pulls.length < cfg.pulls)
|
|
800
|
+
return;
|
|
801
|
+
pulls = [];
|
|
802
|
+
options.onAbuse?.('scroll', undone);
|
|
803
|
+
if (options.stop !== false)
|
|
804
|
+
halt(tile, frame, 'scroll');
|
|
805
|
+
};
|
|
806
|
+
// The page's own scrolling, declared for it: the calls a page scrolls
|
|
807
|
+
// itself with are wrapped so that each says so first. Only when asked: a
|
|
808
|
+
// page that would rather not have them touched calls expect() itself.
|
|
809
|
+
const undeclare = options.own === 'patch' ? declareOwn(win, () => { expected = Math.max(expected, now() + 1000); }) : undefined;
|
|
810
|
+
const opts = { capture: true, passive: true };
|
|
811
|
+
const onResize = () => {
|
|
812
|
+
docHeight = root().scrollHeight;
|
|
813
|
+
frameHeight = frame.getBoundingClientRect().height;
|
|
814
|
+
seen = visible();
|
|
815
|
+
y = win.scrollY;
|
|
816
|
+
};
|
|
817
|
+
doc.addEventListener('wheel', onWheel, opts);
|
|
818
|
+
doc.addEventListener('keydown', onKey, true);
|
|
819
|
+
doc.addEventListener('touchstart', onTouchStart, opts);
|
|
820
|
+
doc.addEventListener('touchmove', onTouchMove, opts);
|
|
821
|
+
doc.addEventListener('touchend', onTouchEnd, opts);
|
|
822
|
+
doc.addEventListener('pointerdown', onDown, opts);
|
|
823
|
+
doc.addEventListener('pointerup', onUp, opts);
|
|
824
|
+
win.addEventListener('hashchange', onNav);
|
|
825
|
+
win.addEventListener('popstate', onNav);
|
|
826
|
+
win.addEventListener('resize', onResize);
|
|
827
|
+
doc.addEventListener('scroll', onScroll, opts);
|
|
828
|
+
const stop = () => {
|
|
829
|
+
if (stopped)
|
|
830
|
+
return;
|
|
831
|
+
stopped = true;
|
|
832
|
+
undeclare?.();
|
|
833
|
+
offPointer();
|
|
834
|
+
others.delete(frame);
|
|
835
|
+
doc.removeEventListener('wheel', onWheel, opts);
|
|
836
|
+
doc.removeEventListener('keydown', onKey, true);
|
|
837
|
+
doc.removeEventListener('touchstart', onTouchStart, opts);
|
|
838
|
+
doc.removeEventListener('touchmove', onTouchMove, opts);
|
|
839
|
+
doc.removeEventListener('touchend', onTouchEnd, opts);
|
|
840
|
+
doc.removeEventListener('pointerdown', onDown, opts);
|
|
841
|
+
doc.removeEventListener('pointerup', onUp, opts);
|
|
842
|
+
doc.removeEventListener('scroll', onScroll, opts);
|
|
843
|
+
win.removeEventListener('hashchange', onNav);
|
|
844
|
+
win.removeEventListener('popstate', onNav);
|
|
845
|
+
win.removeEventListener('resize', onResize);
|
|
846
|
+
tile.removeEventListener('close', stop);
|
|
847
|
+
};
|
|
848
|
+
// A tile that is closed has no frame left to guard.
|
|
849
|
+
tile.addEventListener('close', stop);
|
|
850
|
+
return {
|
|
851
|
+
expect(ms = 1000) { expected = Math.max(expected, now() + ms); },
|
|
852
|
+
stop,
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
/** The class of the layer's element. */
|
|
856
|
+
export const ENGAGE_CLASS = 'tile-engage';
|
|
857
|
+
/**
|
|
858
|
+
* "Press to use": until the user picks the tile, a layer of the host's lies
|
|
859
|
+
* over it. The tile runs and shows, and gets no input. A wheel or a finger
|
|
860
|
+
* on it scrolls the page, the page sees every pointer that crosses it, and
|
|
861
|
+
* focus arriving in the frame is by definition the tile's own doing. A
|
|
862
|
+
* press on the layer picks the tile (that press is spent on it: nothing can
|
|
863
|
+
* hand an event on to another origin's frame). A press anywhere else on the
|
|
864
|
+
* page, the tile leaving the viewport, or Escape, puts the layer back.
|
|
865
|
+
*
|
|
866
|
+
* Until it is picked the tile is not a stop on the way through the page with
|
|
867
|
+
* Tab either (its frame has `tabindex="-1"` meanwhile): the layer is, right
|
|
868
|
+
* after it, and Enter or Space there picks the tile and goes into it.
|
|
869
|
+
*
|
|
870
|
+
* The layer paints, though too faintly to see: a block that paints nothing
|
|
871
|
+
* does not take a press over a frame. `stop()` takes it away for good.
|
|
872
|
+
*/
|
|
873
|
+
export function engage(tile, frame, options = {}) {
|
|
874
|
+
const doc = frame.ownerDocument;
|
|
875
|
+
const win = doc.defaultView;
|
|
876
|
+
const wrapper = options.wrapper ?? frame.parentElement;
|
|
877
|
+
if (!wrapper)
|
|
878
|
+
throw new Error('engage() needs the frame to be in an element that holds it.');
|
|
879
|
+
const position = win.getComputedStyle(wrapper).position;
|
|
880
|
+
if (!position || position === 'static')
|
|
881
|
+
wrapper.style.position = 'relative';
|
|
882
|
+
const layer = doc.createElement('div');
|
|
883
|
+
layer.className = ENGAGE_CLASS;
|
|
884
|
+
layer.setAttribute('role', 'button');
|
|
885
|
+
layer.setAttribute('tabindex', '0');
|
|
886
|
+
layer.setAttribute('aria-label', options.label ?? 'Use this tile');
|
|
887
|
+
layer.style.cssText = 'position:absolute;inset:0;z-index:1;background:rgba(127,127,127,0.03);cursor:pointer;';
|
|
888
|
+
let engaged = false;
|
|
889
|
+
let stopped = false;
|
|
890
|
+
// A tile that is not picked is not to be tabbed into: Tab would take the
|
|
891
|
+
// keyboard past the layer and into it. (With a guard on the keyboard that
|
|
892
|
+
// would be given back, every time, and count against the tile.)
|
|
893
|
+
const tabindex = frame.getAttribute('tabindex');
|
|
894
|
+
const reach = (on) => {
|
|
895
|
+
if (!on)
|
|
896
|
+
frame.setAttribute('tabindex', '-1');
|
|
897
|
+
else if (tabindex === null)
|
|
898
|
+
frame.removeAttribute('tabindex');
|
|
899
|
+
else
|
|
900
|
+
frame.setAttribute('tabindex', tabindex);
|
|
901
|
+
};
|
|
902
|
+
const set = (on, why) => {
|
|
903
|
+
if (on === engaged || stopped)
|
|
904
|
+
return;
|
|
905
|
+
engaged = on;
|
|
906
|
+
layer.style.display = on ? 'none' : 'block';
|
|
907
|
+
reach(on);
|
|
908
|
+
if (!on && holds(frame))
|
|
909
|
+
frame.blur();
|
|
910
|
+
options.onChange?.(on, why);
|
|
911
|
+
};
|
|
912
|
+
layer.addEventListener('click', (ev) => { if (ev.isTrusted)
|
|
913
|
+
set(true, 'picked'); });
|
|
914
|
+
layer.addEventListener('keydown', (ev) => {
|
|
915
|
+
if (!ev.isTrusted || (ev.key !== 'Enter' && ev.key !== ' '))
|
|
916
|
+
return;
|
|
917
|
+
ev.preventDefault();
|
|
918
|
+
set(true, 'picked');
|
|
919
|
+
frame.focus();
|
|
920
|
+
});
|
|
921
|
+
const onDown = (ev) => { if (ev.isTrusted && engaged && !wrapper.contains((ev.composedPath?.()[0] ?? ev.target)))
|
|
922
|
+
set(false, 'pressed elsewhere'); };
|
|
923
|
+
const onKey = (ev) => { if (ev.isTrusted && engaged && ev.key === 'Escape')
|
|
924
|
+
set(false, 'escape'); };
|
|
925
|
+
doc.addEventListener('pointerdown', onDown, true);
|
|
926
|
+
doc.addEventListener('keydown', onKey, true);
|
|
927
|
+
const seen = typeof win.IntersectionObserver === 'function'
|
|
928
|
+
? new win.IntersectionObserver((entries) => { if (entries.some(en => !en.isIntersecting))
|
|
929
|
+
set(false, 'out of view'); })
|
|
930
|
+
: undefined;
|
|
931
|
+
seen?.observe(frame);
|
|
932
|
+
wrapper.append(layer);
|
|
933
|
+
reach(false);
|
|
934
|
+
if (options.start === 'engaged')
|
|
935
|
+
set(true, 'started');
|
|
936
|
+
const stop = () => {
|
|
937
|
+
if (stopped)
|
|
938
|
+
return;
|
|
939
|
+
stopped = true;
|
|
940
|
+
reach(true);
|
|
941
|
+
doc.removeEventListener('pointerdown', onDown, true);
|
|
942
|
+
doc.removeEventListener('keydown', onKey, true);
|
|
943
|
+
seen?.disconnect();
|
|
944
|
+
layer.remove();
|
|
945
|
+
tile.removeEventListener('close', stop);
|
|
946
|
+
};
|
|
947
|
+
// A tile that is closed has no frame left to lay anything over.
|
|
948
|
+
tile.addEventListener('close', stop);
|
|
949
|
+
return {
|
|
950
|
+
layer,
|
|
951
|
+
get engaged() { return engaged; },
|
|
952
|
+
release() { set(false, 'released'); },
|
|
953
|
+
stop,
|
|
954
|
+
};
|
|
955
|
+
}
|
|
956
|
+
//# sourceMappingURL=guard.js.map
|