figme-mcp 1.1.2 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +226 -223
- package/dist/mcp/create.js +3 -0
- package/dist/mcp/tools/context.d.ts +10 -0
- package/dist/mcp/tools/context.js +27 -0
- package/dist/mcp/tools/find.js +36 -6
- package/dist/mcp/tools/image.js +3 -2
- package/dist/mcp/tools/instance.js +6 -3
- package/dist/mcp/tools/node.js +8 -4
- package/dist/mcp/tools/render.js +2 -1
- package/dist/mcp/tools/style.js +5 -3
- package/dist/mcp/tools/text.js +29 -6
- package/dist/mcp/tools/tree.js +25 -11
- package/dist/model/bindings.d.ts +48 -0
- package/dist/model/bindings.js +129 -0
- package/dist/model/components.d.ts +8 -1
- package/dist/model/components.js +75 -30
- package/dist/model/index.d.ts +7 -0
- package/dist/model/index.js +19 -0
- package/dist/model/instance.d.ts +51 -4
- package/dist/model/instance.js +181 -5
- package/dist/model/scene.d.ts +50 -0
- package/dist/model/scene.js +192 -0
- package/dist/model/styles.d.ts +56 -0
- package/dist/model/styles.js +90 -0
- package/dist/model/summarize.d.ts +52 -5
- package/dist/model/summarize.js +255 -46
- package/dist/model/text.d.ts +5 -1
- package/dist/model/text.js +14 -4
- package/dist/model/tree.d.ts +12 -3
- package/dist/model/tree.js +24 -7
- package/dist/model/variables.d.ts +29 -1
- package/dist/model/variables.js +76 -1
- package/dist/render/export.d.ts +25 -0
- package/dist/render/export.js +118 -20
- package/dist/render/index.js +27 -4
- package/dist/render/instance.d.ts +1 -1
- package/dist/render/instance.js +1 -1
- package/dist/render/report.d.ts +1 -1
- package/dist/render/report.js +2 -0
- package/dist/render/style.d.ts +10 -15
- package/dist/render/style.js +7 -43
- package/package.json +3 -3
package/dist/model/index.d.ts
CHANGED
|
@@ -39,12 +39,19 @@ export declare class FileIndex {
|
|
|
39
39
|
private searchIndex;
|
|
40
40
|
private overrideIdentityIndex;
|
|
41
41
|
private propDefIndex;
|
|
42
|
+
private componentKeyIndex;
|
|
42
43
|
constructor(fig: ParsedFig);
|
|
43
44
|
get root(): TreeNode | undefined;
|
|
44
45
|
get nodeCount(): number;
|
|
45
46
|
node(guid: string): TreeNode | undefined;
|
|
46
47
|
/** Resolve a `{assetRef:{key}}` reference to the local node defining it, when it is local. */
|
|
47
48
|
resolveAssetRef(ref: KiwiObject | undefined): TreeNode | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* A component (SYMBOL) by its publish key — `componentKey`, else `key` — for a reference that
|
|
51
|
+
* names a component by `assetRef` instead of by guid (F26). Only components present in this
|
|
52
|
+
* file resolve. Built on first use.
|
|
53
|
+
*/
|
|
54
|
+
componentByKey(key: string): TreeNode | undefined;
|
|
48
55
|
/** DFS pre-order slice of a subtree, so pagination and cursors are stable. */
|
|
49
56
|
subtreeRange(root: TreeNode): {
|
|
50
57
|
start: number;
|
package/dist/model/index.js
CHANGED
|
@@ -18,6 +18,7 @@ export class FileIndex {
|
|
|
18
18
|
searchIndex;
|
|
19
19
|
overrideIdentityIndex;
|
|
20
20
|
propDefIndex;
|
|
21
|
+
componentKeyIndex;
|
|
21
22
|
constructor(fig) {
|
|
22
23
|
this.fig = fig;
|
|
23
24
|
this.tree = buildTree(fig.nodeChanges);
|
|
@@ -94,6 +95,24 @@ export class FileIndex {
|
|
|
94
95
|
const key = assetRefKey(ref);
|
|
95
96
|
return key ? this.byAssetKey.get(key) : undefined;
|
|
96
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* A component (SYMBOL) by its publish key — `componentKey`, else `key` — for a reference that
|
|
100
|
+
* names a component by `assetRef` instead of by guid (F26). Only components present in this
|
|
101
|
+
* file resolve. Built on first use.
|
|
102
|
+
*/
|
|
103
|
+
componentByKey(key) {
|
|
104
|
+
if (!this.componentKeyIndex) {
|
|
105
|
+
const map = new Map();
|
|
106
|
+
for (const t of this.symbols) {
|
|
107
|
+
for (const k of [str(t.node, 'componentKey'), str(t.node, 'key')]) {
|
|
108
|
+
if (k && !map.has(k))
|
|
109
|
+
map.set(k, t);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
this.componentKeyIndex = map;
|
|
113
|
+
}
|
|
114
|
+
return this.componentKeyIndex.get(key);
|
|
115
|
+
}
|
|
97
116
|
/** DFS pre-order slice of a subtree, so pagination and cursors are stable. */
|
|
98
117
|
subtreeRange(root) {
|
|
99
118
|
let end = root.order + 1;
|
package/dist/model/instance.d.ts
CHANGED
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
* an instance gives the component's properties. A symbol descendant bound to a property through
|
|
22
22
|
* `componentPropRefs` takes its `visible`, its text or its symbol from the assignment. Not one of
|
|
23
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.
|
|
24
|
+
* assignments have to be evaluated, not read back. An assignment keeps its value in `value` or,
|
|
25
|
+
* in files from current Figma, only in `varValue` (F26) — `assignmentValue` reads both.
|
|
25
26
|
*/
|
|
26
27
|
import type { KiwiObject } from '../fig/kiwi.js';
|
|
27
28
|
import type { NodeChange } from '../fig/parse.js';
|
|
@@ -29,15 +30,39 @@ import type { FileIndex } from './index.js';
|
|
|
29
30
|
import { type TreeNode } from './tree.js';
|
|
30
31
|
/** Record map for one instance level: identity path (joined by '/') → partial NodeChange. */
|
|
31
32
|
export type OverrideRecords = ReadonlyMap<string, KiwiObject>;
|
|
32
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* Component-property assignments of one instance: defID → the value in `ComponentPropValue`
|
|
35
|
+
* shape, `{textValue|boolValue|guidValue}`, whichever slot the file stored it in.
|
|
36
|
+
*/
|
|
33
37
|
export type PropAssignments = ReadonlyMap<string, KiwiObject>;
|
|
38
|
+
/** The index lookups the instance resolver needs. */
|
|
39
|
+
type ResolverIndex = Pick<FileIndex, 'node' | 'componentByKey'>;
|
|
34
40
|
export interface ResolvedInstance {
|
|
35
41
|
readonly symbol: TreeNode;
|
|
36
42
|
readonly records: OverrideRecords;
|
|
37
43
|
readonly props: PropAssignments;
|
|
38
44
|
}
|
|
39
45
|
/**
|
|
40
|
-
*
|
|
46
|
+
* A component property's value held as `VariableData` — the `varValue` of an assignment or of a
|
|
47
|
+
* property definition — in the `ComponentPropValue` shape the rest of the resolver reads, or
|
|
48
|
+
* undefined when it holds nothing this file can turn into one (F26).
|
|
49
|
+
*
|
|
50
|
+
* The names differ between the two shapes: a swap is `symbolIdValue { guid | assetRef }`, not
|
|
51
|
+
* `guidValue`, and text is `textDataValue`, because a `VariableAnyValue`'s `textValue` is a plain
|
|
52
|
+
* string. A `propRefValue` (a property of the enclosing instance) or an `alias` (a variable)
|
|
53
|
+
* carries no value of its own and is left to the caller.
|
|
54
|
+
*/
|
|
55
|
+
export declare function propValueOfVariableData(index: Pick<FileIndex, 'componentByKey'>, data: KiwiObject | undefined): KiwiObject | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* The value one assignment gives its property, in `ComponentPropValue` shape: the `value` slot
|
|
58
|
+
* when the file wrote it, else what `varValue` holds — a value of its own (F26), or a PROP_REF to
|
|
59
|
+
* a property of the enclosing instance, looked up in `outerProps`. Undefined when the assignment
|
|
60
|
+
* carries nothing usable, and the component's own default then stands.
|
|
61
|
+
*/
|
|
62
|
+
export declare function assignmentValue(index: ResolverIndex, assignment: KiwiObject, outerProps?: PropAssignments): KiwiObject | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* `{ ...base, ...patch }` minus the addressing fields, with property assignments merged per
|
|
65
|
+
* definition and binding maps per bound field.
|
|
41
66
|
*
|
|
42
67
|
* Figma re-derives geometry only for nodes that paint something (F20): of the sample's 22 097
|
|
43
68
|
* resized painted nodes, 18 lack it. A record that resizes a node without supplying geometry
|
|
@@ -56,7 +81,7 @@ export declare function mergeNode(base: NodeChange, patch: KiwiObject | undefine
|
|
|
56
81
|
* which an assignment of this instance may point at instead of carrying a value (PROP_REF —
|
|
57
82
|
* none in the sample, so this path is untested against real data).
|
|
58
83
|
*/
|
|
59
|
-
export declare function resolveInstance(index:
|
|
84
|
+
export declare function resolveInstance(index: ResolverIndex, node: NodeChange, inherited?: OverrideRecords, outerProps?: PropAssignments): ResolvedInstance | undefined;
|
|
60
85
|
/**
|
|
61
86
|
* The records an enclosing instance contributes to a nested one: every entry whose path starts
|
|
62
87
|
* with the nested instance's own identity, with that segment removed.
|
|
@@ -77,6 +102,27 @@ export interface AppliedProps {
|
|
|
77
102
|
* wrong words — so the caller reports it instead.
|
|
78
103
|
*/
|
|
79
104
|
export declare function applyProps(node: NodeChange, props: PropAssignments, outlinesFromRecord: boolean): AppliedProps;
|
|
105
|
+
/**
|
|
106
|
+
* F27 — the stored record of a nested instance whose component is swapped, as the swapped-in
|
|
107
|
+
* component shows it. The record is complete for the component it was written for, so it holds
|
|
108
|
+
* copies of that component root's own appearance — an icon's filled circle and round corners —
|
|
109
|
+
* that are not overrides. Swapped, the instance keeps only what it overrides: every unit of
|
|
110
|
+
* `ROOT_APPEARANCE` equal to the old root's takes the new root's value (or none, where the new
|
|
111
|
+
* root has none), and the outline, which traced the old component's shape, is dropped.
|
|
112
|
+
*/
|
|
113
|
+
export declare function swapAppearance(stored: NodeChange, from: NodeChange, to: NodeChange): NodeChange;
|
|
114
|
+
export interface ShownLayer extends AppliedProps {
|
|
115
|
+
/** True when a property value changed the layer. */
|
|
116
|
+
readonly propsApplied: boolean;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* A component layer as the instance that expands it shows it: the stored node merged with its
|
|
120
|
+
* record, with the instance's property values applied (F17, F18) — and, for a nested instance
|
|
121
|
+
* whose component that swaps, with the copies of the old component's root appearance taken out
|
|
122
|
+
* before the record goes on top (F27). Shared by the exporter and the scene, so the picture and
|
|
123
|
+
* the inspection tools see the same layer.
|
|
124
|
+
*/
|
|
125
|
+
export declare function showLayer(index: Pick<FileIndex, 'node'>, t: TreeNode, records: OverrideRecords, props: PropAssignments): ShownLayer;
|
|
80
126
|
/**
|
|
81
127
|
* The node a record path addresses, walked exactly the way the renderer expands the instance:
|
|
82
128
|
* each segment names a node inside the current symbol by identity, and a segment that lands on a
|
|
@@ -84,4 +130,5 @@ export declare function applyProps(node: NodeChange, props: PropAssignments, out
|
|
|
84
130
|
* enclosing level's records and property assignments, which may have swapped it.
|
|
85
131
|
*/
|
|
86
132
|
export declare function resolveOverridePath(index: FileIndex, instance: TreeNode, path: readonly string[]): TreeNode | undefined;
|
|
133
|
+
export {};
|
|
87
134
|
//# sourceMappingURL=instance.d.ts.map
|
package/dist/model/instance.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { guidKey, overrideIdentity } from './tree.js';
|
|
2
|
-
import { bool, num, obj, objArr, str } from './access.js';
|
|
2
|
+
import { assetRefKey, bool, num, obj, objArr, str } from './access.js';
|
|
3
3
|
/** Addressing fields that must never be merged into a node as content. */
|
|
4
4
|
const OVERRIDE_META = new Set(['guidPath', 'overrideLevel', 'guid', 'phase', 'parentIndex']);
|
|
5
5
|
function pathOf(record) {
|
|
@@ -11,6 +11,55 @@ function pathOf(record) {
|
|
|
11
11
|
function defIdOf(assignment) {
|
|
12
12
|
return guidKey(obj(assignment, 'defID'));
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* A component property's value held as `VariableData` — the `varValue` of an assignment or of a
|
|
16
|
+
* property definition — in the `ComponentPropValue` shape the rest of the resolver reads, or
|
|
17
|
+
* undefined when it holds nothing this file can turn into one (F26).
|
|
18
|
+
*
|
|
19
|
+
* The names differ between the two shapes: a swap is `symbolIdValue { guid | assetRef }`, not
|
|
20
|
+
* `guidValue`, and text is `textDataValue`, because a `VariableAnyValue`'s `textValue` is a plain
|
|
21
|
+
* string. A `propRefValue` (a property of the enclosing instance) or an `alias` (a variable)
|
|
22
|
+
* carries no value of its own and is left to the caller.
|
|
23
|
+
*/
|
|
24
|
+
export function propValueOfVariableData(index, data) {
|
|
25
|
+
const value = obj(data, 'value');
|
|
26
|
+
if (!value)
|
|
27
|
+
return undefined;
|
|
28
|
+
const flag = bool(value, 'boolValue');
|
|
29
|
+
if (flag !== undefined)
|
|
30
|
+
return { boolValue: flag };
|
|
31
|
+
const text = obj(value, 'textDataValue');
|
|
32
|
+
if (text)
|
|
33
|
+
return { textValue: text };
|
|
34
|
+
const symbol = obj(value, 'symbolIdValue');
|
|
35
|
+
if (symbol) {
|
|
36
|
+
const byGuid = obj(symbol, 'guid');
|
|
37
|
+
if (guidKey(byGuid))
|
|
38
|
+
return { guidValue: byGuid };
|
|
39
|
+
const key = assetRefKey(symbol);
|
|
40
|
+
const local = key ? index.componentByKey(key) : undefined;
|
|
41
|
+
const guid = local ? obj(local.node, 'guid') : undefined;
|
|
42
|
+
if (guid)
|
|
43
|
+
return { guidValue: guid };
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The value one assignment gives its property, in `ComponentPropValue` shape: the `value` slot
|
|
49
|
+
* when the file wrote it, else what `varValue` holds — a value of its own (F26), or a PROP_REF to
|
|
50
|
+
* a property of the enclosing instance, looked up in `outerProps`. Undefined when the assignment
|
|
51
|
+
* carries nothing usable, and the component's own default then stands.
|
|
52
|
+
*/
|
|
53
|
+
export function assignmentValue(index, assignment, outerProps) {
|
|
54
|
+
const value = obj(assignment, 'value');
|
|
55
|
+
if (value && Object.keys(value).length > 0)
|
|
56
|
+
return value;
|
|
57
|
+
const varValue = obj(assignment, 'varValue');
|
|
58
|
+
const ref = guidKey(obj(obj(obj(varValue, 'value'), 'propRefValue'), 'defId'));
|
|
59
|
+
if (ref)
|
|
60
|
+
return outerProps?.get(ref);
|
|
61
|
+
return propValueOfVariableData(index, varValue);
|
|
62
|
+
}
|
|
14
63
|
/**
|
|
15
64
|
* Property assignments merge per definition: a record that reassigns a nested instance's
|
|
16
65
|
* properties lists only the ones it changes (1 251 of the sample's 2 591 such records are
|
|
@@ -36,8 +85,36 @@ function sizeDiffers(a, b) {
|
|
|
36
85
|
return (Math.abs((num(a, 'x') ?? 0) - (num(b, 'x') ?? 0)) > 1e-3 ||
|
|
37
86
|
Math.abs((num(a, 'y') ?? 0) - (num(b, 'y') ?? 0)) > 1e-3);
|
|
38
87
|
}
|
|
88
|
+
/** The maps that bind a node's fields to variables and properties (model/bindings.ts). */
|
|
89
|
+
const BINDING_MAPS = new Set(['variableConsumptionMap', 'parameterConsumptionMap']);
|
|
39
90
|
/**
|
|
40
|
-
*
|
|
91
|
+
* F28 — binding maps merge per bound field. A record lists only the bindings it changes: one
|
|
92
|
+
* that restyles a text writes an EMPTY `variableConsumptionMap` beside its text style, and its
|
|
93
|
+
* layer stays bound — Figma's own layout of the instance still follows the binding — so replacing
|
|
94
|
+
* the map would unbind every field of every restyled layer. An entry that names a field but has
|
|
95
|
+
* no `variableData` unbinds that field.
|
|
96
|
+
*/
|
|
97
|
+
function mergeBindings(base, patch) {
|
|
98
|
+
const out = new Map();
|
|
99
|
+
for (const entry of objArr(base, 'entries')) {
|
|
100
|
+
const field = str(entry, 'variableField');
|
|
101
|
+
if (field)
|
|
102
|
+
out.set(field, entry);
|
|
103
|
+
}
|
|
104
|
+
for (const entry of objArr(patch, 'entries')) {
|
|
105
|
+
const field = str(entry, 'variableField');
|
|
106
|
+
if (!field)
|
|
107
|
+
continue;
|
|
108
|
+
if (obj(entry, 'variableData'))
|
|
109
|
+
out.set(field, entry);
|
|
110
|
+
else
|
|
111
|
+
out.delete(field);
|
|
112
|
+
}
|
|
113
|
+
return { ...patch, entries: [...out.values()] };
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* `{ ...base, ...patch }` minus the addressing fields, with property assignments merged per
|
|
117
|
+
* definition and binding maps per bound field.
|
|
41
118
|
*
|
|
42
119
|
* Figma re-derives geometry only for nodes that paint something (F20): of the sample's 22 097
|
|
43
120
|
* resized painted nodes, 18 lack it. A record that resizes a node without supplying geometry
|
|
@@ -51,7 +128,10 @@ export function mergeNode(base, patch) {
|
|
|
51
128
|
for (const [k, v] of Object.entries(patch)) {
|
|
52
129
|
if (OVERRIDE_META.has(k))
|
|
53
130
|
continue;
|
|
54
|
-
out[k] =
|
|
131
|
+
out[k] =
|
|
132
|
+
k === 'componentPropAssignments' ? mergeAssignments(base[k], v)
|
|
133
|
+
: BINDING_MAPS.has(k) ? mergeBindings(base[k], v)
|
|
134
|
+
: v;
|
|
55
135
|
}
|
|
56
136
|
if (sizeDiffers(obj(base, 'size'), obj(patch, 'size'))) {
|
|
57
137
|
if (patch['fillGeometry'] === undefined)
|
|
@@ -105,8 +185,7 @@ export function resolveInstance(index, node, inherited, outerProps) {
|
|
|
105
185
|
const id = defIdOf(a);
|
|
106
186
|
if (!id)
|
|
107
187
|
continue;
|
|
108
|
-
const
|
|
109
|
-
const value = obj(a, 'value') ?? (ref ? outerProps?.get(guidKey(ref) ?? '') : undefined);
|
|
188
|
+
const value = assignmentValue(index, a, outerProps);
|
|
110
189
|
if (value)
|
|
111
190
|
props.set(id, value);
|
|
112
191
|
}
|
|
@@ -177,6 +256,103 @@ export function applyProps(node, props, outlinesFromRecord) {
|
|
|
177
256
|
}
|
|
178
257
|
return { node: out, staleOutlines };
|
|
179
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* The fields of an instance's own box that come from its component's root unless the instance
|
|
261
|
+
* overrides them, in the units Figma writes together: paints with their style reference, the
|
|
262
|
+
* stroke-weight group, the corner group (F27).
|
|
263
|
+
*/
|
|
264
|
+
const ROOT_APPEARANCE = [
|
|
265
|
+
['fillPaints', 'styleIdForFill'],
|
|
266
|
+
['strokePaints', 'styleIdForStrokeFill'],
|
|
267
|
+
['strokeWeight', 'borderStrokeWeightsIndependent', 'borderTopWeight', 'borderRightWeight',
|
|
268
|
+
'borderBottomWeight', 'borderLeftWeight'],
|
|
269
|
+
['strokeAlign'],
|
|
270
|
+
['strokeJoin'],
|
|
271
|
+
['strokeCap'],
|
|
272
|
+
['dashPattern'],
|
|
273
|
+
['effects', 'styleIdForEffect'],
|
|
274
|
+
['cornerRadius', 'rectangleCornerRadiiIndependent', 'rectangleTopLeftCornerRadius',
|
|
275
|
+
'rectangleTopRightCornerRadius', 'rectangleBottomRightCornerRadius', 'rectangleBottomLeftCornerRadius'],
|
|
276
|
+
['cornerSmoothing'],
|
|
277
|
+
['opacity'],
|
|
278
|
+
['blendMode'],
|
|
279
|
+
['frameMaskDisabled'],
|
|
280
|
+
];
|
|
281
|
+
/** Structural equality of two decoded values: primitives, bytes, arrays and records. */
|
|
282
|
+
function sameValue(a, b) {
|
|
283
|
+
if (a === b)
|
|
284
|
+
return true;
|
|
285
|
+
if (a instanceof Uint8Array || b instanceof Uint8Array) {
|
|
286
|
+
return a instanceof Uint8Array && b instanceof Uint8Array &&
|
|
287
|
+
a.length === b.length && a.every((v, i) => v === b[i]);
|
|
288
|
+
}
|
|
289
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
290
|
+
return Array.isArray(a) && Array.isArray(b) &&
|
|
291
|
+
a.length === b.length && a.every((v, i) => sameValue(v, b[i]));
|
|
292
|
+
}
|
|
293
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
294
|
+
const ka = Object.keys(a);
|
|
295
|
+
const kb = Object.keys(b);
|
|
296
|
+
return ka.length === kb.length &&
|
|
297
|
+
ka.every((k) => Object.hasOwn(b, k) && sameValue(a[k], b[k]));
|
|
298
|
+
}
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
/** The component an INSTANCE record shows: its swap target, else its own symbol. */
|
|
302
|
+
function componentOf(node) {
|
|
303
|
+
return (guidKey(obj(node, 'overriddenSymbolID')) ??
|
|
304
|
+
guidKey(obj(obj(node, 'symbolData'), 'symbolID')));
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* F27 — the stored record of a nested instance whose component is swapped, as the swapped-in
|
|
308
|
+
* component shows it. The record is complete for the component it was written for, so it holds
|
|
309
|
+
* copies of that component root's own appearance — an icon's filled circle and round corners —
|
|
310
|
+
* that are not overrides. Swapped, the instance keeps only what it overrides: every unit of
|
|
311
|
+
* `ROOT_APPEARANCE` equal to the old root's takes the new root's value (or none, where the new
|
|
312
|
+
* root has none), and the outline, which traced the old component's shape, is dropped.
|
|
313
|
+
*/
|
|
314
|
+
export function swapAppearance(stored, from, to) {
|
|
315
|
+
const out = { ...stored };
|
|
316
|
+
delete out['fillGeometry'];
|
|
317
|
+
delete out['strokeGeometry'];
|
|
318
|
+
for (const unit of ROOT_APPEARANCE) {
|
|
319
|
+
if (!unit.every((field) => sameValue(stored[field], from[field])))
|
|
320
|
+
continue;
|
|
321
|
+
for (const field of unit) {
|
|
322
|
+
if (to[field] === undefined)
|
|
323
|
+
delete out[field];
|
|
324
|
+
else
|
|
325
|
+
out[field] = to[field];
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return out;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* A component layer as the instance that expands it shows it: the stored node merged with its
|
|
332
|
+
* record, with the instance's property values applied (F17, F18) — and, for a nested instance
|
|
333
|
+
* whose component that swaps, with the copies of the old component's root appearance taken out
|
|
334
|
+
* before the record goes on top (F27). Shared by the exporter and the scene, so the picture and
|
|
335
|
+
* the inspection tools see the same layer.
|
|
336
|
+
*/
|
|
337
|
+
export function showLayer(index, t, records, props) {
|
|
338
|
+
const record = records.get(overrideIdentity(t));
|
|
339
|
+
const outlines = record?.['derivedTextData'] !== undefined;
|
|
340
|
+
const merged = mergeNode(t.node, record);
|
|
341
|
+
const applied = applyProps(merged, props, outlines);
|
|
342
|
+
if (str(t.node, 'type') === 'INSTANCE') {
|
|
343
|
+
const from = componentOf(t.node);
|
|
344
|
+
const to = componentOf(applied.node);
|
|
345
|
+
const old = from && to && from !== to ? index.node(from) : undefined;
|
|
346
|
+
const swapped = old && to ? index.node(to) : undefined;
|
|
347
|
+
if (old && swapped) {
|
|
348
|
+
// A component missing from the file (a library one) cannot be compared; nothing changes.
|
|
349
|
+
const cleaned = mergeNode(swapAppearance(t.node, old.node, swapped.node), record);
|
|
350
|
+
const reapplied = applyProps(cleaned, props, outlines);
|
|
351
|
+
return { ...reapplied, propsApplied: reapplied.node !== cleaned };
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return { ...applied, propsApplied: applied.node !== merged };
|
|
355
|
+
}
|
|
180
356
|
/**
|
|
181
357
|
* The node a record path addresses, walked exactly the way the renderer expands the instance:
|
|
182
358
|
* each segment names a node inside the current symbol by identity, and a segment that lands on a
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { KiwiObject } from '../fig/kiwi.js';
|
|
2
|
+
import type { FileIndex } from './index.js';
|
|
3
|
+
import { type TreeNode } from './tree.js';
|
|
4
|
+
import { type ResolvedInstance } from './instance.js';
|
|
5
|
+
import { type ModeMap } from './variables.js';
|
|
6
|
+
/** The records and property values in force for the layers of one expanded instance. */
|
|
7
|
+
export interface Level {
|
|
8
|
+
/** Guid of the outermost, stored instance: the first segment of every sublayer id. */
|
|
9
|
+
readonly root: string;
|
|
10
|
+
/** Override-identity path from that instance down to the instance this level expands. */
|
|
11
|
+
readonly path: readonly string[];
|
|
12
|
+
readonly instance: ResolvedInstance;
|
|
13
|
+
/** Symbols expanded on the way here, so a self-referential component cannot loop. */
|
|
14
|
+
readonly active: ReadonlySet<string>;
|
|
15
|
+
/**
|
|
16
|
+
* The variable modes in force at the instance this level expands — its own picks over those
|
|
17
|
+
* of the layers above it (F28). The component root's own picks do not apply: the instance's
|
|
18
|
+
* record stands in for the root.
|
|
19
|
+
*/
|
|
20
|
+
readonly modes: ModeMap;
|
|
21
|
+
}
|
|
22
|
+
export interface SceneNode extends TreeNode {
|
|
23
|
+
/** The stored node this layer shows: itself, or the symbol descendant for a sublayer. */
|
|
24
|
+
readonly source: TreeNode;
|
|
25
|
+
/** True for a layer inside an instance — one Figma shows but the file does not store. */
|
|
26
|
+
readonly virtual: boolean;
|
|
27
|
+
/** For a sublayer, the expansion it belongs to. */
|
|
28
|
+
readonly level: Level | undefined;
|
|
29
|
+
/** The variable modes in force at this layer: its own picks over those above it (F28). */
|
|
30
|
+
readonly modes: ModeMap;
|
|
31
|
+
readonly children: SceneNode[];
|
|
32
|
+
}
|
|
33
|
+
export declare function isSublayerId(id: string): boolean;
|
|
34
|
+
/** A stored node as a scene node: its record unchanged, its instances expandable. */
|
|
35
|
+
export declare function sceneOf(index: FileIndex, t: TreeNode): SceneNode;
|
|
36
|
+
/**
|
|
37
|
+
* Resolve a stored guid or a sublayer id (`I<instance>;<id>;…`) to its scene node, with the
|
|
38
|
+
* whole chain of enclosing layers as its parents. Undefined when nothing matches: an unknown
|
|
39
|
+
* guid, a first segment that is not an instance, or a segment its instance does not contain.
|
|
40
|
+
*/
|
|
41
|
+
export declare function resolveScene(index: FileIndex, id: string): SceneNode | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* The override records the enclosing instances apply inside a nested instance `s` — the ones
|
|
44
|
+
* whose `guidPath` runs through it — re-addressed from `s`, innermost instance first. Its own
|
|
45
|
+
* records are `s.node.symbolData.symbolOverrides`; these come on top, and the outer one wins.
|
|
46
|
+
*/
|
|
47
|
+
export declare function inheritedOverrides(s: TreeNode): KiwiObject[];
|
|
48
|
+
/** True when the node, or a layer above it, is hidden. */
|
|
49
|
+
export declare function isHidden(s: TreeNode): boolean;
|
|
50
|
+
//# sourceMappingURL=scene.d.ts.map
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { guidKey, overrideIdentity } from './tree.js';
|
|
2
|
+
import { descend, resolveInstance, showLayer } from './instance.js';
|
|
3
|
+
import { applyVisibility } from './bindings.js';
|
|
4
|
+
import { NO_MODES, withModes } from './variables.js';
|
|
5
|
+
import { obj, objArr, str } from './access.js';
|
|
6
|
+
/** Instances nested deeper than this are almost certainly a cycle in a damaged file. */
|
|
7
|
+
const MAX_INSTANCE_DEPTH = 24;
|
|
8
|
+
/** `I<guid>;<guid>…` — Figma's id for a layer inside an instance. */
|
|
9
|
+
const SUBLAYER_ID = /^I(\d+:\d+)((?:;\d+:\d+)+)$/;
|
|
10
|
+
export function isSublayerId(id) {
|
|
11
|
+
return SUBLAYER_ID.test(id);
|
|
12
|
+
}
|
|
13
|
+
class Layer {
|
|
14
|
+
key;
|
|
15
|
+
node;
|
|
16
|
+
source;
|
|
17
|
+
virtual;
|
|
18
|
+
level;
|
|
19
|
+
order;
|
|
20
|
+
depth;
|
|
21
|
+
page;
|
|
22
|
+
index;
|
|
23
|
+
parentLayer;
|
|
24
|
+
expanded;
|
|
25
|
+
expansion;
|
|
26
|
+
modeMap;
|
|
27
|
+
constructor(index, init) {
|
|
28
|
+
this.index = index;
|
|
29
|
+
this.key = init.key;
|
|
30
|
+
this.node = init.node;
|
|
31
|
+
this.source = init.source;
|
|
32
|
+
this.level = init.level;
|
|
33
|
+
this.virtual = init.level !== undefined;
|
|
34
|
+
this.parentLayer = init.parent;
|
|
35
|
+
this.order = this.virtual ? -1 : init.source.order;
|
|
36
|
+
this.depth = init.parent && this.virtual ? init.parent.depth + 1 : init.source.depth;
|
|
37
|
+
this.page = this.virtual ? init.parent?.page : init.source.page;
|
|
38
|
+
}
|
|
39
|
+
/** A stored node's parent is wrapped on demand, so wrapping one node never walks the tree. */
|
|
40
|
+
get parent() {
|
|
41
|
+
if (!this.parentLayer && !this.virtual && this.source.parent) {
|
|
42
|
+
this.parentLayer = new Layer(this.index, {
|
|
43
|
+
key: this.source.parent.key,
|
|
44
|
+
node: this.source.parent.node,
|
|
45
|
+
source: this.source.parent,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return this.parentLayer;
|
|
49
|
+
}
|
|
50
|
+
set parent(_value) {
|
|
51
|
+
throw new Error('a scene node cannot be re-parented');
|
|
52
|
+
}
|
|
53
|
+
get children() {
|
|
54
|
+
this.expanded ??= this.expand();
|
|
55
|
+
return this.expanded;
|
|
56
|
+
}
|
|
57
|
+
get modes() {
|
|
58
|
+
this.modeMap ??= withModes(this.index, this.parent?.modes ?? NO_MODES, this.node);
|
|
59
|
+
return this.modeMap;
|
|
60
|
+
}
|
|
61
|
+
/** The expansion this INSTANCE opens — its symbol, records and property values. */
|
|
62
|
+
instanceLevel() {
|
|
63
|
+
if (this.expansion === undefined)
|
|
64
|
+
this.expansion = this.resolveLevel() ?? null;
|
|
65
|
+
return this.expansion ?? undefined;
|
|
66
|
+
}
|
|
67
|
+
resolveLevel() {
|
|
68
|
+
if (str(this.node, 'type') !== 'INSTANCE')
|
|
69
|
+
return undefined;
|
|
70
|
+
const outer = this.level;
|
|
71
|
+
const identity = overrideIdentity(this.source);
|
|
72
|
+
// As the renderer does: the instance as seen in its context, the enclosing records that
|
|
73
|
+
// address its descendants, and the enclosing property values its own may point at.
|
|
74
|
+
const instance = resolveInstance(this.index, this.node, outer ? descend(outer.instance.records, identity) : undefined, outer?.instance.props);
|
|
75
|
+
if (!instance)
|
|
76
|
+
return undefined;
|
|
77
|
+
const active = outer?.active ?? new Set();
|
|
78
|
+
if (active.has(instance.symbol.key) || active.size >= MAX_INSTANCE_DEPTH)
|
|
79
|
+
return undefined;
|
|
80
|
+
return {
|
|
81
|
+
root: outer ? outer.root : this.key,
|
|
82
|
+
path: outer ? [...outer.path, identity] : [],
|
|
83
|
+
instance,
|
|
84
|
+
active: new Set([...active, instance.symbol.key]),
|
|
85
|
+
modes: this.modes,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
expand() {
|
|
89
|
+
if (str(this.node, 'type') === 'INSTANCE') {
|
|
90
|
+
const level = this.instanceLevel();
|
|
91
|
+
return level ? level.instance.symbol.children.map((c) => sublayer(this.index, c, level, this)) : [];
|
|
92
|
+
}
|
|
93
|
+
const level = this.level;
|
|
94
|
+
if (level)
|
|
95
|
+
return this.source.children.map((c) => sublayer(this.index, c, level, this));
|
|
96
|
+
return this.source.children.map((c) => new Layer(this.index, { key: c.key, node: c.node, source: c, parent: this }));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* A symbol descendant as the instance expanded at `level` shows it — including a visibility it
|
|
101
|
+
* binds to a variable, read in the modes in force at it (F28), as the renderer reads it.
|
|
102
|
+
*/
|
|
103
|
+
function sublayer(index, c, level, parent) {
|
|
104
|
+
const { records, props } = level.instance;
|
|
105
|
+
const shown = showLayer(index, c, records, props).node;
|
|
106
|
+
return new Layer(index, {
|
|
107
|
+
key: `I${level.root};${[...level.path, overrideIdentity(c)].join(';')}`,
|
|
108
|
+
node: applyVisibility(index, shown, withModes(index, parent.modes, shown)).node,
|
|
109
|
+
source: c,
|
|
110
|
+
level,
|
|
111
|
+
parent,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/** A stored node as a scene node: its record unchanged, its instances expandable. */
|
|
115
|
+
export function sceneOf(index, t) {
|
|
116
|
+
return new Layer(index, { key: t.key, node: t.node, source: t });
|
|
117
|
+
}
|
|
118
|
+
/** The layer inside `symbol` (never the symbol itself) that a path segment names. */
|
|
119
|
+
function layerIn(index, symbol, segment) {
|
|
120
|
+
return (index.byOverrideIdentity().get(segment) ?? []).find((c) => c !== symbol && index.isDescendant(c, symbol));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Resolve a stored guid or a sublayer id (`I<instance>;<id>;…`) to its scene node, with the
|
|
124
|
+
* whole chain of enclosing layers as its parents. Undefined when nothing matches: an unknown
|
|
125
|
+
* guid, a first segment that is not an instance, or a segment its instance does not contain.
|
|
126
|
+
*/
|
|
127
|
+
export function resolveScene(index, id) {
|
|
128
|
+
const m = SUBLAYER_ID.exec(id);
|
|
129
|
+
if (!m) {
|
|
130
|
+
const t = index.node(id);
|
|
131
|
+
return t ? sceneOf(index, t) : undefined;
|
|
132
|
+
}
|
|
133
|
+
const rootNode = index.node(m[1]);
|
|
134
|
+
if (!rootNode)
|
|
135
|
+
return undefined;
|
|
136
|
+
let current = sceneOf(index, rootNode);
|
|
137
|
+
for (const segment of m[2].slice(1).split(';')) {
|
|
138
|
+
const level = current instanceof Layer ? current.instanceLevel() : undefined;
|
|
139
|
+
if (!level)
|
|
140
|
+
return undefined;
|
|
141
|
+
const symbol = level.instance.symbol;
|
|
142
|
+
const target = layerIn(index, symbol, segment);
|
|
143
|
+
if (!target)
|
|
144
|
+
return undefined;
|
|
145
|
+
// Materialise the layers between the symbol and the target, so the target has real parents.
|
|
146
|
+
const chain = [];
|
|
147
|
+
for (let cur = target; cur && cur !== symbol; cur = cur.parent)
|
|
148
|
+
chain.push(cur);
|
|
149
|
+
let node = current;
|
|
150
|
+
for (let i = chain.length - 1; i >= 0; i--)
|
|
151
|
+
node = sublayer(index, chain[i], level, node);
|
|
152
|
+
current = node;
|
|
153
|
+
}
|
|
154
|
+
return current;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The override records the enclosing instances apply inside a nested instance `s` — the ones
|
|
158
|
+
* whose `guidPath` runs through it — re-addressed from `s`, innermost instance first. Its own
|
|
159
|
+
* records are `s.node.symbolData.symbolOverrides`; these come on top, and the outer one wins.
|
|
160
|
+
*/
|
|
161
|
+
export function inheritedOverrides(s) {
|
|
162
|
+
if (!(s instanceof Layer) || !s.level)
|
|
163
|
+
return [];
|
|
164
|
+
const full = [...s.level.path, overrideIdentity(s.source)];
|
|
165
|
+
const out = [];
|
|
166
|
+
for (let cur = s.parent; cur; cur = cur.parent) {
|
|
167
|
+
const level = cur instanceof Layer ? cur.instanceLevel() : undefined;
|
|
168
|
+
if (level && level.root === s.level.root) {
|
|
169
|
+
const prefix = full.slice(level.path.length);
|
|
170
|
+
for (const record of objArr(obj(cur.node, 'symbolData'), 'symbolOverrides')) {
|
|
171
|
+
const path = objArr(obj(record, 'guidPath'), 'guids').map((g) => guidKey(g) ?? '');
|
|
172
|
+
if (path.length <= prefix.length || prefix.some((p, i) => path[i] !== p))
|
|
173
|
+
continue;
|
|
174
|
+
out.push({ ...record, guidPath: { guids: objArr(obj(record, 'guidPath'), 'guids').slice(prefix.length) } });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (cur.key === s.level.root)
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
/** True when the node, or a layer above it, is hidden. */
|
|
183
|
+
export function isHidden(s) {
|
|
184
|
+
for (let cur = s; cur; cur = cur.parent) {
|
|
185
|
+
const type = str(cur.node, 'type');
|
|
186
|
+
if (type === 'CANVAS' || type === 'DOCUMENT')
|
|
187
|
+
break;
|
|
188
|
+
if (cur.node['visible'] === false)
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared styles are the live source of the values they define (plan F19, F22).
|
|
3
|
+
*
|
|
4
|
+
* A node that references a local style — `styleIdForFill`, `styleIdForStrokeFill`,
|
|
5
|
+
* `styleIdForEffect`, `styleIdForText` — takes the STYLE's current value. The copy cached on the
|
|
6
|
+
* node beside the reference is what the node looked like when it was last touched, and it can be
|
|
7
|
+
* stale (1 779 nodes and 1 064 override records in the sample disagree with their colour style)
|
|
8
|
+
* or missing altogether (3 905 override records and 265 text-run entries carry only the
|
|
9
|
+
* reference). Text styles behave the same way: on the iOS 16 UI Kit community file 75 text nodes
|
|
10
|
+
* cache typography that disagrees with their text style, and wherever the two differ Figma's own
|
|
11
|
+
* text layout (`derivedTextData`: glyph size, font, metrics) follows the STYLE — 39 of 39 font
|
|
12
|
+
* sizes, 22 of 22 families, 26 of 26 weights.
|
|
13
|
+
*
|
|
14
|
+
* A style that is not in the file (a library style) cannot be followed, and neither can the
|
|
15
|
+
* "no style" sentinel a detached override writes; both leave the cached copy in charge.
|
|
16
|
+
*
|
|
17
|
+
* Shared by the renderer and the inspection tools, so a colour never differs between the picture
|
|
18
|
+
* and the numbers. Only `node` and `resolveAssetRef` of the index are used.
|
|
19
|
+
*/
|
|
20
|
+
import type { KiwiObject } from '../fig/kiwi.js';
|
|
21
|
+
import type { NodeChange } from '../fig/parse.js';
|
|
22
|
+
import type { FileIndex } from './index.js';
|
|
23
|
+
import { type TreeNode } from './tree.js';
|
|
24
|
+
export type StyleType = 'FILL' | 'EFFECT' | 'TEXT' | 'GRID';
|
|
25
|
+
type StyleIndex = Pick<FileIndex, 'node' | 'resolveAssetRef'>;
|
|
26
|
+
/** The guid a detached style reference is written with (plan F19). */
|
|
27
|
+
export declare const DETACHED_STYLE_GUID = "4294967295:4294967295";
|
|
28
|
+
/**
|
|
29
|
+
* The local node defining a style reference: by publishable key for `{assetRef:{key}}`, by guid
|
|
30
|
+
* for a style that was never published (`{guid}`). Undefined for a library style, for the
|
|
31
|
+
* detached sentinel, and for a node of another style type than `type`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveStyle(index: StyleIndex, ref: unknown, type?: StyleType): TreeNode | undefined;
|
|
34
|
+
/** The paints of a local colour style, or undefined when the reference cannot be followed. */
|
|
35
|
+
export declare function stylePaints(index: StyleIndex, ref: unknown): KiwiObject[] | undefined;
|
|
36
|
+
/** The effects of a local effect style, or undefined when the reference cannot be followed. */
|
|
37
|
+
export declare function styleEffects(index: StyleIndex, ref: unknown): KiwiObject[] | undefined;
|
|
38
|
+
/** The local text style a reference names, or undefined when it cannot be followed. */
|
|
39
|
+
export declare function styleText(index: StyleIndex, ref: unknown): NodeChange | undefined;
|
|
40
|
+
export interface ApplyStylesOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Also resolve `styleIdForText` to the text style's typography (F22). The renderer does not
|
|
43
|
+
* need it — glyph outlines are baked — but every number reported about a text node does.
|
|
44
|
+
*/
|
|
45
|
+
readonly text?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The node is a PARTIAL record (an override, or a `styleOverrideTable` entry) that reports only
|
|
48
|
+
* the fields it changes: a text style's defaults are then not written, since an absent field
|
|
49
|
+
* there means "inherited", not "default".
|
|
50
|
+
*/
|
|
51
|
+
readonly partial?: boolean;
|
|
52
|
+
}
|
|
53
|
+
/** The node with every style reference it carries resolved to the style's live value. */
|
|
54
|
+
export declare function applyStyles(index: StyleIndex, node: NodeChange, opts?: ApplyStylesOptions): NodeChange;
|
|
55
|
+
export {};
|
|
56
|
+
//# sourceMappingURL=styles.d.ts.map
|