figme-mcp 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +447 -0
- package/dist/cache.d.ts +29 -0
- package/dist/cache.js +68 -0
- package/dist/fig/bytebuffer.d.ts +31 -0
- package/dist/fig/bytebuffer.js +98 -0
- package/dist/fig/container.d.ts +32 -0
- package/dist/fig/container.js +80 -0
- package/dist/fig/imagemeta.d.ts +17 -0
- package/dist/fig/imagemeta.js +96 -0
- package/dist/fig/invariants.d.ts +10 -0
- package/dist/fig/invariants.js +63 -0
- package/dist/fig/kiwi.d.ts +38 -0
- package/dist/fig/kiwi.js +144 -0
- package/dist/fig/parse.d.ts +37 -0
- package/dist/fig/parse.js +91 -0
- package/dist/fig/zip.d.ts +23 -0
- package/dist/fig/zip.js +95 -0
- package/dist/mcp/create.d.ts +12 -0
- package/dist/mcp/create.js +48 -0
- package/dist/mcp/respond.d.ts +76 -0
- package/dist/mcp/respond.js +124 -0
- package/dist/mcp/server.d.ts +3 -0
- package/dist/mcp/server.js +22 -0
- package/dist/mcp/tools/blob.d.ts +7 -0
- package/dist/mcp/tools/blob.js +55 -0
- package/dist/mcp/tools/components.d.ts +7 -0
- package/dist/mcp/tools/components.js +54 -0
- package/dist/mcp/tools/context.d.ts +24 -0
- package/dist/mcp/tools/context.js +48 -0
- package/dist/mcp/tools/find.d.ts +7 -0
- package/dist/mcp/tools/find.js +97 -0
- package/dist/mcp/tools/image.d.ts +6 -0
- package/dist/mcp/tools/image.js +100 -0
- package/dist/mcp/tools/instance.d.ts +7 -0
- package/dist/mcp/tools/instance.js +44 -0
- package/dist/mcp/tools/node.d.ts +7 -0
- package/dist/mcp/tools/node.js +49 -0
- package/dist/mcp/tools/overview.d.ts +7 -0
- package/dist/mcp/tools/overview.js +45 -0
- package/dist/mcp/tools/render.d.ts +6 -0
- package/dist/mcp/tools/render.js +128 -0
- package/dist/mcp/tools/style.d.ts +7 -0
- package/dist/mcp/tools/style.js +38 -0
- package/dist/mcp/tools/text.d.ts +7 -0
- package/dist/mcp/tools/text.js +89 -0
- package/dist/mcp/tools/tree.d.ts +7 -0
- package/dist/mcp/tools/tree.js +100 -0
- package/dist/mcp/tools/variables.d.ts +7 -0
- package/dist/mcp/tools/variables.js +71 -0
- package/dist/model/access.d.ts +28 -0
- package/dist/model/access.js +106 -0
- package/dist/model/components.d.ts +72 -0
- package/dist/model/components.js +157 -0
- package/dist/model/index.d.ts +73 -0
- package/dist/model/index.js +192 -0
- package/dist/model/instance.d.ts +87 -0
- package/dist/model/instance.js +208 -0
- package/dist/model/summarize.d.ts +83 -0
- package/dist/model/summarize.js +541 -0
- package/dist/model/text.d.ts +29 -0
- package/dist/model/text.js +61 -0
- package/dist/model/tree.d.ts +47 -0
- package/dist/model/tree.js +89 -0
- package/dist/model/variables.d.ts +50 -0
- package/dist/model/variables.js +141 -0
- package/dist/render/bounds.d.ts +64 -0
- package/dist/render/bounds.js +173 -0
- package/dist/render/color.d.ts +20 -0
- package/dist/render/color.js +41 -0
- package/dist/render/effects.d.ts +34 -0
- package/dist/render/effects.js +196 -0
- package/dist/render/export.d.ts +51 -0
- package/dist/render/export.js +573 -0
- package/dist/render/index.d.ts +42 -0
- package/dist/render/index.js +94 -0
- package/dist/render/instance.d.ts +12 -0
- package/dist/render/instance.js +72 -0
- package/dist/render/matrix.d.ts +46 -0
- package/dist/render/matrix.js +94 -0
- package/dist/render/node.d.ts +41 -0
- package/dist/render/node.js +95 -0
- package/dist/render/paint.d.ts +27 -0
- package/dist/render/paint.js +301 -0
- package/dist/render/path.d.ts +42 -0
- package/dist/render/path.js +156 -0
- package/dist/render/raster.d.ts +31 -0
- package/dist/render/raster.js +69 -0
- package/dist/render/report.d.ts +72 -0
- package/dist/render/report.js +106 -0
- package/dist/render/style.d.ts +31 -0
- package/dist/render/style.js +67 -0
- package/dist/render/svg.d.ts +60 -0
- package/dist/render/svg.js +150 -0
- package/dist/render/text.d.ts +50 -0
- package/dist/render/text.js +90 -0
- package/package.json +47 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { cursorArg, fileArg, handler, load, requireNode } from './context.js';
|
|
3
|
+
import { Packer, decodeCursor, encodeCursor, jsonResult, measure } from '../respond.js';
|
|
4
|
+
import { compactStyle, textView } from '../../model/text.js';
|
|
5
|
+
import { breadcrumb } from '../../model/tree.js';
|
|
6
|
+
import { str } from '../../model/access.js';
|
|
7
|
+
export const NAME = 'fig_text';
|
|
8
|
+
export const DESCRIPTION = [
|
|
9
|
+
'Extract every string of copy in the file (or in one subtree via `scope`), in document order:',
|
|
10
|
+
'guid, layer name, the characters, the page, and the base typography (font, size, line-height,',
|
|
11
|
+
'colour). With includeRuns:true each node also carries its styled runs — the mixed-format',
|
|
12
|
+
'spans Figma stores per UTF-16 code unit — with only the fields each run overrides.',
|
|
13
|
+
'Use this for copy audits and translation passes rather than walking the tree.',
|
|
14
|
+
].join(' ');
|
|
15
|
+
export function register(server, ctx) {
|
|
16
|
+
server.registerTool(NAME, {
|
|
17
|
+
title: 'Text inventory',
|
|
18
|
+
description: DESCRIPTION,
|
|
19
|
+
inputSchema: {
|
|
20
|
+
file: fileArg,
|
|
21
|
+
scope: z.string().optional().describe('Only collect text inside this guid subtree.'),
|
|
22
|
+
includeRuns: z
|
|
23
|
+
.boolean()
|
|
24
|
+
.optional()
|
|
25
|
+
.describe('Include per-run style overrides (default false; costs many more tokens).'),
|
|
26
|
+
includeStyle: z
|
|
27
|
+
.boolean()
|
|
28
|
+
.optional()
|
|
29
|
+
.describe('Include font / size / line-height / colour per node (default true).'),
|
|
30
|
+
includePath: z
|
|
31
|
+
.boolean()
|
|
32
|
+
.optional()
|
|
33
|
+
.describe('Include the ancestor breadcrumb per node (default false; costs tokens).'),
|
|
34
|
+
cursor: cursorArg,
|
|
35
|
+
},
|
|
36
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
37
|
+
}, handler((args) => {
|
|
38
|
+
const { index } = load(ctx, args.file);
|
|
39
|
+
const scope = args.scope ? requireNode(index, args.scope) : undefined;
|
|
40
|
+
const range = scope ? index.subtreeRange(scope) : { start: 0, end: index.nodeCount };
|
|
41
|
+
const includeRuns = args.includeRuns === true;
|
|
42
|
+
const includeStyle = args.includeStyle !== false;
|
|
43
|
+
const { after } = decodeCursor(args.cursor);
|
|
44
|
+
const packer = new Packer();
|
|
45
|
+
let skipping = after !== undefined;
|
|
46
|
+
let lastGuid;
|
|
47
|
+
let totalTextNodes = 0;
|
|
48
|
+
for (let i = range.start; i < range.end; i++) {
|
|
49
|
+
const t = index.tree.ordered[i];
|
|
50
|
+
if (str(t.node, 'type') !== 'TEXT')
|
|
51
|
+
continue;
|
|
52
|
+
const view = textView(index, t, includeRuns);
|
|
53
|
+
if (!view)
|
|
54
|
+
continue;
|
|
55
|
+
totalTextNodes++;
|
|
56
|
+
if (skipping) {
|
|
57
|
+
if (t.key === after)
|
|
58
|
+
skipping = false;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (packer.full)
|
|
62
|
+
continue;
|
|
63
|
+
const item = {
|
|
64
|
+
guid: t.key,
|
|
65
|
+
name: str(t.node, 'name'),
|
|
66
|
+
characters: view.characters,
|
|
67
|
+
page: t.page ? str(t.page.node, 'name') : undefined,
|
|
68
|
+
path: args.includePath === true ? breadcrumb(t) || undefined : undefined,
|
|
69
|
+
style: includeStyle ? compactStyle(view.baseStyle) : undefined,
|
|
70
|
+
hasStyledRuns: view.hasStyledRuns || undefined,
|
|
71
|
+
runs: includeRuns ? view.runs : undefined,
|
|
72
|
+
};
|
|
73
|
+
if (packer.add(item, measure(item)))
|
|
74
|
+
lastGuid = t.key;
|
|
75
|
+
}
|
|
76
|
+
const truncated = packer.truncated;
|
|
77
|
+
return jsonResult({
|
|
78
|
+
scope: scope?.key,
|
|
79
|
+
totalTextNodes,
|
|
80
|
+
returned: packer.items.length,
|
|
81
|
+
texts: packer.items,
|
|
82
|
+
truncated: truncated || undefined,
|
|
83
|
+
nextCursor: truncated && lastGuid ? encodeCursor({ after: lastGuid }) : undefined,
|
|
84
|
+
hint: truncated
|
|
85
|
+
? 'pass nextCursor to continue, or narrow with scope / includeRuns:false'
|
|
86
|
+
: undefined,
|
|
87
|
+
}, { listKey: 'texts' });
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** `fig_tree` — shallow-by-default structural exploration of a subtree. */
|
|
2
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
+
import { type ToolContext } from './context.js';
|
|
4
|
+
export declare const NAME = "fig_tree";
|
|
5
|
+
export declare const DESCRIPTION: string;
|
|
6
|
+
export declare function register(server: McpServer, ctx: ToolContext): void;
|
|
7
|
+
//# sourceMappingURL=tree.d.ts.map
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { cursorArg, fileArg, handler, load, resolveRoot } from './context.js';
|
|
3
|
+
import { Packer, decodeCursor, encodeCursor, jsonResult, measure, textResult } from '../respond.js';
|
|
4
|
+
import { outlineLine, summarize } from '../../model/summarize.js';
|
|
5
|
+
import { str } from '../../model/access.js';
|
|
6
|
+
export const NAME = 'fig_tree';
|
|
7
|
+
export const DESCRIPTION = [
|
|
8
|
+
'List the layers under a node, breadth-limited by depth (default 2, max 6).',
|
|
9
|
+
'Returns a FLAT list in document order; each entry carries `depth` (relative to the requested',
|
|
10
|
+
'root) and `parent`, so the hierarchy is reconstructable, and `children` (count) so you can',
|
|
11
|
+
'see where to deepen. format:"outline" returns indented text lines instead of JSON and is',
|
|
12
|
+
'roughly 4x denser for browsing. Large subtrees are truncated with truncated:true and a',
|
|
13
|
+
'nextCursor you can pass back to continue.',
|
|
14
|
+
].join(' ');
|
|
15
|
+
const MAX_DEPTH = 6;
|
|
16
|
+
export function register(server, ctx) {
|
|
17
|
+
server.registerTool(NAME, {
|
|
18
|
+
title: 'Explore the layer tree',
|
|
19
|
+
description: DESCRIPTION,
|
|
20
|
+
inputSchema: {
|
|
21
|
+
file: fileArg,
|
|
22
|
+
root: z
|
|
23
|
+
.string()
|
|
24
|
+
.optional()
|
|
25
|
+
.describe('Guid to start from, e.g. "2:1339". Defaults to the DOCUMENT node.'),
|
|
26
|
+
depth: z
|
|
27
|
+
.number()
|
|
28
|
+
.int()
|
|
29
|
+
.min(0)
|
|
30
|
+
.max(MAX_DEPTH)
|
|
31
|
+
.optional()
|
|
32
|
+
.describe(`Levels below the root to include (default 2, max ${MAX_DEPTH}).`),
|
|
33
|
+
types: z
|
|
34
|
+
.array(z.string())
|
|
35
|
+
.optional()
|
|
36
|
+
.describe('Only include these node types, e.g. ["FRAME","TEXT"]. Depth still applies.'),
|
|
37
|
+
format: z.enum(['json', 'outline']).optional().describe('Default "json".'),
|
|
38
|
+
cursor: cursorArg,
|
|
39
|
+
},
|
|
40
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
41
|
+
}, handler((args) => {
|
|
42
|
+
const { index } = load(ctx, args.file);
|
|
43
|
+
const rootNode = resolveRoot(index, args.root);
|
|
44
|
+
const depth = args.depth ?? 2;
|
|
45
|
+
const format = args.format ?? 'json';
|
|
46
|
+
const typeFilter = args.types?.length ? new Set(args.types) : undefined;
|
|
47
|
+
const { after } = decodeCursor(args.cursor);
|
|
48
|
+
const { start, end } = index.subtreeRange(rootNode);
|
|
49
|
+
const packer = new Packer();
|
|
50
|
+
let skipping = after !== undefined;
|
|
51
|
+
let lastGuid;
|
|
52
|
+
let matched = 0;
|
|
53
|
+
for (let i = start; i < end; i++) {
|
|
54
|
+
const t = index.tree.ordered[i];
|
|
55
|
+
const relDepth = t.depth - rootNode.depth;
|
|
56
|
+
if (relDepth > depth)
|
|
57
|
+
continue;
|
|
58
|
+
if (typeFilter && !typeFilter.has(str(t.node, 'type') ?? ''))
|
|
59
|
+
continue;
|
|
60
|
+
if (skipping) {
|
|
61
|
+
if (t.key === after)
|
|
62
|
+
skipping = false;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
matched++;
|
|
66
|
+
const item = format === 'outline'
|
|
67
|
+
? outlineLine(t, relDepth)
|
|
68
|
+
: { ...summarize(t), depth: relDepth, parent: t.parent?.key };
|
|
69
|
+
if (!packer.add(item, measure(item))) {
|
|
70
|
+
matched--;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
lastGuid = t.key;
|
|
74
|
+
}
|
|
75
|
+
const truncated = packer.truncated;
|
|
76
|
+
const nextCursor = truncated && lastGuid ? encodeCursor({ after: lastGuid }) : undefined;
|
|
77
|
+
const hint = truncated
|
|
78
|
+
? 'response budget reached: pass nextCursor to continue, or lower depth / add a types filter'
|
|
79
|
+
: undefined;
|
|
80
|
+
if (format === 'outline') {
|
|
81
|
+
const header = [
|
|
82
|
+
`root ${rootNode.key} ${JSON.stringify(str(rootNode.node, 'name') ?? '')} depth<=${depth}`,
|
|
83
|
+
...packer.items,
|
|
84
|
+
];
|
|
85
|
+
if (truncated) {
|
|
86
|
+
header.push('', `... truncated after ${matched} nodes. nextCursor: ${nextCursor}`);
|
|
87
|
+
}
|
|
88
|
+
return textResult(header.join('\n'));
|
|
89
|
+
}
|
|
90
|
+
return jsonResult({
|
|
91
|
+
root: summarize(rootNode),
|
|
92
|
+
depth,
|
|
93
|
+
count: matched,
|
|
94
|
+
nodes: packer.items,
|
|
95
|
+
truncated: truncated || undefined,
|
|
96
|
+
nextCursor,
|
|
97
|
+
hint,
|
|
98
|
+
}, { listKey: 'nodes' });
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** `fig_variables` — variable collections, their modes, and per-mode values. */
|
|
2
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
+
import { type ToolContext } from './context.js';
|
|
4
|
+
export declare const NAME = "fig_variables";
|
|
5
|
+
export declare const DESCRIPTION: string;
|
|
6
|
+
export declare function register(server: McpServer, ctx: ToolContext): void;
|
|
7
|
+
//# sourceMappingURL=variables.d.ts.map
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { cursorArg, fileArg, handler, load } from './context.js';
|
|
3
|
+
import { Packer, decodeCursor, encodeCursor, jsonResult, measure } from '../respond.js';
|
|
4
|
+
import { variableSetOf, variableSetView, variableView } from '../../model/variables.js';
|
|
5
|
+
import { str } from '../../model/access.js';
|
|
6
|
+
export const NAME = 'fig_variables';
|
|
7
|
+
export const DESCRIPTION = [
|
|
8
|
+
'List the design tokens in the file: every variable collection (VARIABLE_SET) with its modes,',
|
|
9
|
+
'and every variable with its value per mode — colours as hex, numbers and strings verbatim.',
|
|
10
|
+
'Aliases are followed when the target variable lives in this file; a variable published from',
|
|
11
|
+
'another library is returned as its opaque assetRef instead. Filter with `set` (collection',
|
|
12
|
+
'name or guid) or `query` (substring of the variable name).',
|
|
13
|
+
].join(' ');
|
|
14
|
+
const DEFAULT_LIMIT = 100;
|
|
15
|
+
export function register(server, ctx) {
|
|
16
|
+
server.registerTool(NAME, {
|
|
17
|
+
title: 'Variables and modes',
|
|
18
|
+
description: DESCRIPTION,
|
|
19
|
+
inputSchema: {
|
|
20
|
+
file: fileArg,
|
|
21
|
+
set: z.string().optional().describe('Only variables from this collection (name or guid).'),
|
|
22
|
+
query: z.string().optional().describe('Case-insensitive substring of the variable name.'),
|
|
23
|
+
includeSets: z
|
|
24
|
+
.boolean()
|
|
25
|
+
.optional()
|
|
26
|
+
.describe('Include the collection list with their modes (default true).'),
|
|
27
|
+
limit: z.number().int().min(1).max(300).optional().describe(`Default ${DEFAULT_LIMIT}.`),
|
|
28
|
+
cursor: cursorArg,
|
|
29
|
+
},
|
|
30
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
31
|
+
}, handler((args) => {
|
|
32
|
+
const { index } = load(ctx, args.file);
|
|
33
|
+
const needle = args.query?.toLowerCase();
|
|
34
|
+
const setFilter = args.set?.toLowerCase();
|
|
35
|
+
const matches = index.variables.filter((v) => {
|
|
36
|
+
if (needle && !(str(v.node, 'name') ?? '').toLowerCase().includes(needle))
|
|
37
|
+
return false;
|
|
38
|
+
if (!setFilter)
|
|
39
|
+
return true;
|
|
40
|
+
const set = variableSetOf(index, v);
|
|
41
|
+
if (!set)
|
|
42
|
+
return false;
|
|
43
|
+
return (set.key.toLowerCase() === setFilter ||
|
|
44
|
+
(str(set.node, 'name') ?? '').toLowerCase() === setFilter);
|
|
45
|
+
});
|
|
46
|
+
const { offset = 0 } = decodeCursor(args.cursor);
|
|
47
|
+
const packer = new Packer({ limit: args.limit ?? DEFAULT_LIMIT });
|
|
48
|
+
let i = offset;
|
|
49
|
+
for (; i < matches.length; i++) {
|
|
50
|
+
const item = variableView(index, matches[i]);
|
|
51
|
+
if (!packer.add(item, measure(item)))
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
const truncated = packer.truncated && i < matches.length;
|
|
55
|
+
return jsonResult({
|
|
56
|
+
totalSets: index.variableSets.length,
|
|
57
|
+
totalVariables: index.variables.length,
|
|
58
|
+
matching: matches.length,
|
|
59
|
+
returned: packer.items.length,
|
|
60
|
+
sets: args.includeSets === false || offset > 0
|
|
61
|
+
? undefined
|
|
62
|
+
: index.variableSets.map((s) => variableSetView(index, s)),
|
|
63
|
+
variables: packer.items,
|
|
64
|
+
truncated: truncated || undefined,
|
|
65
|
+
nextCursor: truncated ? encodeCursor({ offset: i }) : undefined,
|
|
66
|
+
hint: truncated
|
|
67
|
+
? 'pass nextCursor to continue, or narrow with set / query'
|
|
68
|
+
: undefined,
|
|
69
|
+
}, { listKey: 'variables' });
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed accessors over decoded Kiwi objects.
|
|
3
|
+
*
|
|
4
|
+
* Decoded nodes are open records (`KiwiObject`) because the schema grows constantly; these
|
|
5
|
+
* helpers read a field only if it has the expected shape, so an unexpected type degrades to
|
|
6
|
+
* `undefined` instead of throwing somewhere far away.
|
|
7
|
+
*/
|
|
8
|
+
import type { KiwiObject, KiwiValue } from '../fig/kiwi.js';
|
|
9
|
+
export declare function str(o: KiwiObject | undefined, k: string): string | undefined;
|
|
10
|
+
export declare function num(o: KiwiObject | undefined, k: string): number | undefined;
|
|
11
|
+
export declare function bool(o: KiwiObject | undefined, k: string): boolean | undefined;
|
|
12
|
+
export declare function obj(o: KiwiObject | undefined, k: string): KiwiObject | undefined;
|
|
13
|
+
export declare function arr(o: KiwiObject | undefined, k: string): KiwiValue[] | undefined;
|
|
14
|
+
export declare function objArr(o: KiwiObject | undefined, k: string): KiwiObject[];
|
|
15
|
+
export declare function bytes(o: KiwiObject | undefined, k: string): Uint8Array | undefined;
|
|
16
|
+
export declare function hex(v: Uint8Array | undefined): string | undefined;
|
|
17
|
+
/** Drop undefined/empty entries so responses never spend tokens on "not set". */
|
|
18
|
+
export declare function compact<T extends Record<string, unknown>>(o: T): Partial<T>;
|
|
19
|
+
/** Round presentation floats; raw float32 noise (0.20000000298023224) is correct but noisy. */
|
|
20
|
+
export declare function r2(n: number | undefined): number | undefined;
|
|
21
|
+
/** `{r,g,b,a}` floats 0–1 → `#RRGGBB`, or `#RRGGBBAA` when alpha < 1. */
|
|
22
|
+
export declare function colorHex(c: KiwiObject | undefined): string | undefined;
|
|
23
|
+
/** `{ assetRef: { key, version } }` → `"key@version"`; a library reference, opaque offline. */
|
|
24
|
+
export declare function assetRefString(o: KiwiObject | undefined): string | undefined;
|
|
25
|
+
export declare function assetRefKey(o: KiwiObject | undefined): string | undefined;
|
|
26
|
+
/** JSON-safe projection: BigInt → string, bytes → hex (truncated), everything else untouched. */
|
|
27
|
+
export declare function jsonSafe(value: KiwiValue | undefined): unknown;
|
|
28
|
+
//# sourceMappingURL=access.d.ts.map
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export function str(o, k) {
|
|
2
|
+
const v = o?.[k];
|
|
3
|
+
return typeof v === 'string' ? v : undefined;
|
|
4
|
+
}
|
|
5
|
+
export function num(o, k) {
|
|
6
|
+
const v = o?.[k];
|
|
7
|
+
return typeof v === 'number' && Number.isFinite(v) ? v : undefined;
|
|
8
|
+
}
|
|
9
|
+
export function bool(o, k) {
|
|
10
|
+
const v = o?.[k];
|
|
11
|
+
return typeof v === 'boolean' ? v : undefined;
|
|
12
|
+
}
|
|
13
|
+
export function obj(o, k) {
|
|
14
|
+
const v = o?.[k];
|
|
15
|
+
return v && typeof v === 'object' && !Array.isArray(v) && !(v instanceof Uint8Array)
|
|
16
|
+
? v
|
|
17
|
+
: undefined;
|
|
18
|
+
}
|
|
19
|
+
export function arr(o, k) {
|
|
20
|
+
const v = o?.[k];
|
|
21
|
+
return Array.isArray(v) ? v : undefined;
|
|
22
|
+
}
|
|
23
|
+
export function objArr(o, k) {
|
|
24
|
+
const v = arr(o, k);
|
|
25
|
+
if (!v)
|
|
26
|
+
return [];
|
|
27
|
+
return v.filter((e) => !!e && typeof e === 'object' && !Array.isArray(e));
|
|
28
|
+
}
|
|
29
|
+
export function bytes(o, k) {
|
|
30
|
+
const v = o?.[k];
|
|
31
|
+
return v instanceof Uint8Array ? v : undefined;
|
|
32
|
+
}
|
|
33
|
+
export function hex(v) {
|
|
34
|
+
return v ? Buffer.from(v).toString('hex') : undefined;
|
|
35
|
+
}
|
|
36
|
+
/** Drop undefined/empty entries so responses never spend tokens on "not set". */
|
|
37
|
+
export function compact(o) {
|
|
38
|
+
const out = {};
|
|
39
|
+
for (const [k, v] of Object.entries(o)) {
|
|
40
|
+
if (v === undefined || v === null)
|
|
41
|
+
continue;
|
|
42
|
+
if (Array.isArray(v) && v.length === 0)
|
|
43
|
+
continue;
|
|
44
|
+
if (typeof v === 'object' && !Array.isArray(v) && Object.keys(v).length === 0)
|
|
45
|
+
continue;
|
|
46
|
+
out[k] = v;
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
/** Round presentation floats; raw float32 noise (0.20000000298023224) is correct but noisy. */
|
|
51
|
+
export function r2(n) {
|
|
52
|
+
if (n === undefined)
|
|
53
|
+
return undefined;
|
|
54
|
+
return Math.round(n * 100) / 100;
|
|
55
|
+
}
|
|
56
|
+
/** `{r,g,b,a}` floats 0–1 → `#RRGGBB`, or `#RRGGBBAA` when alpha < 1. */
|
|
57
|
+
export function colorHex(c) {
|
|
58
|
+
if (!c)
|
|
59
|
+
return undefined;
|
|
60
|
+
const ch = (v) => Math.max(0, Math.min(255, Math.round((v ?? 0) * 255)))
|
|
61
|
+
.toString(16)
|
|
62
|
+
.padStart(2, '0');
|
|
63
|
+
const a = num(c, 'a') ?? 1;
|
|
64
|
+
const base = `#${ch(num(c, 'r'))}${ch(num(c, 'g'))}${ch(num(c, 'b'))}`.toUpperCase();
|
|
65
|
+
return a >= 1 ? base : `${base}${ch(a).toUpperCase()}`;
|
|
66
|
+
}
|
|
67
|
+
/** `{ assetRef: { key, version } }` → `"key@version"`; a library reference, opaque offline. */
|
|
68
|
+
export function assetRefString(o) {
|
|
69
|
+
const ref = obj(o, 'assetRef');
|
|
70
|
+
if (!ref)
|
|
71
|
+
return undefined;
|
|
72
|
+
const key = str(ref, 'key');
|
|
73
|
+
if (!key)
|
|
74
|
+
return undefined;
|
|
75
|
+
const version = str(ref, 'version');
|
|
76
|
+
return version ? `${key}@${version}` : key;
|
|
77
|
+
}
|
|
78
|
+
export function assetRefKey(o) {
|
|
79
|
+
return str(obj(o, 'assetRef'), 'key');
|
|
80
|
+
}
|
|
81
|
+
const MAX_INLINE_BYTES = 96;
|
|
82
|
+
/** JSON-safe projection: BigInt → string, bytes → hex (truncated), everything else untouched. */
|
|
83
|
+
export function jsonSafe(value) {
|
|
84
|
+
if (value === undefined)
|
|
85
|
+
return undefined;
|
|
86
|
+
if (typeof value === 'bigint')
|
|
87
|
+
return value.toString();
|
|
88
|
+
if (value instanceof Uint8Array) {
|
|
89
|
+
if (value.length <= MAX_INLINE_BYTES)
|
|
90
|
+
return Buffer.from(value).toString('hex');
|
|
91
|
+
return {
|
|
92
|
+
hex: Buffer.from(value.subarray(0, MAX_INLINE_BYTES)).toString('hex'),
|
|
93
|
+
byteLength: value.length,
|
|
94
|
+
truncated: true,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
if (Array.isArray(value))
|
|
98
|
+
return value.map((v) => jsonSafe(v));
|
|
99
|
+
if (typeof value === 'object') {
|
|
100
|
+
const out = {};
|
|
101
|
+
for (const [k, v] of Object.entries(value))
|
|
102
|
+
out[k] = jsonSafe(v);
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Components (SYMBOL) and instances (fig-reading-solution.md §9.7).
|
|
3
|
+
*
|
|
4
|
+
* Two addressing quirks matter here and are not obvious from the schema:
|
|
5
|
+
* 1. `symbolData.symbolOverrides[].guidPath.guids` addresses descendants of the component by
|
|
6
|
+
* their override identity — `overrideKey` when present, else guid — one segment per
|
|
7
|
+
* instance-nesting level. See `overrideIdentity` and `resolveOverridePath` (model/instance).
|
|
8
|
+
* 2. A variant member SYMBOL repeats its component-set's property ids with no name; the named
|
|
9
|
+
* definitions live on the component set (a FRAME with `isStateGroup`).
|
|
10
|
+
*/
|
|
11
|
+
import type { NodeChange } from '../fig/parse.js';
|
|
12
|
+
import type { FileIndex } from './index.js';
|
|
13
|
+
import { type TreeNode } from './tree.js';
|
|
14
|
+
export interface ComponentView {
|
|
15
|
+
guid: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
key?: string;
|
|
19
|
+
page?: string;
|
|
20
|
+
path?: string;
|
|
21
|
+
size?: string;
|
|
22
|
+
/** Name of the component set when this SYMBOL is one of its variants. */
|
|
23
|
+
variantOf?: string;
|
|
24
|
+
variantOfGuid?: string;
|
|
25
|
+
propDefs?: {
|
|
26
|
+
name?: string;
|
|
27
|
+
type?: string;
|
|
28
|
+
default?: unknown;
|
|
29
|
+
}[];
|
|
30
|
+
instances: number;
|
|
31
|
+
}
|
|
32
|
+
export declare function componentView(idx: FileIndex, t: TreeNode): ComponentView;
|
|
33
|
+
export interface OverrideView {
|
|
34
|
+
/** overrideKey path from the component root down to the overridden descendant. */
|
|
35
|
+
path: string[];
|
|
36
|
+
targetGuid?: string;
|
|
37
|
+
targetName?: string;
|
|
38
|
+
targetType?: string;
|
|
39
|
+
fields: Record<string, unknown>;
|
|
40
|
+
/** Field names present in the record that this mapper did not translate. */
|
|
41
|
+
otherFields?: string[];
|
|
42
|
+
}
|
|
43
|
+
/** Map one partial NodeChange (an override or a derived record) to a compact field set. */
|
|
44
|
+
export declare function overrideFields(idx: FileIndex, partial: NodeChange): {
|
|
45
|
+
fields: Record<string, unknown>;
|
|
46
|
+
otherFields: string[];
|
|
47
|
+
};
|
|
48
|
+
export declare function instanceOverrides(idx: FileIndex, t: TreeNode, field: string): OverrideView[];
|
|
49
|
+
export interface InstanceView {
|
|
50
|
+
guid: string;
|
|
51
|
+
name?: string;
|
|
52
|
+
page?: string;
|
|
53
|
+
path?: string;
|
|
54
|
+
symbol?: {
|
|
55
|
+
guid: string;
|
|
56
|
+
name?: string;
|
|
57
|
+
page?: string;
|
|
58
|
+
variantOf?: string;
|
|
59
|
+
inFile: boolean;
|
|
60
|
+
};
|
|
61
|
+
propAssignments?: {
|
|
62
|
+
defID: string;
|
|
63
|
+
name?: string;
|
|
64
|
+
type?: string;
|
|
65
|
+
value?: unknown;
|
|
66
|
+
}[];
|
|
67
|
+
overrides?: OverrideView[];
|
|
68
|
+
derivedRecords?: number;
|
|
69
|
+
scaleFactor?: number;
|
|
70
|
+
}
|
|
71
|
+
export declare function instanceView(idx: FileIndex, t: TreeNode): InstanceView;
|
|
72
|
+
//# sourceMappingURL=components.d.ts.map
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { guidKey, breadcrumb } from './tree.js';
|
|
2
|
+
import { resolveOverridePath } from './instance.js';
|
|
3
|
+
import { bool, colorHex, compact, jsonSafe, num, obj, objArr, r2, str } from './access.js';
|
|
4
|
+
import { effectView, paintView, textBasics } from './summarize.js';
|
|
5
|
+
import { describeVariableData } from './variables.js';
|
|
6
|
+
/** Fields present on every override/derived record that are addressing, not content. */
|
|
7
|
+
const OVERRIDE_META = new Set(['guidPath', 'overrideLevel', 'guid', 'phase', 'parentIndex']);
|
|
8
|
+
function componentPropValue(idx, value) {
|
|
9
|
+
if (!value)
|
|
10
|
+
return undefined;
|
|
11
|
+
const text = obj(value, 'textValue');
|
|
12
|
+
if (text)
|
|
13
|
+
return str(text, 'characters');
|
|
14
|
+
const b = bool(value, 'boolValue');
|
|
15
|
+
if (b !== undefined)
|
|
16
|
+
return b;
|
|
17
|
+
const f = num(value, 'floatValue');
|
|
18
|
+
if (f !== undefined)
|
|
19
|
+
return r2(f);
|
|
20
|
+
const guid = guidKey(obj(value, 'guidValue'));
|
|
21
|
+
if (guid) {
|
|
22
|
+
const target = idx.node(guid);
|
|
23
|
+
return target ? { guid, name: str(target.node, 'name') } : { guid };
|
|
24
|
+
}
|
|
25
|
+
const color = obj(value, 'colorValue');
|
|
26
|
+
if (color)
|
|
27
|
+
return colorHex(color);
|
|
28
|
+
const keys = Object.keys(value);
|
|
29
|
+
return keys.length ? { [keys[0]]: jsonSafe(value[keys[0]]) } : undefined;
|
|
30
|
+
}
|
|
31
|
+
export function componentView(idx, t) {
|
|
32
|
+
const node = t.node;
|
|
33
|
+
const size = obj(node, 'size');
|
|
34
|
+
const parent = t.parent;
|
|
35
|
+
const isVariant = parent !== undefined && bool(parent.node, 'isStateGroup') === true;
|
|
36
|
+
const propDefs = objArr(node, 'componentPropDefs')
|
|
37
|
+
.concat(isVariant ? objArr(parent.node, 'componentPropDefs') : [])
|
|
38
|
+
.map((d) => {
|
|
39
|
+
const id = guidKey(obj(d, 'id'));
|
|
40
|
+
const resolved = id ? idx.propDefs().get(id) : undefined;
|
|
41
|
+
return compact({
|
|
42
|
+
name: str(d, 'name') ?? resolved?.name,
|
|
43
|
+
type: str(d, 'type') ?? resolved?.type,
|
|
44
|
+
default: componentPropValue(idx, obj(d, 'initialValue')),
|
|
45
|
+
});
|
|
46
|
+
})
|
|
47
|
+
.filter((d) => d.name !== undefined);
|
|
48
|
+
const w = num(size, 'x');
|
|
49
|
+
const h = num(size, 'y');
|
|
50
|
+
return compact({
|
|
51
|
+
guid: t.key,
|
|
52
|
+
name: str(node, 'name'),
|
|
53
|
+
description: str(node, 'symbolDescription'),
|
|
54
|
+
key: str(node, 'componentKey') ?? str(node, 'key'),
|
|
55
|
+
page: t.page ? str(t.page.node, 'name') : undefined,
|
|
56
|
+
path: breadcrumb(t) || undefined,
|
|
57
|
+
size: w !== undefined && h !== undefined ? `${r2(w)}x${r2(h)}` : undefined,
|
|
58
|
+
variantOf: isVariant ? str(parent.node, 'name') : undefined,
|
|
59
|
+
variantOfGuid: isVariant ? parent.key : undefined,
|
|
60
|
+
propDefs: propDefs.length ? propDefs : undefined,
|
|
61
|
+
instances: idx.instanceCounts.get(t.key) ?? 0,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/** Map one partial NodeChange (an override or a derived record) to a compact field set. */
|
|
65
|
+
export function overrideFields(idx, partial) {
|
|
66
|
+
const size = obj(partial, 'size');
|
|
67
|
+
const transform = obj(partial, 'transform');
|
|
68
|
+
const fills = objArr(partial, 'fillPaints');
|
|
69
|
+
const strokes = objArr(partial, 'strokePaints');
|
|
70
|
+
const effects = objArr(partial, 'effects');
|
|
71
|
+
const w = num(size, 'x');
|
|
72
|
+
const h = num(size, 'y');
|
|
73
|
+
const fields = compact({
|
|
74
|
+
size: w !== undefined && h !== undefined ? `${r2(w)}x${r2(h)}` : undefined,
|
|
75
|
+
x: r2(num(transform, 'm02')),
|
|
76
|
+
y: r2(num(transform, 'm12')),
|
|
77
|
+
visible: bool(partial, 'visible'),
|
|
78
|
+
opacity: r2(num(partial, 'opacity')),
|
|
79
|
+
// An empty fills array is itself an override ("remove all fills"), so report the count.
|
|
80
|
+
fills: 'fillPaints' in partial ? fills.map((p) => paintView(idx, p)) : undefined,
|
|
81
|
+
fillsCleared: 'fillPaints' in partial && fills.length === 0 ? true : undefined,
|
|
82
|
+
strokes: 'strokePaints' in partial ? strokes.map((p) => paintView(idx, p)) : undefined,
|
|
83
|
+
strokesCleared: 'strokePaints' in partial && strokes.length === 0 ? true : undefined,
|
|
84
|
+
effects: effects.length ? effects.map((e) => effectView(idx, e)) : undefined,
|
|
85
|
+
text: textBasics(partial),
|
|
86
|
+
cornerRadius: r2(num(partial, 'cornerRadius')),
|
|
87
|
+
symbolID: guidKey(obj(obj(partial, 'symbolData'), 'symbolID')),
|
|
88
|
+
});
|
|
89
|
+
const known = new Set([
|
|
90
|
+
'size', 'transform', 'visible', 'opacity', 'fillPaints', 'strokePaints', 'effects',
|
|
91
|
+
'textData', 'fontName', 'fontSize', 'lineHeight', 'letterSpacing', 'cornerRadius',
|
|
92
|
+
'symbolData',
|
|
93
|
+
]);
|
|
94
|
+
const otherFields = Object.keys(partial).filter((k) => !known.has(k) && !OVERRIDE_META.has(k));
|
|
95
|
+
return { fields, otherFields };
|
|
96
|
+
}
|
|
97
|
+
export function instanceOverrides(idx, t, field) {
|
|
98
|
+
const symbolData = field === 'symbolOverrides' ? obj(t.node, 'symbolData') : t.node;
|
|
99
|
+
const records = objArr(symbolData, field);
|
|
100
|
+
return records.map((record) => {
|
|
101
|
+
const path = objArr(obj(record, 'guidPath'), 'guids')
|
|
102
|
+
.map((g) => guidKey(g))
|
|
103
|
+
.filter((g) => g !== undefined);
|
|
104
|
+
const last = path[path.length - 1];
|
|
105
|
+
// When the walk fails (a library symbol that is not in the file) the best that can be done
|
|
106
|
+
// is any node carrying the last key.
|
|
107
|
+
const target = resolveOverridePath(idx, t, path) ??
|
|
108
|
+
(last ? idx.byOverrideIdentity().get(last)?.[0] : undefined);
|
|
109
|
+
const { fields, otherFields } = overrideFields(idx, record);
|
|
110
|
+
return compact({
|
|
111
|
+
path,
|
|
112
|
+
targetGuid: target?.key,
|
|
113
|
+
targetName: target ? str(target.node, 'name') : undefined,
|
|
114
|
+
targetType: target ? str(target.node, 'type') : undefined,
|
|
115
|
+
fields,
|
|
116
|
+
otherFields: otherFields.length ? otherFields : undefined,
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
export function instanceView(idx, t) {
|
|
121
|
+
const symbolData = obj(t.node, 'symbolData');
|
|
122
|
+
const symbolId = guidKey(obj(symbolData, 'symbolID'));
|
|
123
|
+
const symbol = symbolId ? idx.node(symbolId) : undefined;
|
|
124
|
+
const symbolParent = symbol?.parent;
|
|
125
|
+
const assignments = objArr(t.node, 'componentPropAssignments').map((a) => {
|
|
126
|
+
const defID = guidKey(obj(a, 'defID')) ?? '?';
|
|
127
|
+
const def = idx.propDefs().get(defID);
|
|
128
|
+
const value = componentPropValue(idx, obj(a, 'value'));
|
|
129
|
+
return compact({
|
|
130
|
+
defID,
|
|
131
|
+
name: def?.name,
|
|
132
|
+
type: def?.type,
|
|
133
|
+
value: value ?? describeVariableData(idx, obj(a, 'varValue')),
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
return compact({
|
|
137
|
+
guid: t.key,
|
|
138
|
+
name: str(t.node, 'name'),
|
|
139
|
+
page: t.page ? str(t.page.node, 'name') : undefined,
|
|
140
|
+
path: breadcrumb(t) || undefined,
|
|
141
|
+
symbol: symbolId
|
|
142
|
+
? compact({
|
|
143
|
+
guid: symbolId,
|
|
144
|
+
name: symbol ? str(symbol.node, 'name') : undefined,
|
|
145
|
+
page: symbol?.page ? str(symbol.page.node, 'name') : undefined,
|
|
146
|
+
variantOf: symbolParent && bool(symbolParent.node, 'isStateGroup')
|
|
147
|
+
? str(symbolParent.node, 'name')
|
|
148
|
+
: undefined,
|
|
149
|
+
inFile: symbol !== undefined,
|
|
150
|
+
})
|
|
151
|
+
: undefined,
|
|
152
|
+
propAssignments: assignments.length ? assignments : undefined,
|
|
153
|
+
overrides: instanceOverrides(idx, t, 'symbolOverrides'),
|
|
154
|
+
derivedRecords: objArr(t.node, 'derivedSymbolData').length || undefined,
|
|
155
|
+
scaleFactor: num(symbolData, 'uniformScaleFactor') === 1 ? undefined : num(symbolData, 'uniformScaleFactor'),
|
|
156
|
+
});
|
|
157
|
+
}
|