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
|
@@ -0,0 +1,962 @@
|
|
|
1
|
+
import { approximations, hexOf } from "../core/css";
|
|
2
|
+
import { fontStyle, foundationsOf, runKey, strokeWidth, svgColor, typeKey } from "../core/foundations";
|
|
3
|
+
import { layerOrder, layoutOf, normalize, type Layout, type Parent, type Place } from "../core/layout";
|
|
4
|
+
import { nameOf, textName } from "../core/naming";
|
|
5
|
+
import type { Imported, ImportSummary } from "../core/protocol";
|
|
6
|
+
import { counterpart, optionalIn, reuseGap } from "../core/reuse";
|
|
7
|
+
import {
|
|
8
|
+
messageOf,
|
|
9
|
+
selectorOf,
|
|
10
|
+
textOf,
|
|
11
|
+
walk,
|
|
12
|
+
type El,
|
|
13
|
+
type FrameEl,
|
|
14
|
+
type ImageEl,
|
|
15
|
+
type Run,
|
|
16
|
+
type SvgEl,
|
|
17
|
+
type TextEl,
|
|
18
|
+
} from "../core/snapshot";
|
|
19
|
+
import { assignRules, droppedContent, type Assigned, type ComponentRule, type ImportSpec } from "../core/spec";
|
|
20
|
+
import { createKit, type Kit } from "./kit";
|
|
21
|
+
import { own, write } from "./owned";
|
|
22
|
+
import { run, type Hook, type Options } from "./rerun";
|
|
23
|
+
import { insideInstance } from "./review";
|
|
24
|
+
|
|
25
|
+
export type { ComponentRule, ImportSpec, Match } from "../core/spec";
|
|
26
|
+
|
|
27
|
+
export type { El } from "../core/snapshot";
|
|
28
|
+
|
|
29
|
+
declare const IS_PLUGIN: boolean | undefined;
|
|
30
|
+
|
|
31
|
+
type Variant = { comp: ComponentNode; el: El; paths: Map<string, number[]> };
|
|
32
|
+
|
|
33
|
+
type BuiltRule = { node: ComponentNode | ComponentSetNode; variants: Map<string, Variant> };
|
|
34
|
+
|
|
35
|
+
type Scope = { root: El; component: boolean; library: boolean; paths: Map<string, number[]> };
|
|
36
|
+
|
|
37
|
+
type Built = FrameNode | ComponentNode | InstanceNode | TextNode | RectangleNode;
|
|
38
|
+
|
|
39
|
+
const TEXT_ALIGN = new Map(
|
|
40
|
+
Object.entries({ center: "CENTER", right: "RIGHT", end: "RIGHT", justify: "JUSTIFIED" } as const),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const ICONS_GROUP = "Icons";
|
|
44
|
+
const varsKey = (el: El) => JSON.stringify(el.vars ?? {});
|
|
45
|
+
|
|
46
|
+
// SAFETY: a path in `paths` steps only through frames, the one kind of El with children.
|
|
47
|
+
const at = (el: El, path: number[]) => path.reduce((e, i) => (e as FrameEl).children[i], el);
|
|
48
|
+
const mainIdOf = (id: string) => id.slice(id.lastIndexOf(";") + 1);
|
|
49
|
+
const luminance = (c: RGB) => 0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b;
|
|
50
|
+
const flowOf = (name: string) => name.slice(0, Math.max(0, name.lastIndexOf("/")));
|
|
51
|
+
|
|
52
|
+
const boxKey = ({ style: s, ...el }: El) =>
|
|
53
|
+
JSON.stringify([
|
|
54
|
+
s.bg,
|
|
55
|
+
s.bgImage,
|
|
56
|
+
s.bgSize,
|
|
57
|
+
s.bgPos,
|
|
58
|
+
s.objectFit,
|
|
59
|
+
el.kind === "frame" ? el.bgImages : el.kind === "image" ? [el.image, el.video] : 0,
|
|
60
|
+
s.border,
|
|
61
|
+
s.borderColor,
|
|
62
|
+
s.borderStyle,
|
|
63
|
+
s.radius,
|
|
64
|
+
s.opacity,
|
|
65
|
+
s.shadow,
|
|
66
|
+
s.backdrop,
|
|
67
|
+
s.filter,
|
|
68
|
+
s.mixBlendMode,
|
|
69
|
+
el.vars,
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
// Sizing modes first: a hugging frame would undo a resize made before it turns FIXED.
|
|
73
|
+
function size(node: FrameNode | ComponentNode | InstanceNode | RectangleNode, el: El, w: Place["w"], h: Place["h"]) {
|
|
74
|
+
node.layoutSizingHorizontal = w;
|
|
75
|
+
node.layoutSizingVertical = h;
|
|
76
|
+
|
|
77
|
+
if (w === "FIXED" || h === "FIXED")
|
|
78
|
+
node.resize(w === "FIXED" ? el.box[2] : node.width, h === "FIXED" ? el.box[3] : node.height);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function place(node: Built, el: El, layout: Layout) {
|
|
82
|
+
const p: Place = layout.place;
|
|
83
|
+
|
|
84
|
+
if (p.positioning === "ABSOLUTE") {
|
|
85
|
+
node.layoutPositioning = "ABSOLUTE";
|
|
86
|
+
node.constraints = p.constraints;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (node.type === "TEXT") {
|
|
90
|
+
if (p.w === "HUG") node.textAutoResize = "WIDTH_AND_HEIGHT";
|
|
91
|
+
else {
|
|
92
|
+
node.resize(el.box[2], node.height);
|
|
93
|
+
node.textAutoResize = "HEIGHT";
|
|
94
|
+
|
|
95
|
+
if (p.w === "FILL") node.layoutSizingHorizontal = "FILL";
|
|
96
|
+
}
|
|
97
|
+
} else size(node, el, p.w, p.h);
|
|
98
|
+
|
|
99
|
+
if (p.positioning === "ABSOLUTE") {
|
|
100
|
+
node.x = p.x;
|
|
101
|
+
node.y = p.y;
|
|
102
|
+
} else if (p.cell) {
|
|
103
|
+
if (p.cell.colSpan > 1) node.gridColumnSpan = p.cell.colSpan;
|
|
104
|
+
|
|
105
|
+
if (p.cell.rowSpan > 1) node.gridRowSpan = p.cell.rowSpan;
|
|
106
|
+
|
|
107
|
+
if (p.w !== "FILL") node.gridChildHorizontalAlign = p.cell.h;
|
|
108
|
+
|
|
109
|
+
if (p.h !== "FILL") node.gridChildVerticalAlign = p.cell.v;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (layout.maxWidth && "maxWidth" in node) node.maxWidth = layout.maxWidth;
|
|
113
|
+
|
|
114
|
+
if (layout.minHeight && "minHeight" in node) node.minHeight = layout.minHeight;
|
|
115
|
+
|
|
116
|
+
if (layout.rotation && "rotation" in node) node.rotation = layout.rotation;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Absolute children move in the layer list without reflowing their siblings, and so does every child of a grid, which stays in its cell.
|
|
120
|
+
// `nodes` are the children built for `children`, in the same order, whatever order the frame lists them in.
|
|
121
|
+
function reorderChildren(f: FrameNode | ComponentNode, children: El[], nodes: SceneNode[]) {
|
|
122
|
+
const grid = f.layoutMode === "GRID";
|
|
123
|
+
const movable = (i: number) => grid || ("layoutPositioning" in nodes[i] && nodes[i].layoutPositioning === "ABSOLUTE");
|
|
124
|
+
|
|
125
|
+
if (!children.some((_, i) => movable(i))) return;
|
|
126
|
+
layerOrder(children, movable, f.layoutMode !== "NONE").forEach((i, pos) => f.insertChild(pos, nodes[i]));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function layoutComponentsPage(parent: BaseNode & ChildrenMixin, groups: Record<string, SceneNode[]>) {
|
|
130
|
+
const sections: SectionNode[] = [];
|
|
131
|
+
let sectionY = 0;
|
|
132
|
+
|
|
133
|
+
for (const [title, nodes] of Object.entries(groups)) {
|
|
134
|
+
const section = figma.createSection();
|
|
135
|
+
section.name = title;
|
|
136
|
+
parent.appendChild(section);
|
|
137
|
+
const compact = title === ICONS_GROUP;
|
|
138
|
+
const gap = compact ? 40 : 120;
|
|
139
|
+
const maxRow = compact ? 9 * 64 : 1600;
|
|
140
|
+
|
|
141
|
+
let x = 40,
|
|
142
|
+
y = 80,
|
|
143
|
+
rowH = 0;
|
|
144
|
+
|
|
145
|
+
for (const n of nodes) {
|
|
146
|
+
if (x + n.width > maxRow + 40 && x > 40) {
|
|
147
|
+
x = 40;
|
|
148
|
+
y += rowH + gap;
|
|
149
|
+
rowH = 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
section.appendChild(n);
|
|
153
|
+
n.x = x;
|
|
154
|
+
n.y = y;
|
|
155
|
+
x += n.width + gap;
|
|
156
|
+
rowH = Math.max(rowH, n.height);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
section.resizeWithoutConstraints(Math.max(maxRow, x) + 80, y + rowH + 80);
|
|
160
|
+
section.x = 0;
|
|
161
|
+
section.y = sectionY;
|
|
162
|
+
sectionY += section.height + 120;
|
|
163
|
+
sections.push(section);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return sections;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// A shared INSTANCE_SWAP property shows one icon in every variant, so an icon layer whose icon differs between variants stays unbound.
|
|
170
|
+
function exposeProperties(
|
|
171
|
+
rule: ComponentRule,
|
|
172
|
+
owner: ComponentNode | ComponentSetNode,
|
|
173
|
+
comps: ComponentNode[],
|
|
174
|
+
iconComps: Map<string, ComponentNode>,
|
|
175
|
+
iconOf: Map<string, ComponentNode>,
|
|
176
|
+
unknownTextProperties: string[],
|
|
177
|
+
elOf: (n: SceneNode) => { el: El; parent?: El } | undefined,
|
|
178
|
+
) {
|
|
179
|
+
const text = rule.text ?? {};
|
|
180
|
+
const keys = new Map<string, string>();
|
|
181
|
+
const preferredValues = [...iconComps.values()].map((c) => ({ type: "COMPONENT" as const, key: c.key }));
|
|
182
|
+
|
|
183
|
+
const layers = comps.flatMap((comp) => {
|
|
184
|
+
const out: { n: SceneNode; name: string; id: string }[] = [];
|
|
185
|
+
const seen = new Map<string, number>();
|
|
186
|
+
|
|
187
|
+
const visit = (n: SceneNode) => {
|
|
188
|
+
if (n.type === "TEXT" || iconOf.has(n.id)) {
|
|
189
|
+
const count = (seen.get(n.name) ?? 0) + 1;
|
|
190
|
+
seen.set(n.name, count);
|
|
191
|
+
const name = count > 1 ? `${n.name} ${count}` : n.name;
|
|
192
|
+
out.push({ n, name, id: `${n.type}:${name}` });
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (n.type !== "INSTANCE" && "children" in n) n.children.forEach(visit);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
comp.children.forEach(visit);
|
|
199
|
+
|
|
200
|
+
return out;
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const iconsAt = new Map<string, Set<string>>();
|
|
204
|
+
|
|
205
|
+
for (const { n, id } of layers) {
|
|
206
|
+
const icon = iconOf.get(n.id);
|
|
207
|
+
|
|
208
|
+
if (icon) iconsAt.set(id, (iconsAt.get(id) ?? new Set()).add(icon.id));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
for (const { n, name, id } of layers) {
|
|
212
|
+
if (n.type === "TEXT") {
|
|
213
|
+
// Setting a TEXT property rewrites the whole layer in one style, so a mixed-style layer keeps its ranges unbound.
|
|
214
|
+
if (n.getStyledTextSegments(["fills", "textStyleId", "textDecoration"]).length > 1) continue;
|
|
215
|
+
const key = keys.get(id) ?? owner.addComponentProperty(name, "TEXT", text[name] ?? n.characters);
|
|
216
|
+
n.componentPropertyReferences = { characters: key };
|
|
217
|
+
keys.set(id, key);
|
|
218
|
+
} else if (iconsAt.get(id)!.size === 1) {
|
|
219
|
+
const key =
|
|
220
|
+
keys.get(id) ?? owner.addComponentProperty(name, "INSTANCE_SWAP", iconOf.get(n.id)!.id, { preferredValues });
|
|
221
|
+
|
|
222
|
+
n.componentPropertyReferences = { mainComponent: key };
|
|
223
|
+
keys.set(id, key);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
exposeBooleans(rule, owner, comps, elOf);
|
|
228
|
+
|
|
229
|
+
const textLayers = new Set(layers.flatMap((l) => (l.n.type === "TEXT" ? [l.name] : [])));
|
|
230
|
+
|
|
231
|
+
for (const name of Object.keys(text)) {
|
|
232
|
+
if (!textLayers.has(name))
|
|
233
|
+
unknownTextProperties.push(
|
|
234
|
+
`${rule.name} has no text layer "${name}". Its text layers: ${[...textLayers].join(", ")}`,
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** A BOOLEAN property per `booleans` entry, wired to the visibility of every layer of the mains it matches. */
|
|
240
|
+
function exposeBooleans(
|
|
241
|
+
rule: ComponentRule,
|
|
242
|
+
owner: ComponentNode | ComponentSetNode,
|
|
243
|
+
comps: ComponentNode[],
|
|
244
|
+
elOf: (n: SceneNode) => { el: El; parent?: El } | undefined,
|
|
245
|
+
) {
|
|
246
|
+
for (const [name, match] of Object.entries(rule.booleans ?? {})) {
|
|
247
|
+
let key: string | undefined;
|
|
248
|
+
|
|
249
|
+
for (const comp of comps) {
|
|
250
|
+
for (const n of comp.findAll((d) => !insideInstance(d))) {
|
|
251
|
+
const at = elOf(n);
|
|
252
|
+
|
|
253
|
+
if (!at?.parent || !match(at.el, at.parent)) continue;
|
|
254
|
+
key ??= owner.addComponentProperty(name, "BOOLEAN", true);
|
|
255
|
+
n.componentPropertyReferences = { ...n.componentPropertyReferences, visible: key };
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async function linkScreens(
|
|
262
|
+
specName: string,
|
|
263
|
+
links: Record<string, string>,
|
|
264
|
+
pages: FrameNode[],
|
|
265
|
+
sectionRules: ComponentRule[],
|
|
266
|
+
resolved: Map<ComponentRule, BuiltRule>,
|
|
267
|
+
) {
|
|
268
|
+
const screenByName = new Map(pages.map((p) => [p.name, p]));
|
|
269
|
+
|
|
270
|
+
for (const rule of sectionRules) {
|
|
271
|
+
const v = resolved.get(rule)?.variants.get("");
|
|
272
|
+
|
|
273
|
+
if (!v) continue;
|
|
274
|
+
|
|
275
|
+
for (const [id, path] of v.paths) {
|
|
276
|
+
const dest = at(v.el, path)
|
|
277
|
+
.cls.map((c) => screenByName.get(links[c]))
|
|
278
|
+
.find(Boolean);
|
|
279
|
+
|
|
280
|
+
if (!dest) continue;
|
|
281
|
+
const node = await figma.getNodeByIdAsync(id);
|
|
282
|
+
|
|
283
|
+
if (!node || !("setReactionsAsync" in node)) continue;
|
|
284
|
+
|
|
285
|
+
const navigate: Action = {
|
|
286
|
+
type: "NODE",
|
|
287
|
+
destinationId: dest.id,
|
|
288
|
+
navigation: "NAVIGATE",
|
|
289
|
+
transition: { type: "DISSOLVE", easing: { type: "EASE_OUT" }, duration: 0.2 },
|
|
290
|
+
resetScrollPosition: true,
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
await node.setReactionsAsync([{ trigger: { type: "ON_CLICK" }, actions: [navigate] }]);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
figma.currentPage.flowStartingPoints = [{ nodeId: pages[0].id, name: specName }];
|
|
298
|
+
// A rerun removes only the flows it finds tagged so, and leaves a person's flows alone.
|
|
299
|
+
write(pages[0], "flow", specName);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function arrangeCanvas(
|
|
303
|
+
spec: ImportSpec,
|
|
304
|
+
kit: Kit,
|
|
305
|
+
pages: FrameNode[],
|
|
306
|
+
sectionEls: { el: El; rule: ComponentRule }[],
|
|
307
|
+
libraryRules: ComponentRule[],
|
|
308
|
+
resolved: Map<ComponentRule, BuiltRule>,
|
|
309
|
+
others: SceneNode[],
|
|
310
|
+
kept: Vector | undefined,
|
|
311
|
+
warnings: Set<string>,
|
|
312
|
+
) {
|
|
313
|
+
let x = Math.max(0, ...others.map((n) => n.x + n.width)) + 400;
|
|
314
|
+
// A Make embed is the source of its own rebuild, so the rebuild takes its place. Any other page lands beside what the file holds.
|
|
315
|
+
const replace = spec.replace ?? spec.snapshot.url.includes(".figma.site");
|
|
316
|
+
|
|
317
|
+
const source =
|
|
318
|
+
kept || !replace
|
|
319
|
+
? undefined
|
|
320
|
+
: // SAFETY: widening to string only lets the check name CODE_INSTANCE, the embed's type, which the plugin typings lack.
|
|
321
|
+
others.find((n) => (n.type as string) === "CODE_INSTANCE" && n.id === spec.snapshot.embed);
|
|
322
|
+
|
|
323
|
+
if (replace && !kept && !source) {
|
|
324
|
+
warnings.add(
|
|
325
|
+
"The rebuild was to take the place of the Figma Make embed it was captured from, but the capture names no embed on this page, so it lands beside the page's content and hides nothing.",
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const anchor = kept ?? (source && { x: source.x, y: source.y });
|
|
330
|
+
const y = anchor?.y ?? 0;
|
|
331
|
+
const PAD = 80;
|
|
332
|
+
|
|
333
|
+
const wrap = (name: string, fill: (s: SectionNode) => void, origin?: Vector) => {
|
|
334
|
+
const s = own(figma.createSection(), spec.name);
|
|
335
|
+
s.name = name;
|
|
336
|
+
fill(s);
|
|
337
|
+
const kids = s.children;
|
|
338
|
+
|
|
339
|
+
for (const k of kids) {
|
|
340
|
+
k.x += PAD;
|
|
341
|
+
k.y += PAD;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
s.resizeWithoutConstraints(
|
|
345
|
+
Math.max(...kids.map((k) => k.x + k.width)) + PAD,
|
|
346
|
+
Math.max(...kids.map((k) => k.y + k.height)) + PAD,
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
if (origin) {
|
|
350
|
+
s.x = origin.x - PAD;
|
|
351
|
+
s.y = origin.y - PAD;
|
|
352
|
+
} else {
|
|
353
|
+
s.x = x;
|
|
354
|
+
s.y = y - PAD;
|
|
355
|
+
x += s.width + 200;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return s;
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const flows = [...new Set(pages.map((p) => flowOf(p.name)))].map((flow) =>
|
|
362
|
+
pages.filter((p) => flowOf(p.name) === flow),
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
const landing = wrap(
|
|
366
|
+
spec.screens ? "Screens" : "Landing Page",
|
|
367
|
+
(s) => {
|
|
368
|
+
let top = 0;
|
|
369
|
+
|
|
370
|
+
for (const row of flows) {
|
|
371
|
+
let left = 0;
|
|
372
|
+
|
|
373
|
+
for (const page of row) {
|
|
374
|
+
s.appendChild(page);
|
|
375
|
+
page.x = left;
|
|
376
|
+
page.y = top;
|
|
377
|
+
left += page.width + 120;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
top += Math.max(...row.map((p) => p.height)) + 240;
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
anchor,
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
if (anchor) write(landing, "anchor", JSON.stringify(anchor));
|
|
387
|
+
|
|
388
|
+
const sections = wrap("Sections", (s) => {
|
|
389
|
+
let top = 0;
|
|
390
|
+
|
|
391
|
+
for (const rule of new Set(sectionEls.map((e) => e.rule))) {
|
|
392
|
+
const node = resolved.get(rule)!.node;
|
|
393
|
+
s.appendChild(node);
|
|
394
|
+
node.y = top;
|
|
395
|
+
top += node.height + 120;
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
const components = wrap("Components", (s) => {
|
|
400
|
+
const groups = layoutComponentsPage(s, {
|
|
401
|
+
[ICONS_GROUP]: Object.values(kit.iconSet),
|
|
402
|
+
...Object.fromEntries(libraryRules.map((r) => [r.name, [resolved.get(r)!.node]])),
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
// A group whose mains mostly came from dark backdrops is shown on the darkest of them, so light-on-dark components stay legible.
|
|
406
|
+
for (const group of groups) {
|
|
407
|
+
const rule = libraryRules.find((r) => r.name === group.name);
|
|
408
|
+
|
|
409
|
+
if (!rule) continue;
|
|
410
|
+
const backdrops = [...resolved.get(rule)!.variants.values()].map((v) => kit.backdropOf(v.el));
|
|
411
|
+
|
|
412
|
+
const dark = backdrops
|
|
413
|
+
.filter((p): p is SolidPaint => !!p && luminance(p.color) < 0.4)
|
|
414
|
+
.sort((a, b) => luminance(a.color) - luminance(b.color));
|
|
415
|
+
|
|
416
|
+
if (dark.length * 2 > backdrops.length) group.fills = [{ ...dark[0], opacity: 1 }];
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
return { ours: [landing, sections, components], source };
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function resolvePageBackground(root: El, viewport: [number, number], kit: Kit): Paint[] {
|
|
424
|
+
let background: Paint[] = [
|
|
425
|
+
kit.solidOf(root.style.bg, root) ??
|
|
426
|
+
kit.solidOf("rgb(255, 255, 255)", root) ?? { type: "SOLID", color: { r: 1, g: 1, b: 1 } },
|
|
427
|
+
];
|
|
428
|
+
|
|
429
|
+
walk(root, (el) => {
|
|
430
|
+
const bg = el.box[2] >= viewport[0] && el.box[3] >= viewport[1] - 1 && kit.solidOf(el.style.bg, el);
|
|
431
|
+
|
|
432
|
+
if (bg) background = [bg];
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
return background;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function buildImportSummary(
|
|
439
|
+
kit: Kit,
|
|
440
|
+
ours: SectionNode[],
|
|
441
|
+
root: El,
|
|
442
|
+
reached: Set<El>,
|
|
443
|
+
fallbacks: { rule: string; reason: string }[],
|
|
444
|
+
unknownTextProperties: string[],
|
|
445
|
+
warnings: Set<string>,
|
|
446
|
+
): ImportSummary {
|
|
447
|
+
const all = ours.flatMap((s) => s.findAll(() => true));
|
|
448
|
+
const dropped = droppedContent(root, reached);
|
|
449
|
+
kit.flushGuessWarnings();
|
|
450
|
+
|
|
451
|
+
return {
|
|
452
|
+
variables: kit.variableCount,
|
|
453
|
+
textStyles: kit.textStyleCount,
|
|
454
|
+
effectStyles: kit.effectStyleCount,
|
|
455
|
+
components: all.filter((n) => n.type === "COMPONENT").length,
|
|
456
|
+
componentSets: all.filter((n) => n.type === "COMPONENT_SET").length,
|
|
457
|
+
instances: all.filter((n) => n.type === "INSTANCE" && !insideInstance(n)).length,
|
|
458
|
+
fallbacks: fallbacks.length,
|
|
459
|
+
fallbackRules: [...new Set(fallbacks.map((f) => f.rule))],
|
|
460
|
+
fallbackReasons: [...new Set(fallbacks.map((f) => `${f.rule}: ${f.reason}`))],
|
|
461
|
+
unknownTextProperties,
|
|
462
|
+
dropped: dropped.sections,
|
|
463
|
+
lost: dropped.elements,
|
|
464
|
+
warnings: [...warnings, ...dropped.sections, ...dropped.elements],
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
async function loadFontMatcher() {
|
|
469
|
+
const styles = new Map<string, string[]>();
|
|
470
|
+
|
|
471
|
+
for (const { fontName } of await figma.listAvailableFontsAsync())
|
|
472
|
+
styles.set(fontName.family, [...(styles.get(fontName.family) ?? []), fontName.style]);
|
|
473
|
+
|
|
474
|
+
const squash = (s: string) => s.replace(/\s/g, "").toLowerCase();
|
|
475
|
+
|
|
476
|
+
return {
|
|
477
|
+
styleOf: (r: Run, family: string) =>
|
|
478
|
+
styles.get(family)?.find((s) => squash(s) === squash(fontStyle(r))) ?? fontStyle(r),
|
|
479
|
+
available: (family: string) => styles.has(family),
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
async function importInto(
|
|
484
|
+
spec: ImportSpec,
|
|
485
|
+
root: El,
|
|
486
|
+
rules: ReturnType<typeof assignRules>,
|
|
487
|
+
others: SceneNode[],
|
|
488
|
+
kept?: Vector,
|
|
489
|
+
) {
|
|
490
|
+
const tag = <N extends BaseNode>(n: N) => own(n, spec.name);
|
|
491
|
+
const { libraryRules, sectionRules, assignments } = rules;
|
|
492
|
+
const warnings = new Set(rules.warnings);
|
|
493
|
+
const font = spec.font ?? "Inter";
|
|
494
|
+
const fonts = await loadFontMatcher();
|
|
495
|
+
const foundations = foundationsOf(spec, root, font, fonts.styleOf, fonts.available);
|
|
496
|
+
|
|
497
|
+
const fontNames = Object.values(foundations.theme.textStyles).map((t) => ({
|
|
498
|
+
family: t.family ?? font,
|
|
499
|
+
style: t.style,
|
|
500
|
+
}));
|
|
501
|
+
|
|
502
|
+
await Promise.all(
|
|
503
|
+
[...new Map(fontNames.map((f) => [`${f.family}|${f.style}`, f])).values()].map((f) => figma.loadFontAsync(f)),
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
if (foundations.missingFamilies.length)
|
|
507
|
+
warnings.add(
|
|
508
|
+
`Figma has no font ${foundations.missingFamilies.map((f) => `"${f}"`).join(", ")}, so text the page sets in ${foundations.missingFamilies.length > 1 ? "them" : "it"} is set in ${font}. Install the font, or pick an available one with the spec's font.`,
|
|
509
|
+
);
|
|
510
|
+
|
|
511
|
+
const parentOf = new Map<El, FrameEl>();
|
|
512
|
+
walk(root, (el, parent) => parent?.kind === "frame" && parentOf.set(el, parent));
|
|
513
|
+
const kit = createKit(spec, foundations, parentOf, warnings);
|
|
514
|
+
|
|
515
|
+
const usedVideos = new Set<string>();
|
|
516
|
+
const recordVideos = (node: El) => walk(node, (el) => el.kind === "image" && el.video && usedVideos.add(el.video));
|
|
517
|
+
|
|
518
|
+
for (const variants of rules.mains.values()) for (const main of variants.values()) recordVideos(main);
|
|
519
|
+
walk(root, (el) => {
|
|
520
|
+
const a = assignments.get(el);
|
|
521
|
+
|
|
522
|
+
if (a && sectionRules.includes(a.rule)) recordVideos(el);
|
|
523
|
+
});
|
|
524
|
+
await kit.uploadVideos(usedVideos);
|
|
525
|
+
|
|
526
|
+
const iconOf = new Map<string, ComponentNode>();
|
|
527
|
+
const reached = new Set<El>();
|
|
528
|
+
const built = new Map<ComponentRule, Promise<BuiltRule>>();
|
|
529
|
+
const fallbacks: { rule: string; reason: string }[] = [];
|
|
530
|
+
const unknownTextProperties: string[] = [];
|
|
531
|
+
|
|
532
|
+
const ensureRule = (rule: ComponentRule) => {
|
|
533
|
+
if (!built.has(rule)) built.set(rule, buildRule(rule));
|
|
534
|
+
|
|
535
|
+
return built.get(rule)!;
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
const variantOf = async (a: Assigned) => (await ensureRule(a.rule)).variants.get(a.value)!;
|
|
539
|
+
|
|
540
|
+
async function textNode(el: TextEl, parent: El | undefined, scope: Scope) {
|
|
541
|
+
const t = figma.createText();
|
|
542
|
+
// Set in the first run's text style font, which is loaded, before the runs take their styles.
|
|
543
|
+
const first = foundations.theme.textStyles[foundations.textStyleNames.get(typeKey(el.runs[0]))!];
|
|
544
|
+
t.fontName = { family: first.family ?? font, style: first.style };
|
|
545
|
+
t.characters = textOf(el);
|
|
546
|
+
await kit.styleRuns(t, el);
|
|
547
|
+
t.textAlignHorizontal = TEXT_ALIGN.get(el.style.textAlign) ?? "LEFT";
|
|
548
|
+
t.name = textName(el, parent, scope.library);
|
|
549
|
+
|
|
550
|
+
return t;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function svgNode(el: SvgEl) {
|
|
554
|
+
const comp = kit.iconComponent(el);
|
|
555
|
+
|
|
556
|
+
if (!comp) {
|
|
557
|
+
const f = kit.buildComplexSvg(el);
|
|
558
|
+
f.name = nameOf(el);
|
|
559
|
+
|
|
560
|
+
return f;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const inst = comp.createInstance();
|
|
564
|
+
inst.name = "Icon";
|
|
565
|
+
inst.resize(el.box[2], el.box[3]);
|
|
566
|
+
kit.tintIcon(inst, el);
|
|
567
|
+
iconOf.set(inst.id, comp);
|
|
568
|
+
|
|
569
|
+
return inst;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
async function imageNode(el: ImageEl) {
|
|
573
|
+
const r = figma.createRectangle();
|
|
574
|
+
r.name = nameOf(el);
|
|
575
|
+
r.resize(el.box[2], el.box[3]);
|
|
576
|
+
await kit.applyBox(r, el);
|
|
577
|
+
|
|
578
|
+
return r;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
async function frameNode(el: FrameEl, layout: Layout, path: number[], scope: Scope) {
|
|
582
|
+
const asComponent = el === scope.root && scope.component;
|
|
583
|
+
const f = asComponent ? figma.createComponent() : figma.createFrame();
|
|
584
|
+
f.name = nameOf(el);
|
|
585
|
+
f.resize(Math.max(el.box[2], 0.01), Math.max(el.box[3], 0.01));
|
|
586
|
+
await kit.applyBox(f, el);
|
|
587
|
+
f.clipsContent = /hidden|clip/.test(el.style.overflow);
|
|
588
|
+
const container = layout.container!;
|
|
589
|
+
kit.applyContainer(f, container);
|
|
590
|
+
// Children may FILL, which a hugging parent would refuse. The caller sets the final sizing.
|
|
591
|
+
size(f, el, "FIXED", "FIXED");
|
|
592
|
+
const self: Parent = { el, container, w: layout.place.w, h: layout.place.h };
|
|
593
|
+
const nodes: SceneNode[] = [];
|
|
594
|
+
|
|
595
|
+
for (const [i, c] of el.children.entries()) {
|
|
596
|
+
const child = await build(c, self, [...path, i], scope);
|
|
597
|
+
nodes.push(child.node);
|
|
598
|
+
const cell = child.layout.place.positioning === "AUTO" ? child.layout.place.cell : undefined;
|
|
599
|
+
const gridRows = f.layoutMode === "GRID" && !cell ? f.gridRowCount : undefined;
|
|
600
|
+
|
|
601
|
+
if (cell) f.appendChildAt(child.node, cell.row, cell.col);
|
|
602
|
+
else f.appendChild(child.node);
|
|
603
|
+
place(child.node, c, child.layout);
|
|
604
|
+
|
|
605
|
+
if (gridRows !== undefined && f.gridRowCount > gridRows) f.gridRowCount = gridRows;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
reorderChildren(f, el.children, nodes);
|
|
609
|
+
|
|
610
|
+
return f;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
async function build(
|
|
614
|
+
el: El,
|
|
615
|
+
parent: Parent | undefined,
|
|
616
|
+
path: number[],
|
|
617
|
+
scope: Scope,
|
|
618
|
+
): Promise<{ node: Built; layout: Layout }> {
|
|
619
|
+
reached.add(el);
|
|
620
|
+
const layout = layoutOf(el, parent);
|
|
621
|
+
const assigned = el === scope.root ? undefined : assignments.get(el);
|
|
622
|
+
|
|
623
|
+
const node = assigned
|
|
624
|
+
? await instanceNode(el, assigned, parent, path, scope)
|
|
625
|
+
: el.kind === "text"
|
|
626
|
+
? await textNode(el, parent?.el, scope)
|
|
627
|
+
: el.kind === "svg"
|
|
628
|
+
? svgNode(el)
|
|
629
|
+
: el.kind === "image"
|
|
630
|
+
? await imageNode(el)
|
|
631
|
+
: await frameNode(el, layout, path, scope);
|
|
632
|
+
|
|
633
|
+
scope.paths.set(node.id, path);
|
|
634
|
+
|
|
635
|
+
if (spec.annotations && !assigned) annotate(node, el);
|
|
636
|
+
|
|
637
|
+
return { node, layout };
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Dev Mode shows a layer's annotations, so a developer sees where it came from and what Figma could not draw.
|
|
641
|
+
function annotate(node: Built, el: El) {
|
|
642
|
+
if (!("annotations" in node) || el.tag.startsWith("#")) return;
|
|
643
|
+
const label = [`From \`${selectorOf(el)}\``, ...approximations(el).map((n) => `- ${n}`)].join("\n");
|
|
644
|
+
|
|
645
|
+
try {
|
|
646
|
+
node.annotations = [{ labelMarkdown: label }];
|
|
647
|
+
} catch (e) {
|
|
648
|
+
warnings.add(`Dev Mode annotations were left out: ${messageOf(e)}`);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
async function buildMain(el: El, scope: Scope): Promise<ComponentNode> {
|
|
653
|
+
const layout = layoutOf(el);
|
|
654
|
+
|
|
655
|
+
if (el.kind === "frame") {
|
|
656
|
+
// SAFETY: buildMain's scope has `el` as its root and `component` set, and frameNode makes such a root a component.
|
|
657
|
+
const comp = (await frameNode(el, layout, [], scope)) as ComponentNode;
|
|
658
|
+
size(comp, el, layout.place.w, layout.place.h);
|
|
659
|
+
|
|
660
|
+
return comp;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const comp = figma.createComponent();
|
|
664
|
+
comp.layoutMode = "VERTICAL";
|
|
665
|
+
comp.fills = [];
|
|
666
|
+
const child = await build(el, undefined, [], scope);
|
|
667
|
+
comp.appendChild(child.node);
|
|
668
|
+
place(child.node, el, child.layout);
|
|
669
|
+
comp.layoutSizingHorizontal = comp.layoutSizingVertical = "HUG";
|
|
670
|
+
|
|
671
|
+
return comp;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
async function buildRule(rule: ComponentRule): Promise<BuiltRule> {
|
|
675
|
+
const library = libraryRules.includes(rule);
|
|
676
|
+
const variants = new Map<string, Variant>();
|
|
677
|
+
|
|
678
|
+
for (const [value, el] of rules.mains.get(rule)!) {
|
|
679
|
+
const paths = new Map<string, number[]>();
|
|
680
|
+
const comp = tag(await buildMain(el, { root: el, component: true, library, paths }));
|
|
681
|
+
comp.name = rule.variant ? `${rule.variant.property}=${value}` : rule.name;
|
|
682
|
+
variants.set(value, { comp, el, paths });
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const comps = [...variants.values()].map((v) => v.comp);
|
|
686
|
+
const node = rule.variant ? tag(kit.variantSet(rule.name, comps, rule.description ?? "")) : comps[0];
|
|
687
|
+
|
|
688
|
+
if (!rule.variant && rule.description) node.description = rule.description;
|
|
689
|
+
|
|
690
|
+
// The element a layer of a main was built from, and its parent, for the rule's boolean matches.
|
|
691
|
+
const elOf = (n: SceneNode) => {
|
|
692
|
+
const v = [...variants.values()].find((x) => x.paths.has(n.id));
|
|
693
|
+
const path = v?.paths.get(n.id);
|
|
694
|
+
|
|
695
|
+
return v && path?.length ? { el: at(v.el, path), parent: at(v.el, path.slice(0, -1)) } : undefined;
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
if (library) exposeProperties(rule, node, comps, kit.iconComps, iconOf, unknownTextProperties, elOf);
|
|
699
|
+
else exposeBooleans(rule, node, comps, elOf);
|
|
700
|
+
|
|
701
|
+
return { node, variants };
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
async function restyle(n: FrameNode | RectangleNode | InstanceNode, s: El, w: El) {
|
|
705
|
+
if (boxKey(s) !== boxKey(w)) await kit.applyBox(n, w);
|
|
706
|
+
|
|
707
|
+
if (n.type !== "RECTANGLE" && s.style.overflow !== w.style.overflow)
|
|
708
|
+
n.clipsContent = /hidden|clip/.test(w.style.overflow);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
async function diff(owner: InstanceNode, n: SceneNode, s: El, w: El) {
|
|
712
|
+
reached.add(w);
|
|
713
|
+
|
|
714
|
+
if (n.type === "TEXT" && s.kind === "text" && w.kind === "text") {
|
|
715
|
+
const key = n.componentPropertyReferences?.characters;
|
|
716
|
+
|
|
717
|
+
// Against the node, not the main's occurrence: a shared TEXT property gives every variant the first variant's text.
|
|
718
|
+
if (n.characters !== textOf(w)) {
|
|
719
|
+
if (key) owner.setProperties({ [key]: textOf(w) });
|
|
720
|
+
else n.characters = textOf(w);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
if (s.runs.map(runKey).join() !== w.runs.map(runKey).join() || varsKey(s) !== varsKey(w))
|
|
724
|
+
await kit.styleRuns(n, w);
|
|
725
|
+
|
|
726
|
+
if (s.style.textAlign !== w.style.textAlign) n.textAlignHorizontal = TEXT_ALIGN.get(w.style.textAlign) ?? "LEFT";
|
|
727
|
+
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
if (s === w) return;
|
|
732
|
+
|
|
733
|
+
const comp = s.kind === "svg" && w.kind === "svg" ? kit.iconComponent(w) : undefined;
|
|
734
|
+
|
|
735
|
+
if (n.type === "INSTANCE" && comp && s.kind === "svg" && w.kind === "svg") {
|
|
736
|
+
// Against the node, for the same reason as text: a shared INSTANCE_SWAP property resets every variant's icon.
|
|
737
|
+
const swap = (await n.getMainComponentAsync())?.id !== comp.id;
|
|
738
|
+
|
|
739
|
+
if (swap) {
|
|
740
|
+
const key = n.componentPropertyReferences?.mainComponent;
|
|
741
|
+
|
|
742
|
+
if (key) owner.setProperties({ [key]: comp.id });
|
|
743
|
+
else n.swapComponent(comp);
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
if (swap || svgColor(s) !== svgColor(w) || strokeWidth(s) !== strokeWidth(w) || varsKey(s) !== varsKey(w))
|
|
747
|
+
kit.tintIcon(n, w);
|
|
748
|
+
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
const nested = assignments.get(w);
|
|
753
|
+
|
|
754
|
+
if (n.type === "INSTANCE" && nested) {
|
|
755
|
+
const v = await variantOf(nested);
|
|
756
|
+
|
|
757
|
+
if (assignments.get(s)?.value !== nested.value) {
|
|
758
|
+
n.swapComponent(v.comp);
|
|
759
|
+
await override(n, v.el, v.el, w, v.paths);
|
|
760
|
+
} else await override(n, v.el, s, w, v.paths);
|
|
761
|
+
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
if (n.type === "FRAME" || n.type === "RECTANGLE") await restyle(n, s, w);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
async function override(inst: InstanceNode, main: El, shown: El, want: El, paths: Map<string, number[]>) {
|
|
769
|
+
// A frame main is the instance itself. Other mains wrap their element, which the walk below reaches at path [].
|
|
770
|
+
if (shown.kind === "frame") await restyle(inst, shown, want);
|
|
771
|
+
const optional = optionalIn(assignments.get(want)?.rule);
|
|
772
|
+
|
|
773
|
+
const visit = async (n: SceneNode): Promise<void> => {
|
|
774
|
+
const path = paths.get(mainIdOf(n.id));
|
|
775
|
+
const w = path && counterpart(main, want, path, optional);
|
|
776
|
+
|
|
777
|
+
// An optional layer this occurrence leaves out is hidden, through its BOOLEAN property when it has one.
|
|
778
|
+
if (path && !w) {
|
|
779
|
+
const key = n.componentPropertyReferences?.visible;
|
|
780
|
+
|
|
781
|
+
if (key) inst.setProperties({ [key]: false });
|
|
782
|
+
else n.visible = false;
|
|
783
|
+
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
if (path && w) {
|
|
788
|
+
const s = shown === main ? at(main, path) : counterpart(main, shown, path, optional);
|
|
789
|
+
|
|
790
|
+
if (!s) {
|
|
791
|
+
const key = n.componentPropertyReferences?.visible;
|
|
792
|
+
|
|
793
|
+
if (key) inst.setProperties({ [key]: true });
|
|
794
|
+
else n.visible = true;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
await diff(inst, n, s ?? at(main, path), w);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
if (n.type !== "INSTANCE" && "children" in n) for (const c of n.children) await visit(c);
|
|
801
|
+
};
|
|
802
|
+
|
|
803
|
+
for (const c of inst.children) await visit(c);
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
async function instanceNode(
|
|
807
|
+
el: El,
|
|
808
|
+
a: Assigned,
|
|
809
|
+
parent: Parent | undefined,
|
|
810
|
+
path: number[],
|
|
811
|
+
scope: Scope,
|
|
812
|
+
): Promise<Built> {
|
|
813
|
+
const v = await variantOf(a);
|
|
814
|
+
const gap = reuseGap(v.el, el, rules);
|
|
815
|
+
|
|
816
|
+
if (gap) {
|
|
817
|
+
fallbacks.push({ rule: `${a.rule.name} ${a.value}`.trim(), reason: gap });
|
|
818
|
+
|
|
819
|
+
return (await build(el, parent, path, { ...scope, root: el, component: false })).node;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
const inst = v.comp.createInstance();
|
|
823
|
+
reached.add(el);
|
|
824
|
+
await override(inst, v.el, v.el, el, v.paths);
|
|
825
|
+
|
|
826
|
+
return inst;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
const sectionEls: { el: El; rule: ComponentRule }[] = [];
|
|
830
|
+
walk(root, (el) => {
|
|
831
|
+
const a = assignments.get(el);
|
|
832
|
+
|
|
833
|
+
if (a && sectionRules.includes(a.rule)) sectionEls.push({ el, rule: a.rule });
|
|
834
|
+
});
|
|
835
|
+
const resolved = new Map<ComponentRule, BuiltRule>();
|
|
836
|
+
|
|
837
|
+
for (const rule of [...libraryRules, ...sectionRules]) resolved.set(rule, await ensureRule(rule));
|
|
838
|
+
|
|
839
|
+
const background = resolvePageBackground(root, spec.snapshot.viewport, kit);
|
|
840
|
+
|
|
841
|
+
const frameOf = async (name: string, els: typeof sectionEls, width: number) => {
|
|
842
|
+
const page = tag(figma.createFrame());
|
|
843
|
+
page.name = name;
|
|
844
|
+
page.layoutMode = "VERTICAL";
|
|
845
|
+
page.fills = background;
|
|
846
|
+
page.resize(width, spec.snapshot.viewport[1]);
|
|
847
|
+
|
|
848
|
+
for (const { el } of els) {
|
|
849
|
+
const node = await instanceNode(el, assignments.get(el)!, undefined, [], {
|
|
850
|
+
root: el,
|
|
851
|
+
component: false,
|
|
852
|
+
library: false,
|
|
853
|
+
paths: new Map(),
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
write(node, "box", JSON.stringify(el.box));
|
|
857
|
+
// Parity composites the section's clear parts over this color, the one the section sat on in the page.
|
|
858
|
+
const under = kit.backdropOf(el) ?? background.find((p): p is SolidPaint => p.type === "SOLID");
|
|
859
|
+
|
|
860
|
+
if (under) write(node, "bg", hexOf(under.color));
|
|
861
|
+
page.appendChild(node);
|
|
862
|
+
node.layoutSizingHorizontal = "FILL";
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
page.layoutSizingHorizontal = "FIXED";
|
|
866
|
+
page.layoutSizingVertical = "HUG";
|
|
867
|
+
|
|
868
|
+
if (spec.grid) {
|
|
869
|
+
page.layoutGrids = [
|
|
870
|
+
{
|
|
871
|
+
pattern: "COLUMNS",
|
|
872
|
+
alignment: "STRETCH",
|
|
873
|
+
count: spec.grid.count,
|
|
874
|
+
gutterSize: spec.grid.gutter,
|
|
875
|
+
offset: spec.grid.margin,
|
|
876
|
+
visible: true,
|
|
877
|
+
color: { r: 1, g: 0, b: 0, a: 0.06 },
|
|
878
|
+
},
|
|
879
|
+
];
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
return page;
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
const pages: FrameNode[] = [];
|
|
886
|
+
|
|
887
|
+
if (!spec.screens) pages.push(await frameOf("Landing Page", sectionEls, spec.snapshot.viewport[0]));
|
|
888
|
+
else for (const s of sectionEls) pages.push(await frameOf(s.rule.name.slice("Section/".length), [s], s.el.box[2]));
|
|
889
|
+
|
|
890
|
+
if (spec.slides) await slidesOf(sectionEls);
|
|
891
|
+
|
|
892
|
+
// Figma Slides holds slides in its own grid, outside the canvas a rerun replaces, so a rerun adds a fresh set.
|
|
893
|
+
async function slidesOf(els: typeof sectionEls) {
|
|
894
|
+
if (figma.editorType !== "slides" || !("createSlide" in figma)) {
|
|
895
|
+
warnings.add("The spec asks for slides, but this is not a Figma Slides file, so no slides were made.");
|
|
896
|
+
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
for (const { el } of els) {
|
|
901
|
+
const slide = tag(figma.createSlide());
|
|
902
|
+
slide.name = nameOf(el);
|
|
903
|
+
const v = await variantOf(assignments.get(el)!);
|
|
904
|
+
const inst = v.comp.createInstance();
|
|
905
|
+
slide.appendChild(inst);
|
|
906
|
+
inst.x = inst.y = 0;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
warnings.add(
|
|
910
|
+
`Made ${els.length} slides. A rerun adds a fresh set and leaves these, so delete them before you run again.`,
|
|
911
|
+
);
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
const { ours, source } = arrangeCanvas(spec, kit, pages, sectionEls, libraryRules, resolved, others, kept, warnings);
|
|
915
|
+
|
|
916
|
+
if (spec.links) await linkScreens(spec.name, spec.links, pages, sectionRules, resolved);
|
|
917
|
+
|
|
918
|
+
return {
|
|
919
|
+
summary: buildImportSummary(kit, ours, root, reached, fallbacks, unknownTextProperties, warnings),
|
|
920
|
+
source,
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
type Slot = Omit<Options, "hook"> & { calls: (() => Promise<Imported>)[] };
|
|
925
|
+
|
|
926
|
+
export function importDesign(spec: ImportSpec) {
|
|
927
|
+
// SAFETY: `figdriver run` sets figdriverImport, and `bun run e2e` sets figdriverHook, in these shapes. Either may be unset.
|
|
928
|
+
const holder = globalThis as { figdriverImport?: Slot; figdriverHook?: Hook };
|
|
929
|
+
const slot = holder.figdriverImport;
|
|
930
|
+
|
|
931
|
+
// Read when the run starts, so a spec that cannot build rejects it before the file changes.
|
|
932
|
+
const start = async (options: Options) => {
|
|
933
|
+
const root = normalize(spec.snapshot.root);
|
|
934
|
+
const rules = assignRules(spec, root);
|
|
935
|
+
|
|
936
|
+
return run(spec.name, { ...options, renames: spec.renames }, (others, kept) =>
|
|
937
|
+
importInto(spec, root, rules, others, kept),
|
|
938
|
+
);
|
|
939
|
+
};
|
|
940
|
+
|
|
941
|
+
if (slot) {
|
|
942
|
+
slot.calls.push(() => {
|
|
943
|
+
const hook = holder.figdriverHook;
|
|
944
|
+
delete holder.figdriverHook;
|
|
945
|
+
|
|
946
|
+
return start({ force: slot.force, page: slot.page, hook });
|
|
947
|
+
});
|
|
948
|
+
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
const result = start({ force: false });
|
|
953
|
+
|
|
954
|
+
if (typeof IS_PLUGIN !== "undefined" && IS_PLUGIN) {
|
|
955
|
+
result.then(
|
|
956
|
+
(r) => figma.closePlugin(r.message),
|
|
957
|
+
(e: Error) => figma.closePlugin(`Failed: ${e.message}`),
|
|
958
|
+
);
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
Object.assign(globalThis, { figdriverResult: result });
|
|
962
|
+
}
|