-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
324 lines (282 loc) · 8.36 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
figma.showUI(__html__, { width: 800, height: 500 });
let allowedSpacings = [0, 2, 4, 8, 12, 16, 24, 32, 40, 48, 64, 80, 96, 160];
function isNodeVisible(node: SceneNode): boolean {
if (!node.visible) {
return false;
}
if (
node.parent &&
node.parent.type !== "DOCUMENT" &&
"visible" in node.parent
) {
return isNodeVisible(node.parent as SceneNode);
}
return true;
}
function getSelectedInstanceData(): {
instanceName: string;
hasSuffix: boolean;
instanceId: string;
}[] {
let data: { instanceName: string; hasSuffix: boolean; instanceId: string }[] =
[];
function exploreNode(node: SceneNode) {
if (node.type === "INSTANCE") {
const instanceNode = node as InstanceNode;
if (isNodeVisible(instanceNode)) {
const instanceName = instanceNode.name || "Undefined";
const instanceId = instanceNode.id;
const mainComponent = instanceNode.mainComponent;
let mainComponentName = "";
let containsKeyword = true;
if (mainComponent) {
mainComponentName =
mainComponent.parent && mainComponent.parent.type === "COMPONENT_SET"
? mainComponent.parent.name
: mainComponent.name;
const descriptionToCheck = mainComponent.parent && mainComponent.parent.type === "COMPONENT_SET"
? mainComponent.parent.description
: mainComponent.description;
containsKeyword = descriptionToCheck.includes("[ds-radar]");
}
// check if component name has suffix
const hasSuffix = mainComponentName.includes("- ds") || containsKeyword;
data.push({ instanceName, hasSuffix, instanceId });
}
}
if ("children" in node) {
node.children.forEach(exploreNode);
}
}
figma.currentPage.selection.forEach(exploreNode);
return data;
}
interface UnboundNode {
name: string;
id: string;
color?: string;
}
function analyzeStyles(): {
totalNodes: number;
boundNodes: number;
unboundNodes: UnboundNode[];
} {
let totalNodes = 0;
let boundNodes = 0;
let unboundNodes: UnboundNode[] = [];
function nodeHasFillOrStroke(node: SceneNode): boolean {
const fills = "fills" in node ? (node as any).fills : null;
const hasVisibleFill =
Array.isArray(fills) &&
fills.some((fill) => fill.type === "SOLID" && fill.visible !== false);
const strokes = "strokes" in node ? (node as any).strokes : null;
const hasVisibleStroke =
Array.isArray(strokes) &&
strokes.some(
(stroke) => stroke.type === "SOLID" && stroke.visible !== false
);
return hasVisibleFill || hasVisibleStroke;
}
function nodeIsBoundToVariable(node: SceneNode): boolean {
if ("boundVariables" in node) {
const variables = (node as any).boundVariables;
for (const key in variables) {
if (variables[key] !== undefined) {
return true;
}
}
}
return false;
}
function exploreNode(node: SceneNode) {
if (!node.visible) {
return;
}
if ("children" in node) {
node.children.forEach(exploreNode);
}
if (nodeHasFillOrStroke(node)) {
totalNodes++;
if (nodeIsBoundToVariable(node)) {
boundNodes++;
} else {
let rgbaColor = "N/A";
const fills =
"fills" in node ? ((node as any).fills as ReadonlyArray<Paint>) : [];
if (
fills.length > 0 &&
fills[0].type === "SOLID" &&
fills[0].visible !== false
) {
const color = fills[0].color;
const alpha = "opacity" in node ? (node as any).opacity : 1;
rgbaColor = `rgba(${Math.round(color.r * 255)}, ${Math.round(
color.g * 255
)}, ${Math.round(color.b * 255)}, ${alpha})`;
}
unboundNodes.push({ name: node.name, id: node.id, color: rgbaColor });
}
}
}
figma.currentPage.selection.forEach(exploreNode);
return { totalNodes, boundNodes, unboundNodes };
}
interface NonConformSpacingNode {
name: string;
id: string;
spacing: number;
}
function isSpacingConform(node: SceneNode): boolean {
if (
node.type === "FRAME" ||
node.type === "COMPONENT" ||
node.type === "INSTANCE"
) {
const frameNode = node as FrameNode;
if (
"itemSpacing" in frameNode &&
allowedSpacings.indexOf(node.itemSpacing) === -1
) {
return false;
}
if (
"paddingTop" in frameNode &&
allowedSpacings.indexOf(node.paddingTop) === -1
)
return false;
if (
"paddingRight" in frameNode &&
allowedSpacings.indexOf(node.paddingRight) === -1
)
return false;
if (
"paddingBottom" in frameNode &&
allowedSpacings.indexOf(node.paddingBottom) === -1
)
return false;
if (
"paddingLeft" in frameNode &&
allowedSpacings.indexOf(node.paddingLeft) === -1
)
return false;
}
return true;
}
interface NonConformSpacingNode {
name: string;
id: string;
spacing: number;
spacingProp: string;
}
function analyzeSpacing(): NonConformSpacingNode[] {
let nonConformNodes: NonConformSpacingNode[] = [];
const selection = figma.currentPage.selection;
selection.forEach((node) => {
if (
(node.type === "FRAME" ||
node.type === "INSTANCE" ||
node.type === "COMPONENT") &&
node.visible
) {
exploreNode(node as FrameNode, nonConformNodes, allowedSpacings);
}
});
return nonConformNodes;
}
function checkAndAddSpacing(
node: FrameNode,
spacingValue: number | undefined,
spacingProp: string,
nonConformNodes: NonConformSpacingNode[]
) {
if (
spacingValue !== undefined &&
allowedSpacings.indexOf(spacingValue) === -1
) {
nonConformNodes.push({
name: node.name,
id: node.id,
spacing: spacingValue,
spacingProp: spacingProp,
});
}
}
function exploreNode(
node: FrameNode,
nonConformNodes: NonConformSpacingNode[],
allowedSpacings: number[]
) {
if (!node.visible) return;
checkAndAddSpacing(node, node.itemSpacing, "itemSpacing", nonConformNodes);
checkAndAddSpacing(node, node.paddingTop, "paddingTop", nonConformNodes);
checkAndAddSpacing(node, node.paddingRight, "paddingRight", nonConformNodes);
checkAndAddSpacing(
node,
node.paddingBottom,
"paddingBottom",
nonConformNodes
);
checkAndAddSpacing(node, node.paddingLeft, "paddingLeft", nonConformNodes);
if ("children" in node) {
node.children.forEach((child) => {
if (
child.type === "FRAME" ||
child.type === "INSTANCE" ||
child.type === "COMPONENT"
) {
exploreNode(child as FrameNode, nonConformNodes, allowedSpacings);
}
});
}
}
figma.ui.onmessage = (msg) => {
if (msg.type === "analyze-selection") {
const analysisResults = analyzeStyles();
figma.ui.postMessage({
type: "style-analysis-result",
data: analysisResults,
});
}
if (msg.type === "navigate-to-instance") {
const instanceId = msg.instanceId;
const instance = figma.getNodeById(instanceId) as SceneNode;
if (instance) {
figma.currentPage.selection = [instance];
figma.viewport.scrollAndZoomIntoView([instance]);
}
}
if (msg.type === "get-data") {
const data = getSelectedInstanceData();
figma.ui.postMessage({ type: "instance-data", data });
}
if (msg.type === "navigate-to-node") {
const nodeId = msg.nodeId;
const node = figma.getNodeById(nodeId) as SceneNode;
if (node) {
figma.currentPage.selection = [node];
figma.viewport.scrollAndZoomIntoView([node]);
}
}
if (msg.type === "analyze-dimensions") {
const dimensionResults = analyzeSpacing();
figma.ui.postMessage({
type: "dimension-analysis-result",
data: dimensionResults,
});
}
if (msg.type === 'update-spacings') {
updateAllowedSpacings(msg.newSpacings);
}
};
figma.clientStorage.getAsync('allowedSpacings').then((spacings) => {
const allowedSpacings = spacings || [0, 2, 4, 8, 12, 16, 24, 32, 40, 48, 64, 80, 96, 160];
figma.ui.postMessage({ type: 'set-allowed-spacings', allowedSpacings });
});
function updateAllowedSpacings(newSpacings: number[]) {
figma.clientStorage.setAsync('allowedSpacings', newSpacings).then(() => {
figma.ui.postMessage({ type: 'update-success' });
allowedSpacings = newSpacings;
figma.notify("Dimensions updated: "+ newSpacings);
console.log(allowedSpacings);
});
}