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
|
@@ -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
|
+
}
|
|
@@ -11,6 +11,14 @@ import type { NodeChange } from '../fig/parse.js';
|
|
|
11
11
|
import type { KiwiObject } from '../fig/kiwi.js';
|
|
12
12
|
import type { FileIndex } from './index.js';
|
|
13
13
|
import { type TreeNode } from './tree.js';
|
|
14
|
+
import { type ApplyStylesOptions } from './styles.js';
|
|
15
|
+
/**
|
|
16
|
+
* `node` — the layer `t`, or one of its partial run entries — with paints, effects and typography
|
|
17
|
+
* as Figma shows them: a referenced style wins over the copy cached beside it (F19, F22), and for
|
|
18
|
+
* a layer inside an instance a colour bound to a variable is read in the modes in force there
|
|
19
|
+
* (F29), exactly as the renderer reads it.
|
|
20
|
+
*/
|
|
21
|
+
export declare function shownStyle(idx: FileIndex, t: TreeNode, node?: NodeChange, opts?: ApplyStylesOptions): NodeChange;
|
|
14
22
|
export interface NodeSummary {
|
|
15
23
|
guid: string;
|
|
16
24
|
type: string;
|
|
@@ -18,6 +26,8 @@ export interface NodeSummary {
|
|
|
18
26
|
/** `"134x40"` — denser than `{x,y}` and still trivially parseable. */
|
|
19
27
|
size?: string;
|
|
20
28
|
children?: number;
|
|
29
|
+
/** The layer itself is hidden — set directly, or by a component property. */
|
|
30
|
+
hidden?: boolean;
|
|
21
31
|
page?: string;
|
|
22
32
|
}
|
|
23
33
|
export declare function summarize(t: TreeNode, opts?: {
|
|
@@ -30,7 +40,31 @@ export declare function absoluteOrigin(t: TreeNode): {
|
|
|
30
40
|
x: number;
|
|
31
41
|
y: number;
|
|
32
42
|
} | undefined;
|
|
33
|
-
|
|
43
|
+
/**
|
|
44
|
+
* One radius, or four in CSS order (top-left, top-right, bottom-right, bottom-left). With
|
|
45
|
+
* independent corners, a corner the record does not write is square, whatever `cornerRadius`
|
|
46
|
+
* says (F24): on the iOS 16 UI Kit every one of the 162 such corners is square in Figma's own
|
|
47
|
+
* outline, 38 of them beside a non-zero `cornerRadius` — a notification rounded only at the top.
|
|
48
|
+
*/
|
|
49
|
+
export declare function cornerRadiusOf(node: NodeChange): unknown;
|
|
50
|
+
/**
|
|
51
|
+
* Per-side stroke weights, when the sides are set independently (F25): Figma then draws each side
|
|
52
|
+
* at its own weight, whatever the node's `strokeWeight` says — the hairline under a navigation
|
|
53
|
+
* bar is `bottom: 0.5` beside a `strokeWeight` of 0, the one over a tab bar `top: 0.5` beside 1.
|
|
54
|
+
*/
|
|
55
|
+
export declare function sideWeights(node: NodeChange): Record<string, number> | undefined;
|
|
56
|
+
/** The size of the box a paint fills, which a gradient's direction is relative to. */
|
|
57
|
+
export interface PaintBox {
|
|
58
|
+
readonly w: number;
|
|
59
|
+
readonly h: number;
|
|
60
|
+
}
|
|
61
|
+
/** The node's own box, for `paintView`. */
|
|
62
|
+
export declare function paintBox(node: NodeChange): PaintBox | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* One paint. Given the box it fills, a linear gradient also comes back as `css` — its direction
|
|
65
|
+
* is otherwise nowhere in the output.
|
|
66
|
+
*/
|
|
67
|
+
export declare function paintView(idx: FileIndex, p: KiwiObject, box?: PaintBox): Record<string, unknown>;
|
|
34
68
|
export declare function effectView(idx: FileIndex, e: KiwiObject): Record<string, unknown>;
|
|
35
69
|
export interface Padding {
|
|
36
70
|
top: number;
|
|
@@ -45,14 +79,25 @@ export interface Padding {
|
|
|
45
79
|
export declare function paddingOf(node: NodeChange): Padding | undefined;
|
|
46
80
|
/** `{value, units}` rendered compactly: `"1.5"` (RAW multiplier), `"13px"`, `"150%"`. */
|
|
47
81
|
export declare function lengthString(o: KiwiObject | undefined): string | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* A line height as CSS reads it. Figma stores its "Auto" line height — the font's own, about 1.2x
|
|
84
|
+
* the size — as 100 PERCENT (the REST API calls that unit INTRINSIC_%). On the iOS 16 UI Kit all
|
|
85
|
+
* 56 such texts are laid out at exactly their font's natural line height, which a CSS `100%`
|
|
86
|
+
* (1.0x the size) would not give, so it is reported as "auto" (CSS `normal`).
|
|
87
|
+
*/
|
|
88
|
+
export declare function lineHeightString(o: KiwiObject | undefined): string | undefined;
|
|
89
|
+
/** A letter spacing as CSS reads it: Figma's percent is of the font size, which CSS spells `em`. */
|
|
90
|
+
export declare function letterSpacingString(o: KiwiObject | undefined): string | undefined;
|
|
48
91
|
export declare function textBasics(node: NodeChange): Record<string, unknown> | undefined;
|
|
49
92
|
/**
|
|
50
93
|
* Shared-style references.
|
|
51
94
|
*
|
|
52
|
-
* A style definition is stored as an ordinary (hidden) node parked on a canvas
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
95
|
+
* A style definition is stored as an ordinary (hidden) node parked on a canvas — text styles are
|
|
96
|
+
* TEXT nodes, fill styles are ROUNDED_RECTANGLE nodes. A published style is referenced by the
|
|
97
|
+
* publishable `key` it carries (`styleIdFor*.assetRef.key`), a style that was never published by
|
|
98
|
+
* its guid (`styleIdFor*.guid`). Either way a style used from the same file resolves to a real
|
|
99
|
+
* node and, with `resolve`, to the values it defines. Styles from other libraries stay opaque
|
|
100
|
+
* refs, and the detached-style sentinel is no style at all.
|
|
56
101
|
*/
|
|
57
102
|
export declare function styleRefsOf(idx: FileIndex, node: NodeChange, opts?: {
|
|
58
103
|
resolve?: boolean;
|
|
@@ -68,6 +113,8 @@ export declare function nodeDetail(idx: FileIndex, t: TreeNode, opts?: NodeDetai
|
|
|
68
113
|
/**
|
|
69
114
|
* Typography of a node — or of a partial style-override record from a text run — reporting only
|
|
70
115
|
* the fields it actually sets, so a run's block shows exactly what differs from the base style.
|
|
116
|
+
* Pass the node with its styles applied (`applyStyles(…, { text: true })`): the values cached
|
|
117
|
+
* beside a style reference can be stale (F19, F22).
|
|
71
118
|
*/
|
|
72
119
|
export declare function typography(idx: FileIndex, node: NodeChange, opts?: {
|
|
73
120
|
includeStyleRefs?: boolean;
|
package/dist/model/summarize.js
CHANGED
|
@@ -2,6 +2,19 @@ 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';
|
|
6
|
+
import { applyColours, bindingModesOf } from './bindings.js';
|
|
7
|
+
/**
|
|
8
|
+
* `node` — the layer `t`, or one of its partial run entries — with paints, effects and typography
|
|
9
|
+
* as Figma shows them: a referenced style wins over the copy cached beside it (F19, F22), and for
|
|
10
|
+
* a layer inside an instance a colour bound to a variable is read in the modes in force there
|
|
11
|
+
* (F29), exactly as the renderer reads it.
|
|
12
|
+
*/
|
|
13
|
+
export function shownStyle(idx, t, node = t.node, opts = { text: true }) {
|
|
14
|
+
const styled = applyStyles(idx, node, opts);
|
|
15
|
+
const modes = bindingModesOf(t);
|
|
16
|
+
return modes ? applyColours(idx, styled, modes).node : styled;
|
|
17
|
+
}
|
|
5
18
|
export function summarize(t, opts = {}) {
|
|
6
19
|
const size = obj(t.node, 'size');
|
|
7
20
|
const w = num(size, 'x');
|
|
@@ -12,6 +25,7 @@ export function summarize(t, opts = {}) {
|
|
|
12
25
|
name: str(t.node, 'name'),
|
|
13
26
|
size: w !== undefined && h !== undefined ? `${r2(w)}x${r2(h)}` : undefined,
|
|
14
27
|
children: t.children.length || undefined,
|
|
28
|
+
hidden: bool(t.node, 'visible') === false || undefined,
|
|
15
29
|
page: opts.page && t.page ? str(t.page.node, 'name') : undefined,
|
|
16
30
|
});
|
|
17
31
|
}
|
|
@@ -25,6 +39,8 @@ export function outlineLine(t, depth) {
|
|
|
25
39
|
parts.push(`(${s.guid})`);
|
|
26
40
|
if (s.children)
|
|
27
41
|
parts.push(`x${s.children}`);
|
|
42
|
+
if (s.hidden)
|
|
43
|
+
parts.push('hidden');
|
|
28
44
|
return parts.join(' ');
|
|
29
45
|
}
|
|
30
46
|
// --------------------------------------------------------------------------- geometry
|
|
@@ -72,20 +88,121 @@ function geometryOf(t) {
|
|
|
72
88
|
});
|
|
73
89
|
return Object.keys(g).length ? g : undefined;
|
|
74
90
|
}
|
|
75
|
-
|
|
91
|
+
/**
|
|
92
|
+
* One radius, or four in CSS order (top-left, top-right, bottom-right, bottom-left). With
|
|
93
|
+
* independent corners, a corner the record does not write is square, whatever `cornerRadius`
|
|
94
|
+
* says (F24): on the iOS 16 UI Kit every one of the 162 such corners is square in Figma's own
|
|
95
|
+
* outline, 38 of them beside a non-zero `cornerRadius` — a notification rounded only at the top.
|
|
96
|
+
*/
|
|
97
|
+
export function cornerRadiusOf(node) {
|
|
76
98
|
const uniform = num(node, 'cornerRadius');
|
|
77
99
|
if (!bool(node, 'rectangleCornerRadiiIndependent'))
|
|
78
100
|
return r2(uniform);
|
|
79
101
|
const corners = [
|
|
80
|
-
num(node, 'rectangleTopLeftCornerRadius') ??
|
|
81
|
-
num(node, 'rectangleTopRightCornerRadius') ??
|
|
82
|
-
num(node, 'rectangleBottomRightCornerRadius') ??
|
|
83
|
-
num(node, 'rectangleBottomLeftCornerRadius') ??
|
|
102
|
+
num(node, 'rectangleTopLeftCornerRadius') ?? 0,
|
|
103
|
+
num(node, 'rectangleTopRightCornerRadius') ?? 0,
|
|
104
|
+
num(node, 'rectangleBottomRightCornerRadius') ?? 0,
|
|
105
|
+
num(node, 'rectangleBottomLeftCornerRadius') ?? 0,
|
|
84
106
|
].map((v) => r2(v));
|
|
85
107
|
return corners.every((v) => v === corners[0]) ? corners[0] : corners;
|
|
86
108
|
}
|
|
87
|
-
|
|
88
|
-
|
|
109
|
+
/** Does any stroke paint show? A weight, alignment, cap or dash describes nothing without one. */
|
|
110
|
+
function hasVisibleStroke(live) {
|
|
111
|
+
return objArr(live, 'strokePaints').some((p) => bool(p, 'visible') !== false);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Per-side stroke weights, when the sides are set independently (F25): Figma then draws each side
|
|
115
|
+
* at its own weight, whatever the node's `strokeWeight` says — the hairline under a navigation
|
|
116
|
+
* bar is `bottom: 0.5` beside a `strokeWeight` of 0, the one over a tab bar `top: 0.5` beside 1.
|
|
117
|
+
*/
|
|
118
|
+
export function sideWeights(node) {
|
|
119
|
+
if (bool(node, 'borderStrokeWeightsIndependent') !== true)
|
|
120
|
+
return undefined;
|
|
121
|
+
return {
|
|
122
|
+
top: r2(num(node, 'borderTopWeight') ?? 0),
|
|
123
|
+
right: r2(num(node, 'borderRightWeight') ?? 0),
|
|
124
|
+
bottom: r2(num(node, 'borderBottomWeight') ?? 0),
|
|
125
|
+
left: r2(num(node, 'borderLeftWeight') ?? 0),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** The node's own box, for `paintView`. */
|
|
129
|
+
export function paintBox(node) {
|
|
130
|
+
const size = obj(node, 'size');
|
|
131
|
+
const w = num(size, 'x');
|
|
132
|
+
const h = num(size, 'y');
|
|
133
|
+
return w !== undefined && h !== undefined && w > 0 && h > 0 ? { w, h } : undefined;
|
|
134
|
+
}
|
|
135
|
+
/** Steps a stretch between two stops is cut into when CSS would blend it differently. */
|
|
136
|
+
const BLEND_STEPS = 8;
|
|
137
|
+
/**
|
|
138
|
+
* Figma blends gradient colours unpremultiplied, CSS premultiplied: across a stretch where both
|
|
139
|
+
* the colour and the alpha change — transparent white into 50 % black — CSS loses the pale
|
|
140
|
+
* fringe Figma draws. Sampling such a stretch as Figma blends it keeps the two within a level or
|
|
141
|
+
* two; a stretch where only one of them changes blends the same either way and is left alone.
|
|
142
|
+
*/
|
|
143
|
+
function blendLikeFigma(stops) {
|
|
144
|
+
const out = [];
|
|
145
|
+
stops.forEach((s, i) => {
|
|
146
|
+
const prev = stops[i - 1];
|
|
147
|
+
if (prev && prev.a !== s.a && (prev.r !== s.r || prev.g !== s.g || prev.b !== s.b)) {
|
|
148
|
+
for (let k = 1; k < BLEND_STEPS; k++) {
|
|
149
|
+
const t = k / BLEND_STEPS;
|
|
150
|
+
const mix = (x, y) => x + (y - x) * t;
|
|
151
|
+
out.push({
|
|
152
|
+
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),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
out.push(s);
|
|
157
|
+
});
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* A linear gradient as CSS, for the box it fills. `transform` maps the normalized box to
|
|
162
|
+
* gradient space, where the gradient runs along x from 0 to 1 (F7), so its value at a pixel is
|
|
163
|
+
* `m00·x/w + m01·y/h + m02`: it grows along `(m00/w, m01/h)` — CSS's angle — and each stop goes
|
|
164
|
+
* where CSS's gradient line (through the centre, `|w·sin θ| + |h·cos θ|` long) reaches that
|
|
165
|
+
* value. Exact for any transform; stops take the paint's opacity into their alpha. Drawn by a
|
|
166
|
+
* browser, it matches `fig_render` to a mean of under one level per channel.
|
|
167
|
+
*/
|
|
168
|
+
function linearGradientCss(p, box) {
|
|
169
|
+
const m = obj(p, 'transform');
|
|
170
|
+
const a = num(m, 'm00') ?? 1;
|
|
171
|
+
const b = num(m, 'm01') ?? 0;
|
|
172
|
+
const c = num(m, 'm02') ?? 0;
|
|
173
|
+
const nx = a / box.w;
|
|
174
|
+
const ny = b / box.h;
|
|
175
|
+
const rate = Math.hypot(nx, ny);
|
|
176
|
+
if (!(rate > 1e-9))
|
|
177
|
+
return undefined;
|
|
178
|
+
const theta = Math.atan2(nx, -ny);
|
|
179
|
+
const line = Math.abs(box.w * Math.sin(theta)) + Math.abs(box.h * Math.cos(theta));
|
|
180
|
+
const atCentre = a / 2 + b / 2 + c;
|
|
181
|
+
const opacity = num(p, 'opacity') ?? 1;
|
|
182
|
+
const stops = objArr(p, 'stops')
|
|
183
|
+
.map((s) => {
|
|
184
|
+
const color = obj(s, 'color');
|
|
185
|
+
return {
|
|
186
|
+
at: Math.max(0, Math.min(1, num(s, 'position') ?? 0)),
|
|
187
|
+
r: num(color, 'r') ?? 0,
|
|
188
|
+
g: num(color, 'g') ?? 0,
|
|
189
|
+
b: num(color, 'b') ?? 0,
|
|
190
|
+
a: (num(color, 'a') ?? 1) * opacity,
|
|
191
|
+
};
|
|
192
|
+
})
|
|
193
|
+
.sort((x, y) => x.at - y.at);
|
|
194
|
+
if (stops.length === 0)
|
|
195
|
+
return undefined;
|
|
196
|
+
const css = blendLikeFigma(stops).map((s) => `${colorHex({ r: s.r, g: s.g, b: s.b, a: s.a })} ` +
|
|
197
|
+
`${r2(((s.at - atCentre) / (rate * line) + 0.5) * 100)}%`);
|
|
198
|
+
const degrees = (((theta * 180) / Math.PI) % 360 + 360) % 360;
|
|
199
|
+
return `linear-gradient(${r2(degrees)}deg, ${css.join(', ')})`;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* One paint. Given the box it fills, a linear gradient also comes back as `css` — its direction
|
|
203
|
+
* is otherwise nowhere in the output.
|
|
204
|
+
*/
|
|
205
|
+
export function paintView(idx, p, box) {
|
|
89
206
|
const image = obj(p, 'image');
|
|
90
207
|
const stops = objArr(p, 'stops').map((s) => compact({ at: r2(num(s, 'position')), color: colorHex(obj(s, 'color')) }));
|
|
91
208
|
const w = num(p, 'originalImageWidth');
|
|
@@ -97,6 +214,7 @@ export function paintView(idx, p) {
|
|
|
97
214
|
visible: bool(p, 'visible') === false ? false : undefined,
|
|
98
215
|
blendMode: str(p, 'blendMode') === 'NORMAL' ? undefined : str(p, 'blendMode'),
|
|
99
216
|
stops: stops.length ? stops : undefined,
|
|
217
|
+
css: box && str(p, 'type') === 'GRADIENT_LINEAR' ? linearGradientCss(p, box) : undefined,
|
|
100
218
|
imageHash: hex(bytes(image, 'hash')),
|
|
101
219
|
imageName: str(image, 'name'),
|
|
102
220
|
imageScaleMode: str(p, 'imageScaleMode'),
|
|
@@ -142,16 +260,49 @@ export function paddingOf(node) {
|
|
|
142
260
|
left: r2(left ?? 0),
|
|
143
261
|
};
|
|
144
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Figma's "Auto" spacing: the gap is whatever spreads the children across the box, and the stored
|
|
265
|
+
* `stackSpacing` is ignored. Kiwi writes it as SPACE_EVENLY; the plugin and REST APIs call it
|
|
266
|
+
* SPACE_BETWEEN, and so does newer kiwi. Measured on the iOS 16 UI Kit: all 93 SPACE_EVENLY frames
|
|
267
|
+
* with two or more in-flow children sit flush against both paddings with equal gaps — CSS
|
|
268
|
+
* `justify-content: space-between` — and in 88 of them that gap is not the stored `stackSpacing`.
|
|
269
|
+
*/
|
|
270
|
+
const AUTO_SPACING = new Set(['SPACE_EVENLY', 'SPACE_BETWEEN']);
|
|
271
|
+
const HUG = new Set(['RESIZE_TO_FIT', 'RESIZE_TO_FIT_WITH_IMPLICIT_SIZE']);
|
|
272
|
+
/**
|
|
273
|
+
* How the stored spacings apply to a flow layout (HORIZONTAL / VERTICAL). Auto spacing has
|
|
274
|
+
* nothing to spread on an axis that hugs its content, so there the stored gap is kept. The
|
|
275
|
+
* counter spacing — the gap between wrapped lines — is used only when the layout wraps.
|
|
276
|
+
*/
|
|
277
|
+
function flowSpacing(node) {
|
|
278
|
+
const spaced = AUTO_SPACING.has(str(node, 'stackPrimaryAlignItems') ?? '');
|
|
279
|
+
const autoPrimary = spaced && !HUG.has(str(node, 'stackPrimarySizing') ?? '');
|
|
280
|
+
const wraps = str(node, 'stackWrap') === 'WRAP';
|
|
281
|
+
const autoCounter = wraps &&
|
|
282
|
+
str(node, 'stackCounterAlignContent') === 'SPACE_BETWEEN' &&
|
|
283
|
+
!HUG.has(str(node, 'stackCounterSizing') ?? '');
|
|
284
|
+
return {
|
|
285
|
+
primary: autoPrimary ? undefined : r2(num(node, 'stackSpacing')),
|
|
286
|
+
counter: wraps && !autoCounter ? r2(num(node, 'stackCounterSpacing')) : undefined,
|
|
287
|
+
spaced,
|
|
288
|
+
autoPrimary,
|
|
289
|
+
autoCounter,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
145
292
|
function autoLayoutOf(node) {
|
|
146
293
|
const mode = str(node, 'stackMode');
|
|
147
294
|
if (!mode || mode === 'NONE')
|
|
148
295
|
return undefined;
|
|
296
|
+
const flow = mode === 'HORIZONTAL' || mode === 'VERTICAL' ? flowSpacing(node) : undefined;
|
|
297
|
+
const primaryAlign = str(node, 'stackPrimaryAlignItems');
|
|
149
298
|
return compact({
|
|
150
299
|
mode,
|
|
151
|
-
spacing: r2(num(node, 'stackSpacing')),
|
|
152
|
-
counterSpacing:
|
|
300
|
+
spacing: flow ? (flow.autoPrimary ? 'auto' : flow.primary) : r2(num(node, 'stackSpacing')),
|
|
301
|
+
counterSpacing: flow
|
|
302
|
+
? flow.autoCounter ? 'auto' : flow.counter
|
|
303
|
+
: r2(num(node, 'stackCounterSpacing')),
|
|
153
304
|
padding: paddingOf(node),
|
|
154
|
-
primaryAlign:
|
|
305
|
+
primaryAlign: flow?.spaced ? 'SPACE_BETWEEN' : primaryAlign,
|
|
155
306
|
counterAlign: str(node, 'stackCounterAlignItems'),
|
|
156
307
|
counterAlignContent: str(node, 'stackCounterAlignContent'),
|
|
157
308
|
primarySizing: str(node, 'stackPrimarySizing'),
|
|
@@ -192,6 +343,34 @@ export function lengthString(o) {
|
|
|
192
343
|
return String(r2(v));
|
|
193
344
|
}
|
|
194
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* A line height as CSS reads it. Figma stores its "Auto" line height — the font's own, about 1.2x
|
|
348
|
+
* the size — as 100 PERCENT (the REST API calls that unit INTRINSIC_%). On the iOS 16 UI Kit all
|
|
349
|
+
* 56 such texts are laid out at exactly their font's natural line height, which a CSS `100%`
|
|
350
|
+
* (1.0x the size) would not give, so it is reported as "auto" (CSS `normal`).
|
|
351
|
+
*/
|
|
352
|
+
export function lineHeightString(o) {
|
|
353
|
+
if (str(o, 'units') === 'PERCENT' && num(o, 'value') === 100)
|
|
354
|
+
return 'auto';
|
|
355
|
+
return lengthString(o);
|
|
356
|
+
}
|
|
357
|
+
/** A letter spacing as CSS reads it: Figma's percent is of the font size, which CSS spells `em`. */
|
|
358
|
+
export function letterSpacingString(o) {
|
|
359
|
+
const v = num(o, 'value');
|
|
360
|
+
if (str(o, 'units') !== 'PERCENT' || v === undefined)
|
|
361
|
+
return lengthString(o);
|
|
362
|
+
return `${Math.round(v * 100) / 10_000}em`;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* The colour of one SOLID paint as it shows: the paint's own opacity folded into the alpha, so
|
|
366
|
+
* `#3C3C43` at 60 % comes back as `#3C3C4399` rather than as an opaque colour.
|
|
367
|
+
*/
|
|
368
|
+
function shownColor(paint) {
|
|
369
|
+
const color = obj(paint, 'color');
|
|
370
|
+
if (!color)
|
|
371
|
+
return undefined;
|
|
372
|
+
return colorHex({ ...color, a: (num(color, 'a') ?? 1) * (num(paint, 'opacity') ?? 1) });
|
|
373
|
+
}
|
|
195
374
|
const CHARACTERS_PREVIEW = 400;
|
|
196
375
|
export function textBasics(node) {
|
|
197
376
|
const textData = obj(node, 'textData');
|
|
@@ -207,8 +386,8 @@ export function textBasics(node) {
|
|
|
207
386
|
charactersTruncated: truncated || undefined,
|
|
208
387
|
font: font ? `${str(font, 'family') ?? ''} ${str(font, 'style') ?? ''}`.trim() : undefined,
|
|
209
388
|
fontSize: r2(num(node, 'fontSize')),
|
|
210
|
-
lineHeight:
|
|
211
|
-
letterSpacing:
|
|
389
|
+
lineHeight: lineHeightString(obj(node, 'lineHeight')),
|
|
390
|
+
letterSpacing: letterSpacingString(obj(node, 'letterSpacing')),
|
|
212
391
|
paragraphSpacing: r2(num(node, 'paragraphSpacing')),
|
|
213
392
|
align: str(node, 'textAlignHorizontal'),
|
|
214
393
|
verticalAlign: str(node, 'textAlignVertical'),
|
|
@@ -229,10 +408,12 @@ const STYLE_REF_FIELDS = {
|
|
|
229
408
|
/**
|
|
230
409
|
* Shared-style references.
|
|
231
410
|
*
|
|
232
|
-
* A style definition is stored as an ordinary (hidden) node parked on a canvas
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
411
|
+
* A style definition is stored as an ordinary (hidden) node parked on a canvas — text styles are
|
|
412
|
+
* TEXT nodes, fill styles are ROUNDED_RECTANGLE nodes. A published style is referenced by the
|
|
413
|
+
* publishable `key` it carries (`styleIdFor*.assetRef.key`), a style that was never published by
|
|
414
|
+
* its guid (`styleIdFor*.guid`). Either way a style used from the same file resolves to a real
|
|
415
|
+
* node and, with `resolve`, to the values it defines. Styles from other libraries stay opaque
|
|
416
|
+
* refs, and the detached-style sentinel is no style at all.
|
|
236
417
|
*/
|
|
237
418
|
export function styleRefsOf(idx, node, opts = {}) {
|
|
238
419
|
const out = {};
|
|
@@ -240,21 +421,30 @@ export function styleRefsOf(idx, node, opts = {}) {
|
|
|
240
421
|
const ref = obj(node, field);
|
|
241
422
|
if (!ref)
|
|
242
423
|
continue;
|
|
243
|
-
const local = idx.resolveAssetRef(ref);
|
|
244
424
|
const guid = guidKey(obj(ref, 'guid'));
|
|
425
|
+
const assetRef = assetRefString(ref);
|
|
426
|
+
if (guid === DETACHED_STYLE_GUID && !assetRef)
|
|
427
|
+
continue;
|
|
428
|
+
const local = idx.resolveAssetRef(ref) ?? resolveStyle(idx, ref);
|
|
245
429
|
out[label] = compact({
|
|
246
430
|
name: local ? str(local.node, 'name') : undefined,
|
|
247
431
|
guid: local?.key ?? guid,
|
|
248
|
-
assetRef
|
|
432
|
+
assetRef,
|
|
249
433
|
defines: opts.resolve && local ? styleDefinition(idx, label, local.node) : undefined,
|
|
250
434
|
});
|
|
251
435
|
}
|
|
252
436
|
return orUndefined(out);
|
|
253
437
|
}
|
|
438
|
+
/** Typography fields a text style does not define: its node carries them only for its preview. */
|
|
439
|
+
const NOT_IN_TEXT_STYLE = ['color', 'fills', 'align', 'verticalAlign', 'styles'];
|
|
254
440
|
/** The values a locally-defined style carries: typography for text, paints for fill/stroke. */
|
|
255
441
|
function styleDefinition(idx, label, definition) {
|
|
256
|
-
if (label === 'text')
|
|
257
|
-
|
|
442
|
+
if (label === 'text') {
|
|
443
|
+
const defined = typography(idx, definition);
|
|
444
|
+
for (const field of NOT_IN_TEXT_STYLE)
|
|
445
|
+
delete defined[field];
|
|
446
|
+
return orUndefined(defined);
|
|
447
|
+
}
|
|
258
448
|
if (label === 'effect') {
|
|
259
449
|
const effects = objArr(definition, 'effects').map((e) => effectView(idx, e));
|
|
260
450
|
return effects.length ? { effects } : undefined;
|
|
@@ -291,7 +481,9 @@ function componentOf(idx, t) {
|
|
|
291
481
|
const type = str(node, 'type');
|
|
292
482
|
if (type === 'INSTANCE') {
|
|
293
483
|
const symbolData = obj(node, 'symbolData');
|
|
294
|
-
|
|
484
|
+
// A layer inside an instance may have had its symbol swapped by a record or a property.
|
|
485
|
+
const symbolId = guidKey(obj(node, 'overriddenSymbolID')) ??
|
|
486
|
+
guidKey(obj(symbolData, 'symbolID'));
|
|
295
487
|
const symbol = symbolId ? idx.node(symbolId) : undefined;
|
|
296
488
|
return compact({
|
|
297
489
|
role: 'INSTANCE',
|
|
@@ -350,12 +542,16 @@ function orUndefined(o) {
|
|
|
350
542
|
}
|
|
351
543
|
export function nodeDetail(idx, t, opts = {}) {
|
|
352
544
|
const node = t.node;
|
|
545
|
+
// Paints, effects and typography as Figma shows them (F19, F22, F29).
|
|
546
|
+
const live = shownStyle(idx, t);
|
|
353
547
|
const maxChildren = opts.maxChildren ?? 100;
|
|
354
|
-
const
|
|
355
|
-
const
|
|
356
|
-
const
|
|
357
|
-
const
|
|
548
|
+
const box = paintBox(node);
|
|
549
|
+
const fills = objArr(live, 'fillPaints').map((p) => paintView(idx, p, box));
|
|
550
|
+
const strokes = objArr(live, 'strokePaints').map((p) => paintView(idx, p, box));
|
|
551
|
+
const effects = objArr(live, 'effects').map((e) => effectView(idx, e));
|
|
552
|
+
const images = nodeImageHashes(live);
|
|
358
553
|
const dashPattern = node['dashPattern'];
|
|
554
|
+
const stroked = hasVisibleStroke(live);
|
|
359
555
|
return compact({
|
|
360
556
|
guid: t.key,
|
|
361
557
|
type: str(node, 'type') ?? 'UNKNOWN',
|
|
@@ -378,17 +574,18 @@ export function nodeDetail(idx, t, opts = {}) {
|
|
|
378
574
|
cornerRadius: cornerRadiusOf(node),
|
|
379
575
|
fills: fills.length ? fills : undefined,
|
|
380
576
|
strokes: strokes.length ? strokes : undefined,
|
|
381
|
-
strokeWeight: num(node, 'strokeWeight') ? r2(num(node, 'strokeWeight')) : undefined,
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
577
|
+
strokeWeight: stroked && num(node, 'strokeWeight') ? r2(num(node, 'strokeWeight')) : undefined,
|
|
578
|
+
strokeWeights: stroked ? sideWeights(node) : undefined,
|
|
579
|
+
strokeAlign: stroked ? str(node, 'strokeAlign') : undefined,
|
|
580
|
+
strokeCap: stroked ? str(node, 'strokeCap') : undefined,
|
|
581
|
+
strokeJoin: stroked ? str(node, 'strokeJoin') : undefined,
|
|
582
|
+
dashPattern: stroked && Array.isArray(dashPattern) && dashPattern.length
|
|
386
583
|
? dashPattern.map((v) => r2(v))
|
|
387
584
|
: undefined,
|
|
388
585
|
effects: effects.length ? effects : undefined,
|
|
389
586
|
autoLayout: autoLayoutOf(node),
|
|
390
587
|
layout: layoutChildOf(node),
|
|
391
|
-
text: textBasics(
|
|
588
|
+
text: textBasics(live),
|
|
392
589
|
component: componentOf(idx, t),
|
|
393
590
|
styles: styleRefsOf(idx, node),
|
|
394
591
|
variables: variableBindingsOf(idx, node),
|
|
@@ -403,24 +600,28 @@ export function nodeDetail(idx, t, opts = {}) {
|
|
|
403
600
|
/**
|
|
404
601
|
* Typography of a node — or of a partial style-override record from a text run — reporting only
|
|
405
602
|
* the fields it actually sets, so a run's block shows exactly what differs from the base style.
|
|
603
|
+
* Pass the node with its styles applied (`applyStyles(…, { text: true })`): the values cached
|
|
604
|
+
* beside a style reference can be stale (F19, F22).
|
|
406
605
|
*/
|
|
407
606
|
export function typography(idx, node, opts = {}) {
|
|
408
607
|
const font = obj(node, 'fontName');
|
|
409
|
-
|
|
410
|
-
|
|
608
|
+
// One colour only when one paint shows and it is solid; otherwise every paint that shows.
|
|
609
|
+
// (An invisible paint, or the first solid under two more, is not the text's colour.)
|
|
610
|
+
const shown = objArr(node, 'fillPaints').filter((p) => bool(p, 'visible') !== false);
|
|
611
|
+
const solid = shown.length === 1 && str(shown[0], 'type') === 'SOLID' ? shown[0] : undefined;
|
|
411
612
|
return compact({
|
|
412
613
|
font: font ? `${str(font, 'family') ?? ''} ${str(font, 'style') ?? ''}`.trim() : undefined,
|
|
413
614
|
fontPostScript: str(font, 'postscript'),
|
|
414
615
|
fontSize: r2(num(node, 'fontSize')),
|
|
415
|
-
lineHeight:
|
|
416
|
-
letterSpacing:
|
|
616
|
+
lineHeight: lineHeightString(obj(node, 'lineHeight')),
|
|
617
|
+
letterSpacing: letterSpacingString(obj(node, 'letterSpacing')),
|
|
417
618
|
paragraphSpacing: r2(num(node, 'paragraphSpacing')),
|
|
418
619
|
textCase: str(node, 'textCase'),
|
|
419
620
|
textDecoration: str(node, 'textDecoration'),
|
|
420
621
|
align: str(node, 'textAlignHorizontal'),
|
|
421
622
|
verticalAlign: str(node, 'textAlignVertical'),
|
|
422
|
-
color: solid ?
|
|
423
|
-
fills:
|
|
623
|
+
color: solid ? shownColor(solid) : undefined,
|
|
624
|
+
fills: shown.length && !solid ? shown.map((p) => paintView(idx, p, paintBox(node))) : undefined,
|
|
424
625
|
// Runs express most overrides as a shared-style ref, so keep them here by default; the
|
|
425
626
|
// caller turns them off when it already reports the node's style refs separately.
|
|
426
627
|
styles: opts.includeStyleRefs === false ? undefined : styleRefsOf(idx, node),
|
|
@@ -468,12 +669,15 @@ export function styleBlock(idx, t) {
|
|
|
468
669
|
const size = obj(node, 'size');
|
|
469
670
|
const mode = str(node, 'stackMode');
|
|
470
671
|
const padding = paddingOf(node);
|
|
672
|
+
// Under auto spacing `justify-content` alone reproduces Figma's gap; a `gap` taken from the
|
|
673
|
+
// ignored `stackSpacing` would be a CSS minimum that overflows a narrower box.
|
|
674
|
+
const flow = mode === 'HORIZONTAL' || mode === 'VERTICAL' ? flowSpacing(node) : undefined;
|
|
471
675
|
const layout = mode && mode !== 'NONE'
|
|
472
676
|
? compact({
|
|
473
677
|
display: 'flex',
|
|
474
678
|
direction: mode === 'VERTICAL' ? 'column' : mode === 'GRID' ? 'grid' : 'row',
|
|
475
|
-
gap: r2(num(node, 'stackSpacing')),
|
|
476
|
-
rowGap: r2(num(node, 'stackCounterSpacing')),
|
|
679
|
+
gap: flow ? flow.primary : r2(num(node, 'stackSpacing')),
|
|
680
|
+
rowGap: flow ? flow.counter : r2(num(node, 'stackCounterSpacing')),
|
|
477
681
|
padding: padding ? cssPadding(padding) : undefined,
|
|
478
682
|
paddingBox: padding,
|
|
479
683
|
justifyContent: JUSTIFY[str(node, 'stackPrimaryAlignItems') ?? ''],
|
|
@@ -496,11 +700,15 @@ export function styleBlock(idx, t) {
|
|
|
496
700
|
left: r2(num(node, 'stackChildMarginLeft')),
|
|
497
701
|
})),
|
|
498
702
|
}));
|
|
499
|
-
|
|
500
|
-
const
|
|
501
|
-
const
|
|
703
|
+
// Paints, effects and typography as Figma shows them (F19, F22, F29).
|
|
704
|
+
const live = shownStyle(idx, t);
|
|
705
|
+
const box = paintBox(node);
|
|
706
|
+
const fills = objArr(live, 'fillPaints').map((p) => paintView(idx, p, box));
|
|
707
|
+
const strokes = objArr(live, 'strokePaints').map((p) => paintView(idx, p, box));
|
|
708
|
+
const effects = objArr(live, 'effects').map((e) => effectView(idx, e));
|
|
502
709
|
const type = str(node, 'type');
|
|
503
710
|
const dashPattern = node['dashPattern'];
|
|
711
|
+
const stroked = hasVisibleStroke(live);
|
|
504
712
|
return compact({
|
|
505
713
|
guid: t.key,
|
|
506
714
|
type,
|
|
@@ -512,15 +720,16 @@ export function styleBlock(idx, t) {
|
|
|
512
720
|
cornerRadius: cornerRadiusOf(node),
|
|
513
721
|
fills: fills.length ? fills : undefined,
|
|
514
722
|
strokes: strokes.length ? strokes : undefined,
|
|
515
|
-
strokeWeight: num(node, 'strokeWeight') ? r2(num(node, 'strokeWeight')) : undefined,
|
|
516
|
-
|
|
517
|
-
|
|
723
|
+
strokeWeight: stroked && num(node, 'strokeWeight') ? r2(num(node, 'strokeWeight')) : undefined,
|
|
724
|
+
strokeWeights: stroked ? sideWeights(node) : undefined,
|
|
725
|
+
strokeAlign: stroked ? str(node, 'strokeAlign') : undefined,
|
|
726
|
+
dashPattern: stroked && Array.isArray(dashPattern) && dashPattern.length
|
|
518
727
|
? dashPattern.map((v) => r2(v))
|
|
519
728
|
: undefined,
|
|
520
729
|
effects: effects.length ? effects : undefined,
|
|
521
730
|
layout,
|
|
522
731
|
inParentLayout: inParent,
|
|
523
|
-
typography: type === 'TEXT' ? orUndefined(typography(idx,
|
|
732
|
+
typography: type === 'TEXT' ? orUndefined(typography(idx, live, { includeStyleRefs: false })) : undefined,
|
|
524
733
|
constraints: orUndefined(compact({
|
|
525
734
|
horizontal: str(node, 'horizontalConstraint'),
|
|
526
735
|
vertical: str(node, 'verticalConstraint'),
|