openfig-mcp 0.0.0-stage → 0.1.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/LICENSE +23 -0
- package/README.md +222 -2
- package/bin/openfig-mcp.mjs +71 -0
- package/package.json +55 -4
- package/src/api.mjs +294 -0
- package/src/convert.mjs +484 -0
- package/src/deck.mjs +31 -0
- package/src/doc.mjs +85 -0
- package/src/fonts.mjs +76 -0
- package/src/html.mjs +110 -0
- package/src/index.mjs +8 -0
- package/src/interactions.mjs +274 -0
- package/src/render.mjs +85 -0
- package/src/server.mjs +246 -0
- package/src/tokens.mjs +157 -0
- package/src/util.mjs +48 -0
- package/src/vendor/LICENSE-openfig-cli +21 -0
- package/src/vendor/svg-builder.mjs +1661 -0
package/src/api.mjs
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// High-level, LLM-oriented API over a .fig file.
|
|
2
|
+
import { mkdirSync, writeFileSync } from 'fs';
|
|
3
|
+
import { dirname, join, resolve } from 'path';
|
|
4
|
+
import { loadFig } from './doc.mjs';
|
|
5
|
+
import { Converter, variantStates, animationLoops } from './convert.mjs';
|
|
6
|
+
import { toHtml, countNodes } from './html.mjs';
|
|
7
|
+
import { TokenRegistry } from './tokens.mjs';
|
|
8
|
+
import { nodeToSvg, svgToPng } from './render.mjs';
|
|
9
|
+
import { fontReport, previewFonts, fontWarning } from './fonts.mjs';
|
|
10
|
+
import { gid, idOf, r2, slug, parseNodeRef, log } from './util.mjs';
|
|
11
|
+
|
|
12
|
+
const DEVICE = /iphone|android|pixel|galaxy|macbook|desktop|ipad|tablet|mobile|laptop|surface|\bweb\b|screen|landing|homepage|page\b/i;
|
|
13
|
+
const STD_WIDTHS = [320, 360, 375, 390, 393, 402, 414, 428, 430, 440, 744, 768, 800, 820, 834, 1024, 1080, 1194, 1280, 1366, 1440, 1512, 1536, 1600, 1728, 1920];
|
|
14
|
+
function isScreenLike(n) {
|
|
15
|
+
if (n.type !== 'FRAME' || !n.size) return false;
|
|
16
|
+
if (DEVICE.test(n.name)) return true;
|
|
17
|
+
return STD_WIDTHS.some(w => Math.abs(n.size.x - w) < 1) && n.size.y >= 560;
|
|
18
|
+
}
|
|
19
|
+
function hasScreenInside(doc, n, depth) {
|
|
20
|
+
for (const k of doc.children(n)) {
|
|
21
|
+
if (!['FRAME', 'SECTION', 'GROUP'].includes(k.type) || k.visible === false) continue;
|
|
22
|
+
if (isScreenLike(k)) return true;
|
|
23
|
+
if (depth > 1 && hasScreenInside(doc, k, depth - 1)) return true;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class FigFile {
|
|
29
|
+
static async open(path) { return new FigFile(await loadFig(path)); }
|
|
30
|
+
constructor(doc) { this.doc = doc; }
|
|
31
|
+
get path() { return this.doc.path; }
|
|
32
|
+
|
|
33
|
+
// ------------------------------------------------------------ discovery
|
|
34
|
+
/** Top-level design frames per page (screens, components, sections). */
|
|
35
|
+
listFrames({ page, query, includeComponents = true } = {}) {
|
|
36
|
+
const doc = this.doc, out = [];
|
|
37
|
+
const q = query && query.toLowerCase();
|
|
38
|
+
for (const pg of doc.pages()) {
|
|
39
|
+
if (page && pg.name.toLowerCase() !== String(page).toLowerCase() && idOf(pg) !== page) continue;
|
|
40
|
+
const walk = (parent, depth, section) => {
|
|
41
|
+
for (const c of doc.children(parent)) {
|
|
42
|
+
if (c.visible === false) continue;
|
|
43
|
+
if (c.type === 'SECTION') { walk(c, depth + 1, c.name); continue; }
|
|
44
|
+
if (!['FRAME', 'SYMBOL', 'GROUP'].includes(c.type) || !c.size) continue;
|
|
45
|
+
const kids = doc.children(c);
|
|
46
|
+
// "presentation wrappers": frames that hold device/screen frames (boards, mockup slides)
|
|
47
|
+
const wrapper = c.type !== 'SYMBOL' && !c.isStateGroup && depth < 5 && !isScreenLike(c) && hasScreenInside(doc, c, 2);
|
|
48
|
+
if (wrapper) { walk(c, depth + 1, section || c.name); continue; }
|
|
49
|
+
const kind = c.isStateGroup ? 'component-set' : c.type === 'SYMBOL' ? 'component' : isScreenLike(c) || (c.size.x >= 320 && c.size.y >= 480) ? 'screen' : 'frame';
|
|
50
|
+
if (!includeComponents && kind.startsWith('component')) continue;
|
|
51
|
+
const item = { id: idOf(c), name: c.name, kind, page: pg.name, group: section || undefined, size: `${Math.round(c.size.x)}x${Math.round(c.size.y)}`, children: kids.length };
|
|
52
|
+
if (c.prototypeStartingPoint) item.flowStart = c.prototypeStartingPoint.name || true;
|
|
53
|
+
if (q && !`${c.name} ${section || ''} ${pg.name}`.toLowerCase().includes(q)) continue;
|
|
54
|
+
out.push(item);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
walk(pg, 0, null);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Resolve an id ("1:23"), Figma URL (node-id=1-23) or a (fuzzy) name to a node. */
|
|
63
|
+
findNode(ref) {
|
|
64
|
+
const doc = this.doc;
|
|
65
|
+
const inst = String(ref).trim().match(/^I[\d:;]+;(\d+:\d+)$/); // Figma instance-path id "I1:2;3:4"
|
|
66
|
+
if (inst) { const n = doc.node(inst[1]); if (n) return n; }
|
|
67
|
+
const id = parseNodeRef(ref);
|
|
68
|
+
if (id) { const n = doc.node(id); if (n) return n; throw new Error(`No node with id ${id} in ${doc.name}`); }
|
|
69
|
+
const s = String(ref).trim().toLowerCase();
|
|
70
|
+
const pool = doc.nodes.filter(n => ['FRAME', 'SYMBOL', 'SECTION', 'INSTANCE', 'GROUP', 'CANVAS'].includes(n.type) && !doc.isInternal(n));
|
|
71
|
+
const exact = pool.filter(n => n.name.toLowerCase() === s);
|
|
72
|
+
const list = exact.length ? exact : pool.filter(n => n.name.toLowerCase().includes(s));
|
|
73
|
+
if (!list.length) throw new Error(`No frame named "${ref}". Use list_frames to see available frames.`);
|
|
74
|
+
// prefer top-level frames, then larger ones
|
|
75
|
+
const top = this._top || (this._top = new Set(this.listFrames().map(f => f.id)));
|
|
76
|
+
const rank = n => (top.has(idOf(n)) ? 0 : 1) * 1e9 - (n.size?.x || 0) * (n.size?.y || 0);
|
|
77
|
+
list.sort((a, b) => rank(a) - rank(b));
|
|
78
|
+
if (list.length > 1 && list[0].name === list[1].name && top.has(idOf(list[0])) === top.has(idOf(list[1]))) {
|
|
79
|
+
const alts = list.slice(0, 8).map(n => `${idOf(n)} "${n.name}" (${doc.ancestors(n).slice(0, -1).map(p => p.name).join(' / ')})`).join('\n ');
|
|
80
|
+
throw new Error(`"${ref}" is ambiguous; pass an id instead:\n ${alts}`);
|
|
81
|
+
}
|
|
82
|
+
return list[0];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
search(query, { limit = 50, types } = {}) {
|
|
86
|
+
const doc = this.doc, q = String(query).toLowerCase(), out = [];
|
|
87
|
+
for (const n of doc.nodes) {
|
|
88
|
+
if (doc.isInternal(n) || n.type === 'DOCUMENT') continue;
|
|
89
|
+
if (types && !types.includes(n.type)) continue;
|
|
90
|
+
const text = n.type === 'TEXT' ? n.textData?.characters || '' : '';
|
|
91
|
+
if (!n.name.toLowerCase().includes(q) && !text.toLowerCase().includes(q)) continue;
|
|
92
|
+
const path = doc.ancestors(n);
|
|
93
|
+
out.push({ id: idOf(n), type: n.type, name: n.name, text: text && text !== n.name ? text.slice(0, 120) : undefined, page: path[0]?.name, frame: path.find((p, i) => i > 0 && ['FRAME', 'SYMBOL'].includes(p.type) && p !== n)?.name, path: path.slice(1, -1).map(p => p.name).join(' / ') });
|
|
94
|
+
if (out.length >= limit) break;
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ------------------------------------------------------------ tokens / components
|
|
100
|
+
tokens() {
|
|
101
|
+
const reg = new TokenRegistry(this.doc);
|
|
102
|
+
return { ...reg.collect(), css: reg.toCss() };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
components({ query } = {}) {
|
|
106
|
+
const doc = this.doc, out = new Map();
|
|
107
|
+
const usage = new Map();
|
|
108
|
+
for (const n of doc.nodes) if (n.type === 'INSTANCE' && !doc.isInternal(n)) { const k = gid(n.symbolData?.symbolID); usage.set(k, (usage.get(k) || 0) + 1); }
|
|
109
|
+
for (const sym of doc.nodes.filter(n => n.type === 'SYMBOL')) {
|
|
110
|
+
const set = doc.componentSet(sym);
|
|
111
|
+
const key = set ? idOf(set) : idOf(sym);
|
|
112
|
+
if (!out.has(key)) {
|
|
113
|
+
out.set(key, { id: key, name: set ? set.name : sym.name, kind: set ? 'component-set' : 'component', variants: 0, properties: {}, uses: 0, local: !doc.isInternal(sym), description: (set || sym).symbolDescription || undefined, interactive: false });
|
|
114
|
+
for (const d of (set || sym).componentPropDefs || []) if (d.type !== 'VARIANT' && !d.isDeleted) out.get(key).properties[d.name.replace(/#.*$/, '')] = d.type;
|
|
115
|
+
}
|
|
116
|
+
const c = out.get(key);
|
|
117
|
+
c.variants++; c.uses += usage.get(idOf(sym)) || 0;
|
|
118
|
+
const vp = doc.variantProps(sym);
|
|
119
|
+
if (vp) for (const [k, v] of Object.entries(vp)) { const arr = (c.properties[k] = Array.isArray(c.properties[k]) ? c.properties[k] : []); if (!arr.includes(v)) arr.push(v); }
|
|
120
|
+
if (sym.prototypeInteractions?.length) c.interactive = true;
|
|
121
|
+
}
|
|
122
|
+
let list = [...out.values()].filter(c => c.uses > 0 || c.local);
|
|
123
|
+
if (query) { const q = query.toLowerCase(); list = list.filter(c => c.name.toLowerCase().includes(q)); }
|
|
124
|
+
return list.sort((a, b) => b.uses - a.uses);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ------------------------------------------------------------ prototype
|
|
128
|
+
flows() {
|
|
129
|
+
const doc = this.doc;
|
|
130
|
+
const starts = doc.nodes.filter(n => n.prototypeStartingPoint && !doc.isInternal(n)).map(n => ({ flow: n.prototypeStartingPoint.name, startId: idOf(n), start: n.name }));
|
|
131
|
+
const conv = new Converter(doc);
|
|
132
|
+
const edges = [];
|
|
133
|
+
const screenOf = n => doc.ancestors(n).find(p => p.type === 'FRAME' && doc.parent(p)?.type !== 'FRAME') || doc.ancestors(n)[1];
|
|
134
|
+
for (const n of doc.nodes) {
|
|
135
|
+
if (!n.prototypeInteractions?.length || doc.isInternal(n)) continue;
|
|
136
|
+
for (const ix of conv.interactionsOf(n) || []) {
|
|
137
|
+
if (ix.action === 'SWAP_STATE') continue;
|
|
138
|
+
edges.push({ fromScreen: screenOf(n)?.name, fromNode: `${n.name} (${idOf(n)})`, trigger: ix.trigger, action: ix.action, to: ix.target ? `${ix.target.name} (${ix.target.id})` : ix.url, transition: ix.transition, durationMs: ix.durationMs, overlay: ix.overlay, text: ix.text });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return { starts, edges };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ------------------------------------------------------------ frame export
|
|
145
|
+
/**
|
|
146
|
+
* Convert a frame/component/any node into an LLM-ready bundle.
|
|
147
|
+
* Writes files when outDir is given (or write=true -> default dir next to the .fig).
|
|
148
|
+
*/
|
|
149
|
+
async getFrame(ref, { outDir, write = true, maxChars = 40000, render = true, pinTextBoxes = false } = {}) {
|
|
150
|
+
const doc = this.doc;
|
|
151
|
+
const node = typeof ref === 'object' ? ref : this.findNode(ref);
|
|
152
|
+
const conv = new Converter(doc, { pinTextBoxes });
|
|
153
|
+
const t0 = Date.now();
|
|
154
|
+
const spec = conv.build(node);
|
|
155
|
+
if (!spec) throw new Error(`Node ${idOf(node)} is hidden`);
|
|
156
|
+
const states = variantStates(doc, conv);
|
|
157
|
+
const loops = animationLoops(states);
|
|
158
|
+
const fonts = [...conv.fonts.values()];
|
|
159
|
+
const requiredFonts = fontReport(fonts);
|
|
160
|
+
const full = toHtml(spec, { states });
|
|
161
|
+
const tokensCss = conv.tokens.toCss({ onlyUsed: true });
|
|
162
|
+
const dir = write ? resolve(outDir || join(dirname(doc.path), 'figma-export', `${slug(node.name) || 'frame'}-${idOf(node).replace(':', '-')}`)) : null;
|
|
163
|
+
const files = {};
|
|
164
|
+
const assetSummary = { images: conv.assets.images.size, svgs: conv.assets.svgs.size, rasters: conv.assets.rasters.size };
|
|
165
|
+
if (dir) {
|
|
166
|
+
mkdirSync(join(dir, 'assets'), { recursive: true });
|
|
167
|
+
for (const img of conv.assets.images.values()) if (!img.missing) writeFileSync(join(dir, 'assets', img.file), doc.readImage(img.hash));
|
|
168
|
+
let svgFail = 0;
|
|
169
|
+
for (const { file, node: n, raw, ctx } of conv.assets.svgs.values()) {
|
|
170
|
+
try { writeFileSync(join(dir, 'assets', file), await nodeToSvg(doc, n, { raw, ctx, conv })); } catch (e) { svgFail++; log(`svg export failed for ${n.name}: ${e.message}`); }
|
|
171
|
+
}
|
|
172
|
+
for (const { file, node: n, raw, ctx } of conv.assets.rasters.values()) {
|
|
173
|
+
try { writeFileSync(join(dir, 'assets', file), await svgToPng(await nodeToSvg(doc, n, { raw, ctx, conv }), { scale: 2, maxDim: 4096 })); } catch (e) { log(`raster export failed for ${n.name}: ${e.message}`); }
|
|
174
|
+
}
|
|
175
|
+
assetSummary.svgFailures = svgFail;
|
|
176
|
+
const page = `<!doctype html>\n<html lang="en">\n<head>\n<meta charset="utf-8">\n<meta name="viewport" content="width=device-width, initial-scale=1">\n<title>${node.name}</title>\n${requiredFonts.length ? `<!-- Fonts required (not bundled): ${requiredFonts.map(r => `${r.family} ${r.weights.join('/')}`).join(', ')} -->\n` : ''}<link rel="stylesheet" href="tokens.css">\n<link rel="stylesheet" href="styles.css">\n</head>\n<body style="margin:0">\n${full.html}\n</body>\n</html>\n`;
|
|
177
|
+
writeFileSync(join(dir, 'index.html'), page);
|
|
178
|
+
writeFileSync(join(dir, 'styles.css'), full.css + '\n');
|
|
179
|
+
writeFileSync(join(dir, 'tokens.css'), tokensCss);
|
|
180
|
+
files.html = join(dir, 'index.html'); files.css = join(dir, 'styles.css'); files.tokens = join(dir, 'tokens.css'); files.assets = join(dir, 'assets');
|
|
181
|
+
if (render) {
|
|
182
|
+
try {
|
|
183
|
+
const { buffers } = previewFonts(fonts);
|
|
184
|
+
writeFileSync(join(dir, 'preview.png'), await svgToPng(await nodeToSvg(doc, node), { scale: 1, maxDim: 4096, fonts: buffers }));
|
|
185
|
+
files.preview = join(dir, 'preview.png');
|
|
186
|
+
} catch (e) { log(`preview render failed: ${e.message}`); }
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const meta = {
|
|
190
|
+
file: doc.name, frame: { id: idOf(node), name: node.name, type: node.type, size: { width: r2(node.size?.x), height: r2(node.size?.y) }, page: doc.pageOf(node)?.name },
|
|
191
|
+
stats: { ...conv.stats, cssClasses: full.classCount, ...assetSummary, ms: Date.now() - t0 },
|
|
192
|
+
fonts: requiredFonts, fontWarning: fontWarning(requiredFonts), components: [...conv.components.values()], states, animations: loops,
|
|
193
|
+
interactions: conv.interactions.filter(i => !i.list.every(x => x.action === 'SWAP_STATE')).map(i => ({ node: i.node.name, id: i.node.id, interactions: i.list })),
|
|
194
|
+
scrolling: scrollInfo(spec),
|
|
195
|
+
};
|
|
196
|
+
const summary = summarize(meta, files, dir);
|
|
197
|
+
if (dir) { writeFileSync(join(dir, 'spec.json'), JSON.stringify({ ...meta, tree: spec }, null, 1)); writeFileSync(join(dir, 'DESIGN.md'), summary.split(dir).join('.')); files.spec = join(dir, 'spec.json'); files.design = join(dir, 'DESIGN.md'); }
|
|
198
|
+
|
|
199
|
+
// Fit the inline HTML into the budget by collapsing deep subtrees.
|
|
200
|
+
let html = full.html, depthUsed = Infinity;
|
|
201
|
+
const budget = Math.max(4000, maxChars - summary.length - tokensCss.length - Math.min(full.css.length, maxChars / 3));
|
|
202
|
+
if (html.length > budget) {
|
|
203
|
+
for (let d = 12; d >= 1; d--) { const h = toHtml(spec, { states, maxDepth: d }).html; if (h.length <= budget || d === 1) { html = h; depthUsed = d; break; } }
|
|
204
|
+
}
|
|
205
|
+
let css = full.css;
|
|
206
|
+
if (css.length > maxChars / 3) css = css.slice(0, maxChars / 3) + `\n/* … truncated; full stylesheet in ${files.css || 'styles.css'} */`;
|
|
207
|
+
return { meta, spec, summary, html, css, tokensCss, fullHtml: full.html, fullCss: full.css, files, dir, truncatedDepth: depthUsed === Infinity ? null : depthUsed, sections: (spec.children || []).map(c => ({ id: c.id, name: c.name, type: c.type, nodes: countNodes(c), box: `${c.box.left ?? ''} ${c.box.top ?? ''} ${c.box.width ?? 'auto'}×${c.box.height ?? 'auto'}`.trim() })) };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** PNG preview of a node. */
|
|
211
|
+
async render(ref, { scale = 1, maxDim = 2000 } = {}) {
|
|
212
|
+
const node = typeof ref === 'object' ? ref : this.findNode(ref);
|
|
213
|
+
const conv = new Converter(this.doc); conv.build(node);
|
|
214
|
+
const report = fontReport([...conv.fonts.values()]);
|
|
215
|
+
const { buffers, fallback } = previewFonts([...conv.fonts.values()]);
|
|
216
|
+
const png = await svgToPng(await nodeToSvg(this.doc, node), { scale, maxDim, fonts: buffers });
|
|
217
|
+
return { png, node: { id: idOf(node), name: node.name, size: node.size }, fonts: report, fallbackFont: fallback, fontWarning: fontWarning(report) };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Export a single node (icon/vector/illustration) as SVG text. */
|
|
221
|
+
async svg(ref) { const node = typeof ref === 'object' ? ref : this.findNode(ref); return nodeToSvg(this.doc, node); }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function scrollInfo(spec) {
|
|
225
|
+
const out = [];
|
|
226
|
+
const walk = s => {
|
|
227
|
+
const ov = s.style?.overflow;
|
|
228
|
+
if (ov && /auto/.test(ov)) out.push({ id: s.id, name: s.name, kind: ov === 'hidden auto' ? 'scrolls vertically' : ov === 'auto hidden' ? 'scrolls horizontally' : 'scrolls both ways' });
|
|
229
|
+
if (s.scroll) out.push({ id: s.id, name: s.name, kind: s.scroll === 'sticky' ? 'sticky — sticks once scrolled to (position: sticky)' : 'fixed — stays in place while the parent scrolls (position: fixed/sticky)' });
|
|
230
|
+
(s.children || []).forEach(walk);
|
|
231
|
+
};
|
|
232
|
+
walk(spec);
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function fmtVariant(v) { return v ? Object.entries(v).map(([k, x]) => `${k}=${x}`).join(', ') : ''; }
|
|
237
|
+
|
|
238
|
+
export function summarize(meta, files, dir) {
|
|
239
|
+
const L = [];
|
|
240
|
+
const f = meta.frame;
|
|
241
|
+
L.push(`# ${f.name} (${f.type} ${f.id}, ${f.size.width}×${f.size.height}px, page "${f.page}", file "${meta.file}")`);
|
|
242
|
+
L.push(`Nodes: ${meta.stats.nodes} visible (${meta.stats.hidden} hidden skipped) · text ${meta.stats.text} · component instances ${meta.stats.instances} · images ${meta.stats.images ? meta.stats.images + ' fills' : 0} · icons/vectors ${meta.stats.svgs}${meta.stats.rasters ? ` · masked groups ${meta.stats.rasters}` : ''}`);
|
|
243
|
+
if (dir) {
|
|
244
|
+
L.push(`\n## Files (written to disk)`);
|
|
245
|
+
L.push(`- ${dir}`);
|
|
246
|
+
L.push(` - index.html — absolutely/flex positioned HTML reproduction (open in a browser)\n - styles.css — one class per unique style + :hover/:active state rules\n - tokens.css — CSS variables for the design tokens used\n - assets/ — ${meta.stats.images} image file(s), ${meta.stats.svgs} SVG icon(s)\n - preview.png — rendering of the frame for visual reference\n - spec.json — full machine-readable tree (box, style, text runs, constraints, interactions)`);
|
|
247
|
+
}
|
|
248
|
+
if (meta.fonts.length) {
|
|
249
|
+
L.push(`\n## Fonts — REQUIRED, not bundled`);
|
|
250
|
+
L.push(`> ⚠ This design needs the fonts below. They are not included: add them to the project (self-host with @font-face, or a font service) or agree on substitutes.`);
|
|
251
|
+
for (const ft of meta.fonts) L.push(`- **${ft.family}** — weights ${ft.weights.join(', ')}${ft.styles.length ? ` (${ft.styles.join(', ')})` : ''}${ft.installed ? '' : ' — not installed on this machine'}`);
|
|
252
|
+
}
|
|
253
|
+
if (meta.components.length) {
|
|
254
|
+
L.push(`\n## Components used`);
|
|
255
|
+
const byName = new Map();
|
|
256
|
+
for (const c of meta.components) { if (!byName.has(c.name)) byName.set(c.name, { uses: 0, variants: new Set() }); const e = byName.get(c.name); e.uses += c.uses; if (c.variant) e.variants.add(fmtVariant(c.variant)); }
|
|
257
|
+
for (const [name, e] of [...byName].sort((a, b) => b[1].uses - a[1].uses)) L.push(`- ${name} ×${e.uses}${e.variants.size ? ` — ${[...e.variants].slice(0, 4).join(' | ')}${e.variants.size > 4 ? ` | …+${e.variants.size - 4}` : ''}` : ''}`);
|
|
258
|
+
}
|
|
259
|
+
const changeText = s => {
|
|
260
|
+
const ch = Object.entries(s.changes).map(([p, c]) => `${p}: ${Object.entries(c).filter(([k]) => !k.startsWith('/*')).map(([k, v]) => `${k} → ${v}`).join('; ')}`).filter(x => !x.endsWith(': ')).join(' | ');
|
|
261
|
+
return [ch && ch.slice(0, 400), s.shows && `shows ${s.shows.map(x => `"${x}"`).join(', ')}`, s.hides && `hides ${s.hides.map(x => `"${x}"`).join(', ')}`].filter(Boolean).join(' · ') || 'no visual change';
|
|
262
|
+
};
|
|
263
|
+
const timing = s => `${s.transition && s.transition !== 'INSTANT_TRANSITION' ? s.transition.toLowerCase().replace(/_/g, ' ') : 'instant'}${s.durationMs ? ' ' + s.durationMs + 'ms' : ''}${s.easing ? ' ' + s.easing : ''}`;
|
|
264
|
+
const css = meta.states.filter(s => s.pseudo);
|
|
265
|
+
if (css.length) {
|
|
266
|
+
L.push(`\n## Interactive states → CSS (rules are in styles.css)`);
|
|
267
|
+
L.push(`Pseudo-classes come from prototype interactions ("while hovering/pressing") and from variant naming (State=Hover/Focused/Disabled, Selected=True …).`);
|
|
268
|
+
for (const s of css) L.push(`- ${s.component} [${fmtVariant(s.from)}] \`${s.pseudo.join('`, `')}\` → [${fmtVariant(s.to)}]${s.source === 'variant-naming' ? ' (variant naming)' : ` (${timing(s)})`}: ${changeText(s)}`);
|
|
269
|
+
}
|
|
270
|
+
const jsStates = meta.states.filter(s => !s.pseudo && s.trigger !== 'AFTER_TIMEOUT');
|
|
271
|
+
if (jsStates.length) {
|
|
272
|
+
L.push(`\n## State changes that need JavaScript (click / toggle / key / drag)`);
|
|
273
|
+
for (const s of jsStates) L.push(`- ${s.component} [${fmtVariant(s.via || s.from)}] on ${s.triggerText || s.trigger} → [${fmtVariant(s.to)}] (${timing(s)}): ${changeText(s)}`);
|
|
274
|
+
}
|
|
275
|
+
if (meta.animations.length) {
|
|
276
|
+
L.push(`\n## Auto-playing animations (timed variant loops — carousels, marquees, loaders)`);
|
|
277
|
+
for (const a of meta.animations) L.push(`- ${a.component}: ${a.steps.map(s => `[${fmtVariant(s.from)}] —${s.afterMs ?? 0}ms→ [${fmtVariant(s.to)}] (${timing(s)})`).join(' ; ')}`);
|
|
278
|
+
}
|
|
279
|
+
if (meta.interactions.length) {
|
|
280
|
+
L.push(`\n## Interactions (navigation, overlays/modals, scroll-to, links, variables, conditionals)`);
|
|
281
|
+
let n = 0;
|
|
282
|
+
for (const i of meta.interactions) for (const x of i.interactions) {
|
|
283
|
+
if (x.action === 'SWAP_STATE') continue;
|
|
284
|
+
if (++n > 80) break;
|
|
285
|
+
L.push(`- "${i.node}" (${i.id}): ${x.text}`);
|
|
286
|
+
}
|
|
287
|
+
if (n > 80) L.push(`- … more in spec.json`);
|
|
288
|
+
}
|
|
289
|
+
if (meta.scrolling?.length) {
|
|
290
|
+
L.push(`\n## Scrolling`);
|
|
291
|
+
for (const s of meta.scrolling) L.push(`- "${s.name}" (${s.id}): ${s.kind}`);
|
|
292
|
+
}
|
|
293
|
+
return L.join('\n') + '\n';
|
|
294
|
+
}
|