-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.js
61 lines (61 loc) · 2.31 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
if (figma.command === 'copy') {
let copyNodes = figma.currentPage.selection;
figma.root.setPluginData('copiedNodes', JSON.stringify(copyNodes));
figma.currentPage.setRelaunchData({});
figma.closePlugin(`💾 Saved node in cache`);
}
if (figma.command === 'paste' || figma.command === 'pasteAndAdjustSize' || figma.command === 'placeOver') {
let selectedNodes = figma.currentPage.selection;
let copyNodes = null;
let clonedNodes = [];
try {
copyNodes = JSON.parse(figma.root.getPluginData('copiedNodes'));
}
catch (e) {
figma.closePlugin();
}
if (copyNodes && copyNodes[0] && copyNodes[0].id) {
let findNode = figma.getNodeById(copyNodes[0].id);
if (findNode) {
if (selectedNodes.length > 0) {
selectedNodes.forEach(element => {
if (!findNode.removed) {
let clonedNode = findNode.clone();
clonedNodes.push(clonedNode);
let index = element.parent.children.indexOf(element);
if (figma.command === 'placeOver') {
index++;
}
element.parent.insertChild(index, clonedNode);
clonedNode.x = element.x;
clonedNode.y = element.y;
if (figma.command === 'pasteAndAdjustSize') {
clonedNode.resize(element.width, element.height);
}
if (figma.command !== 'placeOver') {
element.remove();
}
figma.currentPage.selection = clonedNodes;
}
else {
figma.closePlugin(`Original node was removed! 🙁`);
}
});
}
else {
figma.closePlugin(`Select one or more nodes!`);
}
}
else {
figma.closePlugin(`Original node was removed! 🙁`);
}
}
else {
figma.closePlugin('Nothing copied or cache cleared?');
}
}
if (figma.command === 'clear') {
figma.root.setPluginData('copiedNodes', '{}');
figma.closePlugin(`🗑 Cache cleared`);
}
figma.closePlugin();