figme-mcp 1.1.1 → 1.2.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.
@@ -0,0 +1,41 @@
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
+ /** The records and property values in force for the layers of one expanded instance. */
6
+ export interface Level {
7
+ /** Guid of the outermost, stored instance: the first segment of every sublayer id. */
8
+ readonly root: string;
9
+ /** Override-identity path from that instance down to the instance this level expands. */
10
+ readonly path: readonly string[];
11
+ readonly instance: ResolvedInstance;
12
+ /** Symbols expanded on the way here, so a self-referential component cannot loop. */
13
+ readonly active: ReadonlySet<string>;
14
+ }
15
+ export interface SceneNode extends TreeNode {
16
+ /** The stored node this layer shows: itself, or the symbol descendant for a sublayer. */
17
+ readonly source: TreeNode;
18
+ /** True for a layer inside an instance — one Figma shows but the file does not store. */
19
+ readonly virtual: boolean;
20
+ /** For a sublayer, the expansion it belongs to. */
21
+ readonly level: Level | undefined;
22
+ readonly children: SceneNode[];
23
+ }
24
+ export declare function isSublayerId(id: string): boolean;
25
+ /** A stored node as a scene node: its record unchanged, its instances expandable. */
26
+ export declare function sceneOf(index: FileIndex, t: TreeNode): SceneNode;
27
+ /**
28
+ * Resolve a stored guid or a sublayer id (`I<instance>;<id>;…`) to its scene node, with the
29
+ * whole chain of enclosing layers as its parents. Undefined when nothing matches: an unknown
30
+ * guid, a first segment that is not an instance, or a segment its instance does not contain.
31
+ */
32
+ export declare function resolveScene(index: FileIndex, id: string): SceneNode | undefined;
33
+ /**
34
+ * The override records the enclosing instances apply inside a nested instance `s` — the ones
35
+ * whose `guidPath` runs through it — re-addressed from `s`, innermost instance first. Its own
36
+ * records are `s.node.symbolData.symbolOverrides`; these come on top, and the outer one wins.
37
+ */
38
+ export declare function inheritedOverrides(s: TreeNode): KiwiObject[];
39
+ /** True when the node, or a layer above it, is hidden. */
40
+ export declare function isHidden(s: TreeNode): boolean;
41
+ //# sourceMappingURL=scene.d.ts.map
@@ -0,0 +1,182 @@
1
+ import { guidKey, overrideIdentity } from './tree.js';
2
+ import { applyProps, descend, mergeNode, resolveInstance, } from './instance.js';
3
+ import { obj, objArr, str } from './access.js';
4
+ /** Instances nested deeper than this are almost certainly a cycle in a damaged file. */
5
+ const MAX_INSTANCE_DEPTH = 24;
6
+ /** `I<guid>;<guid>…` — Figma's id for a layer inside an instance. */
7
+ const SUBLAYER_ID = /^I(\d+:\d+)((?:;\d+:\d+)+)$/;
8
+ export function isSublayerId(id) {
9
+ return SUBLAYER_ID.test(id);
10
+ }
11
+ class Layer {
12
+ key;
13
+ node;
14
+ source;
15
+ virtual;
16
+ level;
17
+ order;
18
+ depth;
19
+ page;
20
+ index;
21
+ parentLayer;
22
+ expanded;
23
+ expansion;
24
+ constructor(index, init) {
25
+ this.index = index;
26
+ this.key = init.key;
27
+ this.node = init.node;
28
+ this.source = init.source;
29
+ this.level = init.level;
30
+ this.virtual = init.level !== undefined;
31
+ this.parentLayer = init.parent;
32
+ this.order = this.virtual ? -1 : init.source.order;
33
+ this.depth = init.parent && this.virtual ? init.parent.depth + 1 : init.source.depth;
34
+ this.page = this.virtual ? init.parent?.page : init.source.page;
35
+ }
36
+ /** A stored node's parent is wrapped on demand, so wrapping one node never walks the tree. */
37
+ get parent() {
38
+ if (!this.parentLayer && !this.virtual && this.source.parent) {
39
+ this.parentLayer = new Layer(this.index, {
40
+ key: this.source.parent.key,
41
+ node: this.source.parent.node,
42
+ source: this.source.parent,
43
+ });
44
+ }
45
+ return this.parentLayer;
46
+ }
47
+ set parent(_value) {
48
+ throw new Error('a scene node cannot be re-parented');
49
+ }
50
+ get children() {
51
+ this.expanded ??= this.expand();
52
+ return this.expanded;
53
+ }
54
+ /** The expansion this INSTANCE opens — its symbol, records and property values. */
55
+ instanceLevel() {
56
+ if (this.expansion === undefined)
57
+ this.expansion = this.resolveLevel() ?? null;
58
+ return this.expansion ?? undefined;
59
+ }
60
+ resolveLevel() {
61
+ if (str(this.node, 'type') !== 'INSTANCE')
62
+ return undefined;
63
+ const outer = this.level;
64
+ const identity = overrideIdentity(this.source);
65
+ // As the renderer does: the instance as seen in its context, the enclosing records that
66
+ // address its descendants, and the enclosing property values its own may point at.
67
+ const instance = resolveInstance(this.index, this.node, outer ? descend(outer.instance.records, identity) : undefined, outer?.instance.props);
68
+ if (!instance)
69
+ return undefined;
70
+ const active = outer?.active ?? new Set();
71
+ if (active.has(instance.symbol.key) || active.size >= MAX_INSTANCE_DEPTH)
72
+ return undefined;
73
+ return {
74
+ root: outer ? outer.root : this.key,
75
+ path: outer ? [...outer.path, identity] : [],
76
+ instance,
77
+ active: new Set([...active, instance.symbol.key]),
78
+ };
79
+ }
80
+ expand() {
81
+ if (str(this.node, 'type') === 'INSTANCE') {
82
+ const level = this.instanceLevel();
83
+ return level ? level.instance.symbol.children.map((c) => sublayer(this.index, c, level, this)) : [];
84
+ }
85
+ const level = this.level;
86
+ if (level)
87
+ return this.source.children.map((c) => sublayer(this.index, c, level, this));
88
+ return this.source.children.map((c) => new Layer(this.index, { key: c.key, node: c.node, source: c, parent: this }));
89
+ }
90
+ }
91
+ /** A symbol descendant as the instance expanded at `level` shows it. */
92
+ function sublayer(index, c, level, parent) {
93
+ const identity = overrideIdentity(c);
94
+ const record = level.instance.records.get(identity);
95
+ const node = applyProps(mergeNode(c.node, record), level.instance.props, record?.['derivedTextData'] !== undefined).node;
96
+ return new Layer(index, {
97
+ key: `I${level.root};${[...level.path, identity].join(';')}`,
98
+ node,
99
+ source: c,
100
+ level,
101
+ parent,
102
+ });
103
+ }
104
+ /** A stored node as a scene node: its record unchanged, its instances expandable. */
105
+ export function sceneOf(index, t) {
106
+ return new Layer(index, { key: t.key, node: t.node, source: t });
107
+ }
108
+ /** The layer inside `symbol` (never the symbol itself) that a path segment names. */
109
+ function layerIn(index, symbol, segment) {
110
+ return (index.byOverrideIdentity().get(segment) ?? []).find((c) => c !== symbol && index.isDescendant(c, symbol));
111
+ }
112
+ /**
113
+ * Resolve a stored guid or a sublayer id (`I<instance>;<id>;…`) to its scene node, with the
114
+ * whole chain of enclosing layers as its parents. Undefined when nothing matches: an unknown
115
+ * guid, a first segment that is not an instance, or a segment its instance does not contain.
116
+ */
117
+ export function resolveScene(index, id) {
118
+ const m = SUBLAYER_ID.exec(id);
119
+ if (!m) {
120
+ const t = index.node(id);
121
+ return t ? sceneOf(index, t) : undefined;
122
+ }
123
+ const rootNode = index.node(m[1]);
124
+ if (!rootNode)
125
+ return undefined;
126
+ let current = sceneOf(index, rootNode);
127
+ for (const segment of m[2].slice(1).split(';')) {
128
+ const level = current instanceof Layer ? current.instanceLevel() : undefined;
129
+ if (!level)
130
+ return undefined;
131
+ const symbol = level.instance.symbol;
132
+ const target = layerIn(index, symbol, segment);
133
+ if (!target)
134
+ return undefined;
135
+ // Materialise the layers between the symbol and the target, so the target has real parents.
136
+ const chain = [];
137
+ for (let cur = target; cur && cur !== symbol; cur = cur.parent)
138
+ chain.push(cur);
139
+ let node = current;
140
+ for (let i = chain.length - 1; i >= 0; i--)
141
+ node = sublayer(index, chain[i], level, node);
142
+ current = node;
143
+ }
144
+ return current;
145
+ }
146
+ /**
147
+ * The override records the enclosing instances apply inside a nested instance `s` — the ones
148
+ * whose `guidPath` runs through it — re-addressed from `s`, innermost instance first. Its own
149
+ * records are `s.node.symbolData.symbolOverrides`; these come on top, and the outer one wins.
150
+ */
151
+ export function inheritedOverrides(s) {
152
+ if (!(s instanceof Layer) || !s.level)
153
+ return [];
154
+ const full = [...s.level.path, overrideIdentity(s.source)];
155
+ const out = [];
156
+ for (let cur = s.parent; cur; cur = cur.parent) {
157
+ const level = cur instanceof Layer ? cur.instanceLevel() : undefined;
158
+ if (level && level.root === s.level.root) {
159
+ const prefix = full.slice(level.path.length);
160
+ for (const record of objArr(obj(cur.node, 'symbolData'), 'symbolOverrides')) {
161
+ const path = objArr(obj(record, 'guidPath'), 'guids').map((g) => guidKey(g) ?? '');
162
+ if (path.length <= prefix.length || prefix.some((p, i) => path[i] !== p))
163
+ continue;
164
+ out.push({ ...record, guidPath: { guids: objArr(obj(record, 'guidPath'), 'guids').slice(prefix.length) } });
165
+ }
166
+ }
167
+ if (cur.key === s.level.root)
168
+ break;
169
+ }
170
+ return out;
171
+ }
172
+ /** True when the node, or a layer above it, is hidden. */
173
+ export function isHidden(s) {
174
+ for (let cur = s; cur; cur = cur.parent) {
175
+ const type = str(cur.node, 'type');
176
+ if (type === 'CANVAS' || type === 'DOCUMENT')
177
+ break;
178
+ if (cur.node['visible'] === false)
179
+ return true;
180
+ }
181
+ return false;
182
+ }
@@ -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
@@ -0,0 +1,90 @@
1
+ import { guidKey, NO_GUID } from './tree.js';
2
+ import { obj, objArr, str } from './access.js';
3
+ /** The guid a detached style reference is written with (plan F19). */
4
+ export const DETACHED_STYLE_GUID = NO_GUID;
5
+ /**
6
+ * The local node defining a style reference: by publishable key for `{assetRef:{key}}`, by guid
7
+ * for a style that was never published (`{guid}`). Undefined for a library style, for the
8
+ * detached sentinel, and for a node of another style type than `type`.
9
+ */
10
+ export function resolveStyle(index, ref, type) {
11
+ if (!ref || typeof ref !== 'object')
12
+ return undefined;
13
+ const r = ref;
14
+ const local = guidKey(obj(r, 'guid'));
15
+ const t = index.resolveAssetRef(r) ?? (local ? index.node(local) : undefined);
16
+ if (!t)
17
+ return undefined;
18
+ const styleType = str(t.node, 'styleType');
19
+ if (type ? styleType !== type : styleType === undefined)
20
+ return undefined;
21
+ return t;
22
+ }
23
+ /** The paints of a local colour style, or undefined when the reference cannot be followed. */
24
+ export function stylePaints(index, ref) {
25
+ const t = resolveStyle(index, ref, 'FILL');
26
+ if (!t || !Array.isArray(t.node['fillPaints']))
27
+ return undefined;
28
+ return objArr(t.node, 'fillPaints');
29
+ }
30
+ /** The effects of a local effect style, or undefined when the reference cannot be followed. */
31
+ export function styleEffects(index, ref) {
32
+ const t = resolveStyle(index, ref, 'EFFECT');
33
+ if (!t || !Array.isArray(t.node['effects']))
34
+ return undefined;
35
+ return objArr(t.node, 'effects');
36
+ }
37
+ /**
38
+ * What a text style defines, in NodeChange field names (Figma's TextStyle). Alignment, auto-resize
39
+ * and colour are not part of a text style: the style node carries them only for its own preview.
40
+ * The first four are on every text style; the rest are written only when they differ from the
41
+ * default, so a style without one of them means the default.
42
+ */
43
+ const TEXT_STYLE_ALWAYS = ['fontName', 'fontSize', 'lineHeight', 'letterSpacing'];
44
+ const TEXT_STYLE_OPTIONAL = [
45
+ 'paragraphSpacing',
46
+ 'paragraphIndent',
47
+ 'listSpacing',
48
+ 'leadingTrim',
49
+ 'hangingPunctuation',
50
+ 'hangingList',
51
+ 'textCase',
52
+ 'textDecoration',
53
+ 'textDecorationStyle',
54
+ 'textDecorationSkipInk',
55
+ 'textUnderlineOffset',
56
+ 'textDecorationThickness',
57
+ ];
58
+ /** The local text style a reference names, or undefined when it cannot be followed. */
59
+ export function styleText(index, ref) {
60
+ return resolveStyle(index, ref, 'TEXT')?.node;
61
+ }
62
+ /** The node with every style reference it carries resolved to the style's live value. */
63
+ export function applyStyles(index, node, opts = {}) {
64
+ const fill = stylePaints(index, node['styleIdForFill']);
65
+ const stroke = stylePaints(index, node['styleIdForStrokeFill']);
66
+ const effects = styleEffects(index, node['styleIdForEffect']);
67
+ const text = opts.text ? styleText(index, node['styleIdForText']) : undefined;
68
+ if (!fill && !stroke && !effects && !text)
69
+ return node;
70
+ const out = { ...node };
71
+ if (fill)
72
+ out['fillPaints'] = fill;
73
+ if (stroke)
74
+ out['strokePaints'] = stroke;
75
+ if (effects)
76
+ out['effects'] = effects;
77
+ if (text) {
78
+ for (const field of TEXT_STYLE_ALWAYS) {
79
+ if (text[field] !== undefined)
80
+ out[field] = text[field];
81
+ }
82
+ for (const field of TEXT_STYLE_OPTIONAL) {
83
+ if (text[field] !== undefined)
84
+ out[field] = text[field];
85
+ else if (!opts.partial)
86
+ delete out[field];
87
+ }
88
+ }
89
+ return out;
90
+ }
@@ -18,6 +18,8 @@ export interface NodeSummary {
18
18
  /** `"134x40"` — denser than `{x,y}` and still trivially parseable. */
19
19
  size?: string;
20
20
  children?: number;
21
+ /** The layer itself is hidden — set directly, or by a component property. */
22
+ hidden?: boolean;
21
23
  page?: string;
22
24
  }
