figdriver 0.0.0-stage → 0.4.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/CHANGELOG.md +86 -0
- package/LICENSE +21 -0
- package/README.md +87 -2
- package/cli/result.ts +231 -0
- package/dist/cli.js +4125 -0
- package/index.d.ts +12 -0
- package/index.js +8 -0
- package/package.json +78 -4
- package/src/core/color.ts +435 -0
- package/src/core/css.ts +668 -0
- package/src/core/foundations.ts +389 -0
- package/src/core/json.d.ts +4 -0
- package/src/core/layout.ts +684 -0
- package/src/core/naming.ts +129 -0
- package/src/core/protocol.ts +328 -0
- package/src/core/rerun.ts +130 -0
- package/src/core/reuse.ts +149 -0
- package/src/core/snapshot.ts +88 -0
- package/src/core/spec.ts +167 -0
- package/src/core/text.ts +37 -0
- package/src/core/tokens.ts +77 -0
- package/src/core/types.ts +181 -0
- package/src/core/users.ts +17 -0
- package/src/figma/helpers.ts +238 -0
- package/src/figma/import.ts +962 -0
- package/src/figma/inspect.ts +254 -0
- package/src/figma/kit.ts +562 -0
- package/src/figma/migrate.ts +301 -0
- package/src/figma/owned.ts +56 -0
- package/src/figma/read.ts +618 -0
- package/src/figma/rerun.ts +387 -0
- package/src/figma/review.ts +481 -0
- package/src/figma/tokens.ts +97 -0
- package/src/page/capture.ts +999 -0
package/src/figma/kit.ts
ADDED
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
import { blendModeOf, blurOf, gradientOf, hexOf, parseColor, rgb, splitTop, svgWithSrgbColors } from "../core/css";
|
|
2
|
+
import {
|
|
3
|
+
filled,
|
|
4
|
+
iconKey,
|
|
5
|
+
isSimpleIcon,
|
|
6
|
+
runKey,
|
|
7
|
+
strokeWidth,
|
|
8
|
+
svgColor,
|
|
9
|
+
typeKey,
|
|
10
|
+
viewBox,
|
|
11
|
+
type Foundations,
|
|
12
|
+
type PaintUse,
|
|
13
|
+
} from "../core/foundations";
|
|
14
|
+
import { isAbsolute, type Container } from "../core/layout";
|
|
15
|
+
import { messageOf, selectorOf, type El, type FrameEl, type Run, type SvgEl, type TextEl } from "../core/snapshot";
|
|
16
|
+
import type { ImportSpec } from "../core/spec";
|
|
17
|
+
import { own } from "./owned";
|
|
18
|
+
|
|
19
|
+
type Theme = Foundations["theme"];
|
|
20
|
+
|
|
21
|
+
function mergeRuns(runs: Run[]) {
|
|
22
|
+
const out: Run[] = [];
|
|
23
|
+
|
|
24
|
+
for (const r of runs) {
|
|
25
|
+
const prev = out[out.length - 1];
|
|
26
|
+
|
|
27
|
+
if (prev && (runKey(prev) === runKey(r) || r.text === "\n")) prev.text += r.text;
|
|
28
|
+
else out.push({ ...r });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const covers = (a: El, b: El) =>
|
|
35
|
+
a.box[0] <= b.box[0] &&
|
|
36
|
+
a.box[1] <= b.box[1] &&
|
|
37
|
+
a.box[0] + a.box[2] >= b.box[0] + b.box[2] &&
|
|
38
|
+
a.box[1] + a.box[3] >= b.box[1] + b.box[3];
|
|
39
|
+
|
|
40
|
+
function createVariables(owner: string, theme: Theme, warnings: Set<string>) {
|
|
41
|
+
const colorCol = own(figma.variables.createVariableCollection("Color"), owner);
|
|
42
|
+
const light = colorCol.modes[0].modeId;
|
|
43
|
+
colorCol.renameMode(light, "Light");
|
|
44
|
+
const color: Record<string, Variable> = {};
|
|
45
|
+
|
|
46
|
+
// Aliases go last, after the variables they point at exist.
|
|
47
|
+
const colorEntries = Object.entries(theme.colors).sort(
|
|
48
|
+
([a], [b]) => Number(a in theme.colorAliases) - Number(b in theme.colorAliases),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
for (const [name, hex] of colorEntries) {
|
|
52
|
+
const v = figma.variables.createVariable(name, colorCol, "COLOR");
|
|
53
|
+
const target = theme.colorAliases[name];
|
|
54
|
+
v.setValueForMode(light, target ? figma.variables.createVariableAlias(color[target]) : { ...rgb(hex), a: 1 });
|
|
55
|
+
const cssName = theme.cssNames[name];
|
|
56
|
+
|
|
57
|
+
if (cssName) v.setVariableCodeSyntax("WEB", `var(--${cssName})`);
|
|
58
|
+
color[name] = v;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A new mode starts with the first mode's values, so it sets only the colors it changes.
|
|
62
|
+
for (const [mode, values] of Object.entries(theme.colorModes)) {
|
|
63
|
+
let modeId: string;
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
modeId = colorCol.addMode(mode);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
warnings.add(
|
|
69
|
+
`The page's ${mode} theme was left out: Figma could not add a "${mode}" mode to the Color collection: ${messageOf(e)} Figma's Starter plan allows one mode per collection.`,
|
|
70
|
+
);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const [name, hex] of Object.entries(values)) color[name]?.setValueForMode(modeId, { ...rgb(hex), a: 1 });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let dimCol: VariableCollection | undefined;
|
|
78
|
+
|
|
79
|
+
// Created on first use, so a theme without spacing or radii gets no empty collection.
|
|
80
|
+
const dimensions = () => {
|
|
81
|
+
if (!dimCol) {
|
|
82
|
+
dimCol = own(figma.variables.createVariableCollection("Dimension"), owner);
|
|
83
|
+
dimCol.renameMode(dimCol.modes[0].modeId, "Default");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return dimCol;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const dimension = (entries: Record<string, number>, scopes: VariableScope[]) =>
|
|
90
|
+
new Map(
|
|
91
|
+
Object.entries(entries).map(([name, value]) => {
|
|
92
|
+
const col = dimensions();
|
|
93
|
+
const v = figma.variables.createVariable(name, col, "FLOAT");
|
|
94
|
+
v.setValueForMode(col.modes[0].modeId, value);
|
|
95
|
+
v.scopes = scopes;
|
|
96
|
+
|
|
97
|
+
return [value, v];
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
color,
|
|
103
|
+
spaceByValue: dimension(theme.spacing, ["GAP"]),
|
|
104
|
+
radiusByValue: dimension(theme.radii, ["CORNER_RADIUS"]),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function createTextStyles(owner: string, theme: Theme, textStyleNames: Map<string, string>) {
|
|
109
|
+
const textStyles: Record<string, TextStyle> = {};
|
|
110
|
+
|
|
111
|
+
for (const [name, t] of Object.entries(theme.textStyles)) {
|
|
112
|
+
const s = own(figma.createTextStyle(), owner);
|
|
113
|
+
s.name = name;
|
|
114
|
+
s.fontName = { family: t.family ?? theme.font, style: t.style };
|
|
115
|
+
s.fontSize = t.size;
|
|
116
|
+
s.lineHeight = { unit: "PIXELS", value: t.line };
|
|
117
|
+
s.letterSpacing = { unit: "PERCENT", value: t.tracking ?? 0 };
|
|
118
|
+
|
|
119
|
+
if (t.case) s.textCase = t.case;
|
|
120
|
+
textStyles[name] = s;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
textStyles,
|
|
125
|
+
textStyleIds: new Map([...textStyleNames].map(([key, name]) => [key, textStyles[name].id])),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function createElevations(owner: string, theme: Theme) {
|
|
130
|
+
const elevation: Record<string, EffectStyle> = {};
|
|
131
|
+
|
|
132
|
+
for (const [key, e] of Object.entries(theme.elevations)) {
|
|
133
|
+
const s = own(figma.createEffectStyle(), owner);
|
|
134
|
+
s.name = e.name;
|
|
135
|
+
s.effects = e.shadows.map((l): Effect => {
|
|
136
|
+
const base = "token" in l.color ? rgb(theme.colors[l.color.token]) : l.color;
|
|
137
|
+
|
|
138
|
+
const shadow = {
|
|
139
|
+
color: { ...base, a: l.alpha },
|
|
140
|
+
offset: { x: l.x, y: l.y },
|
|
141
|
+
radius: l.radius,
|
|
142
|
+
spread: l.spread,
|
|
143
|
+
visible: true,
|
|
144
|
+
blendMode: "NORMAL",
|
|
145
|
+
} as const;
|
|
146
|
+
|
|
147
|
+
return l.type === "DROP_SHADOW"
|
|
148
|
+
? { ...shadow, type: "DROP_SHADOW", showShadowBehindNode: false }
|
|
149
|
+
: { ...shadow, type: "INNER_SHADOW" };
|
|
150
|
+
});
|
|
151
|
+
elevation[key] = s;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return elevation;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// One component per icon holding a single flattened "Vector" so recolors survive instance swaps.
|
|
158
|
+
function createIconLibrary(
|
|
159
|
+
owner: string,
|
|
160
|
+
theme: Theme,
|
|
161
|
+
iconNames: Map<string, string>,
|
|
162
|
+
solid: (token: string) => SolidPaint,
|
|
163
|
+
) {
|
|
164
|
+
const iconSet: Record<string, ComponentNode> = {};
|
|
165
|
+
|
|
166
|
+
const iconPaint: SolidPaint =
|
|
167
|
+
"token" in theme.iconColor ? solid(theme.iconColor.token) : { type: "SOLID", color: theme.iconColor };
|
|
168
|
+
|
|
169
|
+
for (const [name, def] of Object.entries(theme.icons)) {
|
|
170
|
+
const svg = figma.createNodeFromSvg(theme.svgs[def.svg]);
|
|
171
|
+
const vector = figma.flatten(svg.children, svg);
|
|
172
|
+
vector.name = "Vector";
|
|
173
|
+
vector.constraints = { horizontal: "SCALE", vertical: "SCALE" };
|
|
174
|
+
vector.strokes = def.filled ? [] : [iconPaint];
|
|
175
|
+
vector.fills = def.filled ? [iconPaint] : [];
|
|
176
|
+
const c = own(figma.createComponentFromNode(svg), owner);
|
|
177
|
+
c.name = `Icon/${name}`;
|
|
178
|
+
c.fills = [];
|
|
179
|
+
c.description = `${theme.iconLibrary} "${def.svg}". Recolor the Vector layer; swap via instance swap.`;
|
|
180
|
+
iconSet[name] = c;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
iconSet,
|
|
185
|
+
iconComps: new Map([...iconNames].map(([key, name]) => [key, iconSet[name]])),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function variantSet(name: string, variants: ComponentNode[], description: string) {
|
|
190
|
+
const set = figma.combineAsVariants(variants, figma.currentPage);
|
|
191
|
+
set.name = name;
|
|
192
|
+
set.description = description;
|
|
193
|
+
set.layoutMode = "HORIZONTAL";
|
|
194
|
+
set.itemSpacing = 16;
|
|
195
|
+
set.paddingTop = set.paddingRight = set.paddingBottom = set.paddingLeft = 16;
|
|
196
|
+
set.layoutSizingHorizontal = set.layoutSizingVertical = "HUG";
|
|
197
|
+
set.counterAxisAlignItems = "CENTER";
|
|
198
|
+
set.strokes = [{ type: "SOLID", color: { r: 0.59, g: 0.28, b: 1 } }];
|
|
199
|
+
set.dashPattern = [10, 5];
|
|
200
|
+
set.cornerRadius = 5;
|
|
201
|
+
|
|
202
|
+
return set;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function createKit(
|
|
206
|
+
spec: ImportSpec,
|
|
207
|
+
foundations: Foundations,
|
|
208
|
+
parentOf: Map<El, FrameEl>,
|
|
209
|
+
warnings: Set<string>,
|
|
210
|
+
) {
|
|
211
|
+
const { theme, tokenFor, guessedAmong, textStyleNames, iconNames } = foundations;
|
|
212
|
+
const owner = spec.name;
|
|
213
|
+
const { color, spaceByValue, radiusByValue } = createVariables(owner, theme, warnings);
|
|
214
|
+
const { textStyles, textStyleIds } = createTextStyles(owner, theme, textStyleNames);
|
|
215
|
+
const elevation = createElevations(owner, theme);
|
|
216
|
+
|
|
217
|
+
const solid = (token: string): SolidPaint =>
|
|
218
|
+
figma.variables.setBoundVariableForPaint(
|
|
219
|
+
{ type: "SOLID", color: rgb(theme.colors[token]), opacity: 1 },
|
|
220
|
+
"color",
|
|
221
|
+
color[token],
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const { iconSet, iconComps } = createIconLibrary(owner, theme, iconNames, solid);
|
|
225
|
+
const imageHashes = new Map<string, string>();
|
|
226
|
+
const videoHashes = new Map<string, string>();
|
|
227
|
+
const guesses = new Map<string, { among: string[]; at: Set<string> }>();
|
|
228
|
+
|
|
229
|
+
function imagePaint(name: string, scaleMode: ImagePaint["scaleMode"]): ImagePaint {
|
|
230
|
+
let hash = imageHashes.get(name);
|
|
231
|
+
|
|
232
|
+
if (!hash) {
|
|
233
|
+
hash = figma.createImage(figma.base64Decode(theme.images[name])).hash;
|
|
234
|
+
imageHashes.set(name, hash);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return { type: "IMAGE", imageHash: hash, scaleMode };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function bindSpace(node: FrameNode | ComponentNode, field: VariableBindableNodeField, value: number) {
|
|
241
|
+
const v = spaceByValue.get(value);
|
|
242
|
+
|
|
243
|
+
if (v) node.setBoundVariable(field, v);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function bindRadius(node: FrameNode | ComponentNode | RectangleNode | InstanceNode) {
|
|
247
|
+
for (const corner of ["topLeftRadius", "topRightRadius", "bottomLeftRadius", "bottomRightRadius"] as const) {
|
|
248
|
+
const v = radiusByValue.get(node[corner]);
|
|
249
|
+
|
|
250
|
+
if (v) node.setBoundVariable(corner, v);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function solidOf(css: string, el: El, use?: PaintUse): SolidPaint | undefined {
|
|
255
|
+
const c = parseColor(css);
|
|
256
|
+
|
|
257
|
+
if (!c || c.a === 0) return;
|
|
258
|
+
const hex = hexOf(c);
|
|
259
|
+
const declared = el.vars?.[css];
|
|
260
|
+
const token = tokenFor(hex, use, declared);
|
|
261
|
+
|
|
262
|
+
// Instances render a variable-bound paint at full opacity whatever the main says, so translucent token colors stay raw.
|
|
263
|
+
if (!token || c.a !== 1) return { type: "SOLID", color: { r: c.r, g: c.g, b: c.b }, opacity: c.a };
|
|
264
|
+
const among = guessedAmong(hex, declared);
|
|
265
|
+
|
|
266
|
+
if (among.length) {
|
|
267
|
+
const key = `${hex} ${token}`;
|
|
268
|
+
const guess = guesses.get(key) ?? { among, at: new Set<string>() };
|
|
269
|
+
const parent = parentOf.get(el);
|
|
270
|
+
guess.at.add(el.tag === "#text" && parent ? `text in ${selectorOf(parent)}` : selectorOf(el));
|
|
271
|
+
guesses.set(key, guess);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return solid(token);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Adds each video the build uses to the file, once. Figma takes video only in a paid team's file outside Drafts, so after a
|
|
279
|
+
* plan refusal every remaining video shows its still frame and one warning says why.
|
|
280
|
+
*/
|
|
281
|
+
async function uploadVideos(used?: ReadonlySet<string>) {
|
|
282
|
+
for (const [name, bytes] of Object.entries(spec.videos ?? {})) {
|
|
283
|
+
if (used && !used.has(name)) continue;
|
|
284
|
+
|
|
285
|
+
try {
|
|
286
|
+
videoHashes.set(name, (await figma.createVideoAsync(figma.base64Decode(bytes))).hash);
|
|
287
|
+
} catch (e) {
|
|
288
|
+
const msg = messageOf(e);
|
|
289
|
+
|
|
290
|
+
if (/paid|pro team|drafts|permission|plan/i.test(msg)) {
|
|
291
|
+
warnings.add(`Videos show their still frame, since Figma would not add one to this file: ${msg}`);
|
|
292
|
+
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
warnings.add(`Video "${name}" shows its still frame, since Figma rejected it: ${msg}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function mediaScale([iw, ih]: [number, number], fit: string, position: string, w: number, h: number) {
|
|
302
|
+
const base = fit === "contain" ? ("FIT" as const) : ("FILL" as const);
|
|
303
|
+
|
|
304
|
+
if (!iw || !ih || !w || !h) return { scaleMode: base };
|
|
305
|
+
|
|
306
|
+
if (fit === "fill" && Math.abs(iw / ih - w / h) > 0.01) {
|
|
307
|
+
const transform: Transform = [
|
|
308
|
+
[1, 0, 0],
|
|
309
|
+
[0, 1, 0],
|
|
310
|
+
];
|
|
311
|
+
|
|
312
|
+
return { scaleMode: "CROP" as const, transform };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const [fx = 0.5, fy = 0.5] = position.split(" ").map((v) => (v.endsWith("%") ? parseFloat(v) / 100 : 0.5));
|
|
316
|
+
|
|
317
|
+
if (fit !== "cover" || (fx === 0.5 && fy === 0.5)) return { scaleMode: base };
|
|
318
|
+
const scale = Math.max(w / iw, h / ih);
|
|
319
|
+
const [dw, dh] = [iw * scale, ih * scale];
|
|
320
|
+
|
|
321
|
+
const transform: Transform = [
|
|
322
|
+
[w / dw, 0, (-(w - dw) * fx) / dw],
|
|
323
|
+
[0, h / dh, (-(h - dh) * fy) / dh],
|
|
324
|
+
];
|
|
325
|
+
|
|
326
|
+
return { scaleMode: "CROP" as const, transform };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function imageFill(name: string, fit: string, position: string, w: number, h: number): ImagePaint {
|
|
330
|
+
const { scaleMode, transform } = mediaScale(spec.snapshot.assets[name].size, fit, position, w, h);
|
|
331
|
+
|
|
332
|
+
return { ...imagePaint(name, scaleMode), ...(transform && { imageTransform: transform }) };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function fillsOf(el: El): Paint[] {
|
|
336
|
+
const s = el.style;
|
|
337
|
+
const [, , w, h] = el.box;
|
|
338
|
+
|
|
339
|
+
if (el.kind === "image") {
|
|
340
|
+
const bg = solidOf(s.bg, el);
|
|
341
|
+
const videoHash = el.video && videoHashes.get(el.video);
|
|
342
|
+
let media: Paint;
|
|
343
|
+
|
|
344
|
+
if (videoHash && el.video) {
|
|
345
|
+
const videoSize = spec.snapshot.assets[el.video]?.size;
|
|
346
|
+
const size = videoSize?.[0] && videoSize[1] ? videoSize : spec.snapshot.assets[el.image].size;
|
|
347
|
+
const { scaleMode, transform } = mediaScale(size, s.objectFit, "50% 50%", w, h);
|
|
348
|
+
media = { type: "VIDEO", videoHash, scaleMode, ...(transform && { videoTransform: transform }) };
|
|
349
|
+
} else media = imageFill(el.image, s.objectFit, "50% 50%", w, h);
|
|
350
|
+
|
|
351
|
+
return bg ? [bg, media] : [media];
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const out: Paint[] = [];
|
|
355
|
+
const bg = solidOf(s.bg, el);
|
|
356
|
+
|
|
357
|
+
if (bg) out.push(bg);
|
|
358
|
+
|
|
359
|
+
if (s.bgImage === "none") return out;
|
|
360
|
+
|
|
361
|
+
const sizes = splitTop(s.bgSize),
|
|
362
|
+
positions = splitTop(s.bgPos);
|
|
363
|
+
|
|
364
|
+
let url = 0;
|
|
365
|
+
|
|
366
|
+
const layers = splitTop(s.bgImage).map((layer, i) => {
|
|
367
|
+
if (layer === "none") return;
|
|
368
|
+
const layerSize = sizes[i % sizes.length];
|
|
369
|
+
const layerPos = positions[i % positions.length];
|
|
370
|
+
|
|
371
|
+
if (layer.startsWith("url(") && el.kind === "frame")
|
|
372
|
+
return imageFill(el.bgImages[url++], layerSize, layerPos, w, h);
|
|
373
|
+
const paint = gradientOf(layer, w, h, layerSize, layerPos);
|
|
374
|
+
|
|
375
|
+
if (!paint) warnings.add(`${selectorOf(el)}: background ${layer} cannot be drawn in Figma and was left out.`);
|
|
376
|
+
|
|
377
|
+
return paint;
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
return [...out, ...layers.filter((p) => p !== undefined).reverse()];
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function applyStrokes(n: FrameNode | ComponentNode | RectangleNode | InstanceNode, el: El) {
|
|
384
|
+
const [t, r, b, l] = el.style.border;
|
|
385
|
+
const paint = (t || r || b || l) && solidOf(el.style.borderColor, el, "stroke");
|
|
386
|
+
|
|
387
|
+
if (!paint) {
|
|
388
|
+
n.strokes = [];
|
|
389
|
+
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
n.strokes = [paint];
|
|
394
|
+
n.strokeAlign = "INSIDE";
|
|
395
|
+
|
|
396
|
+
if (t === r && r === b && b === l) n.strokeWeight = t;
|
|
397
|
+
else Object.assign(n, { strokeTopWeight: t, strokeRightWeight: r, strokeBottomWeight: b, strokeLeftWeight: l });
|
|
398
|
+
|
|
399
|
+
if (el.style.borderStyle === "dashed") {
|
|
400
|
+
const dash = 3 * Math.max(t, r, b, l);
|
|
401
|
+
n.dashPattern = [dash, dash];
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async function applyBox(n: FrameNode | ComponentNode | RectangleNode | InstanceNode, el: El) {
|
|
406
|
+
n.fills = fillsOf(el);
|
|
407
|
+
applyStrokes(n, el);
|
|
408
|
+
[n.topLeftRadius, n.topRightRadius, n.bottomRightRadius, n.bottomLeftRadius] = el.style.radius;
|
|
409
|
+
bindRadius(n);
|
|
410
|
+
n.opacity = el.style.opacity;
|
|
411
|
+
n.blendMode = blendModeOf(el.style.mixBlendMode) ?? (n.type === "RECTANGLE" ? "NORMAL" : "PASS_THROUGH");
|
|
412
|
+
|
|
413
|
+
const blurs = (
|
|
414
|
+
[
|
|
415
|
+
["BACKGROUND_BLUR", blurOf(el.style.backdrop)],
|
|
416
|
+
["LAYER_BLUR", blurOf(el.style.filter)],
|
|
417
|
+
] as const
|
|
418
|
+
).flatMap(([type, px]): Effect[] =>
|
|
419
|
+
px === undefined ? [] : [{ type, blurType: "NORMAL", radius: 2 * px, visible: true }],
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
const style = elevation[el.style.shadow];
|
|
423
|
+
|
|
424
|
+
if (style && !blurs.length) await n.setEffectStyleIdAsync(style.id);
|
|
425
|
+
else n.effects = [...(style?.effects ?? []), ...blurs];
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
async function styleRuns(t: TextNode, el: TextEl) {
|
|
429
|
+
let from = 0;
|
|
430
|
+
|
|
431
|
+
for (const r of mergeRuns(el.runs)) {
|
|
432
|
+
const to = from + r.text.length;
|
|
433
|
+
await t.setRangeTextStyleIdAsync(from, to, textStyleIds.get(typeKey(r))!);
|
|
434
|
+
const fill = solidOf(r.color, el, "text");
|
|
435
|
+
t.setRangeFills(from, to, fill ? [fill] : []);
|
|
436
|
+
t.setRangeTextDecoration(from, to, r.underline ? "UNDERLINE" : r.strike ? "STRIKETHROUGH" : "NONE");
|
|
437
|
+
t.setRangeHyperlink(from, to, r.href && /^https?:/.test(r.href) ? { type: "URL", value: r.href } : null);
|
|
438
|
+
from = to;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function tintIcon(inst: InstanceNode, el: SvgEl) {
|
|
443
|
+
const paint = solidOf(svgColor(el), el, "text");
|
|
444
|
+
const vb = viewBox(el).trim();
|
|
445
|
+
const vbWidth = parseFloat(vb.split(/[\s,]+/)[2]);
|
|
446
|
+
const weight = vbWidth > 0 ? (strokeWidth(el) * el.box[2]) / vbWidth : strokeWidth(el);
|
|
447
|
+
|
|
448
|
+
for (const v of inst.findAllWithCriteria({ types: ["VECTOR"] })) {
|
|
449
|
+
if (filled(el)) {
|
|
450
|
+
v.fills = paint ? [paint] : [];
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
v.strokes = paint ? [paint] : [];
|
|
455
|
+
v.strokeWeight = weight;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function applyContainer(f: FrameNode | ComponentNode, c: Container) {
|
|
460
|
+
f.layoutMode = c.mode;
|
|
461
|
+
[f.paddingTop, f.paddingRight, f.paddingBottom, f.paddingLeft] = c.pad;
|
|
462
|
+
(["paddingTop", "paddingRight", "paddingBottom", "paddingLeft"] as const).forEach((field, i) =>
|
|
463
|
+
bindSpace(f, field, c.pad[i]),
|
|
464
|
+
);
|
|
465
|
+
f.strokesIncludedInLayout = true;
|
|
466
|
+
|
|
467
|
+
if (c.mode === "GRID") {
|
|
468
|
+
f.layoutSizingHorizontal = f.layoutSizingVertical = "FIXED";
|
|
469
|
+
f.gridColumnCount = c.cols.length;
|
|
470
|
+
f.gridRowCount = c.rows.length;
|
|
471
|
+
f.gridColumnSizes = c.cols.map((t) =>
|
|
472
|
+
t.type === "FIXED" ? { type: "FIXED", value: t.px } : { type: "FLEX", value: 1 },
|
|
473
|
+
);
|
|
474
|
+
f.gridRowSizes = c.rows.map((t) => ({ type: "FIXED", value: t.px }));
|
|
475
|
+
f.gridRowGap = c.gap[0];
|
|
476
|
+
f.gridColumnGap = c.gap[1];
|
|
477
|
+
bindSpace(f, "gridRowGap", c.gap[0]);
|
|
478
|
+
bindSpace(f, "gridColumnGap", c.gap[1]);
|
|
479
|
+
} else {
|
|
480
|
+
f.itemSpacing = c.gap;
|
|
481
|
+
bindSpace(f, "itemSpacing", c.gap);
|
|
482
|
+
f.primaryAxisAlignItems = c.main;
|
|
483
|
+
f.counterAxisAlignItems = c.mode === "VERTICAL" && c.cross === "BASELINE" ? "MIN" : c.cross;
|
|
484
|
+
f.layoutWrap = c.wrap ? "WRAP" : "NO_WRAP";
|
|
485
|
+
|
|
486
|
+
// Figma spaces wrapped lines only once the frame wraps.
|
|
487
|
+
if (c.wrap && c.rowGap !== undefined) {
|
|
488
|
+
f.counterAxisSpacing = c.rowGap;
|
|
489
|
+
bindSpace(f, "counterAxisSpacing", c.rowGap);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function backdropOf(el: El): SolidPaint | undefined {
|
|
495
|
+
for (let child = el, a = parentOf.get(el); a; child = a, a = parentOf.get(a)) {
|
|
496
|
+
const under = a.children.slice(0, a.children.indexOf(child)).filter((c) => isAbsolute(c) && covers(c, el));
|
|
497
|
+
|
|
498
|
+
for (const layer of [...under.reverse(), a]) {
|
|
499
|
+
const fills = fillsOf(layer);
|
|
500
|
+
const top = fills[fills.length - 1];
|
|
501
|
+
|
|
502
|
+
if (top?.type === "SOLID" && (top.opacity ?? 1) >= 0.5) return top;
|
|
503
|
+
|
|
504
|
+
if (top && "gradientStops" in top && top.gradientStops[0].color.a >= 0.5) {
|
|
505
|
+
const { r, g, b } = top.gradientStops[0].color;
|
|
506
|
+
|
|
507
|
+
return { type: "SOLID", color: { r, g, b } };
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/** An SVG that is no icon, such as a logo or a chart, as a vector frame in its own colors, bound to tokens where they match. */
|
|
514
|
+
function buildComplexSvg(el: SvgEl) {
|
|
515
|
+
const f = figma.createNodeFromSvg(svgWithSrgbColors(el.svg));
|
|
516
|
+
f.resize(Math.max(el.box[2], 0.01), Math.max(el.box[3], 0.01));
|
|
517
|
+
f.fills = [];
|
|
518
|
+
|
|
519
|
+
const bind = (paints: readonly Paint[], use: PaintUse) =>
|
|
520
|
+
paints.map((p) => (p.type === "SOLID" && (p.opacity ?? 1) === 1 ? (solidOf(hexOf(p.color), el, use) ?? p) : p));
|
|
521
|
+
|
|
522
|
+
for (const n of f.findAll()) {
|
|
523
|
+
if ("fills" in n && n.fills !== figma.mixed) n.fills = bind(n.fills, "fill");
|
|
524
|
+
|
|
525
|
+
if ("strokes" in n) n.strokes = bind(n.strokes, "stroke");
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
return f;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function flushGuessWarnings() {
|
|
532
|
+
for (const [key, { among, at }] of guesses) {
|
|
533
|
+
const [hex, token] = key.split(" ");
|
|
534
|
+
warnings.add(
|
|
535
|
+
`${at.size} element${at.size > 1 ? "s paint" : " paints"} ${hex}, which the page names ${among.join(", ")}, and the capture did not record which token ` +
|
|
536
|
+
`${at.size > 1 ? "they use" : "it uses"}, so ${at.size > 1 ? "they bind" : "it binds"} to ${token} by value: ${[...at].slice(0, 5).join(", ")}${at.size > 5 ? ", …" : ""}. ` +
|
|
537
|
+
"Set the color with var(--token) in the page and capture it again.",
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
return {
|
|
543
|
+
iconSet,
|
|
544
|
+
iconComps,
|
|
545
|
+
iconComponent: (el: SvgEl) => (isSimpleIcon(el) ? iconComps.get(iconKey(el)) : undefined),
|
|
546
|
+
buildComplexSvg,
|
|
547
|
+
solidOf,
|
|
548
|
+
uploadVideos,
|
|
549
|
+
applyBox,
|
|
550
|
+
styleRuns,
|
|
551
|
+
tintIcon,
|
|
552
|
+
applyContainer,
|
|
553
|
+
backdropOf,
|
|
554
|
+
variantSet,
|
|
555
|
+
flushGuessWarnings,
|
|
556
|
+
variableCount: Object.keys(color).length + spaceByValue.size + radiusByValue.size,
|
|
557
|
+
textStyleCount: Object.keys(textStyles).length,
|
|
558
|
+
effectStyleCount: Object.keys(elevation).length,
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
export type Kit = ReturnType<typeof createKit>;
|