@dasl/tile-loader 3.1.0 → 4.0.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 +1463 -36
- package/dist/at.d.ts +126 -12
- package/dist/at.d.ts.map +1 -1
- package/dist/at.js +582 -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 +82 -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 +342 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/guard.js +1544 -0
- package/dist/guard.js.map +1 -0
- package/dist/index.d.ts +416 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +999 -74
- 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/lib/tab-order.d.ts +16 -0
- package/dist/lib/tab-order.d.ts.map +1 -0
- package/dist/lib/tab-order.js +66 -0
- package/dist/lib/tab-order.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 +289 -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 +590 -84
- package/src/car.ts +79 -16
- package/src/content-scheme.ts +38 -7
- package/src/guard.ts +1659 -0
- package/src/index.ts +1124 -74
- 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/lib/tab-order.ts +66 -0
- package/src/link.ts +365 -48
- package/src/memory.ts +5 -0
- package/src/mount.ts +359 -0
- package/src/size.ts +304 -16
package/dist/guard.js
ADDED
|
@@ -0,0 +1,1544 @@
|
|
|
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
|
+
// The frame is out of the tab order then, so that nobody tabbing down the
|
|
15
|
+
// page ends up in the tile by chance, and the guard puts a stop of its own
|
|
16
|
+
// in its place, which the user tabs to and presses, and their next Tab
|
|
17
|
+
// takes them in.
|
|
18
|
+
//
|
|
19
|
+
// A tile can also scroll the page it is in: asking the browser to bring an
|
|
20
|
+
// element of its own into view scrolls every scroller above it, the page's
|
|
21
|
+
// document included. That takes the page to the tile, holds it there, or
|
|
22
|
+
// slides a control of the page's under a pointer that has not moved.
|
|
23
|
+
// `guardScroll` accounts for every move of the page and takes back the
|
|
24
|
+
// tile's; `clickGate` keeps what an unexplained move slid under the pointer
|
|
25
|
+
// from taking a press for a moment.
|
|
26
|
+
//
|
|
27
|
+
// Giving back what a tile took is a contest with one that takes it again, and
|
|
28
|
+
// the tile is the faster. So a tile that keeps at it is stopped: hidden at
|
|
29
|
+
// once, closed, and the host told why (a `stopped` event on the tile, with a
|
|
30
|
+
// `reason` of `focus` or `scroll`). `stop: false` has a guard only report.
|
|
31
|
+
//
|
|
32
|
+
// And a tile takes whatever input lands on it. On a touch screen that is the
|
|
33
|
+
// finger that was scrolling the feed: a tile can keep the page from moving
|
|
34
|
+
// for as long as the finger started on it. `engage` puts a layer of the
|
|
35
|
+
// host's over the tile until the user picks it, so that until then the tile
|
|
36
|
+
// runs and shows, and gets no input at all.
|
|
37
|
+
//
|
|
38
|
+
// A guard listens to the page, so unlike the modules that only listen to the
|
|
39
|
+
// tile it does not go when the tile is dropped: it stops when the tile is
|
|
40
|
+
// closed (`tile.close()`), or when it is told to (`stop()`).
|
|
41
|
+
import { withDefaults } from './lib/defaults.js';
|
|
42
|
+
import { SHIELD_CLASS, watchPointer } from './lib/pointer.js';
|
|
43
|
+
import { holdOut, letIn } from './lib/tab-order.js';
|
|
44
|
+
const now = () => performance.now();
|
|
45
|
+
const editable = (el) => Boolean(el) && (el.isContentEditable || /^(input|textarea|select)$/.test(el?.localName || ''));
|
|
46
|
+
// The element that has the keyboard, looked for through shadow trees.
|
|
47
|
+
const focused = (doc) => {
|
|
48
|
+
let el = doc.activeElement;
|
|
49
|
+
while (el?.shadowRoot?.activeElement)
|
|
50
|
+
el = el.shadowRoot.activeElement;
|
|
51
|
+
return el;
|
|
52
|
+
};
|
|
53
|
+
// Whether the keyboard is in `frame`. Asked of the tree the frame is in, so
|
|
54
|
+
// that a frame in a shadow tree is found as well.
|
|
55
|
+
const holds = (frame) => frame.getRootNode().activeElement === frame;
|
|
56
|
+
// The tiles a guard is in the middle of stopping.
|
|
57
|
+
const halting = new WeakSet();
|
|
58
|
+
// Stops a tile that would not stop. It is out of the way at once: a frame
|
|
59
|
+
// that is not rendered takes no focus and scrolls nothing. Its frame stays in
|
|
60
|
+
// the document for the moment closing takes, since a tile's runtime needs it
|
|
61
|
+
// there to clean up after itself; the guards go on until it is gone.
|
|
62
|
+
function halt(tile, frame, reason) {
|
|
63
|
+
if (halting.has(tile))
|
|
64
|
+
return;
|
|
65
|
+
halting.add(tile);
|
|
66
|
+
frame.style.display = 'none';
|
|
67
|
+
tile.dispatchEvent(Object.assign(new Event('stopped'), { reason }));
|
|
68
|
+
// Rendered again, it is a tile like any other.
|
|
69
|
+
tile.close().finally(() => halting.delete(tile));
|
|
70
|
+
}
|
|
71
|
+
const DOCK_STATUS = 'tiles-dock-status';
|
|
72
|
+
const DOCK_KEYBOARD = 'tiles-dock-keyboard';
|
|
73
|
+
function dockStatus(tile, frame, ms, refused) {
|
|
74
|
+
const win = frame.ownerDocument.defaultView;
|
|
75
|
+
const origin = new URL(tile.getLoadSource()).origin;
|
|
76
|
+
const waiting = new Map();
|
|
77
|
+
let n = 0;
|
|
78
|
+
let answers = false;
|
|
79
|
+
const onMessage = (ev) => {
|
|
80
|
+
if (ev.source !== frame.contentWindow || ev.origin !== origin)
|
|
81
|
+
return;
|
|
82
|
+
if (ev.data?.action === DOCK_KEYBOARD) {
|
|
83
|
+
if (ev.data.payload?.arrivalActivated === false)
|
|
84
|
+
refused(ev.data.payload.unsure === true);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (ev.data?.action !== DOCK_STATUS)
|
|
88
|
+
return;
|
|
89
|
+
const { id, activated, arrivalActivated, unsure } = ev.data.payload ?? {};
|
|
90
|
+
if (typeof activated !== 'boolean')
|
|
91
|
+
return;
|
|
92
|
+
answers = true;
|
|
93
|
+
const resolve = waiting.get(id);
|
|
94
|
+
if (!resolve)
|
|
95
|
+
return;
|
|
96
|
+
waiting.delete(id);
|
|
97
|
+
resolve({ activated, arrivalActivated: typeof arrivalActivated === 'boolean' ? arrivalActivated : null, unsure: unsure === true });
|
|
98
|
+
};
|
|
99
|
+
win.addEventListener('message', onMessage);
|
|
100
|
+
return {
|
|
101
|
+
get answers() { return answers; },
|
|
102
|
+
// (`fromPage`: the keyboard has just left the page, and what the dock finds in the tile has just come;
|
|
103
|
+
// `at` says when, by the clock the dock has too, so that it can tell when our word reached it late.)
|
|
104
|
+
watch(on, fromPage) { tile.post(DOCK_STATUS, { id: `w${++n}`, watch: on, ...(fromPage ? { fromPage, at: Date.now() } : {}) }); },
|
|
105
|
+
entered(into) { tile.post(DOCK_STATUS, { id: `e${++n}`, entered: true, ...(into ? { into } : {}) }); },
|
|
106
|
+
ask() {
|
|
107
|
+
const id = `d${++n}`;
|
|
108
|
+
return new Promise((resolve) => {
|
|
109
|
+
waiting.set(id, resolve);
|
|
110
|
+
win.setTimeout(() => { if (waiting.delete(id))
|
|
111
|
+
resolve(undefined); }, ms);
|
|
112
|
+
tile.post(DOCK_STATUS, { id });
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
stop() { win.removeEventListener('message', onMessage); },
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/** The class of the layer `guardFocus` lays over a tile for the user to pick it, besides `ENGAGE_CLASS`. */
|
|
119
|
+
export const PICK_CLASS = 'tile-pick';
|
|
120
|
+
/** The class of what the layer says, inside it, and of what the stop in the frame's place says. */
|
|
121
|
+
export const PICK_LABEL_CLASS = 'tile-pick-label';
|
|
122
|
+
/** The class of the stop `guardFocus` puts in the frame's place in the tab order (see `tabOrder`). */
|
|
123
|
+
export const ENTER_CLASS = 'tile-enter';
|
|
124
|
+
// What the layer looks like where its host gives it no look of its own: a
|
|
125
|
+
// ring round the tile, and what it says in the middle, so that a user whose
|
|
126
|
+
// press seemed to do nothing sees that the tile waits for them. The stop in
|
|
127
|
+
// the frame's place looks the same while it has the keyboard, the only time
|
|
128
|
+
// it can be seen. Every rule here weighs nothing (`:where`), so that any
|
|
129
|
+
// rule of the host's for `.tile-pick`, `.tile-enter` or `.tile-pick-label`
|
|
130
|
+
// wins. Given once to each document, as a sheet of its own where the browser
|
|
131
|
+
// has those (a page whose policy refuses inline style does not refuse one),
|
|
132
|
+
// else as a style element. And once to each shadow tree a frame is in, where
|
|
133
|
+
// the layer and the stop go too: no rule of the document's reaches into one.
|
|
134
|
+
const PICK_LOOK = `
|
|
135
|
+
:where(.${PICK_CLASS}) { display: flex; align-items: center; justify-content: center; box-sizing: border-box;
|
|
136
|
+
box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.6), inset 0 0 0 4px rgba(255, 255, 255, 0.75); }
|
|
137
|
+
:where(.${PICK_CLASS}:focus-visible) { outline: 3px solid Highlight; outline-offset: -3px; }
|
|
138
|
+
:where(.${ENTER_CLASS}) { display: flex; align-items: center; justify-content: center; box-sizing: border-box;
|
|
139
|
+
box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.6), inset 0 0 0 4px rgba(255, 255, 255, 0.75); }
|
|
140
|
+
:where(.${ENTER_CLASS}:focus-visible) { outline: 3px solid Highlight; outline-offset: -3px; }
|
|
141
|
+
:where(.${PICK_LABEL_CLASS}) { max-width: calc(100% - 16px); padding: 6px 12px; border-radius: 999px; box-sizing: border-box;
|
|
142
|
+
background: rgba(0, 0, 0, 0.78); color: #fff; font: 600 13px/1.3 system-ui, sans-serif; text-align: center;
|
|
143
|
+
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; pointer-events: none; }
|
|
144
|
+
`;
|
|
145
|
+
const looked = new WeakSet();
|
|
146
|
+
function pickLook(doc, root) {
|
|
147
|
+
if (looked.has(root))
|
|
148
|
+
return;
|
|
149
|
+
looked.add(root);
|
|
150
|
+
const win = doc.defaultView;
|
|
151
|
+
try {
|
|
152
|
+
if (win && 'adoptedStyleSheets' in root && typeof win.CSSStyleSheet?.prototype.replaceSync === 'function') {
|
|
153
|
+
const sheet = new win.CSSStyleSheet();
|
|
154
|
+
sheet.replaceSync(PICK_LOOK);
|
|
155
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch { /* a style element, then */ }
|
|
160
|
+
const style = doc.createElement('style');
|
|
161
|
+
style.textContent = PICK_LOOK;
|
|
162
|
+
// First, so that the host's own sheets come after it.
|
|
163
|
+
(root === doc ? doc.head : root)?.prepend(style);
|
|
164
|
+
}
|
|
165
|
+
/** `guardFocus`'s numbers where a host gives none. */
|
|
166
|
+
export const FOCUS_GUARD = Object.freeze({
|
|
167
|
+
tabMs: 400,
|
|
168
|
+
typingMs: 1000,
|
|
169
|
+
grabs: 3,
|
|
170
|
+
withinMs: 10000,
|
|
171
|
+
dockMs: 150,
|
|
172
|
+
pollMs: 100,
|
|
173
|
+
spareMs: 60000,
|
|
174
|
+
});
|
|
175
|
+
// How often, at most, a guard with its layer up asks the dock whether its no
|
|
176
|
+
// is over (see `settle`). A guess.
|
|
177
|
+
const SETTLE_MS = 500;
|
|
178
|
+
// How long, at most, a frame is let into the tab order for the Tab from its
|
|
179
|
+
// stop, once the user has pressed the stop (see `enter`). A guess.
|
|
180
|
+
const LET_IN_MS = 10000;
|
|
181
|
+
// How much of a tile's name its stop says, at most, in characters, the
|
|
182
|
+
// ellipsis included. A guess.
|
|
183
|
+
const NAME_MAX = 60;
|
|
184
|
+
/**
|
|
185
|
+
* Gives back keyboard focus that a tile's script took, and leaves it with a
|
|
186
|
+
* user who went into the tile.
|
|
187
|
+
*
|
|
188
|
+
* Focus arriving in the frame is the user's doing when they went in by the
|
|
189
|
+
* host's own way in (a control of its own calling `focus()`), tabbed into
|
|
190
|
+
* it (from the guard's stop in the frame's place in the tab order, which
|
|
191
|
+
* lets the frame in for that Tab, or into a frame the host keeps in the tab
|
|
192
|
+
* order: see `tabOrder`), or pressed on it. The page sees
|
|
193
|
+
* the Tab key. It never sees the press: only that a mouse is over the
|
|
194
|
+
* frame, and not even that for a finger. So focus
|
|
195
|
+
* stays in the tile when a Tab was just pressed on the page; otherwise, when
|
|
196
|
+
* a mouse is in the frame (or the user has none, or, on a screen that takes
|
|
197
|
+
* fingers, the guard has seen no pointer yet and the keyboard came straight
|
|
198
|
+
* from the page) and they were not typing in a field of the page a moment
|
|
199
|
+
* ago, the tile's dock is asked, which knows
|
|
200
|
+
* whether the user pressed or typed inside this tile. In every other case,
|
|
201
|
+
* and when the dock says no, focus goes back to what had it when the tile
|
|
202
|
+
* took it: to the page alone when nothing of the page's had it then, and
|
|
203
|
+
* never to something the user had left.
|
|
204
|
+
*
|
|
205
|
+
* While the keyboard is in a frame of the page's, where the page cannot see
|
|
206
|
+
* it move, the guard has the tile's dock watch it come into the tile, and a
|
|
207
|
+
* dock that found it come in with no one acting there says no, at once and
|
|
208
|
+
* for as long as what the user typed into the tile before it was given back
|
|
209
|
+
* can make them seem to have acted there.
|
|
210
|
+
*
|
|
211
|
+
* Where the dock's no is only its best guess (someone did act in the tile,
|
|
212
|
+
* and it may have been the user pressing in it), the keyboard is given back
|
|
213
|
+
* all the same, but that is not counted against the tile: a layer of the
|
|
214
|
+
* page's lies over it until the user picks it, which puts the keyboard in,
|
|
215
|
+
* or until the dock's no is over (see `pick`).
|
|
216
|
+
*
|
|
217
|
+
* A dock that has answered before and now does not is taken to say no: its
|
|
218
|
+
* silence is not to be had by keeping it busy. One that has never answered
|
|
219
|
+
* (an older loading server's) leaves the guard with what the page can see:
|
|
220
|
+
* a mouse resting on the tile, or a finger, cannot be told from a press.
|
|
221
|
+
*
|
|
222
|
+
* Giving it back does not win against a tile that keeps taking it: a tile
|
|
223
|
+
* that does so `grabs` times within `withinMs` is reported (`onAbuse`) and
|
|
224
|
+
* stopped (see `GuardStoppedEvent`), unless told otherwise (`stop: false`).
|
|
225
|
+
*/
|
|
226
|
+
export function guardFocus(tile, frame, options = {}) {
|
|
227
|
+
// A host that meant to keep the frame in the tab order, and said so in a
|
|
228
|
+
// way the guard does not know, would be left believing in a Tab key that
|
|
229
|
+
// passes over the tile. So that throws, before anything is set up.
|
|
230
|
+
const tabOrder = options.tabOrder ?? 'stop';
|
|
231
|
+
if (tabOrder !== 'stop' && tabOrder !== 'out' && tabOrder !== 'keep')
|
|
232
|
+
throw new Error(`guardFocus() takes 'stop', 'out' or 'keep' for where the frame is in the tab order, not ${JSON.stringify(tabOrder)}.`);
|
|
233
|
+
const doc = frame.ownerDocument;
|
|
234
|
+
const win = doc.defaultView;
|
|
235
|
+
const pointer = watchPointer(doc);
|
|
236
|
+
const cfg = withDefaults(FOCUS_GUARD, options);
|
|
237
|
+
const dock = dockStatus(tile, frame, cfg.dockMs, (unsure) => onRefused(unsure));
|
|
238
|
+
// Whether this dock answers at all is learnt while nothing is at stake:
|
|
239
|
+
// now, and when the tile's runtime says it is up, before the tile runs.
|
|
240
|
+
const probe = () => { dock.ask(); };
|
|
241
|
+
probe();
|
|
242
|
+
tile.addEventListener('load', probe);
|
|
243
|
+
// What had the keyboard before the tile did. Nothing of the page's, when
|
|
244
|
+
// the guard is made with the keyboard in another frame of the page's (the
|
|
245
|
+
// user is in a tile, and the page adds this one): given back there, it
|
|
246
|
+
// would be put in a frame by the page's own script, and Chrome then goes on
|
|
247
|
+
// naming that frame as having it after it has gone elsewhere.
|
|
248
|
+
let holder = focused(doc);
|
|
249
|
+
if (holder === frame || holder === doc.body || framed(holder))
|
|
250
|
+
holder = null;
|
|
251
|
+
// Whether this window has the keyboard. Which element is the active one is
|
|
252
|
+
// not enough to go by: that says which element WOULD have it. The window's
|
|
253
|
+
// own focus and blur events say whether a frame inside it has taken it.
|
|
254
|
+
// To begin with there is only the active element to go by: with a frame
|
|
255
|
+
// active the window does not have it, though the document "has focus"
|
|
256
|
+
// then too, and no blur will say so when this tile takes it from there.
|
|
257
|
+
let winFocused = doc.hasFocus() && !holds(frame) && !framed(focused(doc));
|
|
258
|
+
// Whether the keyboard, when it last left the page, went from the page
|
|
259
|
+
// itself (its window said so) and not from another frame of the page's.
|
|
260
|
+
let straight = winFocused;
|
|
261
|
+
let inTile = false;
|
|
262
|
+
let accepted = false;
|
|
263
|
+
let tabbed = false; // accepted for what the page saw: a Tab, or its own way in
|
|
264
|
+
let returning = false; // a giving back is under way
|
|
265
|
+
let lastTab = -Infinity;
|
|
266
|
+
let into = false; // the way in that set lastTab was taken with a press (see `focus`)
|
|
267
|
+
let lastTyped = -Infinity;
|
|
268
|
+
let returned = 0;
|
|
269
|
+
let round = 0; // which giving back is under way: one that was overtaken does nothing more
|
|
270
|
+
let grabs = [];
|
|
271
|
+
let stopped = false;
|
|
272
|
+
// The layer the user picks the tile on, once a no of the dock's was only
|
|
273
|
+
// its best guess (see `pick`), and how to put back what it took to lay it
|
|
274
|
+
// over the frame.
|
|
275
|
+
let layer;
|
|
276
|
+
let unlay;
|
|
277
|
+
let liftedAt = -Infinity; // when the layer last lifted on its own
|
|
278
|
+
let settling = false; // the dock is being asked whether its no is over
|
|
279
|
+
let settledAt = -Infinity;
|
|
280
|
+
// Whether the dock is watching the keyboard come into the tile. It is
|
|
281
|
+
// asked to while the keyboard is in a frame of the page's (this tile's or
|
|
282
|
+
// another's), which is when the page cannot see it move from one to the
|
|
283
|
+
// next: not while it is on the page, which the page sees at once, nor
|
|
284
|
+
// while the window does not have it at all, and never once stopped.
|
|
285
|
+
// `fromPage`: the window has just said that the keyboard left it.
|
|
286
|
+
let watched = false;
|
|
287
|
+
const watch = (fromPage = false) => {
|
|
288
|
+
const want = !stopped && !winFocused && doc.hasFocus();
|
|
289
|
+
if (want === watched)
|
|
290
|
+
return;
|
|
291
|
+
watched = want;
|
|
292
|
+
dock.watch(want, want && fromPage);
|
|
293
|
+
};
|
|
294
|
+
// Whether `el` is the frame, or something the frame is in (the element
|
|
295
|
+
// whose shadow tree it is in is all the page is told of it, where that
|
|
296
|
+
// tree is closed).
|
|
297
|
+
const around = (el) => {
|
|
298
|
+
for (let node = frame; node; node = node.parentNode ?? node.host ?? null)
|
|
299
|
+
if (node === el)
|
|
300
|
+
return true;
|
|
301
|
+
return false;
|
|
302
|
+
};
|
|
303
|
+
const onFocusIn = (ev) => {
|
|
304
|
+
const target = (ev.composedPath?.()[0] ?? ev.target);
|
|
305
|
+
if (target && target !== frame)
|
|
306
|
+
holder = target;
|
|
307
|
+
// The Tab that brought the user here took them to something of the
|
|
308
|
+
// page's, not into the tile: it explains nothing that arrives there next.
|
|
309
|
+
if (target && !around(target))
|
|
310
|
+
lastTab = -Infinity;
|
|
311
|
+
};
|
|
312
|
+
// What had the keyboard is where it goes back to only for as long as it
|
|
313
|
+
// still has it. A button the user pressed a while ago and then left, for
|
|
314
|
+
// the plain text of the page, has it no longer: put back there, the
|
|
315
|
+
// keyboard would be on a control its user never returned to, and their
|
|
316
|
+
// next Space or Enter would press it. So what lost the keyboard to nothing
|
|
317
|
+
// of the page's is forgotten, and the keyboard then goes back to the page
|
|
318
|
+
// and to no element of it.
|
|
319
|
+
const onNothing = () => {
|
|
320
|
+
const el = focused(doc);
|
|
321
|
+
return !el || el === doc.body || el === doc.documentElement;
|
|
322
|
+
};
|
|
323
|
+
const forget = () => {
|
|
324
|
+
if (winFocused && doc.hasFocus() && onNothing())
|
|
325
|
+
holder = null;
|
|
326
|
+
};
|
|
327
|
+
const onFocusOut = (ev) => {
|
|
328
|
+
// To something else of the page's: `focusin` says what.
|
|
329
|
+
if (ev.relatedTarget)
|
|
330
|
+
return;
|
|
331
|
+
const target = (ev.composedPath?.()[0] ?? ev.target);
|
|
332
|
+
// Out of the tile, and to nothing: the user left it for the page itself.
|
|
333
|
+
if (target === frame)
|
|
334
|
+
holder = null;
|
|
335
|
+
// Out of what had it: to nothing, or into a frame (the tile's, when its
|
|
336
|
+
// script takes the keyboard, or another of the page's, when the user goes
|
|
337
|
+
// into one), and only the window's own events tell which. In a moment
|
|
338
|
+
// they have: `forget` sees to the first, and `check` to a frame.
|
|
339
|
+
else
|
|
340
|
+
win.setTimeout(forget, 0);
|
|
341
|
+
};
|
|
342
|
+
// Whether a key picks the tile, for a host that has it wait to be picked:
|
|
343
|
+
// Enter or Space on the layer over it (see `engage`), which then puts the
|
|
344
|
+
// keyboard into the tile itself.
|
|
345
|
+
const picks = (ev) => {
|
|
346
|
+
if (ev.key !== 'Enter' && ev.key !== ' ')
|
|
347
|
+
return false;
|
|
348
|
+
const target = (ev.composedPath?.()[0] ?? ev.target);
|
|
349
|
+
// Of the layers a focus guard lays, only ours: the box it is in may hold
|
|
350
|
+
// other tiles, each with its guard's layer beside ours.
|
|
351
|
+
if (target?.classList?.contains(PICK_CLASS))
|
|
352
|
+
return target === layer;
|
|
353
|
+
return Boolean(target?.classList?.contains(ENGAGE_CLASS) && target.parentNode?.contains(frame));
|
|
354
|
+
};
|
|
355
|
+
const onKey = (ev) => {
|
|
356
|
+
if (!ev.isTrusted)
|
|
357
|
+
return;
|
|
358
|
+
// (Picking the tile from the keyboard is going into it by the keyboard,
|
|
359
|
+
// as tabbing into it is; on the layer of engage's as well as ours.)
|
|
360
|
+
const pick = picks(ev);
|
|
361
|
+
if (pick)
|
|
362
|
+
lift();
|
|
363
|
+
if (ev.key === 'Tab' || pick) {
|
|
364
|
+
// A Tab takes nobody into a frame that is out of the tab order: the
|
|
365
|
+
// keyboard arriving there next is not explained by it (it may have
|
|
366
|
+
// gone into another frame, whose tile tells this one when). As the
|
|
367
|
+
// frame is now, which its host may have changed. While it is let in
|
|
368
|
+
// for the Tab from its stop, only that Tab can take the user into it:
|
|
369
|
+
// forward, from the stop, which is right before it.
|
|
370
|
+
const tabsIn = frame.tabIndex >= 0 && (!letOut || (!ev.shiftKey && onStop()));
|
|
371
|
+
if (pick || tabsIn) {
|
|
372
|
+
lastTab = now();
|
|
373
|
+
spent = !pick && Boolean(letOut);
|
|
374
|
+
}
|
|
375
|
+
into = false;
|
|
376
|
+
}
|
|
377
|
+
else if (editable(focused(doc)))
|
|
378
|
+
lastTyped = now();
|
|
379
|
+
};
|
|
380
|
+
// Whether focus that has just arrived in the frame can be the user's
|
|
381
|
+
// doing: `tab` when the page saw them tab, `ask` when only the dock can
|
|
382
|
+
// tell, `false` when it cannot be.
|
|
383
|
+
const judge = () => {
|
|
384
|
+
const t = now();
|
|
385
|
+
if (options.engaged && !options.engaged())
|
|
386
|
+
return false;
|
|
387
|
+
// Nor in one that waits for the user to pick it, after a no the dock was not sure of.
|
|
388
|
+
if (layer)
|
|
389
|
+
return false;
|
|
390
|
+
if (t - lastTab < cfg.tabMs)
|
|
391
|
+
return 'tab';
|
|
392
|
+
if (t - lastTyped < cfg.typingMs)
|
|
393
|
+
return false;
|
|
394
|
+
// A finger gives the page nothing to go by.
|
|
395
|
+
if (!pointer.hovers || pointer.state.kind === 'none')
|
|
396
|
+
return 'ask';
|
|
397
|
+
// Nor, on a screen that takes fingers, does a guard that has seen no
|
|
398
|
+
// pointer yet: one may have come down in the frame unseen. The dock is
|
|
399
|
+
// asked then only when the keyboard came straight from the page, and only
|
|
400
|
+
// a dock that has answered before. Found in the tile after it was in
|
|
401
|
+
// another frame of the page's, it may have been taken while the user was
|
|
402
|
+
// typing there: what they typed since is the tile's by the time the guard
|
|
403
|
+
// looks, and the dock would count it as their having acted in the tile.
|
|
404
|
+
// And a dock that has never answered would leave the tile the keyboard.
|
|
405
|
+
if (pointer.fingers && pointer.state.kind === 'unknown' && straight && dock.answers)
|
|
406
|
+
return 'ask';
|
|
407
|
+
return pointer.inside(frame) ? 'ask' : false;
|
|
408
|
+
};
|
|
409
|
+
// Never from inside the blur that tells us: the browser is still moving
|
|
410
|
+
// focus into the frame then, and finishes the move after the handler
|
|
411
|
+
// returns, leaving the element "focused" in a window that has no keyboard.
|
|
412
|
+
// So it is done from a later task, checked by the window's own focus event,
|
|
413
|
+
// and done again if it did not take.
|
|
414
|
+
// `to` is what had the keyboard when the tile took it, if anything had:
|
|
415
|
+
// settled before the frame is blurred here, which is the keyboard leaving
|
|
416
|
+
// the tile for nothing.
|
|
417
|
+
const takeBack = (tries, mine, to) => {
|
|
418
|
+
if (stopped || winFocused || mine !== round)
|
|
419
|
+
return;
|
|
420
|
+
if (tries === 0 && to?.isConnected)
|
|
421
|
+
to.focus({ preventScroll: true });
|
|
422
|
+
else {
|
|
423
|
+
frame.blur();
|
|
424
|
+
win.focus();
|
|
425
|
+
if (to?.isConnected)
|
|
426
|
+
to.focus({ preventScroll: true });
|
|
427
|
+
}
|
|
428
|
+
if (tries < 4)
|
|
429
|
+
win.setTimeout(() => takeBack(tries + 1, mine, to), 30);
|
|
430
|
+
};
|
|
431
|
+
// `guess`: the dock's no was only its best guess (see `pick`).
|
|
432
|
+
const giveBack = (why, guess = false) => {
|
|
433
|
+
returned++;
|
|
434
|
+
returning = true;
|
|
435
|
+
// Such a no is not held against the tile: it may have been the user
|
|
436
|
+
// pressing in it. They pick it on a layer instead, which the tile cannot
|
|
437
|
+
// lift, and until they do, the keyboard coming in is counted.
|
|
438
|
+
// (Not where a layer lifted on its own a moment ago: a tile that could
|
|
439
|
+
// have its dock's nos taken for guesses, and waited each time for the
|
|
440
|
+
// layer to go, would never be counted. It has a layer all the same.)
|
|
441
|
+
const spared = now() - liftedAt >= cfg.spareMs;
|
|
442
|
+
const counted = !(guess && lay() && spared);
|
|
443
|
+
if (counted) {
|
|
444
|
+
const t = now();
|
|
445
|
+
grabs = grabs.filter(g => t - g < cfg.withinMs);
|
|
446
|
+
grabs.push(t);
|
|
447
|
+
}
|
|
448
|
+
takeBack(0, ++round, holder);
|
|
449
|
+
options.onReturn?.(why);
|
|
450
|
+
if (!counted || grabs.length < cfg.grabs)
|
|
451
|
+
return;
|
|
452
|
+
grabs = [];
|
|
453
|
+
options.onAbuse?.('focus', returned);
|
|
454
|
+
if (options.stop !== false)
|
|
455
|
+
halt(tile, frame, 'focus');
|
|
456
|
+
};
|
|
457
|
+
// A layer of the page's over the frame, for the user to pick the tile on:
|
|
458
|
+
// a press or a tap on it, or Enter or Space, all of which the page sees
|
|
459
|
+
// and the tile cannot make. It shows that the tile waits for them, and
|
|
460
|
+
// says so (see `PICK_LOOK`). Put in the frame's box, right after it, which
|
|
461
|
+
// is made one that it is laid out against if it is not; and the frame is
|
|
462
|
+
// out of the tab order meanwhile, the layer in its place, as with
|
|
463
|
+
// `engage`. Whether it went up: not when it was up already, which the
|
|
464
|
+
// tile had to get past.
|
|
465
|
+
const lay = () => {
|
|
466
|
+
const box = (frame.parentElement ?? frame.parentNode?.host);
|
|
467
|
+
if (layer || options.pick === false || stopped || !box)
|
|
468
|
+
return false;
|
|
469
|
+
// (In the shadow tree the frame is in, if it is in one: the layer is.)
|
|
470
|
+
const root = frame.getRootNode();
|
|
471
|
+
pickLook(doc, root instanceof win.ShadowRoot ? root : doc);
|
|
472
|
+
const label = options.pickLabel ?? 'Tap or click to use';
|
|
473
|
+
const el = doc.createElement('div');
|
|
474
|
+
el.className = `${ENGAGE_CLASS} ${PICK_CLASS}`;
|
|
475
|
+
el.setAttribute('role', 'button');
|
|
476
|
+
el.setAttribute('tabindex', '0');
|
|
477
|
+
el.setAttribute('aria-label', label);
|
|
478
|
+
el.style.cssText = 'position:absolute;z-index:1;background:rgba(127,127,127,0.03);cursor:pointer;';
|
|
479
|
+
const words = doc.createElement('span');
|
|
480
|
+
words.className = PICK_LABEL_CLASS;
|
|
481
|
+
words.textContent = label;
|
|
482
|
+
el.append(words);
|
|
483
|
+
el.addEventListener('click', (ev) => { if (ev.isTrusted)
|
|
484
|
+
picked(); });
|
|
485
|
+
el.addEventListener('keydown', (ev) => {
|
|
486
|
+
if (!ev.isTrusted || (ev.key !== 'Enter' && ev.key !== ' '))
|
|
487
|
+
return;
|
|
488
|
+
ev.preventDefault();
|
|
489
|
+
picked();
|
|
490
|
+
});
|
|
491
|
+
const position = win.getComputedStyle(box).position;
|
|
492
|
+
const own = box.style.position;
|
|
493
|
+
if (!position || position === 'static')
|
|
494
|
+
box.style.position = 'relative';
|
|
495
|
+
// (Held out with whatever else holds it, engage's among them: see `holdOut`.)
|
|
496
|
+
const back = holdOut(frame);
|
|
497
|
+
unlay = () => {
|
|
498
|
+
box.style.position = own;
|
|
499
|
+
back();
|
|
500
|
+
};
|
|
501
|
+
frame.after(el);
|
|
502
|
+
layer = el;
|
|
503
|
+
place();
|
|
504
|
+
standBy();
|
|
505
|
+
return true;
|
|
506
|
+
};
|
|
507
|
+
// Over the frame wherever it is in its box, which may change as the page
|
|
508
|
+
// is laid out again: done again at every look.
|
|
509
|
+
const place = () => {
|
|
510
|
+
const box = layer?.offsetParent;
|
|
511
|
+
if (!layer || !box)
|
|
512
|
+
return;
|
|
513
|
+
const at = frame.getBoundingClientRect();
|
|
514
|
+
const of = box.getBoundingClientRect();
|
|
515
|
+
layer.style.left = `${at.left - of.left - box.clientLeft + box.scrollLeft}px`;
|
|
516
|
+
layer.style.top = `${at.top - of.top - box.clientTop + box.scrollTop}px`;
|
|
517
|
+
layer.style.width = `${at.width}px`;
|
|
518
|
+
layer.style.height = `${at.height}px`;
|
|
519
|
+
};
|
|
520
|
+
const lift = () => {
|
|
521
|
+
if (!layer)
|
|
522
|
+
return;
|
|
523
|
+
layer.remove();
|
|
524
|
+
layer = undefined;
|
|
525
|
+
unlay?.();
|
|
526
|
+
unlay = undefined;
|
|
527
|
+
standBy();
|
|
528
|
+
};
|
|
529
|
+
// The layer lifts on its own once the dock's no is over: what it went by,
|
|
530
|
+
// its activation, has run out while the keyboard was out of the tile, and
|
|
531
|
+
// the dock says so when asked, as a no that is no longer its best guess
|
|
532
|
+
// (`unsure`) nor a no that stands. Asked now and then while the keyboard
|
|
533
|
+
// is not in the tile, nor on the layer: a user about to pick it with a key
|
|
534
|
+
// keeps it.
|
|
535
|
+
const settle = () => {
|
|
536
|
+
if (!layer || settling || stopped || holds(frame) || focused(doc) === layer || now() - settledAt < SETTLE_MS)
|
|
537
|
+
return;
|
|
538
|
+
settling = true;
|
|
539
|
+
settledAt = now();
|
|
540
|
+
dock.ask().then((said) => {
|
|
541
|
+
settling = false;
|
|
542
|
+
if (!layer || stopped || holds(frame) || focused(doc) === layer)
|
|
543
|
+
return;
|
|
544
|
+
if (said?.activated !== false || said.unsure || said.arrivalActivated === false)
|
|
545
|
+
return;
|
|
546
|
+
lift();
|
|
547
|
+
liftedAt = now();
|
|
548
|
+
});
|
|
549
|
+
};
|
|
550
|
+
// The user picked the tile on the layer: they go in, as by the host's own
|
|
551
|
+
// way in. (Unless the host has it wait to be picked with `engage`, and
|
|
552
|
+
// its layer is back over the tile since: that is where they pick it now.)
|
|
553
|
+
const picked = () => {
|
|
554
|
+
if (options.engaged && !options.engaged())
|
|
555
|
+
return lift();
|
|
556
|
+
focus();
|
|
557
|
+
};
|
|
558
|
+
// The stop in the frame's place in the tab order (see `tabOrder`): a
|
|
559
|
+
// button of the page's right before the frame, which the user tabs to and
|
|
560
|
+
// presses as they would the host's own way in. Before it, so that a Tab
|
|
561
|
+
// out of the tile's last field goes on to what comes after the tile, not
|
|
562
|
+
// back to the tile's own stop. It takes no room and
|
|
563
|
+
// cannot be seen until it has the keyboard; then it lies over the tile,
|
|
564
|
+
// with the layer's look, and says what it is for. It takes no press then:
|
|
565
|
+
// a press or a tap where it lies is one into the tile, which goes there,
|
|
566
|
+
// to what is under it, as it would with no stop. While another layer is
|
|
567
|
+
// over the tile (the guard's, or engage's for a tile that is not picked),
|
|
568
|
+
// that is where Tab stops, and this one is out of the tab order.
|
|
569
|
+
// The tile's name is the tile's to choose: cut to a name's length, so
|
|
570
|
+
// that what the stop says, and what it is called for those who do not see
|
|
571
|
+
// it, is a name and not whatever the tile would have it say. (By the
|
|
572
|
+
// character, not the code unit: a character is not cut in half.)
|
|
573
|
+
const named = Array.from(typeof tile.manifest?.name === 'string' ? tile.manifest.name.trim() : '');
|
|
574
|
+
const name = named.length > NAME_MAX ? `${named.slice(0, NAME_MAX - 1).join('')}…` : named.join('');
|
|
575
|
+
const enterLabel = options.enterLabel ?? `Use the keyboard in ${name || 'this tile'}`;
|
|
576
|
+
const tabLabel = options.tabLabel ?? 'Press Tab to go in';
|
|
577
|
+
// (Clipped to nothing, and out of the flow, where it is.)
|
|
578
|
+
const UNSEEN = 'position:absolute;width:1px;height:1px;margin:0;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;';
|
|
579
|
+
const standIn = tabOrder === 'stop' ? doc.createElement('div') : undefined;
|
|
580
|
+
const words = standIn ? doc.createElement('span') : undefined;
|
|
581
|
+
let shown = false;
|
|
582
|
+
// What it says, to those who see it and to those who do not.
|
|
583
|
+
const say = (label) => {
|
|
584
|
+
if (!standIn || !words)
|
|
585
|
+
return;
|
|
586
|
+
standIn.setAttribute('aria-label', label);
|
|
587
|
+
words.textContent = label;
|
|
588
|
+
};
|
|
589
|
+
const onStop = () => Boolean(standIn) && (standIn?.getRootNode()).activeElement === standIn;
|
|
590
|
+
// The frame let into the tab order for the Tab from the stop (see
|
|
591
|
+
// `enter`), and how to end that; and whether the Tab that last explained
|
|
592
|
+
// the keyboard coming in was that one (`spent` once it has).
|
|
593
|
+
let letOut;
|
|
594
|
+
let letUntil;
|
|
595
|
+
let spent = false;
|
|
596
|
+
// The Tab from the stop takes the user in from there, and explains the
|
|
597
|
+
// keyboard coming into the frame from the stop and nothing else: should
|
|
598
|
+
// the keyboard be found anywhere else before the tile has it (another
|
|
599
|
+
// tile took it on the way, say), it is spent all the same.
|
|
600
|
+
const spend = () => {
|
|
601
|
+
if (!spent)
|
|
602
|
+
return;
|
|
603
|
+
lastTab = -Infinity;
|
|
604
|
+
spent = false;
|
|
605
|
+
};
|
|
606
|
+
const shut = () => {
|
|
607
|
+
if (!letOut)
|
|
608
|
+
return;
|
|
609
|
+
letOut();
|
|
610
|
+
letOut = undefined;
|
|
611
|
+
win.clearTimeout(letUntil);
|
|
612
|
+
say(enterLabel);
|
|
613
|
+
};
|
|
614
|
+
// Over the frame, wherever the box it is laid out against is: put where
|
|
615
|
+
// that box begins, it is as far from the frame as it has to move. Done
|
|
616
|
+
// again at every look, as the layer is, and without making the frame's
|
|
617
|
+
// box one to lay it out against, which is the layer's to do and undo.
|
|
618
|
+
const placeStandIn = () => {
|
|
619
|
+
if (!standIn || !shown)
|
|
620
|
+
return;
|
|
621
|
+
const at = frame.getBoundingClientRect();
|
|
622
|
+
const was = standIn.getBoundingClientRect();
|
|
623
|
+
standIn.style.left = `${(parseFloat(standIn.style.left) || 0) + at.left - was.left}px`;
|
|
624
|
+
standIn.style.top = `${(parseFloat(standIn.style.top) || 0) + at.top - was.top}px`;
|
|
625
|
+
standIn.style.width = `${at.width}px`;
|
|
626
|
+
standIn.style.height = `${at.height}px`;
|
|
627
|
+
};
|
|
628
|
+
const show = (on) => {
|
|
629
|
+
if (!standIn)
|
|
630
|
+
return;
|
|
631
|
+
shown = on;
|
|
632
|
+
standIn.style.cssText = on ? 'position:absolute;z-index:1;left:0;top:0;pointer-events:none;' : UNSEEN;
|
|
633
|
+
placeStandIn();
|
|
634
|
+
};
|
|
635
|
+
if (standIn && words) {
|
|
636
|
+
standIn.className = ENTER_CLASS;
|
|
637
|
+
standIn.setAttribute('role', 'button');
|
|
638
|
+
standIn.setAttribute('tabindex', '0');
|
|
639
|
+
standIn.style.cssText = UNSEEN;
|
|
640
|
+
words.className = PICK_LABEL_CLASS;
|
|
641
|
+
say(enterLabel);
|
|
642
|
+
standIn.append(words);
|
|
643
|
+
// The user pressed the stop: the frame is let into the tab order for
|
|
644
|
+
// their next Tab, which goes from the stop, right before the frame, into
|
|
645
|
+
// the tile as a Tab into any frame in the tab order does, and which the
|
|
646
|
+
// page sees. The stop says so. Not `focus()`, which puts the keyboard
|
|
647
|
+
// in from the page: in answer to a key, that takes in Chrome but not in
|
|
648
|
+
// Firefox nor in WebKit, which leave the keyboard where it was, and in
|
|
649
|
+
// Chrome it leaves the keyboard in a page of the dock's with nothing on
|
|
650
|
+
// screen to say so. This way is the same in all three: Enter, then Tab.
|
|
651
|
+
// It is over once the keyboard has left the stop (into the tile or
|
|
652
|
+
// anywhere else), or after `LET_IN_MS`.
|
|
653
|
+
// (Not into a tile that waits to be picked: engage's layer is the way in to it.)
|
|
654
|
+
const enter = () => {
|
|
655
|
+
if (options.engaged && !options.engaged())
|
|
656
|
+
return;
|
|
657
|
+
// The user is going in themselves, as by the host's way in (see `focus`).
|
|
658
|
+
lift();
|
|
659
|
+
liftedAt = -Infinity;
|
|
660
|
+
shut();
|
|
661
|
+
// (A press aimed at the stop can come with the keyboard elsewhere, as
|
|
662
|
+
// a screen reader's can: the frame is let in only while the keyboard
|
|
663
|
+
// is on the stop, and the Tab that takes the user in goes from there.)
|
|
664
|
+
if (!onStop())
|
|
665
|
+
standIn.focus({ preventScroll: true });
|
|
666
|
+
letOut = letIn(frame);
|
|
667
|
+
letUntil = win.setTimeout(shut, LET_IN_MS);
|
|
668
|
+
say(tabLabel);
|
|
669
|
+
};
|
|
670
|
+
// (A press the browser makes on the stop itself, as it does for a
|
|
671
|
+
// screen reader's, reaches it all the same: it is aimed at the element,
|
|
672
|
+
// not at where it lies. It puts the keyboard on the stop, see `enter`.)
|
|
673
|
+
standIn.addEventListener('click', (ev) => { if (ev.isTrusted)
|
|
674
|
+
enter(); });
|
|
675
|
+
standIn.addEventListener('keydown', (ev) => {
|
|
676
|
+
if (!ev.isTrusted || (ev.key !== 'Enter' && ev.key !== ' '))
|
|
677
|
+
return;
|
|
678
|
+
ev.preventDefault();
|
|
679
|
+
enter();
|
|
680
|
+
});
|
|
681
|
+
standIn.addEventListener('focus', () => show(true));
|
|
682
|
+
standIn.addEventListener('blur', () => {
|
|
683
|
+
show(false);
|
|
684
|
+
// (A task later: the Tab that takes the keyboard into the frame is still on its way.)
|
|
685
|
+
win.setTimeout(() => { if (!onStop())
|
|
686
|
+
shut(); }, 0);
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
// Right before the frame once it is in a document (a host may guard it
|
|
690
|
+
// before it puts it there), and again where the page has moved the frame
|
|
691
|
+
// or taken the stop away, or put something between them; in the tab order
|
|
692
|
+
// or out of it, as said above.
|
|
693
|
+
// Not where the page has taken the frame out of its document: there is no
|
|
694
|
+
// way into a tile that is not there, and a stop left behind would be one
|
|
695
|
+
// the user tabs to and that does nothing. It goes with the frame, and
|
|
696
|
+
// comes back with it.
|
|
697
|
+
const standBy = () => {
|
|
698
|
+
if (!standIn || stopped)
|
|
699
|
+
return;
|
|
700
|
+
if (!frame.isConnected || !frame.parentNode)
|
|
701
|
+
return standIn.remove();
|
|
702
|
+
if (standIn.nextSibling !== frame) {
|
|
703
|
+
// (In the shadow tree the frame is in, if it is in one: so is its look.)
|
|
704
|
+
const root = frame.getRootNode();
|
|
705
|
+
pickLook(doc, root instanceof win.ShadowRoot ? root : doc);
|
|
706
|
+
frame.before(standIn);
|
|
707
|
+
}
|
|
708
|
+
// Seen only while it has the keyboard. A stop the page took away, or
|
|
709
|
+
// moved, with the keyboard on it has lost it, and some browsers (Firefox)
|
|
710
|
+
// say no blur then: put back, it would lie over the tile, with its words,
|
|
711
|
+
// taking the presses meant for the tile.
|
|
712
|
+
if (shown && standIn.getRootNode().activeElement !== standIn)
|
|
713
|
+
show(false);
|
|
714
|
+
const tabindex = !layer && (!options.engaged || options.engaged()) ? '0' : '-1';
|
|
715
|
+
if (standIn.getAttribute('tabindex') !== tabindex)
|
|
716
|
+
standIn.setAttribute('tabindex', tabindex);
|
|
717
|
+
placeStandIn();
|
|
718
|
+
};
|
|
719
|
+
// `refused`: the dock has just said that the keyboard came into the tile
|
|
720
|
+
// with no one acting there, which is its no without being asked, and
|
|
721
|
+
// `unsure`, that it may be the user's doing all the same.
|
|
722
|
+
const check = (refused = false, unsure = false) => {
|
|
723
|
+
place();
|
|
724
|
+
standBy();
|
|
725
|
+
// (Also where the stop was taken away with the keyboard on it, which
|
|
726
|
+
// some browsers say no blur for; and where something took the frame out
|
|
727
|
+
// of the tab order again, engage's layer back over the tile say, which
|
|
728
|
+
// ends the moment: the stop is not to say that Tab goes in then.)
|
|
729
|
+
if (letOut && (!onStop() || frame.tabIndex < 0))
|
|
730
|
+
shut();
|
|
731
|
+
settle();
|
|
732
|
+
watch();
|
|
733
|
+
if (stopped || winFocused || !doc.hasFocus())
|
|
734
|
+
return;
|
|
735
|
+
if (holds(frame))
|
|
736
|
+
inTile = true;
|
|
737
|
+
// The keyboard is in another frame of the page's. It left the tile, and
|
|
738
|
+
// what was decided when it came no longer stands: should it come back,
|
|
739
|
+
// that is judged afresh. (The window hears nothing of focus going from
|
|
740
|
+
// one frame to another.)
|
|
741
|
+
// Nor does what had the keyboard on the page have it still, unless the
|
|
742
|
+
// page put it in that frame itself: the page hears no focusin when the
|
|
743
|
+
// user presses or tabs into another frame, only that the keyboard left.
|
|
744
|
+
else {
|
|
745
|
+
inTile = accepted = tabbed = straight = returning = false;
|
|
746
|
+
if (focused(doc) !== holder)
|
|
747
|
+
holder = null;
|
|
748
|
+
spend();
|
|
749
|
+
}
|
|
750
|
+
if (!inTile)
|
|
751
|
+
return;
|
|
752
|
+
if (accepted) {
|
|
753
|
+
// It came in again since, where the page did not see it go: what the
|
|
754
|
+
// page saw of the user going in stands, and nothing else does.
|
|
755
|
+
if (!refused || tabbed)
|
|
756
|
+
return;
|
|
757
|
+
accepted = inTile = false;
|
|
758
|
+
return giveBack('dock', unsure);
|
|
759
|
+
}
|
|
760
|
+
const how = judge();
|
|
761
|
+
if (!how || (refused && how === 'ask')) {
|
|
762
|
+
inTile = false;
|
|
763
|
+
return how ? giveBack('dock', unsure) : giveBack('script');
|
|
764
|
+
}
|
|
765
|
+
accepted = true;
|
|
766
|
+
tabbed = how === 'tab';
|
|
767
|
+
round++; // nothing still on its way from an earlier giving back takes this one away
|
|
768
|
+
// The user went in themselves, and the dock is told: no earlier no of its
|
|
769
|
+
// stands against them. (And, where they went in with a press, it puts
|
|
770
|
+
// the keyboard on into the tile.)
|
|
771
|
+
if (tabbed) {
|
|
772
|
+
dock.entered(into);
|
|
773
|
+
into = false;
|
|
774
|
+
// The Tab from the stop has taken the user in, and is spent: it
|
|
775
|
+
// explains nothing that comes into the tile after this, should the
|
|
776
|
+
// keyboard go elsewhere first.
|
|
777
|
+
if (spent)
|
|
778
|
+
lastTab = -Infinity;
|
|
779
|
+
spent = false;
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
// A second opinion, which takes a moment: the dock's. Where it watched
|
|
783
|
+
// the keyboard come in, it also says whether the user had acted in the
|
|
784
|
+
// tile then, which what they typed into the tile since does not change.
|
|
785
|
+
dock.ask().then((said) => {
|
|
786
|
+
if ((said?.activated === true && said.arrivalActivated !== false) || !accepted || winFocused || stopped)
|
|
787
|
+
return;
|
|
788
|
+
// No answer, from a dock that never gave one: nothing more to go by.
|
|
789
|
+
if (said === undefined && !dock.answers)
|
|
790
|
+
return;
|
|
791
|
+
accepted = false;
|
|
792
|
+
inTile = false;
|
|
793
|
+
giveBack('dock', said?.unsure === true);
|
|
794
|
+
});
|
|
795
|
+
};
|
|
796
|
+
// The dock's no, unasked: judged as the guard's own look would, then and
|
|
797
|
+
// there, unless the keyboard is on its way back already.
|
|
798
|
+
const onRefused = (unsure) => { if (!returning)
|
|
799
|
+
check(true, unsure); };
|
|
800
|
+
const onWinBlur = () => {
|
|
801
|
+
winFocused = false;
|
|
802
|
+
watch(true);
|
|
803
|
+
// Straight from the page, unless the page names another frame of its own
|
|
804
|
+
// as having the keyboard by now: then it went there, and what comes into
|
|
805
|
+
// the tile next comes from that frame, however soon (see `judge`).
|
|
806
|
+
straight = holds(frame) || !framed(focused(doc));
|
|
807
|
+
// Where the keyboard went is known by now. Into another frame of the
|
|
808
|
+
// page's: what had it on the page has it no longer (see `check`), and
|
|
809
|
+
// that is settled at once, not a task later. A tile that took the
|
|
810
|
+
// keyboard from that frame in between would have it put back on what
|
|
811
|
+
// the user had left.
|
|
812
|
+
if (!holds(frame) && !onNothing() && focused(doc) !== holder)
|
|
813
|
+
holder = null;
|
|
814
|
+
// The keyboard has gone again, and that is for `check` to judge and to
|
|
815
|
+
// count. A giving back still making sure of an earlier time is over: it
|
|
816
|
+
// would take this one back as well, before it had been counted.
|
|
817
|
+
round++;
|
|
818
|
+
win.setTimeout(check, 0);
|
|
819
|
+
};
|
|
820
|
+
const onWinFocus = () => {
|
|
821
|
+
winFocused = true;
|
|
822
|
+
watch();
|
|
823
|
+
inTile = false;
|
|
824
|
+
accepted = tabbed = returning = false;
|
|
825
|
+
// (Nor does the Tab from the stop explain anything after it: see `spend`.)
|
|
826
|
+
spend();
|
|
827
|
+
// Back in the page, and maybe on nothing of it (a press on its plain
|
|
828
|
+
// text): then nothing has the keyboard to be given it back.
|
|
829
|
+
win.setTimeout(forget, 0);
|
|
830
|
+
};
|
|
831
|
+
// Out of the tab order for as long as the guard runs, unless the host
|
|
832
|
+
// keeps it as it made it (see `tabOrder`). Whoever tabs down the page then
|
|
833
|
+
// never ends up in the tile by chance, and a Tab says nothing of the
|
|
834
|
+
// keyboard arriving in it (see `onKey`).
|
|
835
|
+
const back = tabOrder === 'keep' ? undefined : holdOut(frame);
|
|
836
|
+
win.addEventListener('blur', onWinBlur);
|
|
837
|
+
win.addEventListener('focus', onWinFocus);
|
|
838
|
+
doc.addEventListener('focusin', onFocusIn, true);
|
|
839
|
+
doc.addEventListener('focusout', onFocusOut, true);
|
|
840
|
+
doc.addEventListener('keydown', onKey, true);
|
|
841
|
+
// A guard made with the keyboard in another frame of the page's watches from the start.
|
|
842
|
+
watch();
|
|
843
|
+
// (A task later: what `engaged` reads may be made just after the guard.)
|
|
844
|
+
win.setTimeout(standBy, 0);
|
|
845
|
+
// (The page's own timers throughout: they go with the page.)
|
|
846
|
+
const poll = win.setInterval(check, cfg.pollMs);
|
|
847
|
+
const stop = () => {
|
|
848
|
+
if (stopped)
|
|
849
|
+
return;
|
|
850
|
+
stopped = true;
|
|
851
|
+
lift();
|
|
852
|
+
shut();
|
|
853
|
+
back?.();
|
|
854
|
+
standIn?.remove();
|
|
855
|
+
watch();
|
|
856
|
+
win.clearInterval(poll);
|
|
857
|
+
dock.stop();
|
|
858
|
+
win.removeEventListener('blur', onWinBlur);
|
|
859
|
+
win.removeEventListener('focus', onWinFocus);
|
|
860
|
+
doc.removeEventListener('focusin', onFocusIn, true);
|
|
861
|
+
doc.removeEventListener('focusout', onFocusOut, true);
|
|
862
|
+
doc.removeEventListener('keydown', onKey, true);
|
|
863
|
+
tile.removeEventListener('close', stop);
|
|
864
|
+
tile.removeEventListener('load', probe);
|
|
865
|
+
};
|
|
866
|
+
// A tile that is closed has no frame left to guard.
|
|
867
|
+
tile.addEventListener('close', stop);
|
|
868
|
+
// The host's own way in: what arrives in the tile that way is judged as a
|
|
869
|
+
// Tab into it is, and a Tab is as long ago once it has gone elsewhere.
|
|
870
|
+
// It goes in through the frame's window, not the element: a frame the
|
|
871
|
+
// page's script focused stays the page's active element in Chrome after
|
|
872
|
+
// the user has gone on into another frame, and the guard would not see the
|
|
873
|
+
// keyboard leave the tile, nor the tile take it back.
|
|
874
|
+
// It is the user picking the tile, too, where it waits for them to.
|
|
875
|
+
// That puts the keyboard in the dock's own page, where what is typed
|
|
876
|
+
// reaches nothing; the dock puts it on into the tile when told, for a user
|
|
877
|
+
// who went in with a press (a click or a tap, which this is called from).
|
|
878
|
+
// One who went in from the keyboard is left there: their Tab from there
|
|
879
|
+
// goes to the first thing in the tile, which it would not from inside it.
|
|
880
|
+
const focus = () => {
|
|
881
|
+
lift();
|
|
882
|
+
// The user went in themselves: a guess of the dock's after this is spared
|
|
883
|
+
// again, however soon after a layer lifted on its own (see `spareMs`).
|
|
884
|
+
liftedAt = -Infinity;
|
|
885
|
+
lastTab = now();
|
|
886
|
+
into = pressing();
|
|
887
|
+
frame.contentWindow?.focus();
|
|
888
|
+
};
|
|
889
|
+
// Whether what the page is handling now is a press of the user's, and not
|
|
890
|
+
// a key (a click the keyboard made has no `detail`).
|
|
891
|
+
const pressing = () => {
|
|
892
|
+
const ev = win.event;
|
|
893
|
+
if (!ev?.isTrusted)
|
|
894
|
+
return false;
|
|
895
|
+
if (/click$/.test(ev.type))
|
|
896
|
+
return ev.detail > 0;
|
|
897
|
+
return /^(pointer|mouse|touch)/.test(ev.type);
|
|
898
|
+
};
|
|
899
|
+
return { stop, focus };
|
|
900
|
+
}
|
|
901
|
+
/** The numbers `clickGate` goes by where it is given none. First guesses. */
|
|
902
|
+
export const CLICK_GATE = Object.freeze({
|
|
903
|
+
/** How long presses are ignored, in milliseconds. */
|
|
904
|
+
ms: 600,
|
|
905
|
+
/** How far the pointer has to really move for them to be taken again sooner, in pixels. */
|
|
906
|
+
travel: 24,
|
|
907
|
+
});
|
|
908
|
+
const gates = new WeakMap();
|
|
909
|
+
const PRESSES = ['pointerdown', 'pointerup', 'mousedown', 'mouseup', 'click', 'dblclick', 'auxclick', 'contextmenu'];
|
|
910
|
+
// What keeps its own counsel about presses: the blocks the loader itself
|
|
911
|
+
// puts up, its link prompt, and anything the host marks.
|
|
912
|
+
const PASSES = `.${SHIELD_CLASS}, .tile-link-dialog, [data-tile-gate="pass"]`;
|
|
913
|
+
/**
|
|
914
|
+
* The page's own controls ignore presses and clicks for a moment after
|
|
915
|
+
* something moved them that neither the page nor, as far as it can see, the
|
|
916
|
+
* user did. One per document: `arm(ms)` starts the moment, a press that is
|
|
917
|
+
* ignored starts it again, and a real movement of the pointer across the
|
|
918
|
+
* page, which is the user aiming afresh at what is there now, ends it.
|
|
919
|
+
*
|
|
920
|
+
* A key is not a press: what the keyboard activates is where the focus is,
|
|
921
|
+
* and nothing that moved the page moved that. An element of the host's that
|
|
922
|
+
* must take presses regardless is marked `data-tile-gate="pass"`.
|
|
923
|
+
*/
|
|
924
|
+
export function clickGate(doc = document, options = {}) {
|
|
925
|
+
const known = gates.get(doc);
|
|
926
|
+
if (known)
|
|
927
|
+
return known;
|
|
928
|
+
const win = doc.defaultView;
|
|
929
|
+
const travel = options.travel ?? CLICK_GATE.travel;
|
|
930
|
+
let until = 0;
|
|
931
|
+
let ms = CLICK_GATE.ms;
|
|
932
|
+
let anchor;
|
|
933
|
+
const gate = {
|
|
934
|
+
get closed() { return now() < until; },
|
|
935
|
+
arm(forMs = CLICK_GATE.ms) {
|
|
936
|
+
ms = forMs;
|
|
937
|
+
until = Math.max(until, now() + forMs);
|
|
938
|
+
anchor = undefined;
|
|
939
|
+
},
|
|
940
|
+
open() { until = 0; },
|
|
941
|
+
};
|
|
942
|
+
const swallow = (ev) => {
|
|
943
|
+
const e = ev;
|
|
944
|
+
if (!e.isTrusted || now() >= until)
|
|
945
|
+
return;
|
|
946
|
+
if (e.type === 'click' && e.detail === 0)
|
|
947
|
+
return; // the keyboard's
|
|
948
|
+
if (e.target?.closest?.(PASSES))
|
|
949
|
+
return;
|
|
950
|
+
e.stopImmediatePropagation();
|
|
951
|
+
// 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.
|
|
952
|
+
if (!(e.pointerType === 'touch' && e.type !== 'click'))
|
|
953
|
+
e.preventDefault();
|
|
954
|
+
if (e.type === 'pointerdown' || e.type === 'click')
|
|
955
|
+
until = now() + ms;
|
|
956
|
+
};
|
|
957
|
+
for (const type of PRESSES)
|
|
958
|
+
win.addEventListener(type, swallow, true);
|
|
959
|
+
win.addEventListener('pointermove', (ev) => {
|
|
960
|
+
const e = ev;
|
|
961
|
+
if (!e.isTrusted || now() >= until || e.pointerType === 'touch')
|
|
962
|
+
return;
|
|
963
|
+
if (!anchor) {
|
|
964
|
+
anchor = { x: e.clientX, y: e.clientY };
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
if (Math.hypot(e.clientX - anchor.x, e.clientY - anchor.y) >= travel)
|
|
968
|
+
until = 0;
|
|
969
|
+
}, { capture: true, passive: true });
|
|
970
|
+
gates.set(doc, gate);
|
|
971
|
+
return gate;
|
|
972
|
+
}
|
|
973
|
+
/** `guardScroll`'s numbers where a host gives none. */
|
|
974
|
+
export const SCROLL_GUARD = Object.freeze({
|
|
975
|
+
inputMs: 1200,
|
|
976
|
+
flingMs: 2500,
|
|
977
|
+
slack: 60,
|
|
978
|
+
pulls: 3,
|
|
979
|
+
withinMs: 10000,
|
|
980
|
+
gateMs: CLICK_GATE.ms,
|
|
981
|
+
jumpPx: 150,
|
|
982
|
+
jumpGateMs: 2500,
|
|
983
|
+
});
|
|
984
|
+
// What a key scrolls by: pixels, pages (viewport heights), or all the way.
|
|
985
|
+
const SCROLL_KEYS = { ArrowDown: ['px', 40], ArrowUp: ['px', -40], PageDown: ['page', 1], PageUp: ['page', -1], ' ': ['page', 1], End: ['all', 1], Home: ['all', -1] };
|
|
986
|
+
const framed = (el) => el?.localName === 'iframe' || el?.localName === 'frame';
|
|
987
|
+
// The frames of a page that have a scroll guard on them, each with whether
|
|
988
|
+
// input can reach it: always, unless it waits to be picked. A guard is one
|
|
989
|
+
// tile's, and the page it watches is everyone's. So each has to know of the
|
|
990
|
+
// others: which of two tiles at the viewport moved the page cannot be told.
|
|
991
|
+
const scrollGuarded = new WeakMap();
|
|
992
|
+
// The calls a page scrolls itself with, wrapped so that each says so first:
|
|
993
|
+
// once for a page, however many of its guards asked, and put back as they
|
|
994
|
+
// were when the last of those stops.
|
|
995
|
+
const declaring = new WeakMap();
|
|
996
|
+
function declareOwn(win, tell) {
|
|
997
|
+
let shared = declaring.get(win);
|
|
998
|
+
if (!shared) {
|
|
999
|
+
const made = { tell: new Set(), restore: [] };
|
|
1000
|
+
const wrap = (obj, name) => {
|
|
1001
|
+
const original = obj?.[name];
|
|
1002
|
+
if (!obj || typeof original !== 'function')
|
|
1003
|
+
return;
|
|
1004
|
+
obj[name] = function (...args) {
|
|
1005
|
+
// (A focus() that says it will not scroll explains no scroll: the
|
|
1006
|
+
// focus guard gives the keyboard back with one, whenever a tile
|
|
1007
|
+
// takes it.)
|
|
1008
|
+
if (!(name === 'focus' && args[0]?.preventScroll))
|
|
1009
|
+
for (const fn of made.tell)
|
|
1010
|
+
fn();
|
|
1011
|
+
return original.apply(this, args);
|
|
1012
|
+
};
|
|
1013
|
+
made.restore.push(() => { obj[name] = original; });
|
|
1014
|
+
};
|
|
1015
|
+
const w = win;
|
|
1016
|
+
for (const name of ['scrollTo', 'scrollBy', 'scroll']) {
|
|
1017
|
+
wrap(w, name);
|
|
1018
|
+
wrap(w.Element.prototype, name);
|
|
1019
|
+
}
|
|
1020
|
+
wrap(w.Element.prototype, 'scrollIntoView');
|
|
1021
|
+
wrap(w.HTMLElement.prototype, 'focus');
|
|
1022
|
+
declaring.set(win, made);
|
|
1023
|
+
shared = made;
|
|
1024
|
+
}
|
|
1025
|
+
const mine = shared;
|
|
1026
|
+
mine.tell.add(tell);
|
|
1027
|
+
return () => {
|
|
1028
|
+
mine.tell.delete(tell);
|
|
1029
|
+
if (mine.tell.size || declaring.get(win) !== mine)
|
|
1030
|
+
return;
|
|
1031
|
+
mine.restore.forEach(fn => fn());
|
|
1032
|
+
declaring.delete(win);
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Accounts for every move of the page a tile is in, and takes back the
|
|
1037
|
+
* tile's own.
|
|
1038
|
+
*
|
|
1039
|
+
* The page's vertical scroll position is followed, and each move is one of:
|
|
1040
|
+
*
|
|
1041
|
+
* - `user`: the page saw the input for it (a wheel, a key, a finger, a
|
|
1042
|
+
* mouse button held down, a pointer on the scrollbar), and the move goes
|
|
1043
|
+
* that way and no further than the input asked: a wheel turned by 100
|
|
1044
|
+
* does not explain a jump of 1500.
|
|
1045
|
+
* - `own`: the page said it would (`expect()`, or `own: 'patch'`), or it
|
|
1046
|
+
* navigated, or the user tabbed on.
|
|
1047
|
+
* - `layout`: the document's height changed, by as much as the move or
|
|
1048
|
+
* more. It stands. Where the tile's own box changed with it, the gate is
|
|
1049
|
+
* armed: the move may be the tile's doing after all.
|
|
1050
|
+
* - `ambiguous`: none of those, but the page cannot rule the user out. A
|
|
1051
|
+
* mouse was inside the tile and may have been wheeling there, the
|
|
1052
|
+
* keyboard is in the tile, or a finger may be on it. The same goes for
|
|
1053
|
+
* any other frame of the page's: what the user does in one scrolls the
|
|
1054
|
+
* page too, and the page sees none of it. The move stands, and the gate
|
|
1055
|
+
* is armed: what slid under the pointer takes no press for a moment
|
|
1056
|
+
* (`gateMs`; `jumpGateMs` after a jump), or until the pointer has really
|
|
1057
|
+
* moved.
|
|
1058
|
+
* - `tile`: none of those, and the tile's frame is at or in the viewport
|
|
1059
|
+
* once it is done, which is where its own scrolling leaves it. It is
|
|
1060
|
+
* taken back before it is painted. (The gate is armed only where the page
|
|
1061
|
+
* could not be put back whole: a move that is undone slid nothing under
|
|
1062
|
+
* the pointer.)
|
|
1063
|
+
* - `elsewhere`: unexplained, and nowhere near the tile. Left alone (the
|
|
1064
|
+
* browser and assistive tools scroll pages too), gate armed.
|
|
1065
|
+
*
|
|
1066
|
+
* A tile that pulls the page back against the way the user has just
|
|
1067
|
+
* scrolled is doing it on purpose: `pulls` of those within `withinMs` are
|
|
1068
|
+
* reported (`onAbuse`), and the tile is stopped (see `GuardStoppedEvent`)
|
|
1069
|
+
* unless told otherwise (`stop: false`). Where another tile with a guard on
|
|
1070
|
+
* it is at the viewport as well, which of them it was cannot be told: the
|
|
1071
|
+
* move is taken back all the same, and neither is held to it.
|
|
1072
|
+
*
|
|
1073
|
+
* The page has to say when it scrolls itself, or let the guard wrap its
|
|
1074
|
+
* calls: an unexplained move of the page's own that ends at the tile is
|
|
1075
|
+
* taken back like the tile's. Only the document's own scrolling is
|
|
1076
|
+
* followed, not that of a box inside it.
|
|
1077
|
+
*/
|
|
1078
|
+
export function guardScroll(tile, frame, options = {}) {
|
|
1079
|
+
const doc = frame.ownerDocument;
|
|
1080
|
+
const win = doc.defaultView;
|
|
1081
|
+
const pointer = watchPointer(doc);
|
|
1082
|
+
const cfg = { ...withDefaults(SCROLL_GUARD, options), undo: options.undo !== false };
|
|
1083
|
+
const gate = options.gate === false ? undefined : (options.gate ?? clickGate(doc));
|
|
1084
|
+
const root = () => doc.scrollingElement || doc.documentElement;
|
|
1085
|
+
// How much of the frame is in the viewport, in pixels of height.
|
|
1086
|
+
const visible = () => {
|
|
1087
|
+
const r = frame.getBoundingClientRect();
|
|
1088
|
+
return Math.max(0, Math.min(r.bottom, win.innerHeight) - Math.max(r.top, 0));
|
|
1089
|
+
};
|
|
1090
|
+
// Whether an element is at or in the viewport.
|
|
1091
|
+
const at = (el) => {
|
|
1092
|
+
const r = el.getBoundingClientRect();
|
|
1093
|
+
return r.height > 0 && r.bottom >= -2 && r.top <= win.innerHeight + 2;
|
|
1094
|
+
};
|
|
1095
|
+
const near = () => at(frame);
|
|
1096
|
+
// Whether a point was in a frame's box before the page moved by `d`.
|
|
1097
|
+
const within = (el, p, d) => {
|
|
1098
|
+
const r = el.getBoundingClientRect();
|
|
1099
|
+
const top = r.top + d;
|
|
1100
|
+
return p.x >= r.left - 2 && p.x <= r.right + 2 && p.y >= top - 2 && p.y <= top + r.height + 2;
|
|
1101
|
+
};
|
|
1102
|
+
const others = scrollGuarded.get(doc) ?? new Map();
|
|
1103
|
+
scrollGuarded.set(doc, others);
|
|
1104
|
+
others.set(frame, () => !options.engaged || options.engaged());
|
|
1105
|
+
// Whether ours is the only guarded tile at the viewport: the only one
|
|
1106
|
+
// that can have brought the page to where it is.
|
|
1107
|
+
const alone = () => {
|
|
1108
|
+
for (const el of others.keys())
|
|
1109
|
+
if (el !== frame && el.isConnected && at(el))
|
|
1110
|
+
return false;
|
|
1111
|
+
return true;
|
|
1112
|
+
};
|
|
1113
|
+
// Whether a finger may have been on another frame of the page's: one that
|
|
1114
|
+
// was in view before the page moved by `d`, and that input reaches. (A
|
|
1115
|
+
// frame in a shadow tree is only known of here if it has a guard.)
|
|
1116
|
+
const fingerElsewhere = (d) => {
|
|
1117
|
+
for (const el of new Set([...doc.getElementsByTagName('iframe'), ...others.keys()])) {
|
|
1118
|
+
if (el === frame || !el.isConnected || others.get(el)?.() === false)
|
|
1119
|
+
continue;
|
|
1120
|
+
const r = el.getBoundingClientRect();
|
|
1121
|
+
const top = r.top + d;
|
|
1122
|
+
if (r.width > 0 && r.height > 0 && top < win.innerHeight && top + r.height > 0)
|
|
1123
|
+
return true;
|
|
1124
|
+
}
|
|
1125
|
+
return false;
|
|
1126
|
+
};
|
|
1127
|
+
let y = win.scrollY;
|
|
1128
|
+
let docHeight = root().scrollHeight;
|
|
1129
|
+
let frameHeight = frame.getBoundingClientRect().height;
|
|
1130
|
+
let seen = visible();
|
|
1131
|
+
// What the user has asked for and the page has not scrolled yet, in pixels (signed).
|
|
1132
|
+
let credit = 0;
|
|
1133
|
+
let inputAt = -Infinity;
|
|
1134
|
+
let lastDir = 0; // the way the user last scrolled
|
|
1135
|
+
let lastDirAt = -Infinity;
|
|
1136
|
+
let down = false; // a mouse button is down on the page: a scrollbar, a drag, autoscroll
|
|
1137
|
+
let downAt = -Infinity;
|
|
1138
|
+
let flingUntil = -Infinity;
|
|
1139
|
+
let flingDir = 0;
|
|
1140
|
+
let expected = -Infinity;
|
|
1141
|
+
let restoring; // where the page was put back to, until that move is heard of
|
|
1142
|
+
let undone = 0;
|
|
1143
|
+
let tookBackAt = -Infinity; // when a move of the tile's was last taken back
|
|
1144
|
+
let typedAt = -Infinity; // when the user last typed into a field of the page's
|
|
1145
|
+
let pulls = [];
|
|
1146
|
+
let stopped = false;
|
|
1147
|
+
let touchY;
|
|
1148
|
+
// Where the page was when the pointer was last seen going into a frame.
|
|
1149
|
+
let entered;
|
|
1150
|
+
const onPointer = () => {
|
|
1151
|
+
const p = pointer.state;
|
|
1152
|
+
if (p.kind !== 'frame')
|
|
1153
|
+
entered = undefined;
|
|
1154
|
+
else if (entered?.at !== p.at)
|
|
1155
|
+
entered = { at: p.at, y: win.scrollY };
|
|
1156
|
+
};
|
|
1157
|
+
onPointer();
|
|
1158
|
+
const offPointer = pointer.onChange(onPointer);
|
|
1159
|
+
const ask = (px) => {
|
|
1160
|
+
const t = now();
|
|
1161
|
+
if (t - inputAt > cfg.inputMs || Math.sign(px) !== Math.sign(credit))
|
|
1162
|
+
credit = 0; // stale, or the user turned round
|
|
1163
|
+
credit += px;
|
|
1164
|
+
inputAt = t;
|
|
1165
|
+
lastDir = Math.sign(px);
|
|
1166
|
+
lastDirAt = t;
|
|
1167
|
+
};
|
|
1168
|
+
const onWheel = (ev) => {
|
|
1169
|
+
const e = ev;
|
|
1170
|
+
if (e.isTrusted && e.deltaY)
|
|
1171
|
+
ask(e.deltaY * (e.deltaMode === 1 ? 40 : e.deltaMode === 2 ? win.innerHeight : 1));
|
|
1172
|
+
};
|
|
1173
|
+
const onKey = (ev) => {
|
|
1174
|
+
const e = ev;
|
|
1175
|
+
if (!e.isTrusted || holds(frame))
|
|
1176
|
+
return;
|
|
1177
|
+
if (e.key === 'Tab') {
|
|
1178
|
+
expected = Math.max(expected, now() + 300);
|
|
1179
|
+
return;
|
|
1180
|
+
} // focus moving on may scroll to it
|
|
1181
|
+
// A key typed into a field of the page's scrolls nothing itself, but the
|
|
1182
|
+
// browser may move the page to keep the caret in view.
|
|
1183
|
+
if (editable(focused(doc))) {
|
|
1184
|
+
typedAt = now();
|
|
1185
|
+
return;
|
|
1186
|
+
}
|
|
1187
|
+
const k = SCROLL_KEYS[e.key];
|
|
1188
|
+
if (!k)
|
|
1189
|
+
return;
|
|
1190
|
+
const sign = e.key === ' ' && e.shiftKey ? -1 : 1;
|
|
1191
|
+
ask(sign * (k[0] === 'px' ? k[1] : k[0] === 'page' ? k[1] * win.innerHeight : k[1] * 1e9));
|
|
1192
|
+
};
|
|
1193
|
+
const onTouchStart = (ev) => {
|
|
1194
|
+
const e = ev;
|
|
1195
|
+
if (!e.isTrusted)
|
|
1196
|
+
return;
|
|
1197
|
+
touchY = e.touches[0]?.clientY;
|
|
1198
|
+
flingUntil = -Infinity;
|
|
1199
|
+
};
|
|
1200
|
+
const onTouchMove = (ev) => {
|
|
1201
|
+
const e = ev;
|
|
1202
|
+
if (!e.isTrusted)
|
|
1203
|
+
return;
|
|
1204
|
+
const ty = e.touches[0]?.clientY;
|
|
1205
|
+
if (touchY !== undefined && ty !== undefined && ty !== touchY)
|
|
1206
|
+
ask(touchY - ty);
|
|
1207
|
+
touchY = ty;
|
|
1208
|
+
};
|
|
1209
|
+
// The finger is up; the page may coast on, the way it was going.
|
|
1210
|
+
const onTouchEnd = (ev) => {
|
|
1211
|
+
if (!ev.isTrusted || !lastDir || now() - lastDirAt >= 300)
|
|
1212
|
+
return;
|
|
1213
|
+
flingUntil = now() + cfg.flingMs;
|
|
1214
|
+
flingDir = lastDir;
|
|
1215
|
+
};
|
|
1216
|
+
const onDown = (ev) => { if (ev.isTrusted && ev.pointerType !== 'touch') {
|
|
1217
|
+
down = true;
|
|
1218
|
+
downAt = now();
|
|
1219
|
+
} };
|
|
1220
|
+
const onUp = (ev) => { if (ev.isTrusted && ev.pointerType !== 'touch') {
|
|
1221
|
+
down = false;
|
|
1222
|
+
downAt = now();
|
|
1223
|
+
} };
|
|
1224
|
+
const onNav = () => { expected = now() + 1500; };
|
|
1225
|
+
const byUser = (d, t) => {
|
|
1226
|
+
// A button held down on the page, or just let go: the page may scroll
|
|
1227
|
+
// for it (a drag, a click on something that scrolls). Not while the tile
|
|
1228
|
+
// has just been trying: its next try would pass for the page's answer to
|
|
1229
|
+
// a press that had nothing to do with it.
|
|
1230
|
+
if ((down || t - downAt < 300) && t - tookBackAt >= cfg.gateMs)
|
|
1231
|
+
return true;
|
|
1232
|
+
// The user is typing in a field of the page's, and the browser keeps the
|
|
1233
|
+
// caret in view: a line or two at a time, with the field still there to
|
|
1234
|
+
// see afterwards. (Under the same condition: a tile that keeps trying
|
|
1235
|
+
// does not get to creep the page along under the typing.)
|
|
1236
|
+
if (t - typedAt < 300 && Math.abs(d) <= cfg.slack && t - tookBackAt >= cfg.gateMs) {
|
|
1237
|
+
const field = focused(doc);
|
|
1238
|
+
if (field && editable(field) && at(field))
|
|
1239
|
+
return true;
|
|
1240
|
+
}
|
|
1241
|
+
// The viewport's own scrollbar gives the page no events: a pointer last seen at the right edge may be on it.
|
|
1242
|
+
const p = pointer.state;
|
|
1243
|
+
if (p.kind === 'host' && p.type !== 'touch' && p.x >= Math.min(root().clientWidth, win.innerWidth - 18))
|
|
1244
|
+
return true;
|
|
1245
|
+
if (t < flingUntil && Math.sign(d) === flingDir)
|
|
1246
|
+
return true;
|
|
1247
|
+
if (t - inputAt > cfg.inputMs || Math.sign(d) !== Math.sign(credit))
|
|
1248
|
+
return false;
|
|
1249
|
+
if (Math.abs(d) > Math.abs(credit) + cfg.slack)
|
|
1250
|
+
return false;
|
|
1251
|
+
credit -= d;
|
|
1252
|
+
if (Math.sign(credit) !== Math.sign(d))
|
|
1253
|
+
credit = 0; // spent (the slack is not carried over)
|
|
1254
|
+
return true;
|
|
1255
|
+
};
|
|
1256
|
+
// Whether a mouse the page last saw going into `el` can still have been
|
|
1257
|
+
// in it before the page moved by `d`. The page is told when a mouse goes
|
|
1258
|
+
// into a frame, and where; nothing of where it goes from there. So it may
|
|
1259
|
+
// be resting anywhere in the frame, and is still in it for as long as the
|
|
1260
|
+
// page has not moved by the frame's own height since. One that did not
|
|
1261
|
+
// move there is another matter: the frame came to it, and it is where it
|
|
1262
|
+
// was, which is all the frame's box before the move is asked.
|
|
1263
|
+
const mouseIn = (el, d) => {
|
|
1264
|
+
const p = pointer.state;
|
|
1265
|
+
if (!p.moved)
|
|
1266
|
+
return within(el, p, d);
|
|
1267
|
+
const r = el.getBoundingClientRect();
|
|
1268
|
+
const top = r.top + d;
|
|
1269
|
+
const shifted = Math.abs(y - (entered?.y ?? y));
|
|
1270
|
+
return r.width > 0 && shifted < r.height && top < win.innerHeight && top + r.height > 0;
|
|
1271
|
+
};
|
|
1272
|
+
// Could the user have scrolled through a frame, where the page sees no
|
|
1273
|
+
// input? Judged by where things were BEFORE the move: a tile that scrolls
|
|
1274
|
+
// itself under a pointer resting on the page is then "hovered", and that
|
|
1275
|
+
// must not count for it. `d` is how far the page has just moved.
|
|
1276
|
+
const cannotTell = (d) => {
|
|
1277
|
+
const p = pointer.state;
|
|
1278
|
+
const fingers = !pointer.hovers || p.kind === 'none';
|
|
1279
|
+
const into = !fingers && p.kind === 'frame' ? pointer.frame : undefined;
|
|
1280
|
+
// The page's other frames first. What the user does in one scrolls the
|
|
1281
|
+
// page as well, and is no more the page's to see than what they do in
|
|
1282
|
+
// the tile: the keyboard in one, a mouse that went into one, a finger
|
|
1283
|
+
// that may be on one.
|
|
1284
|
+
const active = focused(doc);
|
|
1285
|
+
if (framed(active) && active !== frame)
|
|
1286
|
+
return true;
|
|
1287
|
+
if (into && into !== frame && mouseIn(into, d))
|
|
1288
|
+
return true;
|
|
1289
|
+
if (fingers && fingerElsewhere(d))
|
|
1290
|
+
return true;
|
|
1291
|
+
if (options.engaged && !options.engaged())
|
|
1292
|
+
return false; // no input reaches a tile that is not picked
|
|
1293
|
+
if (holds(frame))
|
|
1294
|
+
return true; // keys pressed in the tile scroll the page too
|
|
1295
|
+
if (fingers)
|
|
1296
|
+
return seen > 0; // a finger may be on it, if it was in view
|
|
1297
|
+
if (into === frame)
|
|
1298
|
+
return mouseIn(frame, d); // a mouse that went into it
|
|
1299
|
+
return p.kind === 'host' && within(frame, p, d); // or one the page sees over it (it lies under something of the page's)
|
|
1300
|
+
};
|
|
1301
|
+
const onScroll = (ev) => {
|
|
1302
|
+
if (stopped || (ev.target !== doc && ev.target !== root()))
|
|
1303
|
+
return;
|
|
1304
|
+
const to = win.scrollY;
|
|
1305
|
+
const h = root().scrollHeight;
|
|
1306
|
+
const fh = frame.getBoundingClientRect().height;
|
|
1307
|
+
// Our own move, putting the page back. Unless it has been moved again
|
|
1308
|
+
// before this was heard of: that is accounted for like any other move,
|
|
1309
|
+
// from where we had put it.
|
|
1310
|
+
const ours = restoring !== undefined && Math.abs(to - restoring) < 1;
|
|
1311
|
+
if (restoring !== undefined)
|
|
1312
|
+
y = restoring;
|
|
1313
|
+
restoring = undefined;
|
|
1314
|
+
if (ours || to === y) {
|
|
1315
|
+
y = to;
|
|
1316
|
+
docHeight = h;
|
|
1317
|
+
frameHeight = fh;
|
|
1318
|
+
seen = visible();
|
|
1319
|
+
return;
|
|
1320
|
+
}
|
|
1321
|
+
const t = now();
|
|
1322
|
+
const from = y;
|
|
1323
|
+
const d = to - from;
|
|
1324
|
+
const against = lastDir !== 0 && t - lastDirAt < cfg.inputMs && Math.sign(d) !== lastDir;
|
|
1325
|
+
// A jump in one go is not how a wheel or a finger moves a page: what it
|
|
1326
|
+
// put under the pointer stays untouchable for longer.
|
|
1327
|
+
const hush = Math.abs(d) > cfg.jumpPx ? cfg.jumpGateMs : cfg.gateMs;
|
|
1328
|
+
let verdict;
|
|
1329
|
+
if (t < expected)
|
|
1330
|
+
verdict = 'own';
|
|
1331
|
+
else if (h !== docHeight && Math.abs(d) <= Math.abs(h - docHeight) + cfg.slack) {
|
|
1332
|
+
verdict = 'layout';
|
|
1333
|
+
// The tile's own box changed with it: this may be its doing after all.
|
|
1334
|
+
if (fh !== frameHeight)
|
|
1335
|
+
gate?.arm(hush);
|
|
1336
|
+
}
|
|
1337
|
+
else if (byUser(d, t))
|
|
1338
|
+
verdict = 'user';
|
|
1339
|
+
else if (cannotTell(d))
|
|
1340
|
+
verdict = 'ambiguous';
|
|
1341
|
+
else if (near())
|
|
1342
|
+
verdict = 'tile';
|
|
1343
|
+
else
|
|
1344
|
+
verdict = 'elsewhere';
|
|
1345
|
+
docHeight = h;
|
|
1346
|
+
frameHeight = fh;
|
|
1347
|
+
const take = verdict === 'tile' && cfg.undo;
|
|
1348
|
+
// Asked before the page is put back: it is where the move left things
|
|
1349
|
+
// that says which tiles can have made it.
|
|
1350
|
+
const mine = take && alone();
|
|
1351
|
+
// Whether the page is back where it was, to the pixel: nothing has then
|
|
1352
|
+
// moved under the pointer.
|
|
1353
|
+
let whole = false;
|
|
1354
|
+
if (take) {
|
|
1355
|
+
undone++;
|
|
1356
|
+
credit = 0;
|
|
1357
|
+
tookBackAt = t;
|
|
1358
|
+
win.scrollTo({ top: Math.max(0, Math.min(from, h - win.innerHeight)), left: win.scrollX, behavior: 'instant' });
|
|
1359
|
+
restoring = win.scrollY;
|
|
1360
|
+
whole = Math.abs(restoring - from) < 1;
|
|
1361
|
+
}
|
|
1362
|
+
else {
|
|
1363
|
+
y = to;
|
|
1364
|
+
seen = visible();
|
|
1365
|
+
}
|
|
1366
|
+
// What a move left standing slid under the pointer takes no press for a
|
|
1367
|
+
// moment. One that was put back whole slid nothing anywhere: were it to
|
|
1368
|
+
// close the gate, a tile that kept asking would keep the page from
|
|
1369
|
+
// taking any press at all.
|
|
1370
|
+
if (verdict === 'ambiguous' || verdict === 'elsewhere' || (verdict === 'tile' && !take))
|
|
1371
|
+
gate?.arm(hush);
|
|
1372
|
+
else if (take && !whole)
|
|
1373
|
+
gate?.arm(cfg.gateMs);
|
|
1374
|
+
options.onMove?.({ from, to, verdict, undone: take });
|
|
1375
|
+
// Taken back whoever made it. Held against this tile only where no
|
|
1376
|
+
// other can have: a tile is not stopped for what its neighbour did.
|
|
1377
|
+
if (!mine || !against)
|
|
1378
|
+
return;
|
|
1379
|
+
pulls = pulls.filter(p => t - p < cfg.withinMs);
|
|
1380
|
+
pulls.push(t);
|
|
1381
|
+
if (pulls.length < cfg.pulls)
|
|
1382
|
+
return;
|
|
1383
|
+
pulls = [];
|
|
1384
|
+
options.onAbuse?.('scroll', undone);
|
|
1385
|
+
if (options.stop !== false)
|
|
1386
|
+
halt(tile, frame, 'scroll');
|
|
1387
|
+
};
|
|
1388
|
+
// The page's own scrolling, declared for it: the calls a page scrolls
|
|
1389
|
+
// itself with are wrapped so that each says so first. Only when asked: a
|
|
1390
|
+
// page that would rather not have them touched calls expect() itself.
|
|
1391
|
+
const undeclare = options.own === 'patch' ? declareOwn(win, () => { expected = Math.max(expected, now() + 1000); }) : undefined;
|
|
1392
|
+
const opts = { capture: true, passive: true };
|
|
1393
|
+
const onResize = () => {
|
|
1394
|
+
docHeight = root().scrollHeight;
|
|
1395
|
+
frameHeight = frame.getBoundingClientRect().height;
|
|
1396
|
+
seen = visible();
|
|
1397
|
+
y = win.scrollY;
|
|
1398
|
+
};
|
|
1399
|
+
doc.addEventListener('wheel', onWheel, opts);
|
|
1400
|
+
doc.addEventListener('keydown', onKey, true);
|
|
1401
|
+
doc.addEventListener('touchstart', onTouchStart, opts);
|
|
1402
|
+
doc.addEventListener('touchmove', onTouchMove, opts);
|
|
1403
|
+
doc.addEventListener('touchend', onTouchEnd, opts);
|
|
1404
|
+
doc.addEventListener('pointerdown', onDown, opts);
|
|
1405
|
+
doc.addEventListener('pointerup', onUp, opts);
|
|
1406
|
+
win.addEventListener('hashchange', onNav);
|
|
1407
|
+
win.addEventListener('popstate', onNav);
|
|
1408
|
+
win.addEventListener('resize', onResize);
|
|
1409
|
+
doc.addEventListener('scroll', onScroll, opts);
|
|
1410
|
+
const stop = () => {
|
|
1411
|
+
if (stopped)
|
|
1412
|
+
return;
|
|
1413
|
+
stopped = true;
|
|
1414
|
+
undeclare?.();
|
|
1415
|
+
offPointer();
|
|
1416
|
+
others.delete(frame);
|
|
1417
|
+
doc.removeEventListener('wheel', onWheel, opts);
|
|
1418
|
+
doc.removeEventListener('keydown', onKey, true);
|
|
1419
|
+
doc.removeEventListener('touchstart', onTouchStart, opts);
|
|
1420
|
+
doc.removeEventListener('touchmove', onTouchMove, opts);
|
|
1421
|
+
doc.removeEventListener('touchend', onTouchEnd, opts);
|
|
1422
|
+
doc.removeEventListener('pointerdown', onDown, opts);
|
|
1423
|
+
doc.removeEventListener('pointerup', onUp, opts);
|
|
1424
|
+
doc.removeEventListener('scroll', onScroll, opts);
|
|
1425
|
+
win.removeEventListener('hashchange', onNav);
|
|
1426
|
+
win.removeEventListener('popstate', onNav);
|
|
1427
|
+
win.removeEventListener('resize', onResize);
|
|
1428
|
+
tile.removeEventListener('close', stop);
|
|
1429
|
+
};
|
|
1430
|
+
// A tile that is closed has no frame left to guard.
|
|
1431
|
+
tile.addEventListener('close', stop);
|
|
1432
|
+
return {
|
|
1433
|
+
expect(ms = 1000) { expected = Math.max(expected, now() + ms); },
|
|
1434
|
+
stop,
|
|
1435
|
+
};
|
|
1436
|
+
}
|
|
1437
|
+
/** The class of the layer's element. */
|
|
1438
|
+
export const ENGAGE_CLASS = 'tile-engage';
|
|
1439
|
+
/**
|
|
1440
|
+
* "Press to use": until the user picks the tile, a layer of the host's lies
|
|
1441
|
+
* over it. The tile runs and shows, and gets no input. A wheel or a finger
|
|
1442
|
+
* on it scrolls the page, the page sees every pointer that crosses it, and
|
|
1443
|
+
* focus arriving in the frame is by definition the tile's own doing. A
|
|
1444
|
+
* press on the layer picks the tile (that press is spent on it: nothing can
|
|
1445
|
+
* hand an event on to another origin's frame). A press anywhere else on the
|
|
1446
|
+
* page, the tile leaving the viewport, or Escape, puts the layer back.
|
|
1447
|
+
*
|
|
1448
|
+
* Until it is picked the tile is not a stop on the way through the page with
|
|
1449
|
+
* Tab either (its frame has `tabindex="-1"` meanwhile): the layer is, after
|
|
1450
|
+
* it in its box, and Enter or Space there picks the tile and goes into it.
|
|
1451
|
+
* (With the focus guard on as well, the frame stays out of the tab order
|
|
1452
|
+
* once the tile is picked, and the guard's stop is the way back in.)
|
|
1453
|
+
*
|
|
1454
|
+
* The layer paints, though too faintly to see: a block that paints nothing
|
|
1455
|
+
* does not take a press over a frame. `stop()` takes it away for good.
|
|
1456
|
+
*/
|
|
1457
|
+
export function engage(tile, frame, options = {}) {
|
|
1458
|
+
const doc = frame.ownerDocument;
|
|
1459
|
+
const win = doc.defaultView;
|
|
1460
|
+
const wrapper = options.wrapper ?? frame.parentElement;
|
|
1461
|
+
if (!wrapper)
|
|
1462
|
+
throw new Error('engage() needs the frame to be in an element that holds it.');
|
|
1463
|
+
const position = win.getComputedStyle(wrapper).position;
|
|
1464
|
+
if (!position || position === 'static')
|
|
1465
|
+
wrapper.style.position = 'relative';
|
|
1466
|
+
const layer = doc.createElement('div');
|
|
1467
|
+
layer.className = ENGAGE_CLASS;
|
|
1468
|
+
layer.setAttribute('role', 'button');
|
|
1469
|
+
layer.setAttribute('tabindex', '0');
|
|
1470
|
+
layer.setAttribute('aria-label', options.label ?? 'Use this tile');
|
|
1471
|
+
layer.style.cssText = 'position:absolute;inset:0;z-index:1;background:rgba(127,127,127,0.03);cursor:pointer;';
|
|
1472
|
+
let engaged = false;
|
|
1473
|
+
let stopped = false;
|
|
1474
|
+
// A tile that is not picked is not to be tabbed into: Tab would take the
|
|
1475
|
+
// keyboard past the layer and into it. (With a guard on the keyboard that
|
|
1476
|
+
// would be given back, every time, and count against the tile.) Held out
|
|
1477
|
+
// with whatever else holds it, the focus guard's layer among them (see
|
|
1478
|
+
// `holdOut`): picked, the frame is back in only once nothing else holds it.
|
|
1479
|
+
let back;
|
|
1480
|
+
const reach = (on) => {
|
|
1481
|
+
if (!on)
|
|
1482
|
+
back ??= holdOut(frame);
|
|
1483
|
+
else {
|
|
1484
|
+
back?.();
|
|
1485
|
+
back = undefined;
|
|
1486
|
+
}
|
|
1487
|
+
};
|
|
1488
|
+
const set = (on, why) => {
|
|
1489
|
+
if (on === engaged || stopped)
|
|
1490
|
+
return;
|
|
1491
|
+
engaged = on;
|
|
1492
|
+
layer.style.display = on ? 'none' : 'block';
|
|
1493
|
+
reach(on);
|
|
1494
|
+
if (!on && holds(frame))
|
|
1495
|
+
frame.blur();
|
|
1496
|
+
options.onChange?.(on, why);
|
|
1497
|
+
};
|
|
1498
|
+
layer.addEventListener('click', (ev) => { if (ev.isTrusted)
|
|
1499
|
+
set(true, 'picked'); });
|
|
1500
|
+
layer.addEventListener('keydown', (ev) => {
|
|
1501
|
+
if (!ev.isTrusted || (ev.key !== 'Enter' && ev.key !== ' '))
|
|
1502
|
+
return;
|
|
1503
|
+
ev.preventDefault();
|
|
1504
|
+
set(true, 'picked');
|
|
1505
|
+
// Through the frame's window, as the focus guard's own way in goes (see
|
|
1506
|
+
// `guardFocus`), and for its reason.
|
|
1507
|
+
frame.contentWindow?.focus();
|
|
1508
|
+
});
|
|
1509
|
+
const onDown = (ev) => { if (ev.isTrusted && engaged && !wrapper.contains((ev.composedPath?.()[0] ?? ev.target)))
|
|
1510
|
+
set(false, 'pressed elsewhere'); };
|
|
1511
|
+
const onKey = (ev) => { if (ev.isTrusted && engaged && ev.key === 'Escape')
|
|
1512
|
+
set(false, 'escape'); };
|
|
1513
|
+
doc.addEventListener('pointerdown', onDown, true);
|
|
1514
|
+
doc.addEventListener('keydown', onKey, true);
|
|
1515
|
+
const seen = typeof win.IntersectionObserver === 'function'
|
|
1516
|
+
? new win.IntersectionObserver((entries) => { if (entries.some(en => !en.isIntersecting))
|
|
1517
|
+
set(false, 'out of view'); })
|
|
1518
|
+
: undefined;
|
|
1519
|
+
seen?.observe(frame);
|
|
1520
|
+
wrapper.append(layer);
|
|
1521
|
+
reach(false);
|
|
1522
|
+
if (options.start === 'engaged')
|
|
1523
|
+
set(true, 'started');
|
|
1524
|
+
const stop = () => {
|
|
1525
|
+
if (stopped)
|
|
1526
|
+
return;
|
|
1527
|
+
stopped = true;
|
|
1528
|
+
reach(true);
|
|
1529
|
+
doc.removeEventListener('pointerdown', onDown, true);
|
|
1530
|
+
doc.removeEventListener('keydown', onKey, true);
|
|
1531
|
+
seen?.disconnect();
|
|
1532
|
+
layer.remove();
|
|
1533
|
+
tile.removeEventListener('close', stop);
|
|
1534
|
+
};
|
|
1535
|
+
// A tile that is closed has no frame left to lay anything over.
|
|
1536
|
+
tile.addEventListener('close', stop);
|
|
1537
|
+
return {
|
|
1538
|
+
layer,
|
|
1539
|
+
get engaged() { return engaged; },
|
|
1540
|
+
release() { set(false, 'released'); },
|
|
1541
|
+
stop,
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
//# sourceMappingURL=guard.js.map
|