This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
forked from destefanis/design-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.ts
407 lines (332 loc) · 10.5 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
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
import {
checkRadius,
checkEffects,
checkFills,
checkStrokes,
checkType,
customCheckTextFills
// customCheckTextFills,
} from "./lintingFunctions";
figma.showUI(__html__, { width: 360, height: 580 });
let borderRadiusArray = [0, 2, 4, 8, 16, 24, 32];
let originalNodeTree = [];
let lintVectors = false;
figma.ui.onmessage = msg => {
// Fetch a specific node by ID.
if (msg.type === "fetch-layer-data") {
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);
let layerData = JSON.stringify(layer, [
"id",
"name",
"description",
"fills",
"key",
"type",
"remote",
"paints",
"fontName",
"fontSize",
"font"
]);
figma.ui.postMessage({
type: "fetched layer",
message: layerData
});
}
// Could this be made less expensive?
if (msg.type === "update-errors") {
figma.ui.postMessage({
type: "updated errors",
errors: lint(originalNodeTree)
});
}
// Updates client storage with a new ignored error
// when the user selects "ignore" from the context menu
if (msg.type === "update-storage") {
let arrayToBeStored = JSON.stringify(msg.storageArray);
figma.clientStorage.setAsync("storedErrorsToIgnore", arrayToBeStored);
}
// Clears all ignored errors
// invoked from the settings menu
if (msg.type === "update-storage-from-settings") {
let arrayToBeStored = JSON.stringify(msg.storageArray);
figma.clientStorage.setAsync("storedErrorsToIgnore", arrayToBeStored);
figma.ui.postMessage({
type: "reset storage",
storage: arrayToBeStored
});
figma.notify("Cleared ignored errors", { timeout: 1000 });
}
// Changes the linting rules, invoked from the settings menu
if (msg.type === "update-lint-rules-from-settings") {
lintVectors = msg.boolean;
}
// For when the user updates the border radius values to lint from the settings menu.
if (msg.type === "update-border-radius") {
let newString = msg.radiusValues.replace(/\s+/g, "");
let newRadiusArray = newString.split(",");
newRadiusArray = newRadiusArray
.filter(x => x.trim().length && !isNaN(x))
.map(Number);
// Most users won't add 0 to the array of border radius so let's add it in for them.
if (newRadiusArray.indexOf(0) === -1) {
newRadiusArray.unshift(0);
}
// Update the array we pass into checkRadius for linting.
borderRadiusArray = newRadiusArray;
// Save this value in client storage.
let radiusToBeStored = JSON.stringify(borderRadiusArray);
figma.clientStorage.setAsync("storedRadiusValues", radiusToBeStored);
figma.ui.postMessage({
type: "fetched border radius",
storage: JSON.stringify(borderRadiusArray)
});
figma.notify("Saved new border radius values", { timeout: 1000 });
}
if (msg.type === "reset-border-radius") {
borderRadiusArray = [0, 2, 4, 8, 16, 24, 32];
figma.clientStorage.setAsync("storedRadiusValues", []);
figma.ui.postMessage({
type: "fetched border radius",
storage: JSON.stringify(borderRadiusArray)
});
figma.notify("Reset border radius value", { timeout: 1000 });
}
if (msg.type === "select-multiple-layers") {
const layerArray = msg.nodeArray;
let nodesToBeSelected = [];
layerArray.forEach(item => {
let layer = figma.getNodeById(item);
// Using selection and viewport requires an array.
nodesToBeSelected.push(layer);
});
// Moves the layer into focus and selects so the user can update it.
figma.currentPage.selection = nodesToBeSelected;
figma.viewport.scrollAndZoomIntoView(nodesToBeSelected);
figma.notify("Multiple layers selected", { timeout: 1000 });
}
// Traverses the node tree
function traverse(node) {
if ("children" in node) {
if (node.type !== "INSTANCE") {
for (const child of node.children) {
traverse(child);
}
}
}
return node;
}
function traverseNodes(selection) {
let traversedNodes = traverse(selection);
return traversedNodes;
}
// Serialize nodes to pass back to the UI.
function serializeNodes(nodes) {
let serializedNodes = JSON.stringify(nodes, [
"name",
"type",
"children",
"id"
]);
return serializedNodes;
}
function lint(nodes, lockedParentNode?) {
let errorArray = [];
let childArray = [];
nodes.forEach(node => {
let isLayerLocked;
// Create a new object.
let newObject = {};
// Give it the existing node id.
newObject["id"] = node.id;
// Don't lint locked layers or the children/grandchildren of locked layers.
if (lockedParentNode === undefined && node.locked === true) {
isLayerLocked = true;
} else if (lockedParentNode === undefined && node.locked === false) {
isLayerLocked = false;
} else if (lockedParentNode === false && node.locked === true) {
isLayerLocked = true;
} else {
isLayerLocked = lockedParentNode;
}
if (isLayerLocked === true) {
newObject["errors"] = [];
} else {
newObject["errors"] = determineType(node);
}
// Recursively run this function to flatten out children and grandchildren nodes
if (node["children"]) {
node["children"].forEach(childNode => {
childArray.push(childNode.id);
});
newObject["children"] = childArray;
// If the layer is locked, pass the optional parameter to the recursive Lint
// function to indicate this layer is locked.
if (isLayerLocked === true) {
errorArray.push(...lint(node["children"], true));
} else {
errorArray.push(...lint(node["children"], false));
}
}
errorArray.push(newObject);
});
return errorArray;
}
// Initialize the app
if (msg.type === "run-app") {
if (figma.currentPage.selection.length === 0) {
figma.notify("Select a frame(s) to get started", { timeout: 2000 });
return;
} else {
let nodes = traverseNodes(figma.currentPage.selection);
// Maintain the original tree structure so we can enable
// refreshing the tree and live updating errors.
originalNodeTree = nodes;
// Pass the array back to the UI to be displayed.
figma.ui.postMessage({
type: "complete",
message: serializeNodes(nodes),
errors: lint(nodes)
});
figma.notify(`Design lint is running and will auto refresh for changes`, {
timeout: 2000
});
figma.clientStorage.getAsync("storedErrorsToIgnore").then(result => {
figma.ui.postMessage({
type: "fetched storage",
storage: result
});
});
figma.clientStorage.getAsync("storedRadiusValues").then(result => {
if (result.length) {
borderRadiusArray = JSON.parse(result);
figma.ui.postMessage({
type: "fetched border radius",
storage: result
});
}
});
}
}
function determineType(node) {
switch (node.type) {
case "SLICE":
case "GROUP": {
// Groups styles apply to their children so we can skip this node type.
let errors = [];
return errors;
}
case "BOOLEAN_OPERATION":
case "VECTOR": {
return lintVectorRules(node);
}
case "POLYGON":
case "STAR":
case "ELLIPSE": {
return lintShapeRules(node);
}
case "FRAME": {
return lintFrameRules(node);
}
case "INSTANCE":
case "RECTANGLE": {
return lintRectangleRules(node);
}
case "COMPONENT": {
return lintComponentRules(node);
}
case "COMPONENT_SET": {
// Component Set is the frame that wraps a set of variants
// the variants within the set are still linted as components (lintComponentRules)
// this type is generally only present where the variant is defined so it
// doesn't need as many linting requirements.
return lintVariantWrapperRules(node);
}
case "TEXT": {
return lintTextRules(node);
}
case "LINE": {
return lintLineRules(node);
}
default: {
// Do nothing
}
}
}
function lintComponentRules(node) {
let errors = [];
// Example of how we can make a custom rule specifically for components
// if (node.remote === false) {
// errors.push(
// createErrorObject(node, "component", "Component isn't from library")
// );
// }
checkFills(node, errors);
checkRadius(node, errors, borderRadiusArray);
checkEffects(node, errors);
checkStrokes(node, errors);
return errors;
}
function lintVariantWrapperRules(node) {
let errors = [];
checkFills(node, errors);
return errors;
}
function lintLineRules(node) {
let errors = [];
checkStrokes(node, errors);
checkEffects(node, errors);
return errors;
}
function lintFrameRules(node) {
let errors = [];
checkFills(node, errors);
checkStrokes(node, errors);
checkRadius(node, errors, borderRadiusArray);
checkEffects(node, errors);
return errors;
}
function lintTextRules(node) {
let errors = [];
checkType(node, errors);
checkFills(node, errors);
// We could also comment out checkFills and use a custom function instead
// Take a look at line 122 in lintingFunction.ts for an example.
// customCheckTextFills(node, errors);
checkEffects(node, errors);
checkStrokes(node, errors);
return errors;
}
function lintRectangleRules(node) {
let errors = [];
checkFills(node, errors);
checkRadius(node, errors, borderRadiusArray);
checkStrokes(node, errors);
checkEffects(node, errors);
return errors;
}
function lintVectorRules(node) {
let errors = [];
// This can be enabled by the user in settings.
if (lintVectors === true) {
checkFills(node, errors);
checkStrokes(node, errors);
checkEffects(node, errors);
}
return errors;
}
function lintShapeRules(node) {
let errors = [];
checkFills(node, errors);
checkStrokes(node, errors);
checkEffects(node, errors);
return errors;
}
};