@uselazuli/cli 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +98 -0
- package/LICENSE +21 -0
- package/README.md +202 -2
- package/agents/lazuli-design-reviewer.md +30 -0
- package/agents/lazuli-first-look.md +22 -0
- package/agents/lazuli-task-tester.md +25 -0
- package/bin/lazuli.mjs +699 -0
- package/package.json +11 -4
- package/rules/accessibility/HYPOTHESES.md +8 -0
- package/rules/accessibility/_area.yaml +7 -0
- package/rules/accessibility/operable.yaml +100 -0
- package/rules/accessibility/perceivable.yaml +153 -0
- package/rules/accessibility/robust.yaml +24 -0
- package/rules/accessibility/understandable.yaml +72 -0
- package/rules/color/HYPOTHESES.md +27 -0
- package/rules/color/_area.yaml +10 -0
- package/rules/color/adapt.yaml +183 -0
- package/rules/color/create.yaml +259 -0
- package/rules/color/review.yaml +215 -0
- package/rules/color/signal.yaml +196 -0
- package/rules/content/HYPOTHESES.md +18 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +347 -0
- package/rules/content/voice.yaml +144 -0
- package/rules/content/writing.yaml +202 -0
- package/rules/data/HYPOTHESES.md +11 -0
- package/rules/data/_area.yaml +7 -0
- package/rules/data/charts.yaml +189 -0
- package/rules/data/dashboards.yaml +141 -0
- package/rules/data/review.yaml +159 -0
- package/rules/graphic/HYPOTHESES.md +17 -0
- package/rules/graphic/_area.yaml +8 -0
- package/rules/graphic/identity.yaml +141 -0
- package/rules/graphic/pieces.yaml +164 -0
- package/rules/graphic/review.yaml +242 -0
- package/rules/graphic/symbol.yaml +141 -0
- package/rules/motion/HYPOTHESES.md +19 -0
- package/rules/motion/_area.yaml +7 -0
- package/rules/motion/micro.yaml +143 -0
- package/rules/motion/review.yaml +347 -0
- package/rules/motion/transitions.yaml +169 -0
- package/rules/typography/HYPOTHESES.md +12 -0
- package/rules/typography/_area.yaml +8 -0
- package/rules/typography/adapt.yaml +45 -0
- package/rules/typography/create.yaml +126 -0
- package/rules/typography/review.yaml +127 -0
- package/rules/ui/HYPOTHESES.md +23 -0
- package/rules/ui/_area.yaml +9 -0
- package/rules/ui/compose.yaml +219 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +399 -0
- package/rules/ui/states.yaml +257 -0
- package/rules/ux/HYPOTHESES.md +13 -0
- package/rules/ux/_area.yaml +8 -0
- package/rules/ux/flows.yaml +164 -0
- package/rules/ux/forms.yaml +142 -0
- package/rules/ux/review.yaml +186 -0
- package/skills/lazuli/SKILL.md +180 -0
- package/skills/lazuli/procedures/create.md +29 -0
- package/skills/lazuli/procedures/explain-decisions.md +22 -0
- package/skills/lazuli/procedures/improve.md +30 -0
- package/skills/lazuli/procedures/remember-this.md +41 -0
- package/skills/lazuli/procedures/review-design.md +35 -0
- package/skills/lazuli/procedures/show-options.md +32 -0
- package/skills/lazuli-accessibility-check/SKILL.md +68 -0
- package/skills/lazuli-color-context-and-culture/SKILL.md +53 -0
- package/skills/lazuli-color-context-and-culture/references/sectors.md +26 -0
- package/skills/lazuli-color-palette-by-intent/SKILL.md +54 -0
- package/skills/lazuli-color-palette-by-intent/references/intent-chords.md +51 -0
- package/skills/lazuli-color-screen-review/SKILL.md +51 -0
- package/skills/lazuli-color-states-and-signals/SKILL.md +53 -0
- package/skills/lazuli-content-interface-writing/SKILL.md +53 -0
- package/skills/lazuli-content-voice/SKILL.md +39 -0
- package/skills/lazuli-data-charts-and-dashboards/SKILL.md +59 -0
- package/skills/lazuli-graphic-derived-pieces/SKILL.md +40 -0
- package/skills/lazuli-graphic-derived-pieces/references/piece-template.html +24 -0
- package/skills/lazuli-graphic-identity/SKILL.md +43 -0
- package/skills/lazuli-motion-micro-and-transitions/SKILL.md +49 -0
- package/skills/lazuli-typography-choose-and-pair/SKILL.md +50 -0
- package/skills/lazuli-typography-screen-review/SKILL.md +51 -0
- package/skills/lazuli-ui-composition/SKILL.md +42 -0
- package/skills/lazuli-ui-composition/references/option-template.html +34 -0
- package/skills/lazuli-ui-layout-review/SKILL.md +47 -0
- package/skills/lazuli-ui-states-and-system/SKILL.md +54 -0
- package/skills/lazuli-ux-flows-and-forms/SKILL.md +51 -0
- package/skills/lazuli-ux-task-test/SKILL.md +44 -0
- package/src/browser.mjs +161 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +428 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +97 -0
- package/src/commands/draft-memo.mjs +161 -0
- package/src/commands/hooks.mjs +343 -0
- package/src/commands/install-hooks.mjs +120 -0
- package/src/commands/install.mjs +132 -0
- package/src/commands/list-rules.mjs +49 -0
- package/src/commands/map-page.mjs +594 -0
- package/src/commands/record-answers.mjs +79 -0
- package/src/commands/record-log.mjs +296 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +164 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +383 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +549 -0
- package/src/measure/chart-read.mjs +401 -0
- package/src/measure/chart.mjs +264 -0
- package/src/measure/coherence.mjs +145 -0
- package/src/measure/color.mjs +79 -0
- package/src/measure/content.mjs +82 -0
- package/src/measure/cvd.mjs +24 -0
- package/src/measure/extreme.mjs +241 -0
- package/src/measure/forms.mjs +150 -0
- package/src/measure/in-page/a11y.js +489 -0
- package/src/measure/in-page/color.js +346 -0
- package/src/measure/in-page/composition.js +1496 -0
- package/src/measure/in-page/contrast.js +15 -0
- package/src/measure/in-page/cvd.js +173 -0
- package/src/measure/in-page/extreme.js +355 -0
- package/src/measure/in-page/frames.js +781 -0
- package/src/measure/in-page/look.js +28 -0
- package/src/measure/in-page/motion-probe.js +169 -0
- package/src/measure/in-page/style.js +107 -0
- package/src/measure/in-page/svg-strokes.js +112 -0
- package/src/measure/in-page/svg-uses.js +50 -0
- package/src/measure/in-page/symbol.js +123 -0
- package/src/measure/in-page/type.js +266 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +91 -0
- package/src/measure/motion.mjs +508 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +252 -0
- package/src/measure/rhythm.mjs +81 -0
- package/src/measure/signature.mjs +65 -0
- package/src/measure/states.mjs +240 -0
- package/src/measure/style.mjs +13 -0
- package/src/measure/symbol.mjs +268 -0
- package/src/measure/terms.mjs +153 -0
- package/src/measure/tiles.mjs +20 -0
- package/src/measure/timeline.mjs +621 -0
- package/src/measure/type.mjs +167 -0
- package/src/measure/weight.mjs +18 -0
- package/src/private-strings.mjs +81 -0
- package/src/project/blind.mjs +70 -0
- package/src/project/edits.mjs +154 -0
- package/src/project/gate.mjs +159 -0
- package/src/project/lazuli-md.mjs +497 -0
- package/src/project/log.mjs +329 -0
- package/src/project/match.mjs +112 -0
- package/src/project/records.mjs +380 -0
- package/src/project/tasks.mjs +153 -0
- package/src/report/evaluate.mjs +27 -0
- package/src/report/evaluators/a11y.mjs +206 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +86 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +53 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +94 -0
- package/src/report/evaluators/motion.mjs +273 -0
- package/src/report/evaluators/piece.mjs +69 -0
- package/src/report/evaluators/rhythm.mjs +99 -0
- package/src/report/evaluators/states.mjs +39 -0
- package/src/report/evaluators/style.mjs +35 -0
- package/src/report/evaluators/symbol.mjs +110 -0
- package/src/report/evaluators/terms.mjs +49 -0
- package/src/report/evaluators/type.mjs +163 -0
- package/src/report/evaluators/weight.mjs +51 -0
- package/src/report/evidence.mjs +4 -0
- package/src/report/finding.mjs +11 -0
- package/src/report/format.mjs +215 -0
- package/src/rules/hypotheses.mjs +28 -0
- package/src/rules/load.mjs +256 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +212 -0
- package/templates/LAZULI.md +86 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { resolveTarget } from "../browser.mjs";
|
|
5
|
+
import { matchGoals, targetKey } from "../project/match.mjs";
|
|
6
|
+
import { flowStopUrl, isLocalTarget } from "../project/tasks.mjs";
|
|
7
|
+
|
|
8
|
+
// Sampling limit, not a rule cutoff: an asset larger than this is refused instead of read.
|
|
9
|
+
export const MAX_ASSET_BYTES = 10 * 1024 * 1024;
|
|
10
|
+
// Implementation tolerances of the remote loader, not rule thresholds.
|
|
11
|
+
const MAX_HOPS = 5;
|
|
12
|
+
const LOAD_TIMEOUT_MS = 30000;
|
|
13
|
+
// Safety limit, not a rule cutoff: one run of an asset measurer (or one capture of an SVG) may take this long
|
|
14
|
+
// once its context is open. A hostile file can hold the renderer for minutes; past the limit the context is
|
|
15
|
+
// closed (which stops what runs in it) and the run fails with a note.
|
|
16
|
+
export const ASSET_RUN_CAP_MS = 60000;
|
|
17
|
+
|
|
18
|
+
// Runs work() and closes context once, when work ends or when it has not finished within ms (the close stops
|
|
19
|
+
// what still runs in it); past ms the run fails with a note naming what. The caller does not close context.
|
|
20
|
+
export async function withinCap(context, ms, what, work) {
|
|
21
|
+
let timer;
|
|
22
|
+
let closing = null;
|
|
23
|
+
const close = () => (closing ??= context.close().catch(() => {}));
|
|
24
|
+
const cap = new Promise((_, reject) => {
|
|
25
|
+
timer = setTimeout(() => {
|
|
26
|
+
close();
|
|
27
|
+
reject(new Error(`${what} did not finish within ${ms / 1000} s (a safety limit) and was stopped.`));
|
|
28
|
+
}, ms);
|
|
29
|
+
});
|
|
30
|
+
try {
|
|
31
|
+
return await Promise.race([work(), cap]);
|
|
32
|
+
} finally {
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
await close();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class AssetInputError extends Error {
|
|
39
|
+
constructor(message) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = "AssetInputError";
|
|
42
|
+
this.code = "ASSET_INPUT";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const EXT_MESSAGE = (target) => `check-design --asset reads an SVG, PNG or ICO symbol, or an HTML, PNG, JPG or WebP piece: ${target}.`;
|
|
47
|
+
const SIZE_MESSAGE = (target) =>
|
|
48
|
+
`check-design --asset ${target} needs the piece size: add "Size: 1200x630" under the goal whose Target is ${target} in LAZULI.md, or pass --size 1200x630.`;
|
|
49
|
+
export const SIZE_IGNORED = "--size does not apply to a symbol; it was ignored.";
|
|
50
|
+
|
|
51
|
+
const EXT_TYPE = {
|
|
52
|
+
".svg": "image/svg+xml",
|
|
53
|
+
".png": "image/png",
|
|
54
|
+
".ico": "image/x-icon",
|
|
55
|
+
".jpg": "image/jpeg",
|
|
56
|
+
".jpeg": "image/jpeg",
|
|
57
|
+
".webp": "image/webp",
|
|
58
|
+
".html": "text/html",
|
|
59
|
+
".htm": "text/html",
|
|
60
|
+
};
|
|
61
|
+
const TYPE_EXT = {
|
|
62
|
+
"image/svg+xml": ".svg",
|
|
63
|
+
"image/png": ".png",
|
|
64
|
+
"image/x-icon": ".ico",
|
|
65
|
+
"image/vnd.microsoft.icon": ".ico",
|
|
66
|
+
"image/jpeg": ".jpg",
|
|
67
|
+
"image/webp": ".webp",
|
|
68
|
+
"text/html": ".html",
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// The extension of the URL path (never the query), lowercase with its dot, or "".
|
|
72
|
+
function extOf(url) {
|
|
73
|
+
const last = new URL(url).pathname.split("/").pop() ?? "";
|
|
74
|
+
const m = /\.([a-z0-9]+)$/i.exec(last);
|
|
75
|
+
return m ? `.${m[1].toLowerCase()}` : "";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Kind by extension: an SVG is a symbol; a PNG or ICO is a piece with sizes, else a symbol; an HTML file is a
|
|
79
|
+
// piece; a JPG or WebP is a raster piece. A piece needs sizes. explicit: the sizes came from --size.
|
|
80
|
+
function build({ target, url, ext, sizes, explicit }) {
|
|
81
|
+
const base = { target, url, ext };
|
|
82
|
+
if (ext === ".svg") {
|
|
83
|
+
return { ...base, kind: "symbol", raster: false, sizes: null, notes: explicit ? [SIZE_IGNORED] : [] };
|
|
84
|
+
}
|
|
85
|
+
if (ext === ".png" || ext === ".ico") {
|
|
86
|
+
return sizes ? { ...base, kind: "piece", raster: true, sizes, notes: [] } : { ...base, kind: "symbol", raster: true, sizes: null, notes: [] };
|
|
87
|
+
}
|
|
88
|
+
const piece = { ".html": false, ".htm": false, ".jpg": true, ".jpeg": true, ".webp": true };
|
|
89
|
+
if (Object.hasOwn(piece, ext)) {
|
|
90
|
+
if (!sizes) throw new AssetInputError(SIZE_MESSAGE(target));
|
|
91
|
+
return { ...base, kind: "piece", raster: piece[ext], sizes, notes: [] };
|
|
92
|
+
}
|
|
93
|
+
throw new AssetInputError(EXT_MESSAGE(target));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Resolves an asset target before any browser starts: a missing local file throws FileNotFoundError. sizes
|
|
97
|
+
// (from --size) win over the Size of the goal whose Target is this asset. A web URL whose path has no known
|
|
98
|
+
// extension resolves with kind null; assetOfType decides once its bytes are loaded.
|
|
99
|
+
export function resolveAsset({ target, sizes = null, cwd = process.cwd(), md = null }) {
|
|
100
|
+
const url = resolveTarget(target, cwd);
|
|
101
|
+
const ext = extOf(url);
|
|
102
|
+
const goalSizes = matchGoals(md, target, cwd).find((g) => Array.isArray(g.size) && g.size.length > 0)?.size ?? null;
|
|
103
|
+
const given = sizes ?? goalSizes;
|
|
104
|
+
if (/^https?:/i.test(url) && !Object.hasOwn(EXT_TYPE, ext)) {
|
|
105
|
+
return { target, url, ext: null, kind: null, raster: null, sizes: given, notes: [] };
|
|
106
|
+
}
|
|
107
|
+
return build({ target, url, ext, sizes: given, explicit: sizes !== null });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Decides the kind of an asset from the content type of its loaded bytes (a URL with no known extension).
|
|
111
|
+
export function assetOfType(asset, type, { sizes = null } = {}) {
|
|
112
|
+
const ext = TYPE_EXT[String(type ?? "").split(";")[0].trim().toLowerCase()];
|
|
113
|
+
if (!ext) throw new AssetInputError(EXT_MESSAGE(asset.target));
|
|
114
|
+
return build({ target: asset.target, url: asset.url, ext, sizes: sizes ?? asset.sizes, explicit: sizes !== null });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function assetDir(cwd, target) {
|
|
118
|
+
return path.join(cwd, ".lazuli", "assets", targetKey(target, cwd));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// A loopback or private address, judged by the host name as written (a public name that resolves to a
|
|
122
|
+
// private address is not caught).
|
|
123
|
+
export function privateHost(hostname) {
|
|
124
|
+
const h = String(hostname).replace(/^\[|\]$/g, "").toLowerCase();
|
|
125
|
+
if (h === "localhost" || h.endsWith(".localhost")) return true;
|
|
126
|
+
const v4 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(h);
|
|
127
|
+
if (v4) {
|
|
128
|
+
const [a, b] = [Number(v4[1]), Number(v4[2])];
|
|
129
|
+
return a === 0 || a === 10 || a === 127 || (a === 100 && b >= 64 && b <= 127) || (a === 169 && b === 254) || (a === 172 && b >= 16 && b <= 31) || (a === 192 && b === 168);
|
|
130
|
+
}
|
|
131
|
+
if (h.includes(":")) return h === "::" || h === "::1" || /^f[cd]/.test(h) || /^fe[89ab]/.test(h) || h.startsWith("::ffff:");
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Why a redirect from one URL to the next is refused, or null when it may be followed: a scheme other than
|
|
136
|
+
// http or https, a public host sending to a loopback or private address, or a checkout or payment page.
|
|
137
|
+
export function hopRefusal(from, to) {
|
|
138
|
+
let next;
|
|
139
|
+
try {
|
|
140
|
+
next = new URL(to, from);
|
|
141
|
+
} catch {
|
|
142
|
+
return `it redirects to ${to}, which is not an address`;
|
|
143
|
+
}
|
|
144
|
+
if (next.protocol !== "http:" && next.protocol !== "https:") {
|
|
145
|
+
return `it redirects to ${next.href}, which is not an http or https address`;
|
|
146
|
+
}
|
|
147
|
+
if (!privateHost(new URL(from).hostname) && privateHost(next.hostname)) {
|
|
148
|
+
return `it redirects from a public host to the private address ${next.href}`;
|
|
149
|
+
}
|
|
150
|
+
if (!isLocalTarget(next.href) && flowStopUrl(next.href)) {
|
|
151
|
+
return `it redirects to a checkout or payment page, ${next.href}`;
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const tooLarge = (url) => new AssetInputError(`Asset is larger than the ${MAX_ASSET_BYTES / 1024 / 1024} MB limit: ${url}.`);
|
|
157
|
+
const cannotLoad = (url, reason) => new AssetInputError(`Asset could not be loaded: ${url}: ${String(reason).replace(/\.$/, "")}.`);
|
|
158
|
+
|
|
159
|
+
// The asset's bytes and content type. A local file is read from disk; a URL is fetched with the request API
|
|
160
|
+
// of a fresh context (never opened as a document, so nothing in it runs or fetches), redirects followed by
|
|
161
|
+
// hand. Each GET asks for the first MAX_ASSET_BYTES + 1 bytes with a Range header. The request API reads the
|
|
162
|
+
// whole body before it resolves: content-length refuses a large asset before its bytes are handed over, the
|
|
163
|
+
// 30 s timeout bounds a body without one, and the length is checked again.
|
|
164
|
+
export async function loadAssetBytes(asset, browser) {
|
|
165
|
+
if (asset.url.startsWith("file:")) {
|
|
166
|
+
const file = fileURLToPath(asset.url);
|
|
167
|
+
if ((await fs.stat(file)).size > MAX_ASSET_BYTES) throw tooLarge(asset.url);
|
|
168
|
+
return { bytes: await fs.readFile(file), type: EXT_TYPE[asset.ext ?? extOf(asset.url)] ?? "" };
|
|
169
|
+
}
|
|
170
|
+
const context = await browser.newContext();
|
|
171
|
+
try {
|
|
172
|
+
let current = asset.url;
|
|
173
|
+
for (let hop = 0; ; hop++) {
|
|
174
|
+
let response;
|
|
175
|
+
try {
|
|
176
|
+
// The range is inclusive: a server that honors it sends at most MAX_ASSET_BYTES + 1 bytes, so a larger
|
|
177
|
+
// file is still refused below. A server may ignore it and send the whole file first.
|
|
178
|
+
response = await context.request.get(current, {
|
|
179
|
+
maxRedirects: 0,
|
|
180
|
+
timeout: LOAD_TIMEOUT_MS,
|
|
181
|
+
failOnStatusCode: false,
|
|
182
|
+
headers: { Range: `bytes=0-${MAX_ASSET_BYTES}` },
|
|
183
|
+
});
|
|
184
|
+
} catch (err) {
|
|
185
|
+
throw cannotLoad(asset.url, String(err?.message ?? err).split("\n")[0]);
|
|
186
|
+
}
|
|
187
|
+
const status = response.status();
|
|
188
|
+
const location = response.headers().location;
|
|
189
|
+
if (status >= 300 && status < 400 && location) {
|
|
190
|
+
if (hop >= MAX_HOPS) throw cannotLoad(asset.url, `it redirects more than ${MAX_HOPS} times`);
|
|
191
|
+
const refused = hopRefusal(current, location);
|
|
192
|
+
if (refused) throw cannotLoad(asset.url, refused);
|
|
193
|
+
current = new URL(location, current).href;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (status < 200 || status > 299) throw new AssetInputError(`Asset could not be loaded: ${asset.url} (status ${status}).`);
|
|
197
|
+
const length = Number(response.headers()["content-length"]);
|
|
198
|
+
if (Number.isFinite(length) && length > MAX_ASSET_BYTES) throw tooLarge(asset.url);
|
|
199
|
+
const bytes = await response.body();
|
|
200
|
+
if (bytes.length > MAX_ASSET_BYTES) throw tooLarge(asset.url);
|
|
201
|
+
return { bytes, type: response.headers()["content-type"] ?? "" };
|
|
202
|
+
}
|
|
203
|
+
} finally {
|
|
204
|
+
await context.close().catch(() => {});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// "At rest": every measurer that reads looks reads them with nothing moving, so a look is the same on
|
|
2
|
+
// every run whatever frame an animation is on. A style element turns transitions off; running animations
|
|
3
|
+
// (CSS and Web Animations API alike) are walked: a finite one (an entrance fade) is finished, so it stays
|
|
4
|
+
// at its final, visible frame; an endless one (a pulse) is cancelled, so the element shows its resting
|
|
5
|
+
// style. The endless ones cancelled at rest are kept in a page-side list and played again when the read
|
|
6
|
+
// is over. Motion driven by requestAnimationFrame (GSAP and similar) is not an animation the page lists
|
|
7
|
+
// and cannot be stopped. All are page expressions, for page.evaluate or DevTools Runtime.evaluate.
|
|
8
|
+
export const REST_ID = "lazuli-at-rest";
|
|
9
|
+
export const REST_CSS = "*, *::before, *::after { transition: none !important; }";
|
|
10
|
+
const LIST = "__lazuliAtRest";
|
|
11
|
+
|
|
12
|
+
// keep: add the endless animations cancelled now to the list played again by restOff.
|
|
13
|
+
const walk = (keep) => `
|
|
14
|
+
for (const a of document.getAnimations()) {
|
|
15
|
+
try {
|
|
16
|
+
if (a.playState !== "running") continue;
|
|
17
|
+
if (Number.isFinite(a.effect?.getComputedTiming?.().endTime)) a.finish();
|
|
18
|
+
else {
|
|
19
|
+
a.cancel();
|
|
20
|
+
${keep ? `window[${JSON.stringify(LIST)}].push(a);` : ""}
|
|
21
|
+
}
|
|
22
|
+
} catch { /* an animation that cannot be moved is left as it is */ }
|
|
23
|
+
}`;
|
|
24
|
+
|
|
25
|
+
export const restOn = () =>
|
|
26
|
+
`(() => {
|
|
27
|
+
const s = document.createElement("style");
|
|
28
|
+
s.id = ${JSON.stringify(REST_ID)};
|
|
29
|
+
s.textContent = ${JSON.stringify(REST_CSS)};
|
|
30
|
+
(document.head || document.documentElement).appendChild(s);
|
|
31
|
+
window[${JSON.stringify(LIST)}] = [];
|
|
32
|
+
${walk(true)}
|
|
33
|
+
})()`;
|
|
34
|
+
|
|
35
|
+
// After a state is forced (hover or active can start animations): the same walk, but what it cancels
|
|
36
|
+
// belongs to the forced state and goes when the state is cleared, so it is not played again.
|
|
37
|
+
export const restStill = () => `(() => { ${walk(false)} })()`;
|
|
38
|
+
|
|
39
|
+
// Styles are recomputed before the style goes (a forced state cleared just before does not transition
|
|
40
|
+
// back); then the endless animations cancelled at rest play again. No wait: finished ones stay finished.
|
|
41
|
+
export const restOff = () =>
|
|
42
|
+
`(() => {
|
|
43
|
+
document.documentElement.getBoundingClientRect();
|
|
44
|
+
document.getElementById(${JSON.stringify(REST_ID)})?.remove();
|
|
45
|
+
for (const a of window[${JSON.stringify(LIST)}] ?? []) {
|
|
46
|
+
try { a.play(); } catch { /* left cancelled */ }
|
|
47
|
+
}
|
|
48
|
+
delete window[${JSON.stringify(LIST)}];
|
|
49
|
+
})()`;
|