forked from destefanis/auto-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.ts
executable file
·242 lines (214 loc) · 6.68 KB
/
controller.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
// Plugin window dimensions
figma.showUI(__html__, { width: 320, height: 358 });
// Imported themes
import { darkTheme } from "./dark-theme";
import { lightTheme } from "./light-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);
}
}
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 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 {
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.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 {
skippedLayers.push(node);
}
}
default: {
// do nothing
}
}
}
};