-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ts
422 lines (389 loc) · 14.8 KB
/
code.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Code in this file has access to the *figma document* via the figma global object.
// Access browser APIs in the <script> tag inside "ui.html" which has a
// full browser environment (See https://www.figma.com/plugin-docs/how-plugins-run).
const options: ShowUIOptions = {
title: "figvar",
width: 380,
height: 670,
};
figma.showUI(__html__, options);
type VarType = "STRING" | "FLOAT" | "COLOR" | "BOOLEAN";
interface CollectionData {
name: string;
variables: VarData[];
modes: ModeData[];
}
interface ModeData {
name: string;
id: string;
}
interface VarData {
id: string;
name: string;
type: VarType;
value: any;
}
figma.ui.onmessage = (message: any): void => messageFunctionLookup[message.type](message);
const messageFunctionLookup: { [key: string]: (msg: any) => void } = {
start: startPlugin,
close: closePlugin,
"create-rectangles": createRectangles,
"import-json": importVarsJson,
"show-json": showVarsJson,
"save-json": saveVarsAsJson,
"clear-vars": clearVars,
"save-css": saveVarsAsCss,
"show-css": showVarsCss,
"replace-vars": modifyNames,
realias: realias,
};
async function startPlugin(message: any): Promise<void> {
await listCssInUi();
}
async function listVariablesInUi(): Promise<void> {
const varCollections = await figma.variables.getLocalVariableCollectionsAsync();
let json: any = {};
for (let i = 0; i < varCollections.length; i++) {
let collection = varCollections[i];
json[collection.name] = await getCollectionAsJson(collection);
}
let data = JSON.stringify(json, null, 2);
figma.ui.postMessage({ type: "populate-textarea", data: data });
}
async function listCssInUi(): Promise<void> {
const data = await getVarsCssText();
figma.ui.postMessage({ type: "populate-textarea", data: data });
}
async function getVarsCssText(): Promise<string> {
const varCollections = await figma.variables.getLocalVariableCollectionsAsync();
let data: string = ":root {\n";
for (let i = 0; i < varCollections.length; i++) {
let collection = varCollections[i];
let collectionData: CollectionData = await getCollectionAsJson(collection);
data += `\n`;
data += ` /* ${collectionData.name} */\n`;
data += `\n`;
const collName = collectionData.name.replace(/ /g, "").toLowerCase();
let modeCount = collectionData.modes.length;
collectionData.variables.forEach((variable) => {
for (let modeName in variable.value) {
let valueForMode = variable.value[modeName];
let resolvedValue: string | null;
if (valueForMode.type == "VARIABLE_ALIAS") {
resolvedValue = `var(${valueForMode.name})`;
} else {
resolvedValue = valueToString(valueForMode, variable.type);
}
if (resolvedValue != null) {
let modeToken = modeCount > 1 ? `-${modeName.toLowerCase()}` : "";
const safeVarName = variable.name.replace(/[\s/]/g, "-");
data += ` --${collName}-${safeVarName}${modeToken}: ${resolvedValue};\n`;
}
}
});
}
data += `}\n`;
return data;
}
function valueToString(value: any, type: any): string | null {
const toHex = (rgb: number): string => {
let hex = Math.round(rgb * 255).toString(16);
return hex.length == 1 ? "0" + hex : hex;
};
switch (type) {
case "STRING":
return `"${value}"`;
case "FLOAT":
return `${value}`;
case "COLOR":
if (value.a === 1) return `#${toHex(value.r)}${toHex(value.g)}${toHex(value.b)}`;
return `rgba(${value.r}, ${value.g}, ${value.b}, ${value.a})`;
case "BOOLEAN":
return `${value}`;
default:
return null;
}
}
function closePlugin(message: any): void {
figma.closePlugin();
}
function createRectangles(message: any): void {
const { count } = message;
const nodes: SceneNode[] = [];
for (let i = 0; i < count; i++) {
const rect = figma.createRectangle();
rect.x = i * 150;
rect.fills = [{ type: "SOLID", color: { r: 1, g: 0.5, b: 0 } }];
figma.currentPage.appendChild(rect);
nodes.push(rect);
}
figma.currentPage.selection = nodes;
figma.viewport.scrollAndZoomIntoView(nodes);
}
async function getCollectionAsJson(collection: VariableCollection): Promise<CollectionData> {
let varDatas: VarData[] = [];
for (let i = 0; i < collection.variableIds.length; i++) {
let variableId = collection.variableIds[i];
let variable = await figma.variables.getVariableByIdAsync(variableId);
if (variable) {
let valuesByModeName: { [key: string]: any } = {};
let modes: any[] = [];
collection.modes.forEach((mode) => modes.push(mode));
for (let i = 0; i < modes.length; i++) {
let mode = modes[i];
let modeName = mode.name;
let value: any = variable.valuesByMode[mode.modeId];
if (value.type == "VARIABLE_ALIAS") {
let aliasedVar = await figma.variables.getVariableByIdAsync(value.id);
if (aliasedVar) {
let id = aliasedVar.variableCollectionId;
let collection = await figma.variables.getVariableCollectionByIdAsync(id);
if (collection) {
const collName = collection.name.replace(/ /g, "").toLowerCase();
value.name = `--${collName}-${aliasedVar.name}`;
}
}
}
valuesByModeName[modeName] = value;
}
const varData: VarData = {
id: variable.id,
name: variable.name,
type: variable.resolvedType as VarType,
value: valuesByModeName,
};
varDatas.push(varData);
}
}
let modeDatas: ModeData[] = [];
for (let mode of collection.modes) {
modeDatas.push({ name: mode.name, id: mode.modeId });
}
let collectionData: CollectionData = {
variables: varDatas,
name: collection.name,
modes: modeDatas,
};
return collectionData;
}
interface AliasRequirement {
variable: Variable;
modeName: string;
targetVarName: string;
}
async function importVarsJson(message: any): Promise<void> {
const json = JSON.parse(message.text);
let aliasRequirements: { [id: string]: AliasRequirement } = {};
let collectionsByName: { [name: string]: VariableCollection } = {};
let variablesByFullName: { [name: string]: Variable } = {};
for (let collectionName in json) {
const collectionData: CollectionData = json[collectionName] as CollectionData;
const collection = await figma.variables.createVariableCollection(collectionName);
collectionsByName[collectionName] = collection;
const modeDatas = collectionData.modes;
let createdModeIds: string[] = [];
for (let i = 0; i < modeDatas.length; i++) {
let modeData: ModeData = modeDatas[i];
let newModeId = await collection.addMode(modeData.name);
createdModeIds.push(newModeId);
}
// Iterate over collection.modes and delete any that are not in modeDatas
// place collection.modes into an array
let modes = Array.from(collection.modes);
for (let i = modes.length - 1; i >= 0; i--) {
if (createdModeIds.indexOf(modes[i].modeId) === -1) {
//console.log("Removing standard mode: ", modes[i].name);
await collection.removeMode(modes[i].modeId);
}
}
}
for (let collectionName in json) {
const collectionData: CollectionData = json[collectionName] as CollectionData;
const collection = collectionsByName[collectionName];
for (let i = 0; i < collectionData.variables.length; i++) {
let varData = collectionData.variables[i];
let variable = await figma.variables.createVariable(varData.name, collection, varData.type);
let fullName = "--" + collection.name.replace(/ /g, "").toLowerCase() + "-" + varData.name;
variablesByFullName[fullName] = variable;
for (let onFileModeName in varData.value) {
let liveMode = collection.modes.find((liveMode) => {
return liveMode.name === onFileModeName;
});
if (liveMode) {
let value = varData.value[liveMode.name];
if (value.type === "VARIABLE_ALIAS") {
aliasRequirements[value.id] = {
variable: variable,
modeName: liveMode.name,
targetVarName: value.name,
};
} else {
await variable.setValueForMode(liveMode.modeId, value);
}
} else {
console.log(`Variables mode ${onFileModeName} not found in collection '${collection.name}'`);
console.error(`Variables mode ${onFileModeName} not found in collection '${collection.name}'`);
}
}
collection.variableIds.push(variable.id);
}
}
for (let aliasId in aliasRequirements) {
let requirementInfo = aliasRequirements[aliasId];
let variableName = requirementInfo.variable.name;
let collectionId = requirementInfo.variable.variableCollectionId;
let collection = await figma.variables.getVariableCollectionByIdAsync(collectionId);
if (collection) {
let mode = collection.modes.find((mode) => {
return mode.name === requirementInfo.modeName;
});
console.log(
`Variable '${variableName}' needs alias '${requirementInfo.targetVarName}' for mode '${mode?.name}' (${aliasId})`
);
}
}
for (let collectionName in json) {
const collectionData: CollectionData = json[collectionName] as CollectionData;
const collection = collectionsByName[collectionName];
for (let i = 0; i < collectionData.variables.length; i++) {
let varData = collectionData.variables[i];
for (let onFileModeName in varData.value) {
let liveMode = collection.modes.find((liveMode) => {
return liveMode.name === onFileModeName;
});
if (liveMode) {
let value = varData.value[liveMode.name];
if (value.type === "VARIABLE_ALIAS") {
let targetVariable = variablesByFullName[value.name];
let variableAlias = await figma.variables.createVariableAlias(targetVariable);
aliasRequirements[value.id].variable.setValueForMode(liveMode.modeId, variableAlias);
}
} else {
console.log(`Variables mode ${onFileModeName} not found in collection '${collection.name}'`);
console.error(`Variables mode ${onFileModeName} not found in collection '${collection.name}'`);
}
}
}
}
//listVariablesInUi();
listCssInUi();
}
async function showVarsJson(message: any): Promise<void> {
listVariablesInUi();
}
async function clearVars(message: any): Promise<void> {
const varCollections = await figma.variables.getLocalVariableCollectionsAsync();
for (let i = 0; i < varCollections.length; i++) {
let collection = varCollections[i];
await collection.remove();
}
listCssInUi();
}
async function saveVarsAsJson(): Promise<void> {
const varCollections = await figma.variables.getLocalVariableCollectionsAsync();
let json: any = {};
for (let i = 0; i < varCollections.length; i++) {
let collection = varCollections[i];
json[collection.name] = await getCollectionAsJson(collection);
}
const data: any = { figmaDocName: figma.root.name, text: JSON.stringify(json) };
figma.ui.postMessage({ type: "save-json", data: data });
}
async function showVarsCss(): Promise<void> {
listCssInUi();
}
async function findVariableByName(name: string): Promise<Variable | null> {
const collections = await figma.variables.getLocalVariableCollectionsAsync();
for (let i = 0; i < collections.length; i++) {
let variableIds = collections[i].variableIds;
for (let j = 0; j < variableIds.length; j++) {
let variable = await figma.variables.getVariableByIdAsync(variableIds[j]);
if (variable && variable.name == name) return variable;
}
}
return null;
}
async function realias(message: any): Promise<void> {
const varname = message.varname;
const alias = message.alias;
let aliasVariable = await findVariableByName(alias);
if (!aliasVariable) {
figma.ui.postMessage({ type: "realias-info", data: `Could not find variable '${alias}'` });
return;
}
const collections = await figma.variables.getLocalVariableCollectionsAsync();
for (let i = 0; i < collections.length; i++) {
let collection = collections[i];
let variableIds = collection.variableIds;
for (let j = 0; j < variableIds.length; j++) {
let variableId = variableIds[j];
let variable = await figma.variables.getVariableByIdAsync(variableId);
if (variable && variable.name == varname) {
let alias = await figma.variables.createVariableAlias(aliasVariable);
for (let k = 0; k < collection.modes.length; k++) {
const modeId = collection.modes[k].modeId;
await variable.setValueForMode(modeId, alias);
}
}
}
}
listCssInUi();
}
async function modifyNames(message: any): Promise<void> {
const findString = message.findString;
const replaceString = message.replaceString;
let numReplacements: number = 0;
if (message.inVarsAndGroups) {
const varCollections = await figma.variables.getLocalVariableCollectionsAsync();
for (let i = 0; i < varCollections.length; i++) {
let collection = varCollections[i];
for (let j = 0; j < collection.variableIds.length; j++) {
let variableId = collection.variableIds[j];
let variable = await figma.variables.getVariableByIdAsync(variableId);
if (variable) {
let newName = variable.name.replace(findString, replaceString);
if (newName != variable.name) {
numReplacements++;
variable.name = newName;
}
}
}
}
}
if (message.inCollections) {
const varCollections = await figma.variables.getLocalVariableCollectionsAsync();
for (let i = 0; i < varCollections.length; i++) {
let collection = varCollections[i];
let newName = collection.name.replace(findString, replaceString);
if (newName != collection.name) {
numReplacements++;
collection.name = newName;
}
}
}
if (message.inModes) {
const varCollections = await figma.variables.getLocalVariableCollectionsAsync();
for (let i = 0; i < varCollections.length; i++) {
let collection = varCollections[i];
for (let j = 0; j < collection.modes.length; j++) {
let mode = collection.modes[j];
let newName = mode.name.replace(findString, replaceString);
if (newName != mode.name) {
numReplacements++;
collection.renameMode(mode.modeId, newName);
}
}
}
}
if (message.inGroups) {
}
let instancesToken = `${numReplacements} ${numReplacements == 1 ? " instance" : " instances"}`;
const messageText = `Replaced ${instancesToken} of ${findString} with ${replaceString}`;
figma.ui.postMessage({ type: "findreplace-info", data: messageText });
listCssInUi();
}
async function saveVarsAsCss(): Promise<void> {
const cssText = await getVarsCssText();
const data: any = { figmaDocName: figma.root.name, text: cssText };
figma.ui.postMessage({ type: "save-css", data: data });
}