23
25
  export declare function summarize(t: TreeNode, opts?: {
@@ -30,7 +32,31 @@ export declare function absoluteOrigin(t: TreeNode): {
30
32
  x: number;
31
33
  y: number;
32
34
  } | undefined;
33
- export declare function paintView(idx: FileIndex, p: KiwiObject): Record<string, unknown>;
35
+ /**
36
+ * One radius, or four in CSS order (top-left, top-right, bottom-right, bottom-left). With
37
+ * independent corners, a corner the record does not write is square, whatever `cornerRadius`
38
+ * says (F24): on the iOS 16 UI Kit every one of the 162 such corners is square in Figma's own
39
+ * outline, 38 of them beside a non-zero `cornerRadius` — a notification rounded only at the top.
40
+ */
41
+ export declare function cornerRadiusOf(node: NodeChange): unknown;
42
+ /**
43
+ * Per-side stroke weights, when the sides are set independently (F25): Figma then draws each side
44
+ * at its own weight, whatever the node's `strokeWeight` says — the hairline under a navigation
45
+ * bar is `bottom: 0.5` beside a `strokeWeight` of 0, the one over a tab bar `top: 0.5` beside 1.
46
+ */
47
+ export declare function sideWeights(node: NodeChange): Record<string, number> | undefined;
48
+ /** The size of the box a paint fills, which a gradient's direction is relative to. */
49
+ export interface PaintBox {
50
+ readonly w: number;
51
+ readonly h: number;
52
+ }
53
+ /** The node's own box, for `paintView`. */
54
+ export declare function paintBox(node: NodeChange): PaintBox | undefined;
55
+ /**
56
+ * One paint. Given the box it fills, a linear gradient also comes back as `css` — its direction
57
+ * is otherwise nowhere in the output.
58
+ */
59
+ export declare function paintView(idx: FileIndex, p: KiwiObject, box?: PaintBox): Record<string, unknown>;
34
60
  export declare function effectView(idx: FileIndex, e: KiwiObject): Record<string, unknown>;
35
61
  export interface Padding {
36
62
  top: number;
@@ -45,14 +71,25 @@ export interface Padding {
45
71
  export declare function paddingOf(node: NodeChange): Padding | undefined;
46
72
  /** `{value, units}` rendered compactly: `"1.5"` (RAW multiplier), `"13px"`, `"150%"`. */
47
73
  export declare function lengthString(o: KiwiObject | undefined): string | undefined;
74
+ /**
75
+ * A line height as CSS reads it. Figma stores its "Auto" line height — the font's own, about 1.2x
76
+ * the size — as 100 PERCENT (the REST API calls that unit INTRINSIC_%). On the iOS 16 UI Kit all
77
+ * 56 such texts are laid out at exactly their font's natural line height, which a CSS `100%`
78
+ * (1.0x the size) would not give, so it is reported as "auto" (CSS `normal`).
79
+ */
80
+ export declare function lineHeightString(o: KiwiObject | undefined): string | undefined;
81
+ /** A letter spacing as CSS reads it: Figma's percent is of the font size, which CSS spells `em`. */
82
+ export declare function letterSpacingString(o: KiwiObject | undefined): string | undefined;
48
83
  export declare function textBasics(node: NodeChange): Record<string, unknown> | undefined;
49
84
  /**
50
85
  * Shared-style references.
51
86
  *
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.
87
+ * A style definition is stored as an ordinary (hidden) node parked on a canvas — text styles are
88
+ * TEXT nodes, fill styles are ROUNDED_RECTANGLE nodes. A published style is referenced by the
89
+ * publishable `key` it carries (`styleIdFor*.assetRef.key`), a style that was never published by
90
+ * its guid (`styleIdFor*.guid`). Either way a style used from the same file resolves to a real
91
+ * node and, with `resolve`, to the values it defines. Styles from other libraries stay opaque
92
+ * refs, and the detached-style sentinel is no style at all.
56
93
  */
57
94
  export declare function styleRefsOf(idx: FileIndex, node: NodeChange, opts?: {
58
95
  resolve?: boolean;
@@ -68,6 +105,8 @@ export declare function nodeDetail(idx: FileIndex, t: TreeNode, opts?: NodeDetai
68
105
  /**
69
106
  * Typography of a node — or of a partial style-override record from a text run — reporting only
70
107
  * the fields it actually sets, so a run's block shows exactly what differs from the base style.
108
+ * Pass the node with its styles applied (`applyStyles(…, { text: true })`): the values cached
109
+ * beside a style reference can be stale (F19, F22).
71
110
  */
72
111
  export declare function typography(idx: FileIndex, node: NodeChange, opts?: {
73
112
  includeStyleRefs?: boolean;