-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.js
213 lines (213 loc) · 9.49 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
// This plugin will open a modal to prompt the user to enter a number, and
// it will then create that many rectangles on the screen.
// This file holds the main code for the plugins. It has access to the *document*.
// You can access browser APIs in the <script> tag inside "ui.html" which has a
// full browser enviroment (see documentation).
// This shows the HTML page in "ui.html".
figma.showUI(__html__);
// Calls to "parent.postMessage" from within the HTML page will trigger this
// callback. The callback will be passed the "pluginMessage" property of the
// posted message.
// TODO:
// make a function to find all page nodes and store that locally or in an array?
// then use count-instances to access the array
// Can either be incorporated into the count button to first calculate then count
// if the cache is the same then don't have to recalculate
// TODO:
// delete option if component doesn't have instances
figma.ui.onmessage = msg => {
const selection = figma.currentPage.selection;
const pageChildren = figma.currentPage.children;
const nodes = [];
let obj = {
componentName: '',
componentId: '',
instanceCount: 0
};
// count instances
// TODO: check for nested frames and groups
if (msg.type === 'count-instances') {
if (selection.length === 1 && selection[0].type === "COMPONENT") {
let componentName = selection[0].name;
let componentId = selection[0].id;
// loop top level objects in the page
for (let i = 0; i < pageChildren.length; i++) {
const children = pageChildren[i];
// INSTANCE
// count top-level instances - depth 1
if (children.type === 'INSTANCE') {
if (children.masterComponent.id === selection[0].id) {
nodes.push(children);
obj.instanceCount = nodes.length;
}
else {
const frameChild = children.children;
for (let i = 0; i < frameChild.length; i++) {
const child = frameChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
// group within a group - depth 3
if (child.type === 'GROUP') {
const nestGroupChild = child.children;
for (let i = 0; i < nestGroupChild.length; i++) {
const child = nestGroupChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
}
}
}
}
}
// GROUPS
// count nested groups - depth 2
if (children.type === 'GROUP') {
const groupChild = children.children;
for (let i = 0; i < groupChild.length; i++) {
const child = groupChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
// group within a group - depth 3
if (child.type === 'GROUP') {
const nestGroupChild = child.children;
for (let i = 0; i < nestGroupChild.length; i++) {
const child = nestGroupChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
}
}
}
}
// FRAMES
// count nested frame - depth 2
if (children.type === 'FRAME') {
const frameChild = children.children;
for (let i = 0; i < frameChild.length; i++) {
const child = frameChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
// group within a group - depth 3
if (child.type === 'GROUP') {
const nestGroupChild = child.children;
for (let i = 0; i < nestGroupChild.length; i++) {
const child = nestGroupChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
}
}
}
}
// COMPONENTS
// count nested component - depth 2
if (children.type === 'COMPONENT') {
const frameChild = children.children;
for (let i = 0; i < frameChild.length; i++) {
const child = frameChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
// group within a group - depth 3
if (child.type === 'GROUP') {
const nestGroupChild = child.children;
for (let i = 0; i < nestGroupChild.length; i++) {
const child = nestGroupChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
}
}
}
}
}
figma.currentPage.selection = nodes;
obj.componentName = componentName;
obj.componentId = componentId;
figma.ui.postMessage(obj);
}
else {
figma.notify("Please select 1 Component object");
}
}
// zoom to selection
if (msg.type === 'zoom-instances') {
if (figma.currentPage.selection.length > 0) {
figma.viewport.scrollAndZoomIntoView(figma.currentPage.selection);
}
else {
figma.notify("Please select more than 1 object to zoom");
}
}
};
let count = 0;
function traverse(node) {
// if the node has children
if ("children" in node) {
count++;
for (const child of node.children) {
traverse(child);
}
}
}
// traverse page
function walker(children, selection, nodeType, nodes, obj) {
if (nodeType === 'INSTANCE') {
if (children.masterComponent.id === selection[0].id) {
nodes.push(children);
obj.instanceCount = nodes.length;
}
else {
const frameChild = children.children;
for (let i = 0; i < frameChild.length; i++) {
const child = frameChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
// group within a group - depth 3
if (child.type === 'GROUP') {
const nestGroupChild = child.children;
for (let i = 0; i < nestGroupChild.length; i++) {
const child = nestGroupChild[i];
if (child.type === 'INSTANCE') {
if (child.masterComponent.id === selection[0].id) {
nodes.push(child);
obj.instanceCount = nodes.length;
}
}
}
}
}
}
}
}