-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
217 lines (217 loc) · 8.07 KB
/
code.js
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
"use strict";
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) {
if (!node.visible) {
return false;
}
if (node.parent &&
node.parent.type !== "DOCUMENT" &&
"visible" in node.parent) {
return isNodeVisible(node.parent);
}
return true;
}
function getSelectedInstanceData() {
let data = [];
function exploreNode(node) {
if (node.type === "INSTANCE") {
const instanceNode = node;
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;
}
function analyzeStyles() {
let totalNodes = 0;
let boundNodes = 0;
let unboundNodes = [];
function nodeHasFillOrStroke(node) {
const fills = "fills" in node ? node.fills : null;
const hasVisibleFill = Array.isArray(fills) &&
fills.some((fill) => fill.type === "SOLID" && fill.visible !== false);
const strokes = "strokes" in node ? node.strokes : null;
const hasVisibleStroke = Array.isArray(strokes) &&
strokes.some((stroke) => stroke.type === "SOLID" && stroke.visible !== false);
return hasVisibleFill || hasVisibleStroke;
}
function nodeIsBoundToVariable(node) {
if ("boundVariables" in node) {
const variables = node.boundVariables;
for (const key in variables) {
if (variables[key] !== undefined) {
return true;
}
}
}
return false;
}
function exploreNode(node) {
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.fills : [];
if (fills.length > 0 &&
fills[0].type === "SOLID" &&
fills[0].visible !== false) {
const color = fills[0].color;
const alpha = "opacity" in node ? node.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 };
}
function isSpacingConform(node) {
if (node.type === "FRAME" ||
node.type === "COMPONENT" ||
node.type === "INSTANCE") {
const frameNode = node;
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;
}
function analyzeSpacing() {
let nonConformNodes = [];
const selection = figma.currentPage.selection;
selection.forEach((node) => {
if ((node.type === "FRAME" ||
node.type === "INSTANCE" ||
node.type === "COMPONENT") &&
node.visible) {
exploreNode(node, nonConformNodes, allowedSpacings);
}
});
return nonConformNodes;
}
function checkAndAddSpacing(node, spacingValue, spacingProp, nonConformNodes) {
if (spacingValue !== undefined &&
allowedSpacings.indexOf(spacingValue) === -1) {
nonConformNodes.push({
name: node.name,
id: node.id,
spacing: spacingValue,
spacingProp: spacingProp,
});
}
}
function exploreNode(node, nonConformNodes, allowedSpacings) {
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, 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);
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);
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) {
figma.clientStorage.setAsync('allowedSpacings', newSpacings).then(() => {
figma.ui.postMessage({ type: 'update-success' });
allowedSpacings = newSpacings;
figma.notify("Dimensions updated: " + newSpacings);
console.log(allowedSpacings);
});
}