-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcontroller.ts
More file actions
executable file
·358 lines (314 loc) · 10.3 KB
/
controller.ts
File metadata and controls
executable file
·358 lines (314 loc) · 10.3 KB
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
// Plugin window dimensions
figma.showUI(__html__, { width: 320, height: 358 });
// Imported themes
import { darkTheme } from "./dark-to-light-theme";
import { lightTheme } from "./light-to-dark-theme";
// Utility function for serializing nodes to pass back to the UI.
function serializeNodes(nodes) {
let serializedNodes = JSON.stringify(nodes, [
"name",
"type",
"children",
"id"
]);
return serializedNodes;
}
// Utility function for flattening the
// selection of nodes in Figma into an array.
const flatten = obj => {
const array = Array.isArray(obj) ? obj : [obj];
return array.reduce((acc, value) => {
acc.push(value);
if (value.children) {
acc = acc.concat(flatten(value.children));
delete value.children;
}
return acc;
}, []);
};
figma.ui.onmessage = msg => {
let skippedLayers = [];
if (msg.type === "run-app") {
// If nothing's selected, we tell the UI to keep the empty state.
if (figma.currentPage.selection.length === 0) {
figma.ui.postMessage({
type: "selection-updated",
message: 0
});
} else {
let selectedNodes = flatten(figma.currentPage.selection);
// Update the UI with the number of selected nodes.
// This will display our theming controls.
figma.ui.postMessage({
type: "selection-updated",
message: serializeNodes(selectedNodes)
});
}
}
// When a theme is selected
if (msg.type === "theme-update") {
const nodesToTheme = figma.currentPage.selection;
if (msg.message === "dark-to-light-theme") {
// Update the layers with this theme, by passing in the
// selected nodes and the theme object.
nodesToTheme.map(selected => updateTheme(selected, darkTheme));
}
if (msg.message === "light-to-dark-theme") {
// Update the layers with this theme, by passing in the
// selected nodes and the theme object.
nodesToTheme.map(selected => updateTheme(selected, lightTheme));
}
// Need to wait for some promises to resolve before
// sending the skipped layers back to the UI.
setTimeout(function() {
figma.ui.postMessage({
type: "layers-skipped",
message: serializeNodes(skippedLayers)
});
}, 500);
figma.notify(`Theming complete`, { timeout: 750 });
}
// When a layer is selected from the skipped layers.
if (msg.type === "select-layer") {
let layer = figma.getNodeById(msg.id);
let layerArray = [];
// Using selection and viewport requires an array.
layerArray.push(layer);
// Moves the layer into focus and selects so the user can update it.
figma.notify(`Layer ${layer.name} selected`, { timeout: 750 });
figma.currentPage.selection = layerArray;
figma.viewport.scrollAndZoomIntoView(layerArray);
}
// Swap styles with the corresponding/mapped styles
async function replaceStyles(
node,
style,
mappings,
applyStyle: (node, styleId) => void
) {
// Find the style the ID corresponds to in the team library
let importedStyle = await figma.importStyleByKeyAsync(style.key);
// Once the promise is resolved, then see if the
// key matches anything in the mappings object.
if (mappings[importedStyle.key] !== undefined) {
let mappingStyle = mappings[importedStyle.key];
// Use the mapping value to fetch the official style.
let newStyle = await figma.importStyleByKeyAsync(mappingStyle.mapsToKey);
// Update the node with the new color.
applyStyle(node, newStyle.id);
} else {
skippedLayers.push(node);
}
}
// Fix layers with no style attached to them, just hex colors
async function fixStyles(
node,
nodeType,
style,
mappings,
applyStyle: (node, styleId) => void
) {
let styleName = nodeType.toLowerCase() + " " + style;
console.log(styleName);
// See if the key matches anything in the mappings object.
if (mappings[styleName] !== undefined) {
let mappingStyle = mappings[styleName];
// Use the mapping value to fetch the official style.
let newStyle = await figma.importStyleByKeyAsync(mappingStyle.mapsToKey);
// Update the node with the new color.
applyStyle(node, newStyle.id);
} else {
skippedLayers.push(node);
}
}
async function replaceComponent(
node,
key,
mappings,
applyComponent: (node, masterComponent) => void
) {
let componentToSwitchWith = mappings[key];
let importedComponent = await figma.importComponentByKeyAsync(
componentToSwitchWith.mapsToKey
);
// Switch the existing component to a new component.
applyComponent(node, importedComponent);
}
async function swapComponent(node, key, mappings) {
await replaceComponent(
node,
key,
mappings,
(node, masterComponent) => (node.masterComponent = masterComponent)
);
}
async function replaceFills(node, style, mappings) {
await replaceStyles(
node,
style,
mappings,
(node, styleId) => (node.fillStyleId = styleId)
);
}
async function replaceNoStyleFill(node, nodeType, style, mappings) {
await fixStyles(
node,
nodeType,
style,
mappings,
(node, styleId) => (node.fillStyleId = styleId)
);
}
async function replaceStrokes(node, style, mappings) {
await replaceStyles(
node,
style,
mappings,
(node, styleId) => (node.strokeStyleId = styleId)
);
}
async function replaceEffects(node, style, mappings) {
await replaceStyles(
node,
style,
mappings,
(node, styleId) => (node.effectStyleId = styleId)
);
}
// Updates the node with the new theme depending on
// the type of the node.
function updateTheme(node, theme) {
switch (node.type) {
case "COMPONENT":
case "COMPONENT_SET":
case "RECTANGLE":
case "GROUP":
case "ELLIPSE":
case "POLYGON":
case "STAR":
case "LINE":
case "BOOLEAN_OPERATION":
case "FRAME":
case "LINE":
case "VECTOR": {
if (node.children) {
node.children.forEach(child => {
updateTheme(child, theme);
});
}
if (node.fills) {
if (node.fillStyleId && typeof node.fillStyleId !== "symbol") {
let style = figma.getStyleById(node.fillStyleId);
// Pass in the layer we want to change, the style ID the node is using.
// and the set of mappings we want to check against.
replaceFills(node, style, theme);
} else if (node.fillStyleId === "") {
// No style on the layer? Let's fix it for them.
// First we need the fill type determined above ex:is it #ffffff?), then
// we pass that hex into a new function.
let style = determineFill(node.fills);
let nodeType = node.type;
replaceNoStyleFill(node, nodeType, style, theme);
} else {
skippedLayers.push(node);
}
}
if (node.strokeStyleId) {
replaceStrokes(node, figma.getStyleById(node.strokeStyleId), theme);
}
if (node.effectStyleId) {
replaceEffects(node, figma.getStyleById(node.effectStyleId), theme);
}
break;
}
case "INSTANCE": {
let componentKey = node.masterComponent.key;
// If this instance is in mapping, then call it and skip it's children
// otherwise check for the normal differences.
if (theme[componentKey] !== undefined) {
swapComponent(node, componentKey, theme);
} else {
if (node.fills) {
if (node.fillStyleId && typeof node.fillStyleId !== "symbol") {
let style = figma.getStyleById(node.fillStyleId);
// Pass in the layer we want to change, the style ID the node is using.
// and the set of mappings we want to check against.
replaceFills(node, style, theme);
} else if (node.fillStyleId === "") {
// No style on the layer? Let's fix it for them.
// First we need the fill type determined above ex:is it #ffffff?), then
// we pass that hex into a new function.
let style = determineFill(node.fills);
let nodeType = node.type;
replaceNoStyleFill(node, nodeType, style, theme);
} else {
skippedLayers.push(node);
}
}
if (node.strokeStyleId) {
replaceStrokes(node, figma.getStyleById(node.strokeStyleId), theme);
}
if (node.effectStyleId) {
replaceEffects(node, figma.getStyleById(node.effectStyleId), theme);
}
if (node.children) {
node.children.forEach(child => {
updateTheme(child, theme);
});
}
}
break;
}
case "TEXT": {
if (node.fillStyleId && typeof node.fillStyleId !== "symbol") {
replaceFills(node, figma.getStyleById(node.fillStyleId), theme);
} else if (node.fillStyleId === "") {
let style = determineFill(node.fills);
let nodeType = node.type;
replaceNoStyleFill(node, nodeType, style, theme);
} else {
skippedLayers.push(node);
}
}
default: {
// do nothing
}
}
}
// Determine a nodes fills
function determineFill(fills) {
let fillValues = [];
let rgbObj;
fills.forEach(fill => {
if (fill.type === "SOLID" && fill.visible === true) {
rgbObj = convertColor(fill.color);
fillValues.push(RGBToHex(rgbObj["r"], rgbObj["g"], rgbObj["b"]));
}
});
return fillValues[0];
}
// Utility functions for color conversion.
function convertColor(color) {
const colorObj = color;
const figmaColor = {};
Object.entries(colorObj).forEach(cf => {
const [key, value] = cf;
if (["r", "g", "b"].includes(key)) {
figmaColor[key] = (255 * (value as number)).toFixed(0);
}
if (key === "a") {
figmaColor[key] = value;
}
});
return figmaColor;
}
function RGBToHex(r, g, b) {
r = Number(r).toString(16);
g = Number(g).toString(16);
b = Number(b).toString(16);
if (r.length == 1) r = "0" + r;
if (g.length == 1) g = "0" + g;
if (b.length == 1) b = "0" + b;
return "#" + r + g + b;
}
};