@whzxc/agent-design-gallery 0.15.0 → 0.16.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 +5 -2
- package/package.json +2 -1
- package/public/app.js +602 -64
- package/public/assets.js +25 -10
- package/public/boards.js +8 -3
- package/public/frame-identity.js +25 -12
- package/public/icons.js +7 -0
- package/public/index.html +14 -2
- package/public/motion-flow.js +28 -0
- package/public/motion-identity.js +100 -0
- package/public/motion-paint.js +332 -0
- package/public/motion-scene.js +165 -0
- package/public/motion-specimens.js +335 -0
- package/public/player.css +270 -0
- package/public/player.js +753 -0
- package/public/recent-updates.js +535 -0
- package/public/style.css +17 -2
- package/public/tokens.css +6 -0
- package/public/view-boards.css +131 -0
- package/public/view-boards.js +350 -0
- package/public/view-route.js +1 -1
- package/src/boards/motion-board.mjs +202 -0
- package/src/boards/token-board.mjs +9 -5
- package/src/cli-output.mjs +3 -1
- package/src/cli.mjs +52 -18
- package/src/http.mjs +5 -2
- package/src/recent-frames.mjs +295 -0
- package/src/recent-history.mjs +100 -0
- package/src/recent-job.mjs +63 -0
- package/src/recent-worker.mjs +171 -0
- package/src/server.mjs +229 -18
- package/src/toolkit/arch-git.mjs +8 -14
- package/src/toolkit/browser.mjs +1 -13
- package/src/toolkit/build-store.mjs +9 -1
- package/src/toolkit/build-worker.mjs +6 -0
- package/src/toolkit/build.mjs +22 -16
- package/src/toolkit/commands.mjs +93 -2
- package/src/toolkit/compare.mjs +13 -11
- package/src/toolkit/config.mjs +2 -2
- package/src/toolkit/diff-inputs.mjs +35 -26
- package/src/toolkit/diff.mjs +24 -11
- package/src/toolkit/frame-declarations.mjs +36 -0
- package/src/toolkit/git-blobs.mjs +50 -0
- package/src/toolkit/impact-inputs.mjs +11 -19
- package/src/toolkit/impact-selection.mjs +2 -2
- package/src/toolkit/impact.mjs +17 -3
- package/src/toolkit/implementation-map.mjs +17 -6
- package/src/toolkit/input-generation.mjs +15 -0
- package/src/toolkit/inspect-output.mjs +1 -1
- package/src/toolkit/inspect.d.ts +9 -1
- package/src/toolkit/inspect.mjs +25 -1
- package/src/toolkit/motion-check.mjs +382 -0
- package/src/toolkit/motion-css.mjs +105 -0
- package/src/toolkit/motion-flows.mjs +95 -0
- package/src/toolkit/motion-impact.mjs +87 -0
- package/src/toolkit/motion-inventory.mjs +202 -0
- package/src/toolkit/motion-label.mjs +111 -0
- package/src/toolkit/motion-patterns.mjs +216 -0
- package/src/toolkit/motion-primitives.mjs +94 -0
- package/src/toolkit/motion-queries.mjs +169 -0
- package/src/toolkit/motion.mjs +300 -0
- package/src/toolkit/owner.mjs +19 -6
- package/src/toolkit/play-endpoint.mjs +66 -0
- package/src/toolkit/project-index.mjs +2 -2
- package/src/toolkit/recent-structure.mjs +201 -0
- package/src/toolkit/results.mjs +2 -2
- package/src/toolkit/review-document.mjs +9 -4
- package/src/toolkit/review-embed-document.mjs +671 -0
- package/src/toolkit/review-filmstrip.mjs +76 -0
- package/src/toolkit/review-motion-card.mjs +51 -0
- package/src/toolkit/review-motion.mjs +159 -0
- package/src/toolkit/review-playback.js +123 -0
- package/src/toolkit/review-sheet.mjs +36 -28
- package/src/toolkit/review.mjs +131 -31
- package/src/toolkit/runtime.mjs +7 -1
- package/src/viewer-cache.mjs +48 -12
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Authoring provenance survives reduced-motion capture, hidden elements and inactive variants.
|
|
2
|
+
import { parse, generate, walk, ident, lexer } from 'css-tree';
|
|
3
|
+
import { parseFragment } from 'parse5';
|
|
4
|
+
import { resolveMotionToken } from './motion.mjs';
|
|
5
|
+
|
|
6
|
+
const sheetCache = new WeakMap();
|
|
7
|
+
const governed =
|
|
8
|
+
/^(?:-webkit-)?(?:transition|animation)(?:$|-(?:duration|delay|timing-function|property|name)$)|^--tw-(?:duration|ease)$/;
|
|
9
|
+
function declarations(ast, emit, classes = []) {
|
|
10
|
+
if (!classes.length && ast.type === 'Rule' && ast.prelude.type === 'SelectorList') {
|
|
11
|
+
// An outer selector owns its declarations, including any nested group/peer conditions.
|
|
12
|
+
for (const selector of ast.prelude.children) {
|
|
13
|
+
let first;
|
|
14
|
+
walk(selector, {
|
|
15
|
+
visit: 'ClassSelector',
|
|
16
|
+
enter(node) {
|
|
17
|
+
first ??= ident.decode(node.name);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
if (first) classes = [...classes, first];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (ast.type === 'Declaration') {
|
|
24
|
+
const prop = ast.property.startsWith('--') ? ast.property : ast.property.toLowerCase();
|
|
25
|
+
if (governed.test(prop)) emit(classes, prop, generate(ast.value));
|
|
26
|
+
}
|
|
27
|
+
for (const child of ast.children ?? []) declarations(child, emit, classes);
|
|
28
|
+
if (ast.block) declarations(ast.block, emit, classes);
|
|
29
|
+
}
|
|
30
|
+
function utilities(built) {
|
|
31
|
+
if (sheetCache.has(built)) return sheetCache.get(built);
|
|
32
|
+
const rules = new Map();
|
|
33
|
+
const css = built.cssBeforeTokens ?? built.css ?? '';
|
|
34
|
+
if (/(?:transition|animation|--tw-(?:duration|ease))\s*[-:]/i.test(css)) {
|
|
35
|
+
declarations(parse(css), (classes, prop, value) => {
|
|
36
|
+
for (const cls of classes) {
|
|
37
|
+
if (!rules.has(cls)) rules.set(cls, []);
|
|
38
|
+
rules.get(cls).push([prop, value]);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
sheetCache.set(built, rules);
|
|
43
|
+
return rules;
|
|
44
|
+
}
|
|
45
|
+
function split(value, separator) {
|
|
46
|
+
let depth = 0,
|
|
47
|
+
start = 0;
|
|
48
|
+
const parts = [];
|
|
49
|
+
for (let i = 0; i < value.length; i++) {
|
|
50
|
+
if (value[i] === '(') depth++;
|
|
51
|
+
if (value[i] === ')') depth--;
|
|
52
|
+
if (!depth && separator.test(value[i])) {
|
|
53
|
+
if (i > start) parts.push(value.slice(start, i));
|
|
54
|
+
start = i + 1;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (start < value.length) parts.push(value.slice(start));
|
|
58
|
+
return parts.map((p) => p.trim()).filter(Boolean);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function sourceMotion(built, frame, tokens, theme) {
|
|
62
|
+
// Most frames have no authored motion. Do not parse the large generated preflight for them.
|
|
63
|
+
if (!/(?:animat|transition|duration|delay|ease|timing-function)/i.test(frame.html)) return [];
|
|
64
|
+
const rules = utilities(built),
|
|
65
|
+
messages = new Set();
|
|
66
|
+
function check(prop, value, origin, utility = false) {
|
|
67
|
+
const finding = (reason) => messages.add(origin + ' (' + prop + ': ' + value + '): ' + reason);
|
|
68
|
+
const name = prop.replace(/^-webkit-/, '');
|
|
69
|
+
const duration = name.endsWith('duration') || name.endsWith('delay'),
|
|
70
|
+
easing = name.endsWith('timing-function') || name === '--tw-ease';
|
|
71
|
+
const active =
|
|
72
|
+
name === 'animation' || name === 'animation-name' || name === 'transition-property' || name === 'transition';
|
|
73
|
+
if (active && !/^(none|initial|unset)$/i.test(value)) finding('Declare the motion on a frame edge.');
|
|
74
|
+
if (name === 'animation' && /^var\(--animate-[\w-]+\)$/.test(value)) return;
|
|
75
|
+
if (!duration && !easing && !['animation', 'transition'].includes(name)) return;
|
|
76
|
+
for (const item of split(value, /,/)) {
|
|
77
|
+
const list = duration || easing ? [item] : split(item, /\s/);
|
|
78
|
+
for (const word of list) {
|
|
79
|
+
if (utility && /^var\(--tw-(duration|ease),/.test(word)) continue; // Tailwind's shared default, separate authored utility checked below.
|
|
80
|
+
let kind = duration ? 'duration' : easing ? 'easing' : null;
|
|
81
|
+
const ref = /^var\(\s*(--[\w-]+)\s*\)$/.exec(word);
|
|
82
|
+
if (ref && !kind) {
|
|
83
|
+
if (['duration', 'easing'].some((k) => resolveMotionToken(tokens, theme, ref[1], k).value !== null)) continue;
|
|
84
|
+
finding('Shorthand variable must resolve to a project timing token.');
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (!kind && (/^[+-]?(?:\d|\.\d).*m?s$/i.test(word) || lexer.matchType('easing-function', word).matched))
|
|
88
|
+
kind = 'timing';
|
|
89
|
+
if (kind && !(ref && resolveMotionToken(tokens, theme, ref[1], kind).value !== null))
|
|
90
|
+
finding('Use a project motion token for ' + kind + '.');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function visit(node) {
|
|
95
|
+
for (const { name, value } of node.attrs ?? []) {
|
|
96
|
+
if (name === 'class')
|
|
97
|
+
for (const cls of value.split(/\s+/)) for (const [prop, v] of rules.get(cls) ?? []) check(prop, v, cls, true);
|
|
98
|
+
if (name === 'style' && /(?:transition|animat|--tw-(?:duration|ease))/i.test(value))
|
|
99
|
+
declarations(parse(value, { context: 'declarationList' }), (_, prop, v) => check(prop, v, 'style'));
|
|
100
|
+
}
|
|
101
|
+
for (const child of node.childNodes ?? []) visit(child);
|
|
102
|
+
}
|
|
103
|
+
visit(parseFragment(frame.html));
|
|
104
|
+
return [...messages];
|
|
105
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export const frameKey = ({ source, frame }) => JSON.stringify([source, frame]);
|
|
2
|
+
|
|
3
|
+
// Connectivity is undirected. Direction is retained on the edges for playback.
|
|
4
|
+
export function motionFlows(records, declarations) {
|
|
5
|
+
const incident = new Map(), byFrame = new Map(), members = new Map(), findings = [];
|
|
6
|
+
const outgoing = new Set();
|
|
7
|
+
for (const { edge } of records) {
|
|
8
|
+
outgoing.add(frameKey(edge.from));
|
|
9
|
+
for (const ref of [edge.from, edge.to]) {
|
|
10
|
+
const key = frameKey(ref);
|
|
11
|
+
if (!incident.has(key)) incident.set(key, []);
|
|
12
|
+
incident.get(key).push(edge);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
for (const [key] of incident) {
|
|
16
|
+
if (byFrame.has(key)) continue;
|
|
17
|
+
const queue = [key], frames = new Map(), edges = new Set();
|
|
18
|
+
const flow = { frames: [], triggers: [] };
|
|
19
|
+
byFrame.set(key, flow);
|
|
20
|
+
for (const current of queue) {
|
|
21
|
+
for (const edge of incident.get(current)) {
|
|
22
|
+
edges.add(edge);
|
|
23
|
+
for (const ref of [edge.from, edge.to]) {
|
|
24
|
+
const id = frameKey(ref);
|
|
25
|
+
frames.set(id, ref);
|
|
26
|
+
if (byFrame.has(id)) continue;
|
|
27
|
+
byFrame.set(id, flow);
|
|
28
|
+
queue.push(id);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
flow.frames = [...frames.values()];
|
|
33
|
+
const destinations = new Set([...edges].map((edge) => frameKey(edge.to)));
|
|
34
|
+
flow.start = flow.frames.find((ref) => !destinations.has(frameKey(ref))) ?? incident.get(key)[0].from;
|
|
35
|
+
members.set(flow, [...edges]);
|
|
36
|
+
}
|
|
37
|
+
const declared = new Set(), ends = new Set();
|
|
38
|
+
for (const { declaration, source, pointer, locations } of declarations) {
|
|
39
|
+
const flow = byFrame.get(frameKey(declaration.start));
|
|
40
|
+
const message = !flow
|
|
41
|
+
? 'Flow start must be an endpoint of a transition.'
|
|
42
|
+
: declared.has(flow) ? 'Declare one flow per connected component.' : null;
|
|
43
|
+
if (message) {
|
|
44
|
+
findings.push({
|
|
45
|
+
code: 'MOTION_FLOW_SCHEMA', source, pointer, frame: declaration.start.frame,
|
|
46
|
+
theme: null, message, ...(locations[pointer] ?? locations[''])
|
|
47
|
+
});
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
Object.assign(flow, declaration);
|
|
51
|
+
declared.add(flow);
|
|
52
|
+
for (const [index, ref] of (declaration.ends ?? []).entries()) {
|
|
53
|
+
const key = frameKey(ref), at = pointer + '/ends/' + index;
|
|
54
|
+
const problem = byFrame.get(key) !== flow ? 'An end state must belong to the declared flow.'
|
|
55
|
+
: outgoing.has(key) ? 'An end state cannot have outgoing transitions.'
|
|
56
|
+
: ends.has(key) ? 'Declare each end state once.' : null;
|
|
57
|
+
if (problem) {
|
|
58
|
+
findings.push({ code: 'MOTION_FLOW_SCHEMA', source, pointer: at, frame: ref.frame,
|
|
59
|
+
theme: null, message: problem, ...(locations[at] ?? locations[pointer]) });
|
|
60
|
+
} else ends.add(key);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const [flow, edges] of members) {
|
|
64
|
+
if (flow.anchor) continue;
|
|
65
|
+
const start = frameKey(flow.start);
|
|
66
|
+
const ordered = [...edges.filter((edge) => frameKey(edge.from) === start),
|
|
67
|
+
...edges.filter((edge) => frameKey(edge.from) !== start)];
|
|
68
|
+
const triggers = new Map();
|
|
69
|
+
for (const { trigger: { target } } of ordered) {
|
|
70
|
+
if (!target.frame) triggers.set(JSON.stringify(target), target);
|
|
71
|
+
}
|
|
72
|
+
flow.triggers = [...triggers.values()];
|
|
73
|
+
}
|
|
74
|
+
const deadEnds = new Set([...byFrame.keys()].filter(key => !outgoing.has(key) && !ends.has(key)));
|
|
75
|
+
return { byFrame, findings, deadEnds };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Use the first selected incoming edge to locate each dead end in its sidecar.
|
|
79
|
+
export function deadEndFindings(graph, records, strictMotion) {
|
|
80
|
+
const reported = new Set(), findings = [];
|
|
81
|
+
for (const { valid, edge, source, pointer, locations } of records) {
|
|
82
|
+
if (!valid) continue;
|
|
83
|
+
const ref = edge.to, key = frameKey(ref);
|
|
84
|
+
if (!graph.deadEnds.has(key) || reported.has(key)) continue;
|
|
85
|
+
reported.add(key);
|
|
86
|
+
const at = pointer + '/to';
|
|
87
|
+
findings.push({
|
|
88
|
+
code: 'MOTION_FLOW_DEAD_END', source, pointer: at, frame: ref.frame, theme: null,
|
|
89
|
+
warning: !strictMotion,
|
|
90
|
+
message: ref.source + '#' + ref.frame + ' has no outgoing transition. Add an action or declare it in the flow’s ends.',
|
|
91
|
+
...(locations[at] ?? locations[pointer])
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return findings;
|
|
95
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { labelResolver } from './motion-label.mjs';
|
|
2
|
+
import { canonicalBytes } from './impact-proof.mjs';
|
|
3
|
+
import { frameKey } from './motion-flows.mjs';
|
|
4
|
+
import { motionRows } from './motion.mjs';
|
|
5
|
+
import { targetsFor, usesTiming } from './motion-queries.mjs';
|
|
6
|
+
import { parseTokens } from './tokens.mjs';
|
|
7
|
+
|
|
8
|
+
export async function impactedEdges(views, query, themes, assessments, explain, expanded) {
|
|
9
|
+
const git = query.kind === 'git',
|
|
10
|
+
ids = new Set(views.flatMap((v) => [...v.motion.ids.keys()]));
|
|
11
|
+
const output = [];
|
|
12
|
+
const labels = views.map(v => labelResolver(v.config,source=>v.built.get(source) ?? v.index.materialize(source)));
|
|
13
|
+
const tokenMaps = new Map();
|
|
14
|
+
const tokens = (v) => {
|
|
15
|
+
if (!tokenMaps.has(v)) tokenMaps.set(v, parseTokens(v.tokenBytes.toString()));
|
|
16
|
+
return tokenMaps.get(v);
|
|
17
|
+
};
|
|
18
|
+
for (const id of [...ids].sort()) {
|
|
19
|
+
const reasons = new Set(),
|
|
20
|
+
records = views.map((v) => motionRows(v.motion.ids.get(id) ?? [])[0]);
|
|
21
|
+
let kind = 'candidate';
|
|
22
|
+
if (git) {
|
|
23
|
+
kind = !records[0] ? 'added' : !records[1] ? 'removed' : 'changed';
|
|
24
|
+
const declarations = records.map((r, i) => r && canonicalBytes({
|
|
25
|
+
owner: r.owner, edge: r.edge,
|
|
26
|
+
flow: r.valid ? views[i].motion.flows.get(frameKey(r.edge.from)) : null,
|
|
27
|
+
deadEnd: r.valid && views[i].motion.deadEnds.has(frameKey(r.edge.to))
|
|
28
|
+
}));
|
|
29
|
+
if (declarations[0] !== declarations[1]) reasons.add('declaration');
|
|
30
|
+
const selectors = views.map((v) =>
|
|
31
|
+
canonicalBytes(
|
|
32
|
+
v.map.mappings
|
|
33
|
+
.filter((m) => m.design.some((d) => d.transition === id))
|
|
34
|
+
.map((m) => canonicalBytes(m.implementation))
|
|
35
|
+
.sort()
|
|
36
|
+
)
|
|
37
|
+
);
|
|
38
|
+
if (
|
|
39
|
+
views[0].config.check?.lifecycle !== views[1].config.check?.lifecycle ||
|
|
40
|
+
views[0].map.present !== views[1].map.present
|
|
41
|
+
)
|
|
42
|
+
reasons.add('binding');
|
|
43
|
+
if (selectors[0] !== selectors[1]) reasons.add('binding');
|
|
44
|
+
}
|
|
45
|
+
views.forEach((view, i) => {
|
|
46
|
+
const r = records[i];
|
|
47
|
+
if (!r) return;
|
|
48
|
+
if (view.motionBindingHits?.has(id)) reasons.add('binding');
|
|
49
|
+
if (query.kind === 'source' && !query.part && query.sources.includes(r.source)) reasons.add('declaration');
|
|
50
|
+
if (!r.valid) return;
|
|
51
|
+
const edge = r.edge;
|
|
52
|
+
if (
|
|
53
|
+
(query.kind === 'token' || git) &&
|
|
54
|
+
view.motionTokenHits?.some((hit) => usesTiming(edge, tokens(view), [hit.token], hit.themes))
|
|
55
|
+
)
|
|
56
|
+
reasons.add('timing_token');
|
|
57
|
+
for (const side of ['from', 'to']) {
|
|
58
|
+
const ref = edge[side],
|
|
59
|
+
facts = view.facts.get(ref.source),
|
|
60
|
+
frame = facts?.frames.find((f) => f.name === ref.frame);
|
|
61
|
+
if (git) {
|
|
62
|
+
const prior = views[1 - i].facts.get(ref.source)?.frames.find((f) => f.name === ref.frame);
|
|
63
|
+
if (frame?.proposed !== prior?.proposed) reasons.add('endpoint');
|
|
64
|
+
}
|
|
65
|
+
if (query.kind === 'source' && query.part) {
|
|
66
|
+
if (
|
|
67
|
+
frame?.parts.some((p) => p.source === query.sources[0] && p.part === query.part) ||
|
|
68
|
+
targetsFor(edge, side).some((t) => t.part === query.sources[0] + '#' + query.part)
|
|
69
|
+
)
|
|
70
|
+
reasons.add('part');
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (query.kind === 'source' && query.sources.includes(ref.source)) reasons.add('endpoint');
|
|
74
|
+
if (
|
|
75
|
+
themes.some((theme) => {
|
|
76
|
+
const a = assessments.get(JSON.stringify([ref.source, ref.frame, theme]));
|
|
77
|
+
return a?.candidate && (!git || a.disposition !== 'proven_unaffected');
|
|
78
|
+
})
|
|
79
|
+
)
|
|
80
|
+
reasons.add('endpoint');
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
const side = records.findLastIndex(Boolean);
|
|
84
|
+
if (reasons.size) output.push({ id, kind, ...(expanded ? {label:await labels[side](records[side])} : {}), ...(explain ? { reasons: [...reasons].sort() } : {}) });
|
|
85
|
+
}
|
|
86
|
+
return output;
|
|
87
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { existsSync, lstatSync, readdirSync, readFileSync, realpathSync } from 'node:fs';
|
|
4
|
+
import { parse as parseJs } from '@babel/parser';
|
|
5
|
+
import { generate, parse as parseCss, walk } from 'css-tree';
|
|
6
|
+
import { CliError } from '../cli-output.mjs';
|
|
7
|
+
import { loadMotionPatterns, motionPatternFile } from './motion-patterns.mjs';
|
|
8
|
+
import { readMotion } from './motion.mjs';
|
|
9
|
+
|
|
10
|
+
const extension = /\.(?:[cm]?[jt]sx?|css)$/;
|
|
11
|
+
const excluded = new Set(['node_modules','vendor','dist','build','out','coverage','output','test','tests','__tests__']);
|
|
12
|
+
const codeFile = name => extension.test(name) && !/\.(?:d\.ts|(?:spec|test)\.[cm]?[jt]sx?)$/.test(name) &&
|
|
13
|
+
!name.split('/').some(part => part.startsWith('.') || excluded.has(part));
|
|
14
|
+
const sourcePath = (config, name) => path.relative(config.root, name).split(path.sep).join('/');
|
|
15
|
+
const keyName = node => node?.name ?? node?.value;
|
|
16
|
+
const none = /^(?:none|initial|unset|inherit|revert(?:-layer)?|0(?:m?s)?)(?:\s*!important)?$/i;
|
|
17
|
+
const cssMotion = /^(?:-webkit-)?(?:animation|transition)(?:-[a-z-]+)?$/i;
|
|
18
|
+
const jsxMotion = new Set(['layout','layoutId','animate','whileHover','whileTap','whileDrag','whileFocus','whileInView','exit','variants']);
|
|
19
|
+
const importsFromMotion = /^(?:motion(?:\/(?:react(?:-client)?|mini))?|framer-motion)$/;
|
|
20
|
+
const motionExports = new Set(['AnimatePresence','animate','useAnimate','useSpring','spring']);
|
|
21
|
+
|
|
22
|
+
function codeFiles(config) {
|
|
23
|
+
try {
|
|
24
|
+
const output = execFileSync('git', ['ls-files','-z','--cached','--others','--exclude-standard','--','.'],
|
|
25
|
+
{ cwd: config.root, encoding: 'utf8', maxBuffer: 32 * 1024 * 1024, stdio: ['ignore','pipe','ignore'] });
|
|
26
|
+
return [...new Set(output.split('\0').filter(codeFile))].sort();
|
|
27
|
+
} catch {
|
|
28
|
+
const visit = directory => readdirSync(directory, { withFileTypes: true }).flatMap(entry => {
|
|
29
|
+
if (entry.name.startsWith('.') || excluded.has(entry.name)) return [];
|
|
30
|
+
const full = path.join(directory, entry.name);
|
|
31
|
+
return entry.isDirectory() ? visit(full) : entry.isFile() ? [sourcePath(config, full)] : [];
|
|
32
|
+
});
|
|
33
|
+
return visit(config.root).filter(codeFile).sort();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function scanCss(source, record, firstLine = 1, context, location) {
|
|
38
|
+
const ast = parseCss(source, { context: context ?? 'stylesheet', positions: true });
|
|
39
|
+
const lineOf = node => firstLine + node.loc.start.line - 1;
|
|
40
|
+
const seen = new Set();
|
|
41
|
+
walk(ast, {
|
|
42
|
+
enter(node) {
|
|
43
|
+
if (node.type === 'Atrule' && /^(?:-webkit-)?keyframes$/i.test(node.name) && node.prelude)
|
|
44
|
+
record('keyframes:' + generate(node.prelude), lineOf(node), location);
|
|
45
|
+
if (node.type !== 'Declaration' || !cssMotion.test(node.property) || none.test(generate(node.value))) return;
|
|
46
|
+
if (this.atrule?.name === 'media' && /prefers-reduced-motion:reduce/.test(generate(this.atrule.prelude)) &&
|
|
47
|
+
/^(?:0(?:\.0+)?m?s|0?\.01ms)$/.test(generate(node.value))) return;
|
|
48
|
+
const selector = this.rule?.prelude ? generate(this.rule.prelude) : null;
|
|
49
|
+
const name = node.property.replace(/^-webkit-/i, '').toLowerCase().split('-')[0];
|
|
50
|
+
const match = selector ? 'css:' + selector : 'style:' + name;
|
|
51
|
+
const line = this.rule ? lineOf(this.rule) : lineOf(node);
|
|
52
|
+
const id = JSON.stringify([match, this.rule?.loc.start.offset ?? node.loc.start.offset]);
|
|
53
|
+
if (!seen.has(id)) { seen.add(id); record(match, line, location); }
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function scanJs(source, file, record) {
|
|
59
|
+
const extension = path.extname(file);
|
|
60
|
+
const ts = /[cm]?tsx?$/.test(extension);
|
|
61
|
+
const plugins = [...(ts ? ['typescript'] : []), ...(!ts || extension === '.tsx' ? ['jsx'] : [])], bindings = new Map();
|
|
62
|
+
const { program } = parseJs(source, { sourceType: 'unambiguous', plugins, errorRecovery: true });
|
|
63
|
+
for (const node of program.body) if (node.type === 'ImportDeclaration' && importsFromMotion.test(node.source.value)) {
|
|
64
|
+
for (const specifier of node.specifiers) bindings.set(specifier.local.name,
|
|
65
|
+
specifier.type === 'ImportNamespaceSpecifier' ? '*' : keyName(specifier.imported) ?? 'motion');
|
|
66
|
+
}
|
|
67
|
+
const binding = node => {
|
|
68
|
+
if (node?.type === 'Identifier' || node?.type === 'JSXIdentifier') return bindings.get(node.name);
|
|
69
|
+
if (['MemberExpression','JSXMemberExpression'].includes(node?.type) &&
|
|
70
|
+
bindings.get(keyName(node.object)) === '*') return keyName(node.property);
|
|
71
|
+
};
|
|
72
|
+
const motionTag = node => {
|
|
73
|
+
if (['motion','m','Reorder'].includes(binding(node))) return true;
|
|
74
|
+
return node?.type === 'JSXMemberExpression' && motionTag(node.object);
|
|
75
|
+
};
|
|
76
|
+
function visit(node, scopes = [], inMotion = false, inStyle = false) {
|
|
77
|
+
if (typeof node?.type !== 'string') return;
|
|
78
|
+
const scope = /^(?:FunctionDeclaration|ClassDeclaration|VariableDeclarator|ObjectMethod|ClassMethod)$/.test(node.type)
|
|
79
|
+
? keyName(node.id ?? node.key) : null;
|
|
80
|
+
if (typeof scope === 'string') scopes = [...scopes, scope];
|
|
81
|
+
const { start: { line } } = node.loc;
|
|
82
|
+
const add = match => record(match, line, scopes);
|
|
83
|
+
if (node.type === 'JSXOpeningElement') {
|
|
84
|
+
if (binding(node.name) === 'AnimatePresence') add('motion:AnimatePresence');
|
|
85
|
+
for (const attr of node.attributes) {
|
|
86
|
+
const name = keyName(attr.name);
|
|
87
|
+
if (motionTag(node.name) && jsxMotion.has(name) && attr.value?.expression?.value !== false)
|
|
88
|
+
record('motion:' + name, attr.loc.start.line, scopes);
|
|
89
|
+
if (typeof name === 'string' && name.startsWith('data-')) record('attribute:' + name, attr.loc.start.line, scopes, false);
|
|
90
|
+
if (name === 'style' && typeof attr.value?.value === 'string') scanCss(attr.value.value, record, attr.loc.start.line, 'declarationList', scopes);
|
|
91
|
+
}
|
|
92
|
+
inMotion ||= motionTag(node.name);
|
|
93
|
+
}
|
|
94
|
+
if (node.type === 'CallExpression') {
|
|
95
|
+
const name = binding(node.callee);
|
|
96
|
+
if (motionExports.has(name) && name !== 'AnimatePresence') add('motion:' + name);
|
|
97
|
+
if (node.callee.type === 'MemberExpression') {
|
|
98
|
+
const method = keyName(node.callee.property);
|
|
99
|
+
if (method === 'animate') record('web:animate', line, scopes, false);
|
|
100
|
+
if (method === 'spring') record('call:spring', line, scopes, false);
|
|
101
|
+
}
|
|
102
|
+
inMotion ||= motionExports.has(name);
|
|
103
|
+
}
|
|
104
|
+
if (node.type === 'ObjectProperty') {
|
|
105
|
+
const name = keyName(node.key), value = node.value;
|
|
106
|
+
if (name === 'type' && value?.value === 'spring') record('motion:spring', line, scopes, inMotion || (bindings.size > 0 && scopes.some(name => /spring|transition/i.test(name))));
|
|
107
|
+
if (typeof name === 'string' && cssMotion.test(name.replace(/[A-Z]/g, letter => '-' + letter.toLowerCase())) &&
|
|
108
|
+
(typeof value?.value === 'string' ? !none.test(value.value) : inStyle || value?.type === 'TemplateLiteral'))
|
|
109
|
+
add('style:' + name);
|
|
110
|
+
}
|
|
111
|
+
if (node.type === 'TemplateLiteral') {
|
|
112
|
+
const css = node.quasis.map(part => part.value.raw).join('var(--dynamic)');
|
|
113
|
+
if (/@(?:-webkit-)?keyframes\s|\b(?:animation|transition)\s*[-:]/i.test(css) && css.includes('{'))
|
|
114
|
+
scanCss(css, record, line, undefined, scopes);
|
|
115
|
+
}
|
|
116
|
+
inStyle ||= node.type === 'JSXAttribute' && keyName(node.name) === 'style';
|
|
117
|
+
for (const [key, child] of Object.entries(node)) {
|
|
118
|
+
if (['loc','comments','tokens','errors','leadingComments','trailingComments','innerComments'].includes(key)) continue;
|
|
119
|
+
if (Array.isArray(child)) child.forEach(item => visit(item, scopes, inMotion, inStyle));
|
|
120
|
+
else visit(child, scopes, inMotion, inStyle);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
visit(program);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function scanMotion(config, catalog = loadMotionPatterns(config), graph = readMotion(config), { mappedOnly = false } = {}) {
|
|
127
|
+
const declaration = sourcePath(config, path.join(config.design, motionPatternFile));
|
|
128
|
+
const consumers = new Map([...catalog.patterns.keys()].map(id => [id, []]));
|
|
129
|
+
const issues = [];
|
|
130
|
+
const issue = (code, source, message, line = null) => issues.push({ code, source, message, line, column: null });
|
|
131
|
+
const mappings = new Map();
|
|
132
|
+
for (const [id, pattern] of catalog.patterns) {
|
|
133
|
+
for (const frame of pattern.frames ?? []) {
|
|
134
|
+
const file = path.join(config.root, frame.source);
|
|
135
|
+
if (file.startsWith(config.design + path.sep) && existsSync(file)) consumers.get(id).push({ type: 'frame', ...frame });
|
|
136
|
+
else issue('MOTION_CONSUMER_MISSING', declaration, id + ' frame source does not exist in the design directory: ' + frame.source + '.');
|
|
137
|
+
}
|
|
138
|
+
for (const ref of pattern.consumers ?? []) {
|
|
139
|
+
if (!mappings.has(ref.file)) mappings.set(ref.file, []);
|
|
140
|
+
mappings.get(ref.file).push({ ...ref, id, hits: 0 });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
for (const record of graph.records) {
|
|
144
|
+
if (!record.valid || !record.pattern) continue;
|
|
145
|
+
consumers.get(record.pattern.id)?.push({ type: 'edge', id: record.edge.id, ...record.edge.from,
|
|
146
|
+
sidecar: record.source, line: record.locations[record.pointer + '/pattern']?.line ?? record.locations[record.pointer]?.line ?? 1 });
|
|
147
|
+
}
|
|
148
|
+
// Check never follows symlinks or scans ignored/untracked dependencies.
|
|
149
|
+
const root = realpathSync(config.root);
|
|
150
|
+
let animations = 0, unmapped = 0, unused = 0;
|
|
151
|
+
for (const name of mappedOnly ? [...mappings.keys()].filter(codeFile).sort() : codeFiles(config)) {
|
|
152
|
+
if (name === sourcePath(config, path.join(config.design, 'tokens.css'))) continue;
|
|
153
|
+
const full = path.join(root, name);
|
|
154
|
+
try {
|
|
155
|
+
if (!lstatSync(full, { throwIfNoEntry: false })?.isFile() || realpathSync(full) !== full) continue;
|
|
156
|
+
const source = readFileSync(full, 'utf8');
|
|
157
|
+
if (!/animat|transition|keyframes|framer-motion|['"]motion(?:\/|['"])|spring|data-/i.test(source)) continue;
|
|
158
|
+
const refs = mappings.get(name) ?? [];
|
|
159
|
+
const sites = new Map();
|
|
160
|
+
const record = (match, line, within = [], required = true) => {
|
|
161
|
+
const key = JSON.stringify([match, line, within]);
|
|
162
|
+
if (!sites.has(key) || required) sites.set(key, { match, line, within, required });
|
|
163
|
+
};
|
|
164
|
+
if (name.endsWith('.css')) scanCss(source, record);
|
|
165
|
+
else scanJs(source, name, record);
|
|
166
|
+
for (const { match, line, within, required } of sites.values()) {
|
|
167
|
+
const hits = refs.filter(ref => ref.match === match && (!ref.within || within.includes(ref.within)));
|
|
168
|
+
if (required || hits.length) animations++;
|
|
169
|
+
if (!hits.length && required) {
|
|
170
|
+
unmapped++;
|
|
171
|
+
if (!mappedOnly) issue('MOTION_UNMAPPED', name, 'Map ' + match + (within.length ? ' within ' + within.at(-1) : '') + ' to a motion pattern.', line);
|
|
172
|
+
}
|
|
173
|
+
for (const ref of hits) ref.hits++;
|
|
174
|
+
for (const id of new Set(hits.map(ref => ref.id))) consumers.get(id).push({ type: 'code', source: name, line, match });
|
|
175
|
+
}
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (!mappedOnly) issue('MOTION_SCAN_FAILED', name, 'Could not scan source: ' + error.message.split('\n')[0]);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
for (const [file, refs] of mappings) for (const ref of refs) if (!ref.hits)
|
|
181
|
+
issue('MOTION_CONSUMER_MISSING', declaration, ref.id + ' consumer did not match ' + file + ' (' + ref.match + ').');
|
|
182
|
+
for (const id of catalog.declared) if (!consumers.get(id).length &&
|
|
183
|
+
!catalog.variants.get(id)?.some(variant => consumers.get(variant).length)) {
|
|
184
|
+
unused++;
|
|
185
|
+
issue('MOTION_UNUSED', declaration, 'Pattern ' + id + ' has no consumers.');
|
|
186
|
+
}
|
|
187
|
+
return { consumers, issues, animations, unmapped, unused };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function motionCodeExcerpt(config, catalog, source, line) {
|
|
191
|
+
if (!source || !Number.isSafeInteger(line) || line < 1) return null;
|
|
192
|
+
const isEdge = source.endsWith('.motion.json');
|
|
193
|
+
const graph = isEdge ? readMotion(config) : { records: [] };
|
|
194
|
+
const linked = [...scanMotion(config, catalog, graph, { mappedOnly: true }).consumers.values()].flat();
|
|
195
|
+
if (!linked.some(ref => ref.line === line && (isEdge ? ref.type === 'edge' && ref.sidecar === source :
|
|
196
|
+
ref.type === 'code' && ref.source === source))) return null;
|
|
197
|
+
const full = path.join(realpathSync(config.root), source);
|
|
198
|
+
if (realpathSync(full) !== full) return null;
|
|
199
|
+
const lines = readFileSync(full, 'utf8').split(/\r?\n/);
|
|
200
|
+
return lines.slice(Math.max(0, line - 4), line + 3).map((text, index) =>
|
|
201
|
+
[Math.max(1, line - 3) + index, text.slice(0, 500)]);
|
|
202
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { parseFragment } from 'parse5';
|
|
4
|
+
import { candidates } from '../../public/motion-identity.js';
|
|
5
|
+
import { relative } from './project.mjs';
|
|
6
|
+
import { buildSource } from './build.mjs';
|
|
7
|
+
|
|
8
|
+
function targetName(html, design, target) {
|
|
9
|
+
if (!html || target.frame) return '';
|
|
10
|
+
const rows = [],
|
|
11
|
+
ids = new Map();
|
|
12
|
+
function visit(node, parent) {
|
|
13
|
+
if (!node.tagName) return;
|
|
14
|
+
const attrs = Object.fromEntries(node.attrs.map((a) => [a.name, a.value]));
|
|
15
|
+
const i = rows.length;
|
|
16
|
+
const parts = JSON.parse(attrs['data-adg-owners'] ?? '[]').map(
|
|
17
|
+
(o) => [design, o.source].filter(Boolean).join('/') + '#' + o.export
|
|
18
|
+
);
|
|
19
|
+
rows.push({
|
|
20
|
+
i,
|
|
21
|
+
parent,
|
|
22
|
+
node,
|
|
23
|
+
attrs,
|
|
24
|
+
parts,
|
|
25
|
+
keys: JSON.parse(attrs['data-adg-keys'] ?? '[]'),
|
|
26
|
+
anchor: attrs['data-adg-id']
|
|
27
|
+
});
|
|
28
|
+
if (attrs.id) ids.set(attrs.id, node);
|
|
29
|
+
for (const child of node.childNodes) visit(child, i);
|
|
30
|
+
}
|
|
31
|
+
for (const node of parseFragment(html).childNodes) visit(node, -1);
|
|
32
|
+
const hits = candidates(rows, target);
|
|
33
|
+
if (hits.length !== 1) return '';
|
|
34
|
+
const [row] = hits;
|
|
35
|
+
function contents(node, referenced = false) {
|
|
36
|
+
if (!node) return '';
|
|
37
|
+
if (!node.tagName) return node.value ?? '';
|
|
38
|
+
const attrs = Object.fromEntries(node.attrs.map((attr) => [attr.name, attr.value]));
|
|
39
|
+
if (!referenced && (attrs.hidden !== undefined || attrs['aria-hidden'] === 'true')) return '';
|
|
40
|
+
if (attrs['aria-label']) return attrs['aria-label'];
|
|
41
|
+
if (node.tagName === 'img') return attrs.alt ?? '';
|
|
42
|
+
return node.childNodes.map((child) => contents(child, referenced)).join('');
|
|
43
|
+
}
|
|
44
|
+
const a = row.attrs;
|
|
45
|
+
const labelled = (a['aria-labelledby'] ?? '')
|
|
46
|
+
.split(/\s+/)
|
|
47
|
+
.map((id) => contents(ids.get(id), true))
|
|
48
|
+
.join(' ')
|
|
49
|
+
.trim();
|
|
50
|
+
const labels = a.id ? rows.filter((r) => r.node.tagName === 'label' && r.attrs.for === a.id) : [];
|
|
51
|
+
for (let p = rows[row.parent]; p; p = rows[p.parent]) {
|
|
52
|
+
if (p.node.tagName === 'label' && !p.attrs.for) { labels.push(p); break; }
|
|
53
|
+
}
|
|
54
|
+
return (
|
|
55
|
+
labelled ||
|
|
56
|
+
a['aria-label'] ||
|
|
57
|
+
labels.map((r) => contents(r.node)).join(' ') ||
|
|
58
|
+
a.alt ||
|
|
59
|
+
(row.node.tagName === 'input' && ['button', 'submit', 'reset'].includes(a.type) ? a.value : '') ||
|
|
60
|
+
contents(row.node) ||
|
|
61
|
+
a.title ||
|
|
62
|
+
a.placeholder ||
|
|
63
|
+
''
|
|
64
|
+
)
|
|
65
|
+
.trim()
|
|
66
|
+
.replace(/\s+/g, ' ');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function edgePresentation(edge, html, design) {
|
|
70
|
+
const t = edge.trigger;
|
|
71
|
+
const name = t.on === 'key' ? '' : targetName(html, design, t.target);
|
|
72
|
+
const target = name ? '“' + name + '”' : t.target.frame ? 'this frame' : 'the highlighted element';
|
|
73
|
+
const key =
|
|
74
|
+
{
|
|
75
|
+
Escape: 'Esc',
|
|
76
|
+
ArrowUp: 'Up Arrow',
|
|
77
|
+
ArrowDown: 'Down Arrow',
|
|
78
|
+
ArrowRight: 'Right Arrow',
|
|
79
|
+
ArrowLeft: 'Left Arrow',
|
|
80
|
+
' ': 'Space'
|
|
81
|
+
}[t.key] ?? t.key;
|
|
82
|
+
const gesture = {
|
|
83
|
+
click: 'Click ' + target,
|
|
84
|
+
hover: 'Hover over ' + target,
|
|
85
|
+
focus: 'Focus ' + target,
|
|
86
|
+
key: 'Press ' + key,
|
|
87
|
+
input: 'Enter “' + (t.value ?? '') + '” in ' + target
|
|
88
|
+
}[t.on];
|
|
89
|
+
return { label: edge.label ?? gesture, gesture };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function labelResolver(
|
|
93
|
+
config,
|
|
94
|
+
getBuild = (source) => buildSource(config, path.join(config.root, source), { write: false })
|
|
95
|
+
) {
|
|
96
|
+
const builds = new Map();
|
|
97
|
+
return async (record) => {
|
|
98
|
+
if (!record?.valid) return record?.edge?.label;
|
|
99
|
+
const e = record.edge;
|
|
100
|
+
if (e.label) return e.label;
|
|
101
|
+
if (e.trigger.on === 'key' || !existsSync(path.join(config.root, e.from.source)))
|
|
102
|
+
return edgePresentation(e, null, '').label;
|
|
103
|
+
if (!builds.has(e.from.source)) builds.set(e.from.source, getBuild(e.from.source));
|
|
104
|
+
const built = await builds.get(e.from.source);
|
|
105
|
+
return edgePresentation(
|
|
106
|
+
e,
|
|
107
|
+
built?.frames.find((f) => f.name === e.from.frame)?.html,
|
|
108
|
+
relative(config, config.design)
|
|
109
|
+
).label;
|
|
110
|
+
};
|
|
111
|
+
}
|