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,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FileIndex — every lookup the tools need, built once per parsed file.
|
|
3
|
+
*
|
|
4
|
+
* Everything cheap is built eagerly (one pass over 116k nodes); the full-text search index is
|
|
5
|
+
* built on first use because it duplicates every name and text run in lowercase.
|
|
6
|
+
*/
|
|
7
|
+
import type { ParsedFig, NodeChange } from '../fig/parse.js';
|
|
8
|
+
import { type Tree, type TreeNode } from './tree.js';
|
|
9
|
+
import type { KiwiObject } from '../fig/kiwi.js';
|
|
10
|
+
export interface PropDefEntry {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly name?: string;
|
|
13
|
+
readonly type?: string;
|
|
14
|
+
/** The SYMBOL or component-set FRAME that declares the property. */
|
|
15
|
+
readonly owner: TreeNode;
|
|
16
|
+
readonly def: KiwiObject;
|
|
17
|
+
}
|
|
18
|
+
export interface SearchEntry {
|
|
19
|
+
readonly t: TreeNode;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly nameLower: string;
|
|
22
|
+
readonly textLower: string | undefined;
|
|
23
|
+
}
|
|
24
|
+
export declare class FileIndex {
|
|
25
|
+
readonly fig: ParsedFig;
|
|
26
|
+
readonly tree: Tree;
|
|
27
|
+
readonly typeCounts: Readonly<Record<string, number>>;
|
|
28
|
+
readonly pages: readonly TreeNode[];
|
|
29
|
+
readonly symbols: readonly TreeNode[];
|
|
30
|
+
readonly variables: readonly TreeNode[];
|
|
31
|
+
readonly variableSets: readonly TreeNode[];
|
|
32
|
+
/** Publishable `key` (a 40-hex asset key) → the local node that defines it. */
|
|
33
|
+
readonly byAssetKey: ReadonlyMap<string, TreeNode>;
|
|
34
|
+
/** SYMBOL guid → number of INSTANCE nodes pointing at it. */
|
|
35
|
+
readonly instanceCounts: ReadonlyMap<string, number>;
|
|
36
|
+
/** Lowercase hex hashes of every image referenced by a paint. */
|
|
37
|
+
readonly imageHashes: ReadonlySet<string>;
|
|
38
|
+
readonly textNodeCount: number;
|
|
39
|
+
private searchIndex;
|
|
40
|
+
private overrideIdentityIndex;
|
|
41
|
+
private propDefIndex;
|
|
42
|
+
constructor(fig: ParsedFig);
|
|
43
|
+
get root(): TreeNode | undefined;
|
|
44
|
+
get nodeCount(): number;
|
|
45
|
+
node(guid: string): TreeNode | undefined;
|
|
46
|
+
/** Resolve a `{assetRef:{key}}` reference to the local node defining it, when it is local. */
|
|
47
|
+
resolveAssetRef(ref: KiwiObject | undefined): TreeNode | undefined;
|
|
48
|
+
/** DFS pre-order slice of a subtree, so pagination and cursors are stable. */
|
|
49
|
+
subtreeRange(root: TreeNode): {
|
|
50
|
+
start: number;
|
|
51
|
+
end: number;
|
|
52
|
+
};
|
|
53
|
+
isDescendant(candidate: TreeNode, ancestor: TreeNode): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Override identity → the nodes carrying it. Instance override paths (`guidPath.guids`)
|
|
56
|
+
* address component descendants by `overrideIdentity`: the `overrideKey` when the node has
|
|
57
|
+
* one, else its guid. The same key appears on every duplicate of a component, so a key can map
|
|
58
|
+
* to several nodes; a guid maps to exactly one.
|
|
59
|
+
*/
|
|
60
|
+
byOverrideIdentity(): ReadonlyMap<string, readonly TreeNode[]>;
|
|
61
|
+
/**
|
|
62
|
+
* Component property definitions by defID. Named definitions live on standalone SYMBOLs and on
|
|
63
|
+
* component sets (FRAMEs with `isStateGroup`); variant member SYMBOLs repeat the same ids with
|
|
64
|
+
* no name, so named entries always win.
|
|
65
|
+
*/
|
|
66
|
+
propDefs(): ReadonlyMap<string, PropDefEntry>;
|
|
67
|
+
/** Built lazily: it duplicates every name and text body in lowercase. */
|
|
68
|
+
search(): readonly SearchEntry[];
|
|
69
|
+
}
|
|
70
|
+
/** Image hashes referenced by one node's paints, in fill-then-stroke order. */
|
|
71
|
+
export declare function nodeImageHashes(node: NodeChange): string[];
|
|
72
|
+
export type { TreeNode, Tree };
|
|
73
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { buildTree, guidKey, overrideIdentity } from './tree.js';
|
|
2
|
+
import { obj, objArr, str, bytes, hex, assetRefKey } from './access.js';
|
|
3
|
+
export class FileIndex {
|
|
4
|
+
fig;
|
|
5
|
+
tree;
|
|
6
|
+
typeCounts;
|
|
7
|
+
pages;
|
|
8
|
+
symbols;
|
|
9
|
+
variables;
|
|
10
|
+
variableSets;
|
|
11
|
+
/** Publishable `key` (a 40-hex asset key) → the local node that defines it. */
|
|
12
|
+
byAssetKey;
|
|
13
|
+
/** SYMBOL guid → number of INSTANCE nodes pointing at it. */
|
|
14
|
+
instanceCounts;
|
|
15
|
+
/** Lowercase hex hashes of every image referenced by a paint. */
|
|
16
|
+
imageHashes;
|
|
17
|
+
textNodeCount;
|
|
18
|
+
searchIndex;
|
|
19
|
+
overrideIdentityIndex;
|
|
20
|
+
propDefIndex;
|
|
21
|
+
constructor(fig) {
|
|
22
|
+
this.fig = fig;
|
|
23
|
+
this.tree = buildTree(fig.nodeChanges);
|
|
24
|
+
const typeCounts = {};
|
|
25
|
+
const pages = [];
|
|
26
|
+
const symbols = [];
|
|
27
|
+
const variables = [];
|
|
28
|
+
const variableSets = [];
|
|
29
|
+
const byAssetKey = new Map();
|
|
30
|
+
const instanceCounts = new Map();
|
|
31
|
+
const imageHashes = new Set();
|
|
32
|
+
let textNodeCount = 0;
|
|
33
|
+
for (const t of this.tree.ordered) {
|
|
34
|
+
const node = t.node;
|
|
35
|
+
const type = str(node, 'type') ?? 'UNKNOWN';
|
|
36
|
+
typeCounts[type] = (typeCounts[type] ?? 0) + 1;
|
|
37
|
+
switch (type) {
|
|
38
|
+
case 'CANVAS':
|
|
39
|
+
pages.push(t);
|
|
40
|
+
break;
|
|
41
|
+
case 'SYMBOL':
|
|
42
|
+
symbols.push(t);
|
|
43
|
+
break;
|
|
44
|
+
case 'VARIABLE':
|
|
45
|
+
variables.push(t);
|
|
46
|
+
break;
|
|
47
|
+
case 'VARIABLE_SET':
|
|
48
|
+
variableSets.push(t);
|
|
49
|
+
break;
|
|
50
|
+
case 'TEXT':
|
|
51
|
+
textNodeCount++;
|
|
52
|
+
break;
|
|
53
|
+
case 'INSTANCE': {
|
|
54
|
+
const symbolId = guidKey(obj(obj(node, 'symbolData'), 'symbolID'));
|
|
55
|
+
if (symbolId)
|
|
56
|
+
instanceCounts.set(symbolId, (instanceCounts.get(symbolId) ?? 0) + 1);
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
default:
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
const key = str(node, 'key');
|
|
63
|
+
if (key && !byAssetKey.has(key))
|
|
64
|
+
byAssetKey.set(key, t);
|
|
65
|
+
for (const paints of [objArr(node, 'fillPaints'), objArr(node, 'strokePaints')]) {
|
|
66
|
+
for (const paint of paints) {
|
|
67
|
+
const h = hex(bytes(obj(paint, 'image'), 'hash'));
|
|
68
|
+
if (h)
|
|
69
|
+
imageHashes.add(h);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
this.typeCounts = typeCounts;
|
|
74
|
+
this.pages = pages;
|
|
75
|
+
this.symbols = symbols;
|
|
76
|
+
this.variables = variables;
|
|
77
|
+
this.variableSets = variableSets;
|
|
78
|
+
this.byAssetKey = byAssetKey;
|
|
79
|
+
this.instanceCounts = instanceCounts;
|
|
80
|
+
this.imageHashes = imageHashes;
|
|
81
|
+
this.textNodeCount = textNodeCount;
|
|
82
|
+
}
|
|
83
|
+
get root() {
|
|
84
|
+
return this.tree.root;
|
|
85
|
+
}
|
|
86
|
+
get nodeCount() {
|
|
87
|
+
return this.tree.ordered.length;
|
|
88
|
+
}
|
|
89
|
+
node(guid) {
|
|
90
|
+
return this.tree.byKey.get(guid);
|
|
91
|
+
}
|
|
92
|
+
/** Resolve a `{assetRef:{key}}` reference to the local node defining it, when it is local. */
|
|
93
|
+
resolveAssetRef(ref) {
|
|
94
|
+
const key = assetRefKey(ref);
|
|
95
|
+
return key ? this.byAssetKey.get(key) : undefined;
|
|
96
|
+
}
|
|
97
|
+
/** DFS pre-order slice of a subtree, so pagination and cursors are stable. */
|
|
98
|
+
subtreeRange(root) {
|
|
99
|
+
let end = root.order + 1;
|
|
100
|
+
const ordered = this.tree.ordered;
|
|
101
|
+
while (end < ordered.length && ordered[end].depth > root.depth)
|
|
102
|
+
end++;
|
|
103
|
+
return { start: root.order, end };
|
|
104
|
+
}
|
|
105
|
+
isDescendant(candidate, ancestor) {
|
|
106
|
+
const { start, end } = this.subtreeRange(ancestor);
|
|
107
|
+
return candidate.order >= start && candidate.order < end;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Override identity → the nodes carrying it. Instance override paths (`guidPath.guids`)
|
|
111
|
+
* address component descendants by `overrideIdentity`: the `overrideKey` when the node has
|
|
112
|
+
* one, else its guid. The same key appears on every duplicate of a component, so a key can map
|
|
113
|
+
* to several nodes; a guid maps to exactly one.
|
|
114
|
+
*/
|
|
115
|
+
byOverrideIdentity() {
|
|
116
|
+
if (this.overrideIdentityIndex)
|
|
117
|
+
return this.overrideIdentityIndex;
|
|
118
|
+
const map = new Map();
|
|
119
|
+
for (const t of this.tree.ordered) {
|
|
120
|
+
const identity = overrideIdentity(t);
|
|
121
|
+
const list = map.get(identity);
|
|
122
|
+
if (list)
|
|
123
|
+
list.push(t);
|
|
124
|
+
else
|
|
125
|
+
map.set(identity, [t]);
|
|
126
|
+
}
|
|
127
|
+
this.overrideIdentityIndex = map;
|
|
128
|
+
return map;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Component property definitions by defID. Named definitions live on standalone SYMBOLs and on
|
|
132
|
+
* component sets (FRAMEs with `isStateGroup`); variant member SYMBOLs repeat the same ids with
|
|
133
|
+
* no name, so named entries always win.
|
|
134
|
+
*/
|
|
135
|
+
propDefs() {
|
|
136
|
+
if (this.propDefIndex)
|
|
137
|
+
return this.propDefIndex;
|
|
138
|
+
const map = new Map();
|
|
139
|
+
for (const t of this.tree.ordered) {
|
|
140
|
+
for (const def of objArr(t.node, 'componentPropDefs')) {
|
|
141
|
+
const id = guidKey(obj(def, 'id'));
|
|
142
|
+
if (!id)
|
|
143
|
+
continue;
|
|
144
|
+
const name = str(def, 'name');
|
|
145
|
+
const existing = map.get(id);
|
|
146
|
+
if (existing && (!name || existing.name))
|
|
147
|
+
continue;
|
|
148
|
+
map.set(id, { id, name, type: str(def, 'type'), owner: t, def });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
this.propDefIndex = map;
|
|
152
|
+
return map;
|
|
153
|
+
}
|
|
154
|
+
/** Built lazily: it duplicates every name and text body in lowercase. */
|
|
155
|
+
search() {
|
|
156
|
+
if (this.searchIndex)
|
|
157
|
+
return this.searchIndex;
|
|
158
|
+
const entries = [];
|
|
159
|
+
for (const t of this.tree.ordered) {
|
|
160
|
+
const name = str(t.node, 'name') ?? '';
|
|
161
|
+
const characters = str(obj(t.node, 'textData'), 'characters');
|
|
162
|
+
entries.push({
|
|
163
|
+
t,
|
|
164
|
+
name,
|
|
165
|
+
nameLower: name.toLowerCase(),
|
|
166
|
+
textLower: characters ? characters.toLowerCase() : undefined,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
this.searchIndex = entries;
|
|
170
|
+
return entries;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** Image hashes referenced by one node's paints, in fill-then-stroke order. */
|
|
174
|
+
export function nodeImageHashes(node) {
|
|
175
|
+
const out = [];
|
|
176
|
+
for (const key of ['fillPaints', 'strokePaints']) {
|
|
177
|
+
for (const paint of objArr(node, key)) {
|
|
178
|
+
const h = hex(bytes(obj(paint, 'image'), 'hash'));
|
|
179
|
+
if (h && !out.includes(h))
|
|
180
|
+
out.push(h);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// Text runs can carry their own image fills.
|
|
184
|
+
for (const override of objArr(obj(node, 'textData'), 'styleOverrideTable')) {
|
|
185
|
+
for (const paint of objArr(override, 'fillPaints')) {
|
|
186
|
+
const h = hex(bytes(obj(paint, 'image'), 'hash'));
|
|
187
|
+
if (h && !out.includes(h))
|
|
188
|
+
out.push(h);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instance resolution — what an INSTANCE looks like once its records and properties are applied
|
|
3
|
+
* (render-implementation-plan.md F17, F18). Shared by the renderer and by `fig_instance`, so the
|
|
4
|
+
* two never disagree about which node a record addresses.
|
|
5
|
+
*
|
|
6
|
+
* An INSTANCE has NO children of its own — all 38 164 in the sample have zero — so its content
|
|
7
|
+
* is the SYMBOL it points at. Figma supplies two record sets on the instance, both addressed by
|
|
8
|
+
* `guidPath.guids`, a path of override identities (`overrideIdentity`: the node's `overrideKey`,
|
|
9
|
+
* else its guid):
|
|
10
|
+
*
|
|
11
|
+
* `symbolData.symbolOverrides` what the user changed (fillPaints, visible, textData, size, …)
|
|
12
|
+
* `derivedSymbolData` what Figma recomputed as a result — the resolved `size`,
|
|
13
|
+
* `transform`, `fillGeometry`, `strokeGeometry` and
|
|
14
|
+
* `derivedTextData` for each affected descendant
|
|
15
|
+
*
|
|
16
|
+
* `guidPath` counts INSTANCE nesting levels, not node depth: a one-segment path addresses a node
|
|
17
|
+
* anywhere inside this instance's own symbol, a two-segment path `[a, b]` addresses the node `b`
|
|
18
|
+
* inside the nested instance `a`, and so on.
|
|
19
|
+
*
|
|
20
|
+
* A third input is not expressed as records at all (F18): `componentPropAssignments`, the values
|
|
21
|
+
* an instance gives the component's properties. A symbol descendant bound to a property through
|
|
22
|
+
* `componentPropRefs` takes its `visible`, its text or its symbol from the assignment. Not one of
|
|
23
|
+
* the sample's 29 877 BOOLEAN=false assignments is mirrored by a `visible:false` record, so the
|
|
24
|
+
* assignments have to be evaluated, not read back.
|
|
25
|
+
*/
|
|
26
|
+
import type { KiwiObject } from '../fig/kiwi.js';
|
|
27
|
+
import type { NodeChange } from '../fig/parse.js';
|
|
28
|
+
import type { FileIndex } from './index.js';
|
|
29
|
+
import { type TreeNode } from './tree.js';
|
|
30
|
+
/** Record map for one instance level: identity path (joined by '/') → partial NodeChange. */
|
|
31
|
+
export type OverrideRecords = ReadonlyMap<string, KiwiObject>;
|
|
32
|
+
/** Component-property assignments of one instance: defID → `{textValue|boolValue|guidValue}`. */
|
|
33
|
+
export type PropAssignments = ReadonlyMap<string, KiwiObject>;
|
|
34
|
+
export interface ResolvedInstance {
|
|
35
|
+
readonly symbol: TreeNode;
|
|
36
|
+
readonly records: OverrideRecords;
|
|
37
|
+
readonly props: PropAssignments;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* `{ ...base, ...patch }` minus the addressing fields.
|
|
41
|
+
*
|
|
42
|
+
* Figma re-derives geometry only for nodes that paint something (F20): of the sample's 22 097
|
|
43
|
+
* resized painted nodes, 18 lack it. A record that resizes a node without supplying geometry
|
|
44
|
+
* therefore leaves the base geometry describing the OLD size — the 1240x180 symbol behind a
|
|
45
|
+
* 1052x503 panel — so it is dropped and the exporter rebuilds the box from `size` (§4.14).
|
|
46
|
+
*/
|
|
47
|
+
export declare function mergeNode(base: NodeChange, patch: KiwiObject | undefined): NodeChange;
|
|
48
|
+
/**
|
|
49
|
+
* The symbol, the record map and the property assignments for one instance, taken from the node
|
|
50
|
+
* AS SEEN in its context: inside another instance that is the merged node, whose records may
|
|
51
|
+
* reassign this instance's properties or swap its symbol (`overriddenSymbolID`).
|
|
52
|
+
*
|
|
53
|
+
* `inherited` carries records handed down from an enclosing instance, already stripped of the
|
|
54
|
+
* segment that addressed this one. The outer instance wins, because an override applied at the
|
|
55
|
+
* outer level is the more specific edit. `outerProps` are the enclosing instance's assignments,
|
|
56
|
+
* which an assignment of this instance may point at instead of carrying a value (PROP_REF —
|
|
57
|
+
* none in the sample, so this path is untested against real data).
|
|
58
|
+
*/
|
|
59
|
+
export declare function resolveInstance(index: FileIndex, node: NodeChange, inherited?: OverrideRecords, outerProps?: PropAssignments): ResolvedInstance | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* The records an enclosing instance contributes to a nested one: every entry whose path starts
|
|
62
|
+
* with the nested instance's own identity, with that segment removed.
|
|
63
|
+
*/
|
|
64
|
+
export declare function descend(records: OverrideRecords, key: string | undefined): OverrideRecords | undefined;
|
|
65
|
+
export interface AppliedProps {
|
|
66
|
+
readonly node: NodeChange;
|
|
67
|
+
/** True when a text assignment changed the characters but no record supplied outlines for it. */
|
|
68
|
+
readonly staleOutlines: boolean;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* F18 — a symbol descendant bound to a component property (`componentPropRefs`) takes the value
|
|
72
|
+
* the instance assigned. Without an assignment the symbol's own state already IS the default: in
|
|
73
|
+
* the sample every one of the 108 VISIBLE bindings with a known default matches it.
|
|
74
|
+
*
|
|
75
|
+
* `outlinesFromRecord` says whether the enclosing instance's records supplied `derivedTextData`
|
|
76
|
+
* for this node. A text assignment without them would be drawn with the SYMBOL's glyphs — the
|
|
77
|
+
* wrong words — so the caller reports it instead.
|
|
78
|
+
*/
|
|
79
|
+
export declare function applyProps(node: NodeChange, props: PropAssignments, outlinesFromRecord: boolean): AppliedProps;
|
|
80
|
+
/**
|
|
81
|
+
* The node a record path addresses, walked exactly the way the renderer expands the instance:
|
|
82
|
+
* each segment names a node inside the current symbol by identity, and a segment that lands on a
|
|
83
|
+
* nested INSTANCE continues inside the symbol that nested instance resolves to — after the
|
|
84
|
+
* enclosing level's records and property assignments, which may have swapped it.
|
|
85
|
+
*/
|
|
86
|
+
export declare function resolveOverridePath(index: FileIndex, instance: TreeNode, path: readonly string[]): TreeNode | undefined;
|
|
87
|
+
//# sourceMappingURL=instance.d.ts.map
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { guidKey, overrideIdentity } from './tree.js';
|
|
2
|
+
import { bool, num, obj, objArr, str } from './access.js';
|
|
3
|
+
/** Addressing fields that must never be merged into a node as content. */
|
|
4
|
+
const OVERRIDE_META = new Set(['guidPath', 'overrideLevel', 'guid', 'phase', 'parentIndex']);
|
|
5
|
+
function pathOf(record) {
|
|
6
|
+
const guids = objArr(obj(record, 'guidPath'), 'guids')
|
|
7
|
+
.map((g) => guidKey(g))
|
|
8
|
+
.filter((g) => g !== undefined);
|
|
9
|
+
return guids.length ? guids.join('/') : undefined;
|
|
10
|
+
}
|
|
11
|
+
function defIdOf(assignment) {
|
|
12
|
+
return guidKey(obj(assignment, 'defID'));
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Property assignments merge per definition: a record that reassigns a nested instance's
|
|
16
|
+
* properties lists only the ones it changes (1 251 of the sample's 2 591 such records are
|
|
17
|
+
* partial), so replacing the array would silently reset the others to the component's defaults.
|
|
18
|
+
*/
|
|
19
|
+
function mergeAssignments(base, patch) {
|
|
20
|
+
const out = new Map();
|
|
21
|
+
for (const a of Array.isArray(base) ? base : []) {
|
|
22
|
+
const id = defIdOf(a);
|
|
23
|
+
if (id)
|
|
24
|
+
out.set(id, a);
|
|
25
|
+
}
|
|
26
|
+
for (const a of Array.isArray(patch) ? patch : []) {
|
|
27
|
+
const id = defIdOf(a);
|
|
28
|
+
if (id)
|
|
29
|
+
out.set(id, a);
|
|
30
|
+
}
|
|
31
|
+
return [...out.values()];
|
|
32
|
+
}
|
|
33
|
+
function sizeDiffers(a, b) {
|
|
34
|
+
if (!a || !b)
|
|
35
|
+
return false;
|
|
36
|
+
return (Math.abs((num(a, 'x') ?? 0) - (num(b, 'x') ?? 0)) > 1e-3 ||
|
|
37
|
+
Math.abs((num(a, 'y') ?? 0) - (num(b, 'y') ?? 0)) > 1e-3);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* `{ ...base, ...patch }` minus the addressing fields.
|
|
41
|
+
*
|
|
42
|
+
* Figma re-derives geometry only for nodes that paint something (F20): of the sample's 22 097
|
|
43
|
+
* resized painted nodes, 18 lack it. A record that resizes a node without supplying geometry
|
|
44
|
+
* therefore leaves the base geometry describing the OLD size — the 1240x180 symbol behind a
|
|
45
|
+
* 1052x503 panel — so it is dropped and the exporter rebuilds the box from `size` (§4.14).
|
|
46
|
+
*/
|
|
47
|
+
export function mergeNode(base, patch) {
|
|
48
|
+
if (!patch)
|
|
49
|
+
return base;
|
|
50
|
+
const out = { ...base };
|
|
51
|
+
for (const [k, v] of Object.entries(patch)) {
|
|
52
|
+
if (OVERRIDE_META.has(k))
|
|
53
|
+
continue;
|
|
54
|
+
out[k] = k === 'componentPropAssignments' ? mergeAssignments(base[k], v) : v;
|
|
55
|
+
}
|
|
56
|
+
if (sizeDiffers(obj(base, 'size'), obj(patch, 'size'))) {
|
|
57
|
+
if (patch['fillGeometry'] === undefined)
|
|
58
|
+
delete out['fillGeometry'];
|
|
59
|
+
if (patch['strokeGeometry'] === undefined)
|
|
60
|
+
delete out['strokeGeometry'];
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
function collect(target, records) {
|
|
65
|
+
for (const record of records) {
|
|
66
|
+
const path = pathOf(record);
|
|
67
|
+
if (!path)
|
|
68
|
+
continue;
|
|
69
|
+
const existing = target.get(path);
|
|
70
|
+
target.set(path, existing ? mergeNode(existing, record) : record);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The symbol, the record map and the property assignments for one instance, taken from the node
|
|
75
|
+
* AS SEEN in its context: inside another instance that is the merged node, whose records may
|
|
76
|
+
* reassign this instance's properties or swap its symbol (`overriddenSymbolID`).
|
|
77
|
+
*
|
|
78
|
+
* `inherited` carries records handed down from an enclosing instance, already stripped of the
|
|
79
|
+
* segment that addressed this one. The outer instance wins, because an override applied at the
|
|
80
|
+
* outer level is the more specific edit. `outerProps` are the enclosing instance's assignments,
|
|
81
|
+
* which an assignment of this instance may point at instead of carrying a value (PROP_REF —
|
|
82
|
+
* none in the sample, so this path is untested against real data).
|
|
83
|
+
*/
|
|
84
|
+
export function resolveInstance(index, node, inherited, outerProps) {
|
|
85
|
+
const symbolData = obj(node, 'symbolData');
|
|
86
|
+
const symbolId = guidKey(obj(node, 'overriddenSymbolID')) ??
|
|
87
|
+
guidKey(obj(symbolData, 'symbolID'));
|
|
88
|
+
if (!symbolId)
|
|
89
|
+
return undefined;
|
|
90
|
+
const symbol = index.node(symbolId);
|
|
91
|
+
if (!symbol)
|
|
92
|
+
return undefined;
|
|
93
|
+
const records = new Map();
|
|
94
|
+
// Order matters: what the user set, then what Figma recomputed, then the enclosing instance.
|
|
95
|
+
collect(records, objArr(symbolData, 'symbolOverrides'));
|
|
96
|
+
collect(records, objArr(node, 'derivedSymbolData'));
|
|
97
|
+
if (inherited) {
|
|
98
|
+
for (const [path, patch] of inherited) {
|
|
99
|
+
const existing = records.get(path);
|
|
100
|
+
records.set(path, existing ? mergeNode(existing, patch) : patch);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const props = new Map();
|
|
104
|
+
for (const a of objArr(node, 'componentPropAssignments')) {
|
|
105
|
+
const id = defIdOf(a);
|
|
106
|
+
if (!id)
|
|
107
|
+
continue;
|
|
108
|
+
const ref = obj(obj(obj(obj(a, 'varValue'), 'value'), 'propRefValue'), 'defId');
|
|
109
|
+
const value = obj(a, 'value') ?? (ref ? outerProps?.get(guidKey(ref) ?? '') : undefined);
|
|
110
|
+
if (value)
|
|
111
|
+
props.set(id, value);
|
|
112
|
+
}
|
|
113
|
+
return { symbol, records, props };
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The records an enclosing instance contributes to a nested one: every entry whose path starts
|
|
117
|
+
* with the nested instance's own identity, with that segment removed.
|
|
118
|
+
*/
|
|
119
|
+
export function descend(records, key) {
|
|
120
|
+
if (!key)
|
|
121
|
+
return undefined;
|
|
122
|
+
const prefix = `${key}/`;
|
|
123
|
+
let out;
|
|
124
|
+
for (const [path, patch] of records) {
|
|
125
|
+
if (!path.startsWith(prefix))
|
|
126
|
+
continue;
|
|
127
|
+
(out ??= new Map()).set(path.slice(prefix.length), patch);
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* F18 — a symbol descendant bound to a component property (`componentPropRefs`) takes the value
|
|
133
|
+
* the instance assigned. Without an assignment the symbol's own state already IS the default: in
|
|
134
|
+
* the sample every one of the 108 VISIBLE bindings with a known default matches it.
|
|
135
|
+
*
|
|
136
|
+
* `outlinesFromRecord` says whether the enclosing instance's records supplied `derivedTextData`
|
|
137
|
+
* for this node. A text assignment without them would be drawn with the SYMBOL's glyphs — the
|
|
138
|
+
* wrong words — so the caller reports it instead.
|
|
139
|
+
*/
|
|
140
|
+
export function applyProps(node, props, outlinesFromRecord) {
|
|
141
|
+
const refs = objArr(node, 'componentPropRefs');
|
|
142
|
+
if (refs.length === 0 || props.size === 0)
|
|
143
|
+
return { node, staleOutlines: false };
|
|
144
|
+
let out = node;
|
|
145
|
+
let staleOutlines = false;
|
|
146
|
+
for (const ref of refs) {
|
|
147
|
+
const id = guidKey(obj(ref, 'defID'));
|
|
148
|
+
const value = id ? props.get(id) : undefined;
|
|
149
|
+
if (!value)
|
|
150
|
+
continue;
|
|
151
|
+
switch (str(ref, 'componentPropNodeField')) {
|
|
152
|
+
case 'VISIBLE': {
|
|
153
|
+
const visible = bool(value, 'boolValue');
|
|
154
|
+
if (visible !== undefined)
|
|
155
|
+
out = { ...out, visible };
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case 'TEXT_DATA': {
|
|
159
|
+
const text = obj(value, 'textValue');
|
|
160
|
+
if (!text)
|
|
161
|
+
break;
|
|
162
|
+
const before = str(obj(out, 'textData'), 'characters');
|
|
163
|
+
if (str(text, 'characters') !== before && !outlinesFromRecord)
|
|
164
|
+
staleOutlines = true;
|
|
165
|
+
out = { ...out, textData: { ...(obj(out, 'textData') ?? {}), ...text } };
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case 'OVERRIDDEN_SYMBOL_ID': {
|
|
169
|
+
const guid = obj(value, 'guidValue');
|
|
170
|
+
if (guid)
|
|
171
|
+
out = { ...out, overriddenSymbolID: guid };
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
default:
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return { node: out, staleOutlines };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The node a record path addresses, walked exactly the way the renderer expands the instance:
|
|
182
|
+
* each segment names a node inside the current symbol by identity, and a segment that lands on a
|
|
183
|
+
* nested INSTANCE continues inside the symbol that nested instance resolves to — after the
|
|
184
|
+
* enclosing level's records and property assignments, which may have swapped it.
|
|
185
|
+
*/
|
|
186
|
+
export function resolveOverridePath(index, instance, path) {
|
|
187
|
+
let level = resolveInstance(index, instance.node);
|
|
188
|
+
let target;
|
|
189
|
+
for (let i = 0; i < path.length; i++) {
|
|
190
|
+
if (!level)
|
|
191
|
+
return undefined;
|
|
192
|
+
const segment = path[i];
|
|
193
|
+
const { symbol, records, props } = level;
|
|
194
|
+
target =
|
|
195
|
+
overrideIdentity(symbol) === segment
|
|
196
|
+
? symbol
|
|
197
|
+
: (index.byOverrideIdentity().get(segment) ?? []).find((c) => index.isDescendant(c, symbol));
|
|
198
|
+
if (!target)
|
|
199
|
+
return undefined;
|
|
200
|
+
if (i === path.length - 1)
|
|
201
|
+
break;
|
|
202
|
+
if (str(target.node, 'type') !== 'INSTANCE')
|
|
203
|
+
return undefined;
|
|
204
|
+
const effective = applyProps(mergeNode(target.node, records.get(segment)), props, true).node;
|
|
205
|
+
level = resolveInstance(index, effective, descend(records, segment), props);
|
|
206
|
+
}
|
|
207
|
+
return target;
|
|
208
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node projections at three detail levels (plan §4).
|
|
3
|
+
*
|
|
4
|
+
* - `summary` — one line of JSON, safe to return 300 of.
|
|
5
|
+
* - `full` — geometry, paints, strokes, effects, radii, auto-layout, text basics,
|
|
6
|
+
* component/variable links. Floats rounded, colors as hex, unset fields omitted.
|
|
7
|
+
* - `raw` — the decoded NodeChange verbatim (JSON-safe). The forward-compat escape hatch:
|
|
8
|
+
* anything these mappers do not understand is still reachable.
|
|
9
|
+
*/
|
|
10
|
+
import type { NodeChange } from '../fig/parse.js';
|
|
11
|
+
import type { KiwiObject } from '../fig/kiwi.js';
|
|
12
|
+
import type { FileIndex } from './index.js';
|
|
13
|
+
import { type TreeNode } from './tree.js';
|
|
14
|
+
export interface NodeSummary {
|
|
15
|
+
guid: string;
|
|
16
|
+
type: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
/** `"134x40"` — denser than `{x,y}` and still trivially parseable. */
|
|
19
|
+
size?: string;
|
|
20
|
+
children?: number;
|
|
21
|
+
page?: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function summarize(t: TreeNode, opts?: {
|
|
24
|
+
page?: boolean;
|
|
25
|
+
}): NodeSummary;
|
|
26
|
+
/** `[FRAME] 134x40 "Frame 39" (2:1339) x3` — ~4x denser than the JSON summary. */
|
|
27
|
+
export declare function outlineLine(t: TreeNode, depth: number): string;
|
|
28
|
+
/** Origin of the node in its page's coordinate space (§9.3: compose transforms down the tree). */
|
|
29
|
+
export declare function absoluteOrigin(t: TreeNode): {
|
|
30
|
+
x: number;
|
|
31
|
+
y: number;
|
|
32
|
+
} | undefined;
|
|
33
|
+
export declare function paintView(idx: FileIndex, p: KiwiObject): Record<string, unknown>;
|
|
34
|
+
export declare function effectView(idx: FileIndex, e: KiwiObject): Record<string, unknown>;
|
|
35
|
+
export interface Padding {
|
|
36
|
+
top: number;
|
|
37
|
+
right: number;
|
|
38
|
+
bottom: number;
|
|
39
|
+
left: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Figma stores the left/top pair in `stackHorizontalPadding`/`stackVerticalPadding` and only
|
|
43
|
+
* writes `stackPaddingRight`/`stackPaddingBottom` when the box is asymmetric (§9.5).
|
|
44
|
+
*/
|
|
45
|
+
export declare function paddingOf(node: NodeChange): Padding | undefined;
|
|
46
|
+
/** `{value, units}` rendered compactly: `"1.5"` (RAW multiplier), `"13px"`, `"150%"`. */
|
|
47
|
+
export declare function lengthString(o: KiwiObject | undefined): string | undefined;
|
|
48
|
+
export declare function textBasics(node: NodeChange): Record<string, unknown> | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Shared-style references.
|
|
51
|
+
*
|
|
52
|
+
* A style definition is stored as an ordinary (hidden) node parked on a canvas, carrying the
|
|
53
|
+
* publishable `key` that `styleIdFor*.assetRef.key` points at — text styles are TEXT nodes, fill
|
|
54
|
+
* styles are ROUNDED_RECTANGLE nodes. So a style used from the same file resolves to a real node
|
|
55
|
+
* and, with `resolve`, to the values it defines. Styles from other libraries stay opaque refs.
|
|
56
|
+
*/
|
|
57
|
+
export declare function styleRefsOf(idx: FileIndex, node: NodeChange, opts?: {
|
|
58
|
+
resolve?: boolean;
|
|
59
|
+
}): Record<string, unknown> | undefined;
|
|
60
|
+
/** Every `*Var` binding set directly on the node, plus its variable-consumption maps. */
|
|
61
|
+
export declare function variableBindingsOf(idx: FileIndex, node: NodeChange): Record<string, unknown> | undefined;
|
|
62
|
+
export interface NodeDetailOptions {
|
|
63
|
+
/** Include child summaries (default true at the tool boundary). */
|
|
64
|
+
children?: boolean;
|
|
65
|
+
maxChildren?: number;
|
|
66
|
+
}
|
|
67
|
+
export declare function nodeDetail(idx: FileIndex, t: TreeNode, opts?: NodeDetailOptions): Record<string, unknown>;
|
|
68
|
+
/**
|
|
69
|
+
* Typography of a node — or of a partial style-override record from a text run — reporting only
|
|
70
|
+
* the fields it actually sets, so a run's block shows exactly what differs from the base style.
|
|
71
|
+
*/
|
|
72
|
+
export declare function typography(idx: FileIndex, node: NodeChange, opts?: {
|
|
73
|
+
includeStyleRefs?: boolean;
|
|
74
|
+
}): Record<string, unknown>;
|
|
75
|
+
/**
|
|
76
|
+
* The resolved, code-generation-oriented style of one node: paints as hex, radii, typography and
|
|
77
|
+
* auto-layout expressed in CSS-flexbox terms. Shared-style and variable references are named
|
|
78
|
+
* whenever the target is defined in this same file.
|
|
79
|
+
*/
|
|
80
|
+
export declare function styleBlock(idx: FileIndex, t: TreeNode): Record<string, unknown>;
|
|
81
|
+
/** The decoded NodeChange verbatim, JSON-safe (BigInt -> string, bytes -> hex). */
|
|
82
|
+
export declare function nodeRaw(t: TreeNode): Record<string, unknown>;
|
|
83
|
+
//# sourceMappingURL=summarize.d.ts.map
|