-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ts
50 lines (44 loc) · 1.64 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
const command = figma.command;
function getContaining(item:SceneNode, targetType:string):SceneNode|null {
if(item.parent) {
if(item.parent.type === targetType) {
return item.parent as SceneNode;
}
else {
return getContaining(item.parent as SceneNode, targetType);
}
}
else return null;
}
function getTop(item:SceneNode, targetType:string):SceneNode|null {
console.log("Get Top",item, targetType);
let outerNode:SceneNode|null = null;
if(item.type === targetType) {
outerNode = item;
}
let checkItem = item;
while(checkItem.parent) {
if(checkItem.parent.type === targetType) {
outerNode = checkItem.parent as SceneNode;
}
checkItem = checkItem.parent as SceneNode;
}
return outerNode;
}
let commandArgs = {
"containingInstance": {callback:getContaining, type:"INSTANCE"},
"topInstance": {callback:getTop, type:"INSTANCE"},
"containingFrame": {callback:getContaining, type:"FRAME"},
"topFrame": {callback:getTop, type:"FRAME"},
"containingGroup": {callback:getContaining, type:"GROUP"},
"topGroup": {callback:getTop, type:"GROUP"}
}[command];
let selection = figma.currentPage.selection.map(selectedItem=>commandArgs.callback(selectedItem, commandArgs.type)).filter(val=>val!==null);
if(selection.length>0) figma.currentPage.selection = selection;
else {
console.log(`No ${commandArgs.type.toLowerCase()}s above selection`);
figma.notify(`Selection is not the child of any ${commandArgs.type.toLowerCase()}s.`);
}
// Make sure to close the plugin when you're done. Otherwise the plugin will
// keep running, which shows the cancel button at the bottom of the screen.
figma.closePlugin();