-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
46 lines (46 loc) · 1.64 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
const command = figma.command;
function getContaining(item, targetType) {
if (item.parent) {
if (item.parent.type === targetType) {
return item.parent;
}
else {
return getContaining(item.parent, targetType);
}
}
else
return null;
}
function getTop(item, targetType) {
console.log("Get Top", item, targetType);
let outerNode = null;
if (item.type === targetType) {
outerNode = item;
}
let checkItem = item;
while (checkItem.parent) {
if (checkItem.parent.type === targetType) {
outerNode = checkItem.parent;
}
checkItem = checkItem.parent;
}
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();