返回 CodeWhale
sync.template.js
根目录 / vendor / codewhale-design / figma / sync.template.js
1 const DRY = typeof APPLY_FOUNDATIONS === "undefined" || APPLY_FOUNDATIONS !== true;
2 const page = figma.root.children.find(node => node.id === '229:3');
3 if (!page) throw Error('Inspect the existing Foundations page before synchronization');
4 await figma.setCurrentPageAsync(page);
5 const rgba = (v) => {
6 const n = parseInt(v.hex, 16);
7 return { r: ((n >> 16) & 255) / 255, g: ((n >> 8) & 255) / 255, b: (n & 255) / 255, a: v.alpha };
8 };
9 const same = (a, b) =>
10 typeof a === "number" || typeof b === "number"
11 ? a === b
12 : !!a && !!b && ["r", "g", "b", "a"].every((k) => Math.abs((a[k] ?? 1) - (b[k] ?? 1)) < 0.002);
13 const report = { version: PAYLOAD.version, dry: DRY, created: [], updated: [], unchanged: 0, notInTokens: [], styles: { created: [], updated: [], unchanged: 0 } };
14 const collections = await figma.variables.getLocalVariableCollectionsAsync();
15 const variables = await figma.variables.getLocalVariablesAsync();
16 for (const wanted of PAYLOAD.collections) {
17 let collection = collections.find((c) => c.name === wanted.name);
18 if (!collection) {
19 if (DRY) {
20 report.created.push(...wanted.variables.map((v) => `${wanted.name}: ${v.name}`));
21 continue;
22 }
23 collection = figma.variables.createVariableCollection(wanted.name);
24 collection.renameMode(collection.modes[0].modeId, wanted.modes[0]);
25 for (const mode of wanted.modes.slice(1)) collection.addMode(mode);
26 }
27 const modeId = Object.fromEntries(collection.modes.map((m) => [m.name, m.modeId]));
28 const missingMode = wanted.modes.find((m) => !modeId[m]);
29 if (missingMode) throw new Error(`${wanted.name} has no mode named ${missingMode}`);
30 const existing = variables.filter((v) => v.variableCollectionId === collection.id);
31 const names = new Set(wanted.variables.map((v) => v.name));
32 report.notInTokens.push(...existing.filter((v) => !names.has(v.name)).map((v) => `${wanted.name}: ${v.name}`));
33 for (const token of wanted.variables) {
34 let variable = existing.find((v) => v.name === token.name);
35 if (!variable) {
36 report.created.push(`${wanted.name}: ${token.name}`);
37 if (DRY) continue;
38 variable = figma.variables.createVariable(token.name, collection, token.type);
39 variable.scopes = token.scopes;
40 variable.setVariableCodeSyntax("WEB", token.web);
41 } else if (!DRY && (variable.codeSyntax.WEB || "") !== token.web) {
42 variable.setVariableCodeSyntax("WEB", token.web);
43 }
44 let changed = false;
45 for (const mode of wanted.modes) {
46 const next = token.type === "COLOR" ? rgba(token.values[mode]) : token.values[mode];
47 if (same(variable.valuesByMode[modeId[mode]], next)) continue;
48 changed = true;
49 if (!DRY) variable.setValueForMode(modeId[mode], next);
50 }
51 if (changed && !report.created.includes(`${wanted.name}: ${token.name}`)) report.updated.push(`${wanted.name}: ${token.name}`);
52 if (!changed) report.unchanged += 1;
53 }
54 }
55 const textStyles = await figma.getLocalTextStylesAsync();
56 for (const wanted of PAYLOAD.textStyles) {
57 const font = { family: wanted.family, style: wanted.style };
58 let style = textStyles.find((s) => s.name === wanted.name);
59 const matches =
60 style &&
61 style.fontName.family === font.family &&
62 style.fontName.style === font.style &&
63 style.fontSize === wanted.size &&
64 style.lineHeight.unit === "PIXELS" &&
65 style.lineHeight.value === wanted.line;
66 if (matches) {
67 report.styles.unchanged += 1;
68 continue;
69 }
70 report.styles[style ? "updated" : "created"].push(wanted.name);
71 if (DRY) continue;
72 await figma.loadFontAsync(font);
73 if (!style) {
74 style = figma.createTextStyle();
75 style.name = wanted.name;
76 }
77 style.fontName = font;
78 style.fontSize = wanted.size;
79 style.lineHeight = { unit: "PIXELS", value: wanted.line };
80 }
81 report.collectionIds = (await figma.variables.getLocalVariableCollectionsAsync()).map(value => value.id);
82 report.variableIds = (await figma.variables.getLocalVariablesAsync()).map(value => value.id);
83 report.textStyleIds = (await figma.getLocalTextStylesAsync()).map(value => value.id);
84 report.pageId = page.id;
85 return report;
86
86 lines JAVASCRIPT