forked from arbel/instance-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
323 lines (259 loc) · 11.2 KB
/
code.ts
File metadata and controls
323 lines (259 loc) · 11.2 KB
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
// 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).
figma.skipInvisibleInstanceChildren = true;
// 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.
/*let target = []
target.push(figma.getNodeById("2400:3285"));
figma.viewport.scrollAndZoomIntoView(target);
*/
figma.ui.onmessage = msg => {
// One way of distinguishing between different types of messages sent from
if (msg.type =='zoomPage') {
console.log('in zoom page2');
let targetPage= figma.getNodeById(msg.page);
targetPage as PageNode;
figma.currentPage = targetPage as PageNode;
console.log(msg.instances);
let pageInstances = JSON.parse(msg.instances)
console.log(pageInstances, 'page instnaces')
let target = []
for(var i=0; i<pageInstances.length; i++) {
var pageInstance = pageInstances[i];
console.log(pageInstance, 'page instance');
console.log(figma.getNodeById(pageInstance), 'getnodes');
target.push(figma.getNodeById(pageInstance));
// figma.viewport.scrollAndZoomIntoView(target);
}
console.log(target,'tearget');
figma.viewport.scrollAndZoomIntoView(target);
figma.currentPage.selection = target;
}
if (msg.type == 'zoom') {
console.log('zoom');
let targetPage= figma.getNodeById(msg.page);
let target = []
target.push(figma.getNodeById(msg.id));
figma.currentPage = targetPage as PageNode;
figma.viewport.scrollAndZoomIntoView(target);
var page = figma.currentPage as PageNode;
page.selection = target;
}
// your HTML page is to use an object with a "type" property like this.
if (msg.type === 'find') {
const scope = msg.scope;
const selection = figma.currentPage.selection;
console.log('selection', selection);
let frameFrame;
let masterFrame;
let searchInFrame = false;
//if(scope === 'frame') {
if(selection.length == 2) {
if((selection[0].type == 'COMPONENT') || (selection[0].type == 'INSTANCE')) {
if(selection[1].type == 'FRAME') {
searchInFrame = true;
frameFrame = 1;
masterFrame = 0;
}
}
if((selection[1].type == 'COMPONENT') || (selection[1].type == 'INSTANCE')) {
if(selection[0].type == 'FRAME') {
searchInFrame = true;
frameFrame = 0;
masterFrame = 1;
}
}
if(searchInFrame == false ){
//selected two items and scope frame but did not selecte a frame and an insatnce
figma.ui.postMessage(5); // please select only one frame
figma.notify("Please select a single instance/main component and a single frame");
console.log('message 5')
return 'empty';
}
}//selection length ==2
else {
//you need to select a frame and master
if(scope == 'frame') {
figma.notify('Please select a single instance/main component and a single frame')
figma.ui.postMessage(4); // please select only a single component or instance
return 'empty';
}
}//selection != 2
//}//scope == frame
if(searchInFrame) {
console.log('searching in frame');
//SEARCHING IN FRAME
//get all insatnces or frames
const doc = figma.root;
let master;
if(selection[masterFrame].type == 'INSTANCE') {
//get master component
master = (selection[masterFrame] as InstanceNode).mainComponent;
// console.log('type is isntace');
// console.log(master, 'master');
}
else {
// console.log('selection', selection[masterFrame]);
master = selection[masterFrame];
//console.log('type is not instance');
}
let instances = (selection[frameFrame] as FrameNode).findAll(id => id.type === 'INSTANCE');
console.log (instances,'instaces from search in frame');
//cycle through instances and check if id is the same as the master id we are looking for
let result = [];
let matchingInstances = [];
console.log('master id to search for', master.id);
for(const instance of instances as any) {
// console.log(instance, 'instance');
var instanceMasterId = instance.masterComponent.id;
var masterId = master.id;
if(instanceMasterId== masterId) {
console.log('found match', instance);
//build the instance object
let instanceInfo = {} as any;
instanceInfo.id = instance.id;
instanceInfo.name = instance.name;
matchingInstances.push(instanceInfo);
//console.log('found a match', instanceInfo);
}
}
//build the page object
let frameResult = {} as any;
frameResult.searchId = master.id;
frameResult.searchName = master.name;
frameResult.pageId = figma.currentPage;
frameResult.id = figma.currentPage.id;
frameResult.name = selection[frameFrame].name;
frameResult.instances = matchingInstances;
console.log(frameResult,' fraem result');
//send to ui
result.push(frameResult);
figma.ui.postMessage(result);
} // end search in frame
if((scope != "frame") && (!searchInFrame)) {
if(selection.length == 1) {
//check if we have a componnet and a frame)
if((selection[0].type == 'COMPONENT') || (selection[0].type == 'INSTANCE')) {
//instance or component
//get all insatnces or frames
const doc = figma.root;
//console.log('instance or component');
let master;
if(selection[0].type == 'INSTANCE') {
//get master component
master = (selection[0] as InstanceNode).mainComponent;
// console.log('type is isntace');
// console.log(master, 'master');
}
else {
// console.log('selection', selection[0]);
master = selection[0];
//console.log('type is not instance');
}
// console.log(master, 'master');
//go through all pages and create a list of instacnes in those pages
//console.log(doc, 'doc');
let result = [];
const currentPage = figma.currentPage;
for (const page of doc.children) {
console.log(page.name,' page');
//pages.push(page.name); // this is the list of all pages in the doc
//page.name
//search current page in loop for all instances
var checkThis = false;
//console.log(scope,'scope');
if(scope == 'page') {
// console.log('searching in page');
if(page.id == currentPage.id) {
checkThis = true;
}
}
else {
//console.log('searcing entire doc');
//search the entire document
checkThis = true;
}
console.log('checkThis', checkThis);
if(checkThis) {
var masterId = master.id;
var pageActual = page ;
const instances = pageActual.findAllWithCriteria({types: ['INSTANCE']});
console.log(instances.length, "number of instances");
var d = new Date();
console.log(d.toString() );// returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
//cycle through instances and check if id is the same as the master id we are looking for
let matchingInstances = [];
var instancesLength = instances.length;
//var instance = instances[1];
for(let i=0; i<instancesLength; i++) {
var d = new Date();
console.log(d.toString() );// returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
var instance = instances[i];
var instanceMainComponentId = instance.mainComponent.id;
//if(1==1) {
if(instanceMainComponentId== masterId) {
//build the instance object
// console.log(instances[i], "matching instnace");
let instanceInfo = {} as any;
instanceInfo.id = instance.id;
instanceInfo.name =instance.name;
matchingInstances.push(instanceInfo);
//console.log('found a match', instanceInfo);
}
}
var d = new Date();
console.log(d.toString() );// returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
//let obj = instances.filter(instance => instance.mainComponent.id == master.id);
//console.log(obj,'obj');
/*for(const instance of instances as any) {
// console.log(instance, 'instance');
if(instance.mainComponent.id == master.id) {
//build the instance object
let instanceInfo = {} as any;
instanceInfo.id = instance.id;
instanceInfo.name = instance.name;
matchingInstances.push(instanceInfo);
//console.log('found a match', instanceInfo);
}
}
*/
//build the page object
let pageResult = {} as any;
pageResult.id = page.id;
pageResult.name = page.name;
pageResult.instances = matchingInstances;
//send to ui
result.push(pageResult);
}//end if
}
//console.log('the results:', result);
figma.ui.postMessage(result);
}
else {
//not a instance or component
//console.log('not instance or component');
figma.notify('Please select a signle instance/master component when searching in page or entire document')
figma.ui.postMessage(2); // please select only a single component or instance
return 'empty';
}
}
else {
//not a single selection
//console.log('selected more or less than one');
figma.notify('Please select a signle instance/master component when searching in page or entire document')
figma.ui.postMessage(1); // please select only one frame
return 'empty';
} //end else
}
//figma.viewport.scrollAndZoomIntoView(nodes);
} //msg.type find
if(msg.type === 'close') {
figma.closePlugin();
}
}