@hadoku/ui-shell 0.1.95 → 0.1.97
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/dist/motion/css.d.ts +13 -0
- package/dist/motion/css.js +166 -0
- package/dist/motion/index.d.ts +30 -3
- package/dist/motion/index.js +84 -4
- package/dist/usage/index.d.ts +22 -2
- package/dist/usage/index.js +23 -7
- package/package.json +2 -2
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** A selector gated on `test` holding at an ancestor (or at its own leading `:root`/`html`). */
|
|
2
|
+
export declare function gateSelector(selector: string, test: string): string;
|
|
3
|
+
/**
|
|
4
|
+
* The play states that pause `decl`'s infinite entries, or null when it runs none. `looping`: the custom
|
|
5
|
+
* properties the sheet defines as an infinite animation, for an entry that is only `var(--x)` (card.css's
|
|
6
|
+
* flyers keep their path in one).
|
|
7
|
+
*/
|
|
8
|
+
export declare function ambientPlayState(prop: string, value: string, looping?: ReadonlySet<string>): string | null;
|
|
9
|
+
/** The PostCSS plugin. */
|
|
10
|
+
export declare const motionCss: {
|
|
11
|
+
postcssPlugin: string;
|
|
12
|
+
Once(sheet: unknown): void;
|
|
13
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The sheets' motion, answering to the player's switches (`./index.ts`) as well as the system's. A build
|
|
3
|
+
* pass, both builds' (`vite.config.ts`, `vite.embed.config.ts`), over the app's own sheets only:
|
|
4
|
+
*
|
|
5
|
+
* - `@media (prefers-reduced-motion: reduce) { R }` keeps its media query, for a page whose script has
|
|
6
|
+
* not written the switches yet, but each rule in it is gated on `data-motion` not being `full`; and a
|
|
7
|
+
* copy of R, outside the query, applies under `data-motion='reduce'`. Its `@keyframes` are hoisted beside
|
|
8
|
+
* it (a name defined twice at the top is a build error: hoisting it would redefine the other).
|
|
9
|
+
* - `@media (prefers-reduced-motion: no-preference) { R }` likewise: gated on `data-motion` not being
|
|
10
|
+
* `reduce`, and copied for `data-motion='full'`.
|
|
11
|
+
* - Every rule whose `animation` (or `animation-iteration-count`) runs `infinite` gets a sibling that, under
|
|
12
|
+
* `data-ambient='off'`, pauses exactly its infinite entries: the loop stands on its first frame, and a
|
|
13
|
+
* one-shot beside it in the same list (an entrance) still plays. Where a reduce query has a rule for the
|
|
14
|
+
* same selector, that rule is the loop's still look, and it is copied for `data-ambient='off'` as well.
|
|
15
|
+
*
|
|
16
|
+
* Every gate is a `:where()`, so no rewritten rule weighs more than the rule it came from. The attribute is
|
|
17
|
+
* an ANCESTOR's (the document root standalone, the embed's top element inside its shadow tree); a
|
|
18
|
+
* selector that starts at `:root` or `html` takes the gate on that compound instead.
|
|
19
|
+
*/
|
|
20
|
+
const REDUCE = /^\(\s*prefers-reduced-motion\s*:\s*reduce\s*\)$/;
|
|
21
|
+
const NO_PREFERENCE = /^\(\s*prefers-reduced-motion\s*:\s*no-preference\s*\)$/;
|
|
22
|
+
/** A selector gated on `test` holding at an ancestor (or at its own leading `:root`/`html`). */
|
|
23
|
+
export function gateSelector(selector, test) {
|
|
24
|
+
const s = selector.trim();
|
|
25
|
+
const lead = /^(:root|html)((?:\[[^\]]*\]|:[\w-]+(?:\([^)]*\))?)*)/.exec(s);
|
|
26
|
+
if (lead !== null)
|
|
27
|
+
return `${lead[0]}:where(${test})${s.slice(lead[0].length)}`;
|
|
28
|
+
return `:where(${test}) ${s}`;
|
|
29
|
+
}
|
|
30
|
+
/** At an ancestor, or here: the attribute test a gate is built from. */
|
|
31
|
+
const is = (attr, value) => `[${attr}='${value}']`;
|
|
32
|
+
/** No ancestor carries it: true of the root when the root does not, and of nothing below a root that does. */
|
|
33
|
+
const isNot = (attr, value) => `:not(${is(attr, value)}, ${is(attr, value)} *)`;
|
|
34
|
+
/** Splits a comma list at the top level, never inside parentheses. */
|
|
35
|
+
function topLevelList(value) {
|
|
36
|
+
const out = [];
|
|
37
|
+
let depth = 0;
|
|
38
|
+
let start = 0;
|
|
39
|
+
for (let i = 0; i < value.length; i++) {
|
|
40
|
+
const c = value[i];
|
|
41
|
+
if (c === '(')
|
|
42
|
+
depth++;
|
|
43
|
+
else if (c === ')')
|
|
44
|
+
depth--;
|
|
45
|
+
else if (c === ',' && depth === 0) {
|
|
46
|
+
out.push(value.slice(start, i));
|
|
47
|
+
start = i + 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
out.push(value.slice(start));
|
|
51
|
+
return out.map((s) => s.trim());
|
|
52
|
+
}
|
|
53
|
+
const INFINITE = /(?<![\w-])infinite(?![\w-])/;
|
|
54
|
+
/**
|
|
55
|
+
* The play states that pause `decl`'s infinite entries, or null when it runs none. `looping`: the custom
|
|
56
|
+
* properties the sheet defines as an infinite animation, for an entry that is only `var(--x)` (card.css's
|
|
57
|
+
* flyers keep their path in one).
|
|
58
|
+
*/
|
|
59
|
+
export function ambientPlayState(prop, value, looping = new Set()) {
|
|
60
|
+
if (prop !== 'animation' && prop !== 'animation-iteration-count')
|
|
61
|
+
return null;
|
|
62
|
+
const entries = topLevelList(value);
|
|
63
|
+
const loops = (e) => INFINITE.test(e) || [...e.matchAll(/var\(\s*(--[\w-]+)/g)].some((m) => looping.has(m[1]));
|
|
64
|
+
if (!entries.some(loops))
|
|
65
|
+
return null;
|
|
66
|
+
return entries.map((e) => (loops(e) ? 'paused' : 'running')).join(', ');
|
|
67
|
+
}
|
|
68
|
+
function inside(node, names) {
|
|
69
|
+
for (let p = node.parent; p !== undefined; p = p.parent)
|
|
70
|
+
if (p.type === 'atrule' && names.includes(p.name.toLowerCase()))
|
|
71
|
+
return true;
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
const OWN_SRC = /[\\/](?:apps[\\/][^\\/]+|packages[\\/][^\\/]+|games[\\/][^\\/]+[\\/](?:ui|packages[\\/][^\\/]+))[\\/]src[\\/]/;
|
|
75
|
+
function ownSheet(file) {
|
|
76
|
+
return file !== undefined && OWN_SRC.test(file) && !/[\\/]node_modules[\\/]/.test(file);
|
|
77
|
+
}
|
|
78
|
+
const norm = (selector) => selector.trim().replace(/\s+/g, ' ');
|
|
79
|
+
/**
|
|
80
|
+
* `stills`: the selectors of the sheet's looping rules. A reduce query's rule for one of them is the still look
|
|
81
|
+
* its author drew for that loop (the clock's grace ring steady at full, not paused on its first, empty frame),
|
|
82
|
+
* so it is copied for `data-ambient='off'` too, after the pause it then overrides.
|
|
83
|
+
*/
|
|
84
|
+
function mirrorMedia(media, attr, keep, copy, topKeyframes, stills = new Set()) {
|
|
85
|
+
const copies = [];
|
|
86
|
+
media.each((child) => {
|
|
87
|
+
if (child.type === 'rule') {
|
|
88
|
+
const rule = child;
|
|
89
|
+
const still = rule.selectors.filter((sel) => stills.has(norm(sel)));
|
|
90
|
+
if (still.length > 0) {
|
|
91
|
+
const calm = rule.clone();
|
|
92
|
+
calm.selectors = still.map((sel) => gateSelector(sel, is('data-ambient', 'off')));
|
|
93
|
+
copies.push(calm);
|
|
94
|
+
}
|
|
95
|
+
const forced = rule.clone();
|
|
96
|
+
forced.selectors = rule.selectors.map((s) => gateSelector(s, is(attr, copy)));
|
|
97
|
+
rule.selectors = rule.selectors.map((s) => gateSelector(s, isNot(attr, keep)));
|
|
98
|
+
copies.push(forced);
|
|
99
|
+
}
|
|
100
|
+
else if (child.type === 'atrule' && child.name.toLowerCase() === 'keyframes') {
|
|
101
|
+
const name = child.params.trim();
|
|
102
|
+
if (topKeyframes.has(name))
|
|
103
|
+
throw new Error(`${child.source?.input.file ?? 'a stylesheet'}: @keyframes ${name} is defined inside a prefers-reduced-motion query and outside it; give the query's its own name`);
|
|
104
|
+
copies.push(child.clone());
|
|
105
|
+
}
|
|
106
|
+
else if (child.type === 'atrule') {
|
|
107
|
+
throw new Error(`${child.source?.input.file ?? 'a stylesheet'}: @${child.name} inside a prefers-reduced-motion query is not mirrored by motionCss; move it out or teach the pass`);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
if (copies.length > 0)
|
|
111
|
+
media.after(copies);
|
|
112
|
+
}
|
|
113
|
+
/** The PostCSS plugin. */
|
|
114
|
+
export const motionCss = {
|
|
115
|
+
postcssPlugin: 'hadoku-motion',
|
|
116
|
+
// `unknown`, read as the few node methods this uses: postcss is not this package's dependency, and its own
|
|
117
|
+
// types are not structurally the slice declared above.
|
|
118
|
+
Once(sheet) {
|
|
119
|
+
const root = sheet;
|
|
120
|
+
if (!ownSheet(root.source?.input.file))
|
|
121
|
+
return;
|
|
122
|
+
const topKeyframes = new Set();
|
|
123
|
+
root.each((n) => {
|
|
124
|
+
if (n.type === 'atrule' && n.name.toLowerCase() === 'keyframes')
|
|
125
|
+
topKeyframes.add(n.params.trim());
|
|
126
|
+
});
|
|
127
|
+
// Ambient first, so a looping rule inside a motion query is mirrored with its calm sibling. Once runs once per
|
|
128
|
+
// sheet: the rules inserted here are visited by the walk, and carry nothing it rewrites.
|
|
129
|
+
const looping = new Set();
|
|
130
|
+
const stills = new Set();
|
|
131
|
+
root.walkDecls((decl) => {
|
|
132
|
+
if (decl.prop.startsWith('--') && topLevelList(decl.value).some((e) => INFINITE.test(e)))
|
|
133
|
+
looping.add(decl.prop);
|
|
134
|
+
});
|
|
135
|
+
root.walkRules((rule) => {
|
|
136
|
+
if (inside(rule, ['keyframes']))
|
|
137
|
+
return;
|
|
138
|
+
const states = [];
|
|
139
|
+
rule.walkDecls((decl) => {
|
|
140
|
+
const state = ambientPlayState(decl.prop.toLowerCase(), decl.value, looping);
|
|
141
|
+
if (state !== null)
|
|
142
|
+
states.push(state);
|
|
143
|
+
});
|
|
144
|
+
if (states.length === 0)
|
|
145
|
+
return;
|
|
146
|
+
for (const sel of rule.selectors)
|
|
147
|
+
stills.add(norm(sel));
|
|
148
|
+
const calm = rule.clone();
|
|
149
|
+
calm.removeAll();
|
|
150
|
+
calm.selectors = rule.selectors.map((s) => gateSelector(s, is('data-ambient', 'off')));
|
|
151
|
+
// The last declaration wins in the authored rule, so it does here.
|
|
152
|
+
const decl = (rule.nodes ?? []).find((n) => n.type === 'decl').clone({ prop: 'animation-play-state', value: states[states.length - 1] });
|
|
153
|
+
calm.append(decl);
|
|
154
|
+
rule.after(calm);
|
|
155
|
+
});
|
|
156
|
+
root.walkAtRules('media', (media) => {
|
|
157
|
+
const params = media.params.trim();
|
|
158
|
+
if (REDUCE.test(params))
|
|
159
|
+
mirrorMedia(media, 'data-motion', 'full', 'reduce', topKeyframes, stills);
|
|
160
|
+
else if (NO_PREFERENCE.test(params))
|
|
161
|
+
mirrorMedia(media, 'data-motion', 'reduce', 'full', topKeyframes);
|
|
162
|
+
else if (/prefers-reduced-motion/.test(params))
|
|
163
|
+
throw new Error(`${media.source?.input.file ?? 'a stylesheet'}: @media ${params} combines prefers-reduced-motion with another query; motionCss mirrors only the plain one`);
|
|
164
|
+
});
|
|
165
|
+
},
|
|
166
|
+
};
|
package/dist/motion/index.d.ts
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
|
+
/** One player's choices; a switch left `undefined` follows the system. */
|
|
2
|
+
export interface MotionChoices {
|
|
3
|
+
cardMotion?: boolean;
|
|
4
|
+
screenShake?: boolean;
|
|
5
|
+
ambientMotion?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** Every switch resolved: what the page does now. */
|
|
8
|
+
export type MotionState = Required<MotionChoices>;
|
|
9
|
+
export declare const MOTION_ATTR: {
|
|
10
|
+
readonly motion: "data-motion";
|
|
11
|
+
readonly shake: "data-shake";
|
|
12
|
+
readonly ambient: "data-ambient";
|
|
13
|
+
};
|
|
14
|
+
/** Whether the system asks for less motion: the default for every switch nobody has set. */
|
|
15
|
+
export declare function systemReducesMotion(): boolean;
|
|
16
|
+
/** The switches as the page applies them: each the player's, else the system's. */
|
|
17
|
+
export declare function motionState(choices?: MotionChoices): MotionState;
|
|
18
|
+
/** The player's choices, as the page applies them from now on (`applyMotion` writes them on a root). */
|
|
19
|
+
export declare function setMotionChoices(choices: MotionChoices): void;
|
|
1
20
|
/**
|
|
2
|
-
* Whether the
|
|
3
|
-
*
|
|
4
|
-
* re-exports it so none of its 20+ importers moved.
|
|
21
|
+
* Whether the board draws its reduced-motion path: card motion off. Any game's board reads this without
|
|
22
|
+
* caring where the question lives; cardtable's own `play/motion.ts` re-exports it.
|
|
5
23
|
*/
|
|
6
24
|
export declare function prefersReducedMotion(): boolean;
|
|
25
|
+
/** Whether the table may shake. */
|
|
26
|
+
export declare function screenShakeAllowed(): boolean;
|
|
27
|
+
/** Writes the effective switches on `root`, where the sheets' rewritten selectors (`./css.ts`) look for them. */
|
|
28
|
+
export declare function applyMotion(root: Element, state?: MotionState): void;
|
|
29
|
+
export declare function clearMotion(root: Element): void;
|
|
30
|
+
/** Per game (`<key>`, handed in): the choices last applied in this browser, so the first frame is drawn with them. */
|
|
31
|
+
export declare function rememberedMotion(key: string): MotionChoices | null;
|
|
32
|
+
export declare function rememberMotion(key: string, choices: MotionChoices): void;
|
|
33
|
+
export { ambientPlayState, gateSelector, motionCss } from './css.js';
|
package/dist/motion/index.js
CHANGED
|
@@ -1,8 +1,88 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* How much the page moves, the player's say over it (operator, 2026-11-21, after a player said moving
|
|
3
|
+
* elements were bad for their ADHD: "let's just make sure that each of them can be individually toggled").
|
|
4
|
+
*
|
|
5
|
+
* Three switches, each its own:
|
|
6
|
+
* - **Card motion** — what travels, rams, cracks and pops. Off is the board's reduced-motion path:
|
|
7
|
+
* every `prefersReducedMotion()` reader, and every `@media (prefers-reduced-motion: …)` block,
|
|
8
|
+
* which `motionCss` (`./css.ts`) makes answer to the switch.
|
|
9
|
+
* - **Screen shake** — the table jolting under a heavy blow (`screenShakeAllowed()`).
|
|
10
|
+
* - **Ambient motion** — the endless loops: pulses, glows, shimmers, bobbing. Off pauses each, its
|
|
11
|
+
* first frame left standing (`motionCss`).
|
|
12
|
+
*
|
|
13
|
+
* A switch nobody has set follows the system's `prefers-reduced-motion`: asked for less, all three
|
|
14
|
+
* start off. What the player sets wins either way, so a player can also turn motion ON over the OS.
|
|
15
|
+
* The effective values are written on the root the sheets resolve on, as `data-motion`
|
|
16
|
+
* (`full`/`reduce`), `data-shake` and `data-ambient` (`on`/`off`) — always, so the sheets never have
|
|
17
|
+
* to ask the media query themselves.
|
|
18
|
+
*/
|
|
19
|
+
export const MOTION_ATTR = { motion: 'data-motion', shake: 'data-shake', ambient: 'data-ambient' };
|
|
20
|
+
let chosen = {};
|
|
21
|
+
/** Whether the system asks for less motion: the default for every switch nobody has set. */
|
|
22
|
+
export function systemReducesMotion() {
|
|
23
|
+
return typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
24
|
+
}
|
|
25
|
+
/** The switches as the page applies them: each the player's, else the system's. */
|
|
26
|
+
export function motionState(choices = chosen) {
|
|
27
|
+
const system = !systemReducesMotion();
|
|
28
|
+
return {
|
|
29
|
+
cardMotion: choices.cardMotion ?? system,
|
|
30
|
+
screenShake: choices.screenShake ?? system,
|
|
31
|
+
ambientMotion: choices.ambientMotion ?? system,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** The player's choices, as the page applies them from now on (`applyMotion` writes them on a root). */
|
|
35
|
+
export function setMotionChoices(choices) {
|
|
36
|
+
chosen = { ...choices };
|
|
37
|
+
}
|
|
1
38
|
/**
|
|
2
|
-
* Whether the
|
|
3
|
-
*
|
|
4
|
-
* re-exports it so none of its 20+ importers moved.
|
|
39
|
+
* Whether the board draws its reduced-motion path: card motion off. Any game's board reads this without
|
|
40
|
+
* caring where the question lives; cardtable's own `play/motion.ts` re-exports it.
|
|
5
41
|
*/
|
|
6
42
|
export function prefersReducedMotion() {
|
|
7
|
-
return
|
|
43
|
+
return !motionState().cardMotion;
|
|
44
|
+
}
|
|
45
|
+
/** Whether the table may shake. */
|
|
46
|
+
export function screenShakeAllowed() {
|
|
47
|
+
return motionState().screenShake;
|
|
48
|
+
}
|
|
49
|
+
/** Writes the effective switches on `root`, where the sheets' rewritten selectors (`./css.ts`) look for them. */
|
|
50
|
+
export function applyMotion(root, state = motionState()) {
|
|
51
|
+
root.setAttribute(MOTION_ATTR.motion, state.cardMotion ? 'full' : 'reduce');
|
|
52
|
+
root.setAttribute(MOTION_ATTR.shake, state.screenShake ? 'on' : 'off');
|
|
53
|
+
root.setAttribute(MOTION_ATTR.ambient, state.ambientMotion ? 'on' : 'off');
|
|
54
|
+
}
|
|
55
|
+
export function clearMotion(root) {
|
|
56
|
+
for (const attr of Object.values(MOTION_ATTR))
|
|
57
|
+
root.removeAttribute(attr);
|
|
58
|
+
}
|
|
59
|
+
/** Per game (`<key>`, handed in): the choices last applied in this browser, so the first frame is drawn with them. */
|
|
60
|
+
export function rememberedMotion(key) {
|
|
61
|
+
try {
|
|
62
|
+
const raw = globalThis.localStorage?.getItem(key) ?? null;
|
|
63
|
+
if (raw === null)
|
|
64
|
+
return null;
|
|
65
|
+
const parsed = JSON.parse(raw);
|
|
66
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
67
|
+
return null;
|
|
68
|
+
const out = {};
|
|
69
|
+
for (const name of ['cardMotion', 'screenShake', 'ambientMotion']) {
|
|
70
|
+
const v = parsed[name];
|
|
71
|
+
if (typeof v === 'boolean')
|
|
72
|
+
out[name] = v;
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export function rememberMotion(key, choices) {
|
|
81
|
+
try {
|
|
82
|
+
globalThis.localStorage?.setItem(key, JSON.stringify(choices));
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// Not remembered: the next page follows the system until prefs answer.
|
|
86
|
+
}
|
|
8
87
|
}
|
|
88
|
+
export { ambientPlayState, gateSelector, motionCss } from "./css.js";
|
package/dist/usage/index.d.ts
CHANGED
|
@@ -5,28 +5,48 @@
|
|
|
5
5
|
* its ids stripped (`seat-3` → `seat`), so a new control is counted without anyone remembering to;
|
|
6
6
|
* a press on nothing named is not counted. A batch goes when it is full, on a timer, and when the
|
|
7
7
|
* page hides (`sendBeacon`, which survives the tab closing). A failed send is dropped.
|
|
8
|
+
*
|
|
9
|
+
* Every entry carries `at`, this page's clock when it happened, and the batch carries `sentAt`, the
|
|
10
|
+
* same clock when it left: the server places each entry at its own receipt time less
|
|
11
|
+
* `sentAt - at`, so a browser whose clock is wrong still lands its trail in the right order and
|
|
12
|
+
* at the right moment.
|
|
13
|
+
*
|
|
14
|
+
* The screen is read from the whole address (path and hash), not the hash alone: the app's routes
|
|
15
|
+
* are paths written with `history.pushState`, which fires no event, so the screen is re-read on
|
|
16
|
+
* `popstate`/`hashchange`, at every press (before it, so a press counts on the screen it was made
|
|
17
|
+
* on) and once just after it (so a press that navigates opens its new screen at once).
|
|
8
18
|
*/
|
|
9
19
|
export interface UsageEntry {
|
|
10
20
|
kind: 'view' | 'click';
|
|
11
21
|
screen: string;
|
|
12
22
|
target?: string;
|
|
23
|
+
/** This page's `Date.now()` when it happened. */
|
|
24
|
+
at: number;
|
|
13
25
|
}
|
|
14
26
|
export interface UsageBatch {
|
|
15
27
|
entries: UsageEntry[];
|
|
28
|
+
/** This page's `Date.now()` when the batch was sent. */
|
|
29
|
+
sentAt: number;
|
|
30
|
+
}
|
|
31
|
+
/** What `screenOf` is handed: the address bar's path and hash. */
|
|
32
|
+
export interface UsageAddress {
|
|
33
|
+
pathname: string;
|
|
34
|
+
hash: string;
|
|
16
35
|
}
|
|
17
36
|
/** A `data-testid` as a usage name, or null when what is left is not a name. */
|
|
18
37
|
export declare function targetName(testid: string): string | null;
|
|
19
38
|
export interface UsageDeps {
|
|
20
39
|
/** Where a batch is posted; the caller's own `/api/...` path. */
|
|
21
40
|
url: string;
|
|
22
|
-
/** The screen
|
|
23
|
-
screenOf: (
|
|
41
|
+
/** The screen an address names; the caller's own route parsing. */
|
|
42
|
+
screenOf: (address: UsageAddress) => string;
|
|
24
43
|
/** Entries per batch before an early flush; the caller's own schema limit. */
|
|
25
44
|
batchMax: number;
|
|
26
45
|
flushMs?: number;
|
|
27
46
|
doc?: Document;
|
|
28
47
|
win?: Window;
|
|
29
48
|
send?: (url: string, batch: UsageBatch) => void;
|
|
49
|
+
now?: () => number;
|
|
30
50
|
}
|
|
31
51
|
/** Start counting. Returns the stop, which flushes what is left. */
|
|
32
52
|
export declare function startUsage(deps: UsageDeps): () => void;
|
package/dist/usage/index.js
CHANGED
|
@@ -5,6 +5,16 @@
|
|
|
5
5
|
* its ids stripped (`seat-3` → `seat`), so a new control is counted without anyone remembering to;
|
|
6
6
|
* a press on nothing named is not counted. A batch goes when it is full, on a timer, and when the
|
|
7
7
|
* page hides (`sendBeacon`, which survives the tab closing). A failed send is dropped.
|
|
8
|
+
*
|
|
9
|
+
* Every entry carries `at`, this page's clock when it happened, and the batch carries `sentAt`, the
|
|
10
|
+
* same clock when it left: the server places each entry at its own receipt time less
|
|
11
|
+
* `sentAt - at`, so a browser whose clock is wrong still lands its trail in the right order and
|
|
12
|
+
* at the right moment.
|
|
13
|
+
*
|
|
14
|
+
* The screen is read from the whole address (path and hash), not the hash alone: the app's routes
|
|
15
|
+
* are paths written with `history.pushState`, which fires no event, so the screen is re-read on
|
|
16
|
+
* `popstate`/`hashchange`, at every press (before it, so a press counts on the screen it was made
|
|
17
|
+
* on) and once just after it (so a press that navigates opens its new screen at once).
|
|
8
18
|
*/
|
|
9
19
|
const NAME = /^[a-z0-9][a-z0-9_.:/-]*$/;
|
|
10
20
|
/** A `data-testid` as a usage name, or null when what is left is not a name. */
|
|
@@ -35,46 +45,52 @@ export function startUsage(deps) {
|
|
|
35
45
|
const doc = deps.doc ?? document;
|
|
36
46
|
const win = deps.win ?? window;
|
|
37
47
|
const send = deps.send ?? beacon;
|
|
48
|
+
const now = deps.now ?? Date.now;
|
|
38
49
|
let entries = [];
|
|
39
50
|
const flush = () => {
|
|
40
51
|
if (entries.length === 0)
|
|
41
52
|
return;
|
|
42
|
-
const batch = { entries };
|
|
53
|
+
const batch = { entries, sentAt: now() };
|
|
43
54
|
entries = [];
|
|
44
55
|
send(url, batch);
|
|
45
56
|
};
|
|
46
57
|
const push = (entry) => {
|
|
47
|
-
entries.push(entry);
|
|
58
|
+
entries.push({ ...entry, at: now() });
|
|
48
59
|
if (entries.length >= batchMax)
|
|
49
60
|
flush();
|
|
50
61
|
};
|
|
51
|
-
|
|
62
|
+
const read = () => screenOf({ pathname: win.location.pathname, hash: win.location.hash });
|
|
63
|
+
let screen = read();
|
|
52
64
|
push({ kind: 'view', screen });
|
|
53
|
-
const
|
|
54
|
-
const next =
|
|
65
|
+
const onMove = () => {
|
|
66
|
+
const next = read();
|
|
55
67
|
if (next === screen)
|
|
56
68
|
return;
|
|
57
69
|
screen = next;
|
|
58
70
|
push({ kind: 'view', screen });
|
|
59
71
|
};
|
|
60
72
|
const onClick = (event) => {
|
|
73
|
+
onMove();
|
|
61
74
|
const el = event.target instanceof Element ? event.target.closest('[data-testid]') : null;
|
|
62
75
|
const testid = el?.getAttribute('data-testid');
|
|
63
76
|
const target = testid ? targetName(testid) : null;
|
|
64
77
|
if (target !== null)
|
|
65
78
|
push({ kind: 'click', screen, target });
|
|
79
|
+
win.setTimeout(onMove, 0);
|
|
66
80
|
};
|
|
67
81
|
const onHide = () => {
|
|
68
82
|
if (doc.visibilityState === 'hidden')
|
|
69
83
|
flush();
|
|
70
84
|
};
|
|
71
|
-
win.addEventListener('hashchange',
|
|
85
|
+
win.addEventListener('hashchange', onMove);
|
|
86
|
+
win.addEventListener('popstate', onMove);
|
|
72
87
|
doc.addEventListener('click', onClick, { capture: true, passive: true });
|
|
73
88
|
doc.addEventListener('visibilitychange', onHide);
|
|
74
89
|
win.addEventListener('pagehide', flush);
|
|
75
90
|
const timer = win.setInterval(flush, flushMs);
|
|
76
91
|
return () => {
|
|
77
|
-
win.removeEventListener('hashchange',
|
|
92
|
+
win.removeEventListener('hashchange', onMove);
|
|
93
|
+
win.removeEventListener('popstate', onMove);
|
|
78
94
|
doc.removeEventListener('click', onClick, { capture: true });
|
|
79
95
|
doc.removeEventListener('visibilitychange', onHide);
|
|
80
96
|
win.removeEventListener('pagehide', flush);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/ui-shell",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.97",
|
|
4
|
+
"gitHead": "155c8c9335e64b29e1c0f0c9c18a8c79509093fb",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "The parts of a game's UI any game keeps: chrome, trim, room shell, embed mount, theme and settings frame, UI scale, usage counting, the sound mixer, motion and generic components. One appId parameterizes its storage keys, events and chat prefix. MIT; imports only react, react-dom and @wolffm/*.",
|