figme-mcp 1.1.2 → 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.
- 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/components.d.ts +8 -1
- package/dist/model/components.js +68 -28
- package/dist/model/scene.d.ts +41 -0
- package/dist/model/scene.js +182 -0
- package/dist/model/styles.d.ts +56 -0
- package/dist/model/styles.js +90 -0
- package/dist/model/summarize.d.ts +44 -5
- package/dist/model/summarize.js +243 -46
- package/dist/model/text.d.ts +5 -1
- package/dist/model/text.js +13 -3
- package/dist/model/tree.d.ts +12 -3
- package/dist/model/tree.js +24 -7
- package/dist/render/export.d.ts +20 -0
- package/dist/render/export.js +17 -6
- package/dist/render/index.js +25 -4
- package/dist/render/style.d.ts +8 -14
- package/dist/render/style.js +3 -40
- package/package.json +3 -3
package/dist/model/summarize.js
CHANGED
|
@@ -2,6 +2,7 @@ import { nodeImageHashes } from './index.js';
|
|
|
2
2
|
import { breadcrumb, guidKey } from './tree.js';
|
|
3
3
|
import { assetRefString, bool, bytes, colorHex, compact, hex, jsonSafe, num, obj, objArr, r2, str, } from './access.js';
|
|
4
4
|
import { describeVariableData } from './variables.js';
|
|
5
|
+
import { applyStyles, DETACHED_STYLE_GUID, resolveStyle } from './styles.js';
|
|
5
6
|
export function summarize(t, opts = {}) {
|
|
6
7
|
const size = obj(t.node, 'size');
|
|
7
8
|
const w = num(size, 'x');
|
|
@@ -12,6 +13,7 @@ export function summarize(t, opts = {}) {
|
|
|
12
13
|
name: str(t.node, 'name'),
|
|
13
14
|
size: w !== undefined && h !== undefined ? `${r2(w)}x${r2(h)}` : undefined,
|
|
14
15
|
children: t.children.length || undefined,
|
|
16
|
+
hidden: bool(t.node, 'visible') === false || undefined,
|
|
15
17
|
page: opts.page && t.page ? str(t.page.node, 'name') : undefined,
|
|
16
18
|
});
|
|
17
19
|
}
|
|
@@ -25,6 +27,8 @@ export function outlineLine(t, depth) {
|
|
|
25
27
|
parts.push(`(${s.guid})`);
|
|
26
28
|
if (s.children)
|
|
27
29
|
parts.push(`x${s.children}`);
|
|
30
|
+
if (s.hidden)
|
|
31
|
+
parts.push('hidden');
|
|
28
32
|
return parts.join(' ');
|
|
29
33
|
}
|
|
30
34
|
// --------------------------------------------------------------------------- geometry
|
|
@@ -72,20 +76,121 @@ function geometryOf(t) {
|
|
|
72
76
|
});
|
|
73
77
|
return Object.keys(g).length ? g : undefined;
|
|
74
78
|
}
|
|
75
|
-
|
|
79
|
+
/**
|
|
80
|
+
* One radius, or four in CSS order (top-left, top-right, bottom-right, bottom-left). With
|
|
81
|
+
* independent corners, a corner the record does not write is square, whatever `cornerRadius`
|
|
82
|
+
* says (F24): on the iOS 16 UI Kit every one of the 162 such corners is square in Figma's own
|
|
83
|
+
* outline, 38 of them beside a non-zero `cornerRadius` — a notification rounded only at the top.
|
|
84
|
+
*/
|
|
85
|
+
export function cornerRadiusOf(node) {
|
|
76
86
|
const uniform = num(node, 'cornerRadius');
|
|
77
87
|
if (!bool(node, 'rectangleCornerRadiiIndependent'))
|
|
78
88
|
return r2(uniform);
|
|
79
89
|
const corners = [
|
|
80
|
-
num(node, 'rectangleTopLeftCornerRadius') ??
|
|
81
|
-
num(node, 'rectangleTopRightCornerRadius') ??
|
|
82
|
-
num(node, 'rectangleBottomRightCornerRadius') ??
|
|
83
|
-
num(node, 'rectangleBottomLeftCornerRadius') ??
|
|
90
|
+
num(node, 'rectangleTopLeftCornerRadius') ?? 0,
|
|
91
|
+
num(node, 'rectangleTopRightCornerRadius') ?? 0,
|
|
92
|
+
num(node, 'rectangleBottomRightCornerRadius') ?? 0,
|
|
93
|
+
num(node, 'rectangleBottomLeftCornerRadius') ?? 0,
|
|
84
94
|
].map((v) => r2(v));
|
|
85
95
|
return corners.every((v) => v === corners[0]) ? corners[0] : corners;
|
|
86
96
|
}
|
|
87
|
-
|
|
88
|
-
|
|
97
|
+
/** Does any stroke paint show? A weight, alignment, cap or dash describes nothing without one. */
|
|
98
|
+
function hasVisibleStroke(live) {
|
|
99
|
+
return objArr(live, 'strokePaints').some((p) => bool(p, 'visible') !== false);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Per-side stroke weights, when the sides are set independently (F25): Figma then draws each side
|
|
103
|
+
* at its own weight, whatever the node's `strokeWeight` says — the hairline under a navigation
|
|
104
|
+
* bar is `bottom: 0.5` beside a `strokeWeight` of 0, the one over a tab bar `top: 0.5` beside 1.
|
|
105
|
+
*/
|
|
106
|
+
export function sideWeights(node) {
|
|
107
|
+
if (bool(node, 'borderStrokeWeightsIndependent') !== true)
|
|
108
|
+
return undefined;
|
|
109
|
+
return {
|
|
110
|
+
top: r2(num(node, 'borderTopWeight') ?? 0),
|
|
111
|
+
right: r2(num(node, 'borderRightWeight') ?? 0),
|
|
112
|
+
bottom: r2(num(node, 'borderBottomWeight') ?? 0),
|
|
113
|
+
left: r2(num(node, 'borderLeftWeight') ?? 0),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/** The node's own box, for `paintView`. */
|
|
117
|
+
export function paintBox(node) {
|
|
118
|
+
const size = obj(node, 'size');
|
|
119
|
+
const w = num(size, 'x');
|
|
120
|
+
const h = num(size, 'y');
|
|
121
|
+
return w !== undefined && h !== undefined && w > 0 && h > 0 ? { w, h } : undefined;
|
|
122
|
+
}
|
|
123
|
+
/** Steps a stretch between two stops is cut into when CSS would blend it differently. */
|
|
124
|
+
const BLEND_STEPS = 8;
|
|
125
|
+
/**
|
|
126
|
+
* Figma blends gradient colours unpremultiplied, CSS premultiplied: across a stretch where both
|
|
127
|
+
* the colour and the alpha change — transparent white into 50 % black — CSS loses the pale
|
|
128
|
+
* fringe Figma draws. Sampling such a stretch as Figma blends it keeps the two within a level or
|
|
129
|
+
* two; a stretch where only one of them changes blends the same either way and is left alone.
|
|
130
|
+
*/
|
|
131
|
+
function blendLikeFigma(stops) {
|
|
132
|
+
const out = [];
|
|
133
|
+
stops.forEach((s, i) => {
|
|
134
|
+
const prev = stops[i - 1];
|
|
135
|
+
if (prev && prev.a !== s.a && (prev.r !== s.r || prev.g !== s.g || prev.b !== s.b)) {
|
|
136
|
+
for (let k = 1; k < BLEND_STEPS; k++) {
|
|
137
|
+
const t = k / BLEND_STEPS;
|
|
138
|
+
const mix = (x, y) => x + (y - x) * t;
|
|
139
|
+
out.push({
|
|
140
|
+
at: mix(prev.at, s.at), r: mix(prev.r, s.r), g: mix(prev.g, s.g), b: mix(prev.b, s.b), a: mix(prev.a, s.a),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
out.push(s);
|
|
145
|
+
});
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* A linear gradient as CSS, for the box it fills. `transform` maps the normalized box to
|
|
150
|
+
* gradient space, where the gradient runs along x from 0 to 1 (F7), so its value at a pixel is
|
|
151
|
+
* `m00·x/w + m01·y/h + m02`: it grows along `(m00/w, m01/h)` — CSS's angle — and each stop goes
|
|
152
|
+
* where CSS's gradient line (through the centre, `|w·sin θ| + |h·cos θ|` long) reaches that
|
|
153
|
+
* value. Exact for any transform; stops take the paint's opacity into their alpha. Drawn by a
|
|
154
|
+
* browser, it matches `fig_render` to a mean of under one level per channel.
|
|
155
|
+
*/
|
|
156
|
+
function linearGradientCss(p, box) {
|
|
157
|
+
const m = obj(p, 'transform');
|
|
158
|
+
const a = num(m, 'm00') ?? 1;
|
|
159
|
+
const b = num(m, 'm01') ?? 0;
|
|
160
|
+
const c = num(m, 'm02') ?? 0;
|
|
161
|
+
const nx = a / box.w;
|
|
162
|
+
const ny = b / box.h;
|
|
163
|
+
const rate = Math.hypot(nx, ny);
|
|
164
|
+
if (!(rate > 1e-9))
|
|
165
|
+
return undefined;
|
|
166
|
+
const theta = Math.atan2(nx, -ny);
|
|
167
|
+
const line = Math.abs(box.w * Math.sin(theta)) + Math.abs(box.h * Math.cos(theta));
|
|
168
|
+
const atCentre = a / 2 + b / 2 + c;
|
|
169
|
+
const opacity = num(p, 'opacity') ?? 1;
|
|
170
|
+
const stops = objArr(p, 'stops')
|
|
171
|
+
.map((s) => {
|
|
172
|
+
const color = obj(s, 'color');
|
|
173
|
+
return {
|
|
174
|
+
at: Math.max(0, Math.min(1, num(s, 'position') ?? 0)),
|
|
175
|
+
r: num(color, 'r') ?? 0,
|
|
176
|
+
g: num(color, 'g') ?? 0,
|
|
177
|
+
b: num(color, 'b') ?? 0,
|
|
178
|
+
a: (num(color, 'a') ?? 1) * opacity,
|
|
179
|
+
};
|
|
180
|
+
})
|
|
181
|
+
.sort((x, y) => x.at - y.at);
|
|
182
|
+
if (stops.length === 0)
|
|
183
|
+
return undefined;
|
|
184
|
+
const css = blendLikeFigma(stops).map((s) => `${colorHex({ r: s.r, g: s.g, b: s.b, a: s.a })} ` +
|
|
185
|
+
`${r2(((s.at - atCentre) / (rate * line) + 0.5) * 100)}%`);
|
|
186
|
+
const degrees = (((theta * 180) / Math.PI) % 360 + 360) % 360;
|
|
187
|
+
return `linear-gradient(${r2(degrees)}deg, ${css.join(', ')})`;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* One paint. Given the box it fills, a linear gradient also comes back as `css` — its direction
|
|
191
|
+
* is otherwise nowhere in the output.
|
|
192
|
+
*/
|
|
193
|
+
export function paintView(idx, p, box) {
|
|
89
194
|
const image = obj(p, 'image');
|
|
90
195
|
const stops = objArr(p, 'stops').map((s) => compact({ at: r2(num(s, 'position')), color: colorHex(obj(s, 'color')) }));
|
|
91
196
|
const w = num(p, 'originalImageWidth');
|
|
@@ -97,6 +202,7 @@ export function paintView(idx, p) {
|
|
|
97
202
|
visible: bool(p, 'visible') === false ? false : undefined,
|
|
98
203
|
blendMode: str(p, 'blendMode') === 'NORMAL' ? undefined : str(p, 'blendMode'),
|
|
99
204
|
stops: stops.length ? stops : undefined,
|
|
205
|
+
css: box && str(p, 'type') === 'GRADIENT_LINEAR' ? linearGradientCss(p, box) : undefined,
|
|
100
206
|
imageHash: hex(bytes(image, 'hash')),
|
|
101
207
|
imageName: str(image, 'name'),
|
|
102
208
|
imageScaleMode: str(p, 'imageScaleMode'),
|
|
@@ -142,16 +248,49 @@ export function paddingOf(node) {
|
|
|
142
248
|
left: r2(left ?? 0),
|
|
143
249
|
};
|
|
144
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Figma's "Auto" spacing: the gap is whatever spreads the children across the box, and the stored
|
|
253
|
+
* `stackSpacing` is ignored. Kiwi writes it as SPACE_EVENLY; the plugin and REST APIs call it
|
|
254
|
+
* SPACE_BETWEEN, and so does newer kiwi. Measured on the iOS 16 UI Kit: all 93 SPACE_EVENLY frames
|
|
255
|
+
* with two or more in-flow children sit flush against both paddings with equal gaps — CSS
|
|
256
|
+
* `justify-content: space-between` — and in 88 of them that gap is not the stored `stackSpacing`.
|
|
257
|
+
*/
|
|
258
|
+
const AUTO_SPACING = new Set(['SPACE_EVENLY', 'SPACE_BETWEEN']);
|
|
259
|
+
const HUG = new Set(['RESIZE_TO_FIT', 'RESIZE_TO_FIT_WITH_IMPLICIT_SIZE']);
|
|
260
|
+
/**
|
|
261
|
+
* How the stored spacings apply to a flow layout (HORIZONTAL / VERTICAL). Auto spacing has
|
|
262
|
+
* nothing to spread on an axis that hugs its content, so there the stored gap is kept. The
|
|
263
|
+
* counter spacing — the gap between wrapped lines — is used only when the layout wraps.
|
|
264
|
+
*/
|
|
265
|
+
function flowSpacing(node) {
|
|
266
|
+
const spaced = AUTO_SPACING.has(str(node, 'stackPrimaryAlignItems') ?? '');
|
|
267
|
+
const autoPrimary = spaced && !HUG.has(str(node, 'stackPrimarySizing') ?? '');
|
|
268
|
+
const wraps = str(node, 'stackWrap') === 'WRAP';
|
|
269
|
+
const autoCounter = wraps &&
|
|
270
|
+
str(node, 'stackCounterAlignContent') === 'SPACE_BETWEEN' &&
|
|
271
|
+
!HUG.has(str(node, 'stackCounterSizing') ?? '');
|
|
272
|
+
return {
|
|
273
|
+
primary: autoPrimary ? undefined : r2(num(node, 'stackSpacing')),
|
|
274
|
+
counter: wraps && !autoCounter ? r2(num(node, 'stackCounterSpacing')) : undefined,
|
|
275
|
+
spaced,
|
|
276
|
+
autoPrimary,
|
|
277
|
+
autoCounter,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
145
280
|
function autoLayoutOf(node) {
|
|
146
281
|
const mode = str(node, 'stackMode');
|
|
147
282
|
if (!mode || mode === 'NONE')
|
|
148
283
|
return undefined;
|
|
284
|
+
const flow = mode === 'HORIZONTAL' || mode === 'VERTICAL' ? flowSpacing(node) : undefined;
|
|
285
|
+
const primaryAlign = str(node, 'stackPrimaryAlignItems');
|
|
149
286
|
return compact({
|
|
150
287
|
mode,
|
|
151
|
-
spacing: r2(num(node, 'stackSpacing')),
|
|
152
|
-
counterSpacing:
|
|
288
|
+
spacing: flow ? (flow.autoPrimary ? 'auto' : flow.primary) : r2(num(node, 'stackSpacing')),
|
|
289
|
+
counterSpacing: flow
|
|
290
|
+
? flow.autoCounter ? 'auto' : flow.counter
|
|
291
|
+
: r2(num(node, 'stackCounterSpacing')),
|
|
153
292
|
padding: paddingOf(node),
|
|
154
|
-
primaryAlign:
|
|
293
|
+
primaryAlign: flow?.spaced ? 'SPACE_BETWEEN' : primaryAlign,
|
|
155
294
|
counterAlign: str(node, 'stackCounterAlignItems'),
|
|
156
295
|
counterAlignContent: str(node, 'stackCounterAlignContent'),
|
|
157
296
|
primarySizing: str(node, 'stackPrimarySizing'),
|
|
@@ -192,6 +331,34 @@ export function lengthString(o) {
|
|
|
192
331
|
return String(r2(v));
|
|
193
332
|
}
|
|
194
333
|
}
|
|
334
|
+
/**
|
|
335
|
+
* A line height as CSS reads it. Figma stores its "Auto" line height — the font's own, about 1.2x
|
|
336
|
+
* the size — as 100 PERCENT (the REST API calls that unit INTRINSIC_%). On the iOS 16 UI Kit all
|
|
337
|
+
* 56 such texts are laid out at exactly their font's natural line height, which a CSS `100%`
|
|
338
|
+
* (1.0x the size) would not give, so it is reported as "auto" (CSS `normal`).
|
|
339
|
+
*/
|
|
340
|
+
export function lineHeightString(o) {
|
|
341
|
+
if (str(o, 'units') === 'PERCENT' && num(o, 'value') === 100)
|
|
342
|
+
return 'auto';
|
|
343
|
+
return lengthString(o);
|
|
344
|
+
}
|
|
345
|
+
/** A letter spacing as CSS reads it: Figma's percent is of the font size, which CSS spells `em`. */
|
|
346
|
+
export function letterSpacingString(o) {
|
|
347
|
+
const v = num(o, 'value');
|
|
348
|
+
if (str(o, 'units') !== 'PERCENT' || v === undefined)
|
|
349
|
+
return lengthString(o);
|
|
350
|
+
return `${Math.round(v * 100) / 10_000}em`;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* The colour of one SOLID paint as it shows: the paint's own opacity folded into the alpha, so
|
|
354
|
+
* `#3C3C43` at 60 % comes back as `#3C3C4399` rather than as an opaque colour.
|
|
355
|
+
*/
|
|
356
|
+
function shownColor(paint) {
|
|
357
|
+
const color = obj(paint, 'color');
|
|
358
|
+
if (!color)
|
|
359
|
+
return undefined;
|
|
360
|
+
return colorHex({ ...color, a: (num(color, 'a') ?? 1) * (num(paint, 'opacity') ?? 1) });
|
|
361
|
+
}
|
|
195
362
|
const CHARACTERS_PREVIEW = 400;
|
|
196
363
|
export function textBasics(node) {
|
|
197
364
|
const textData = obj(node, 'textData');
|
|
@@ -207,8 +374,8 @@ export function textBasics(node) {
|
|
|
207
374
|
charactersTruncated: truncated || undefined,
|
|
208
375
|
font: font ? `${str(font, 'family') ?? ''} ${str(font, 'style') ?? ''}`.trim() : undefined,
|
|
209
376
|
fontSize: r2(num(node, 'fontSize')),
|
|
210
|
-
lineHeight:
|
|
211
|
-
letterSpacing:
|
|
377
|
+
lineHeight: lineHeightString(obj(node, 'lineHeight')),
|
|
378
|
+
letterSpacing: letterSpacingString(obj(node, 'letterSpacing')),
|
|
212
379
|
paragraphSpacing: r2(num(node, 'paragraphSpacing')),
|
|
213
380
|
align: str(node, 'textAlignHorizontal'),
|
|
214
381
|
verticalAlign: str(node, 'textAlignVertical'),
|
|
@@ -229,10 +396,12 @@ const STYLE_REF_FIELDS = {
|
|
|
229
396
|
/**
|
|
230
397
|
* Shared-style references.
|
|
231
398
|
*
|
|
232
|
-
* A style definition is stored as an ordinary (hidden) node parked on a canvas
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
399
|
+
* A style definition is stored as an ordinary (hidden) node parked on a canvas — text styles are
|
|
400
|
+
* TEXT nodes, fill styles are ROUNDED_RECTANGLE nodes. A published style is referenced by the
|
|
401
|
+
* publishable `key` it carries (`styleIdFor*.assetRef.key`), a style that was never published by
|
|
402
|
+
* its guid (`styleIdFor*.guid`). Either way a style used from the same file resolves to a real
|
|
403
|
+
* node and, with `resolve`, to the values it defines. Styles from other libraries stay opaque
|
|
404
|
+
* refs, and the detached-style sentinel is no style at all.
|
|
236
405
|
*/
|
|
237
406
|
export function styleRefsOf(idx, node, opts = {}) {
|
|
238
407
|
const out = {};
|
|
@@ -240,21 +409,30 @@ export function styleRefsOf(idx, node, opts = {}) {
|
|
|
240
409
|
const ref = obj(node, field);
|
|
241
410
|
if (!ref)
|
|
242
411
|
continue;
|
|
243
|
-
const local = idx.resolveAssetRef(ref);
|
|
244
412
|
const guid = guidKey(obj(ref, 'guid'));
|
|
413
|
+
const assetRef = assetRefString(ref);
|
|
414
|
+
if (guid === DETACHED_STYLE_GUID && !assetRef)
|
|
415
|
+
continue;
|
|
416
|
+
const local = idx.resolveAssetRef(ref) ?? resolveStyle(idx, ref);
|
|
245
417
|
out[label] = compact({
|
|
246
418
|
name: local ? str(local.node, 'name') : undefined,
|
|
247
419
|
guid: local?.key ?? guid,
|
|
248
|
-
assetRef
|
|
420
|
+
assetRef,
|
|
249
421
|
defines: opts.resolve && local ? styleDefinition(idx, label, local.node) : undefined,
|
|
250
422
|
});
|
|
251
423
|
}
|
|
252
424
|
return orUndefined(out);
|
|
253
425
|
}
|
|
426
|
+
/** Typography fields a text style does not define: its node carries them only for its preview. */
|
|
427
|
+
const NOT_IN_TEXT_STYLE = ['color', 'fills', 'align', 'verticalAlign', 'styles'];
|
|
254
428
|
/** The values a locally-defined style carries: typography for text, paints for fill/stroke. */
|
|
255
429
|
function styleDefinition(idx, label, definition) {
|
|
256
|
-
if (label === 'text')
|
|
257
|
-
|
|
430
|
+
if (label === 'text') {
|
|
431
|
+
const defined = typography(idx, definition);
|
|
432
|
+
for (const field of NOT_IN_TEXT_STYLE)
|
|
433
|
+
delete defined[field];
|
|
434
|
+
return orUndefined(defined);
|
|
435
|
+
}
|
|
258
436
|
if (label === 'effect') {
|
|
259
437
|
const effects = objArr(definition, 'effects').map((e) => effectView(idx, e));
|
|
260
438
|
return effects.length ? { effects } : undefined;
|
|
@@ -291,7 +469,9 @@ function componentOf(idx, t) {
|
|
|
291
469
|
const type = str(node, 'type');
|
|
292
470
|
if (type === 'INSTANCE') {
|
|
293
471
|
const symbolData = obj(node, 'symbolData');
|
|
294
|
-
|
|
472
|
+
// A layer inside an instance may have had its symbol swapped by a record or a property.
|
|
473
|
+
const symbolId = guidKey(obj(node, 'overriddenSymbolID')) ??
|
|
474
|
+
guidKey(obj(symbolData, 'symbolID'));
|
|
295
475
|
const symbol = symbolId ? idx.node(symbolId) : undefined;
|
|
296
476
|
return compact({
|
|
297
477
|
role: 'INSTANCE',
|
|
@@ -350,12 +530,16 @@ function orUndefined(o) {
|
|
|
350
530
|
}
|
|
351
531
|
export function nodeDetail(idx, t, opts = {}) {
|
|
352
532
|
const node = t.node;
|
|
533
|
+
// Paints, effects and typography as Figma shows them: a referenced style wins (F19, F22).
|
|
534
|
+
const live = applyStyles(idx, node, { text: true });
|
|
353
535
|
const maxChildren = opts.maxChildren ?? 100;
|
|
354
|
-
const
|
|
355
|
-
const
|
|
356
|
-
const
|
|
357
|
-
const
|
|
536
|
+
const box = paintBox(node);
|
|
537
|
+
const fills = objArr(live, 'fillPaints').map((p) => paintView(idx, p, box));
|
|
538
|
+
const strokes = objArr(live, 'strokePaints').map((p) => paintView(idx, p, box));
|
|
539
|
+
const effects = objArr(live, 'effects').map((e) => effectView(idx, e));
|
|
540
|
+
const images = nodeImageHashes(live);
|
|
358
541
|
const dashPattern = node['dashPattern'];
|
|
542
|
+
const stroked = hasVisibleStroke(live);
|
|
359
543
|
return compact({
|
|
360
544
|
guid: t.key,
|
|
361
545
|
type: str(node, 'type') ?? 'UNKNOWN',
|
|
@@ -378,17 +562,18 @@ export function nodeDetail(idx, t, opts = {}) {
|
|
|
378
562
|
cornerRadius: cornerRadiusOf(node),
|
|
379
563
|
fills: fills.length ? fills : undefined,
|
|
380
564
|
strokes: strokes.length ? strokes : undefined,
|
|
381
|
-
strokeWeight: num(node, 'strokeWeight') ? r2(num(node, 'strokeWeight')) : undefined,
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
565
|
+
strokeWeight: stroked && num(node, 'strokeWeight') ? r2(num(node, 'strokeWeight')) : undefined,
|
|
566
|
+
strokeWeights: stroked ? sideWeights(node) : undefined,
|
|
567
|
+
strokeAlign: stroked ? str(node, 'strokeAlign') : undefined,
|
|
568
|
+
strokeCap: stroked ? str(node, 'strokeCap') : undefined,
|
|
569
|
+
strokeJoin: stroked ? str(node, 'strokeJoin') : undefined,
|
|
570
|
+
dashPattern: stroked && Array.isArray(dashPattern) && dashPattern.length
|
|
386
571
|
? dashPattern.map((v) => r2(v))
|
|
387
572
|
: undefined,
|
|
388
573
|
effects: effects.length ? effects : undefined,
|
|
389
574
|
autoLayout: autoLayoutOf(node),
|
|
390
575
|
layout: layoutChildOf(node),
|
|
391
|
-
text: textBasics(
|
|
576
|
+
text: textBasics(live),
|
|
392
577
|
component: componentOf(idx, t),
|
|
393
578
|
styles: styleRefsOf(idx, node),
|
|
394
579
|
variables: variableBindingsOf(idx, node),
|
|
@@ -403,24 +588,28 @@ export function nodeDetail(idx, t, opts = {}) {
|
|
|
403
588
|
/**
|
|
404
589
|
* Typography of a node — or of a partial style-override record from a text run — reporting only
|
|
405
590
|
* the fields it actually sets, so a run's block shows exactly what differs from the base style.
|
|
591
|
+
* Pass the node with its styles applied (`applyStyles(…, { text: true })`): the values cached
|
|
592
|
+
* beside a style reference can be stale (F19, F22).
|
|
406
593
|
*/
|
|
407
594
|
export function typography(idx, node, opts = {}) {
|
|
408
595
|
const font = obj(node, 'fontName');
|
|
409
|
-
|
|
410
|
-
|
|
596
|
+
// One colour only when one paint shows and it is solid; otherwise every paint that shows.
|
|
597
|
+
// (An invisible paint, or the first solid under two more, is not the text's colour.)
|
|
598
|
+
const shown = objArr(node, 'fillPaints').filter((p) => bool(p, 'visible') !== false);
|
|
599
|
+
const solid = shown.length === 1 && str(shown[0], 'type') === 'SOLID' ? shown[0] : undefined;
|
|
411
600
|
return compact({
|
|
412
601
|
font: font ? `${str(font, 'family') ?? ''} ${str(font, 'style') ?? ''}`.trim() : undefined,
|
|
413
602
|
fontPostScript: str(font, 'postscript'),
|
|
414
603
|
fontSize: r2(num(node, 'fontSize')),
|
|
415
|
-
lineHeight:
|
|
416
|
-
letterSpacing:
|
|
604
|
+
lineHeight: lineHeightString(obj(node, 'lineHeight')),
|
|
605
|
+
letterSpacing: letterSpacingString(obj(node, 'letterSpacing')),
|
|
417
606
|
paragraphSpacing: r2(num(node, 'paragraphSpacing')),
|
|
418
607
|
textCase: str(node, 'textCase'),
|
|
419
608
|
textDecoration: str(node, 'textDecoration'),
|
|
420
609
|
align: str(node, 'textAlignHorizontal'),
|
|
421
610
|
verticalAlign: str(node, 'textAlignVertical'),
|
|
422
|
-
color: solid ?
|
|
423
|
-
fills:
|
|
611
|
+
color: solid ? shownColor(solid) : undefined,
|
|
612
|
+
fills: shown.length && !solid ? shown.map((p) => paintView(idx, p, paintBox(node))) : undefined,
|
|
424
613
|
// Runs express most overrides as a shared-style ref, so keep them here by default; the
|
|
425
614
|
// caller turns them off when it already reports the node's style refs separately.
|
|
426
615
|
styles: opts.includeStyleRefs === false ? undefined : styleRefsOf(idx, node),
|
|
@@ -468,12 +657,15 @@ export function styleBlock(idx, t) {
|
|
|
468
657
|
const size = obj(node, 'size');
|
|
469
658
|
const mode = str(node, 'stackMode');
|
|
470
659
|
const padding = paddingOf(node);
|
|
660
|
+
// Under auto spacing `justify-content` alone reproduces Figma's gap; a `gap` taken from the
|
|
661
|
+
// ignored `stackSpacing` would be a CSS minimum that overflows a narrower box.
|
|
662
|
+
const flow = mode === 'HORIZONTAL' || mode === 'VERTICAL' ? flowSpacing(node) : undefined;
|
|
471
663
|
const layout = mode && mode !== 'NONE'
|
|
472
664
|
? compact({
|
|
473
665
|
display: 'flex',
|
|
474
666
|
direction: mode === 'VERTICAL' ? 'column' : mode === 'GRID' ? 'grid' : 'row',
|
|
475
|
-
gap: r2(num(node, 'stackSpacing')),
|
|
476
|
-
rowGap: r2(num(node, 'stackCounterSpacing')),
|
|
667
|
+
gap: flow ? flow.primary : r2(num(node, 'stackSpacing')),
|
|
668
|
+
rowGap: flow ? flow.counter : r2(num(node, 'stackCounterSpacing')),
|
|
477
669
|
padding: padding ? cssPadding(padding) : undefined,
|
|
478
670
|
paddingBox: padding,
|
|
479
671
|
justifyContent: JUSTIFY[str(node, 'stackPrimaryAlignItems') ?? ''],
|
|
@@ -496,11 +688,15 @@ export function styleBlock(idx, t) {
|
|
|
496
688
|
left: r2(num(node, 'stackChildMarginLeft')),
|
|
497
689
|
})),
|
|
498
690
|
}));
|
|
499
|
-
|
|
500
|
-
const
|
|
501
|
-
const
|
|
691
|
+
// Paints, effects and typography as Figma shows them: a referenced style wins (F19, F22).
|
|
692
|
+
const live = applyStyles(idx, node, { text: true });
|
|
693
|
+
const box = paintBox(node);
|
|
694
|
+
const fills = objArr(live, 'fillPaints').map((p) => paintView(idx, p, box));
|
|
695
|
+
const strokes = objArr(live, 'strokePaints').map((p) => paintView(idx, p, box));
|
|
696
|
+
const effects = objArr(live, 'effects').map((e) => effectView(idx, e));
|
|
502
697
|
const type = str(node, 'type');
|
|
503
698
|
const dashPattern = node['dashPattern'];
|
|
699
|
+
const stroked = hasVisibleStroke(live);
|
|
504
700
|
return compact({
|
|
505
701
|
guid: t.key,
|
|
506
702
|
type,
|
|
@@ -512,15 +708,16 @@ export function styleBlock(idx, t) {
|
|
|
512
708
|
cornerRadius: cornerRadiusOf(node),
|
|
513
709
|
fills: fills.length ? fills : undefined,
|
|
514
710
|
strokes: strokes.length ? strokes : undefined,
|
|
515
|
-
strokeWeight: num(node, 'strokeWeight') ? r2(num(node, 'strokeWeight')) : undefined,
|
|
516
|
-
|
|
517
|
-
|
|
711
|
+
strokeWeight: stroked && num(node, 'strokeWeight') ? r2(num(node, 'strokeWeight')) : undefined,
|
|
712
|
+
strokeWeights: stroked ? sideWeights(node) : undefined,
|
|
713
|
+
strokeAlign: stroked ? str(node, 'strokeAlign') : undefined,
|
|
714
|
+
dashPattern: stroked && Array.isArray(dashPattern) && dashPattern.length
|
|
518
715
|
? dashPattern.map((v) => r2(v))
|
|
519
716
|
: undefined,
|
|
520
717
|
effects: effects.length ? effects : undefined,
|
|
521
718
|
layout,
|
|
522
719
|
inParentLayout: inParent,
|
|
523
|
-
typography: type === 'TEXT' ? orUndefined(typography(idx,
|
|
720
|
+
typography: type === 'TEXT' ? orUndefined(typography(idx, live, { includeStyleRefs: false })) : undefined,
|
|
524
721
|
constraints: orUndefined(compact({
|
|
525
722
|
horizontal: str(node, 'horizontalConstraint'),
|
|
526
723
|
vertical: str(node, 'verticalConstraint'),
|
package/dist/model/text.d.ts
CHANGED
|
@@ -19,7 +19,11 @@ export interface TextView {
|
|
|
19
19
|
/** True when `characterStyleIDs` is present and mentions any non-zero id. */
|
|
20
20
|
hasStyledRuns: boolean;
|
|
21
21
|
}
|
|
22
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* The four fields a copy inventory actually needs, ~6x cheaper than the full typography. Text
|
|
24
|
+
* painted with more than one fill, or with a gradient or image, has no single colour: its fills
|
|
25
|
+
* come back instead.
|
|
26
|
+
*/
|
|
23
27
|
export declare function compactStyle(style: Record<string, unknown> | undefined): Record<string, unknown> | undefined;
|
|
24
28
|
/**
|
|
25
29
|
* Group `characterStyleIDs` into maximal runs of equal id. Consecutive characters sharing an id
|
package/dist/model/text.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { compact, num, obj, objArr, str } from './access.js';
|
|
2
2
|
import { typography } from './summarize.js';
|
|
3
|
-
|
|
3
|
+
import { applyStyles } from './styles.js';
|
|
4
|
+
/**
|
|
5
|
+
* The four fields a copy inventory actually needs, ~6x cheaper than the full typography. Text
|
|
6
|
+
* painted with more than one fill, or with a gradient or image, has no single colour: its fills
|
|
7
|
+
* come back instead.
|
|
8
|
+
*/
|
|
4
9
|
export function compactStyle(style) {
|
|
5
10
|
if (!style)
|
|
6
11
|
return undefined;
|
|
@@ -9,6 +14,7 @@ export function compactStyle(style) {
|
|
|
9
14
|
size: style['fontSize'],
|
|
10
15
|
lineHeight: style['lineHeight'],
|
|
11
16
|
color: style['color'],
|
|
17
|
+
fills: style['color'] === undefined ? style['fills'] : undefined,
|
|
12
18
|
});
|
|
13
19
|
}
|
|
14
20
|
/**
|
|
@@ -22,10 +28,12 @@ export function textView(idx, t, includeRuns) {
|
|
|
22
28
|
return undefined;
|
|
23
29
|
const ids = textData?.['characterStyleIDs'] ?? [];
|
|
24
30
|
const hasStyledRuns = ids.some((id) => id !== 0);
|
|
31
|
+
// The typography Figma lays the text out with: a referenced style wins over the cached copy
|
|
32
|
+
// (F19, F22), for the node and for each run's override entry alike.
|
|
25
33
|
const view = {
|
|
26
34
|
characters,
|
|
27
35
|
characterCount: characters.length,
|
|
28
|
-
baseStyle: orUndefined(typography(idx, t.node)),
|
|
36
|
+
baseStyle: orUndefined(typography(idx, applyStyles(idx, t.node, { text: true }))),
|
|
29
37
|
hasStyledRuns,
|
|
30
38
|
};
|
|
31
39
|
if (!includeRuns)
|
|
@@ -49,7 +57,9 @@ export function textView(idx, t, includeRuns) {
|
|
|
49
57
|
end: i,
|
|
50
58
|
text: characters.slice(start, i),
|
|
51
59
|
styleID,
|
|
52
|
-
style: override
|
|
60
|
+
style: override
|
|
61
|
+
? orUndefined(typography(idx, applyStyles(idx, override, { text: true, partial: true })))
|
|
62
|
+
: undefined,
|
|
53
63
|
}));
|
|
54
64
|
start = i;
|
|
55
65
|
}
|
package/dist/model/tree.d.ts
CHANGED
|
@@ -18,8 +18,10 @@ export declare function guidKey(g: Guid | undefined): string | undefined;
|
|
|
18
18
|
*
|
|
19
19
|
* Figma writes node ids with a dash in links (`node-id=3017-121`), older links percent-encode
|
|
20
20
|
* the colon, and people paste the whole link. A guid is always two integers, so `3017-121` can
|
|
21
|
-
* never be a valid one itself and rewriting it is unambiguous.
|
|
22
|
-
*
|
|
21
|
+
* never be a valid one itself and rewriting it is unambiguous. A layer inside a component
|
|
22
|
+
* instance has an id of the form `I<guid>;<guid>…` (model/scene.ts), which links write as
|
|
23
|
+
* `I3017-121%3B5-6`; it is canonicalised segment by segment. Anything unrecognised is handed back
|
|
24
|
+
* untouched, so a genuine typo still fails loudly instead of being guessed at.
|
|
23
25
|
*/
|
|
24
26
|
export declare function canonicalGuid(input: string): string;
|
|
25
27
|
/** One wording for a missing guid, shared by the MCP tools and the renderer. */
|
|
@@ -36,13 +38,20 @@ export interface TreeNode {
|
|
|
36
38
|
/** Nearest CANVAS ancestor (the page a node lives on); undefined for DOCUMENT and pages. */
|
|
37
39
|
page: TreeNode | undefined;
|
|
38
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* The "no value" guid Figma writes into a guid field that is unset: a detached style reference
|
|
43
|
+
* (render-implementation-plan.md F19) and an `overrideKey` that names nothing.
|
|
44
|
+
*/
|
|
45
|
+
export declare const NO_GUID = "4294967295:4294967295";
|
|
39
46
|
/**
|
|
40
47
|
* The identity that instance override paths (`symbolData.symbolOverrides[].guidPath.guids` and
|
|
41
48
|
* `derivedSymbolData[].guidPath.guids`) use for a component descendant: its `overrideKey` when
|
|
42
49
|
* it has one — a node created by copying a component keeps the original's — else its own guid.
|
|
43
50
|
*
|
|
44
51
|
* Measured on the sample: 8 893 of 10 480 symbol descendants carry an explicit key; the other
|
|
45
|
-
* 1 587 are addressed by guid, by 30 061 of the file's 303 833 records.
|
|
52
|
+
* 1 587 are addressed by guid, by 30 061 of the file's 303 833 records. An `overrideKey` of
|
|
53
|
+
* `NO_GUID` is no key at all (F23): on the iOS 16 UI Kit 30 of the 44 keyed layers carry it, no
|
|
54
|
+
* record path ever names it, and 31 record path segments name those layers by their guid.
|
|
46
55
|
*/
|
|
47
56
|
export declare function overrideIdentity(t: TreeNode): string;
|
|
48
57
|
export interface Tree {
|
package/dist/model/tree.js
CHANGED
|
@@ -9,14 +9,22 @@ export function guidKey(g) {
|
|
|
9
9
|
*
|
|
10
10
|
* Figma writes node ids with a dash in links (`node-id=3017-121`), older links percent-encode
|
|
11
11
|
* the colon, and people paste the whole link. A guid is always two integers, so `3017-121` can
|
|
12
|
-
* never be a valid one itself and rewriting it is unambiguous.
|
|
13
|
-
*
|
|
12
|
+
* never be a valid one itself and rewriting it is unambiguous. A layer inside a component
|
|
13
|
+
* instance has an id of the form `I<guid>;<guid>…` (model/scene.ts), which links write as
|
|
14
|
+
* `I3017-121%3B5-6`; it is canonicalised segment by segment. Anything unrecognised is handed back
|
|
15
|
+
* untouched, so a genuine typo still fails loudly instead of being guessed at.
|
|
14
16
|
*/
|
|
15
17
|
export function canonicalGuid(input) {
|
|
16
18
|
const raw = input.trim();
|
|
17
19
|
const inLink = /(?:^|[?&#])node-id=([^&#\s]+)/i.exec(raw);
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
+
const text = decode(inLink ? inLink[1] : raw).trim();
|
|
21
|
+
const pair = /^(\d+)[:-](\d+)$/.exec(text);
|
|
22
|
+
if (pair)
|
|
23
|
+
return `${pair[1]}:${pair[2]}`;
|
|
24
|
+
const sublayer = /^I(\d+[:-]\d+(?:;\d+[:-]\d+)+)$/.exec(text);
|
|
25
|
+
if (sublayer)
|
|
26
|
+
return `I${sublayer[1].split(';').map((g) => g.replace('-', ':')).join(';')}`;
|
|
27
|
+
return raw;
|
|
20
28
|
}
|
|
21
29
|
function decode(s) {
|
|
22
30
|
try {
|
|
@@ -31,21 +39,30 @@ export function guidNotFound(input, key) {
|
|
|
31
39
|
const read = key === input.trim() ? '' : ` (read as ${JSON.stringify(key)})`;
|
|
32
40
|
return (`no node with guid ${JSON.stringify(input)}${read} in this file. ` +
|
|
33
41
|
'Guids look like "2:1339"; a Figma link spells the same id "node-id=3017-121", and ' +
|
|
34
|
-
'that form, or the whole link, is accepted.
|
|
42
|
+
'that form, or the whole link, is accepted. A layer inside a component instance is ' +
|
|
43
|
+
'"I<instance guid>;<layer guid>", as fig_tree lists it. Use fig_find or fig_tree to find guids.');
|
|
35
44
|
}
|
|
36
45
|
export function readGuid(obj, field) {
|
|
37
46
|
return guidKey(obj?.[field]);
|
|
38
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* The "no value" guid Figma writes into a guid field that is unset: a detached style reference
|
|
50
|
+
* (render-implementation-plan.md F19) and an `overrideKey` that names nothing.
|
|
51
|
+
*/
|
|
52
|
+
export const NO_GUID = '4294967295:4294967295';
|
|
39
53
|
/**
|
|
40
54
|
* The identity that instance override paths (`symbolData.symbolOverrides[].guidPath.guids` and
|
|
41
55
|
* `derivedSymbolData[].guidPath.guids`) use for a component descendant: its `overrideKey` when
|
|
42
56
|
* it has one — a node created by copying a component keeps the original's — else its own guid.
|
|
43
57
|
*
|
|
44
58
|
* Measured on the sample: 8 893 of 10 480 symbol descendants carry an explicit key; the other
|
|
45
|
-
* 1 587 are addressed by guid, by 30 061 of the file's 303 833 records.
|
|
59
|
+
* 1 587 are addressed by guid, by 30 061 of the file's 303 833 records. An `overrideKey` of
|
|
60
|
+
* `NO_GUID` is no key at all (F23): on the iOS 16 UI Kit 30 of the 44 keyed layers carry it, no
|
|
61
|
+
* record path ever names it, and 31 record path segments name those layers by their guid.
|
|
46
62
|
*/
|
|
47
63
|
export function overrideIdentity(t) {
|
|
48
|
-
|
|
64
|
+
const key = readGuid(t.node, 'overrideKey');
|
|
65
|
+
return key !== undefined && key !== NO_GUID ? key : t.key;
|
|
49
66
|
}
|
|
50
67
|
/** Raw code-unit comparison — see pitfall §11.4. */
|
|
51
68
|
function comparePosition(a, b) {
|