-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathmain.js
54 lines (42 loc) · 1.33 KB
/
main.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
const { alert } = require("./lib/dialogs");
/**
* Entry point for the plugin
*
* @param {!Selection} selection
*/
async function swapPosition(selection) {
if (selection.items.length === 0) {
await alert("Swap Position", "No nodes selected. Please select 2 nodes");
return ;
}
if (selection.items.length === 1) {
await alert("Swap Position", "Only one node selected. Please select 2 nodes");
return ;
}
// Rotate selected items's positions. Works for any number of elements.
let selectedItems = selection.items;
let firstNodeBounds = selectedItems[0].boundsInParent;
let length = selectedItems.length;
let localBounds;
// Move ith node to (i + 1)th node's position
for (let i = 0; i < length - 1; ++i) {
let node = selectedItems[i];
localBounds = {
x: node.localBounds.x,
y: node.localBounds.y
};
node.placeInParentCoordinates(localBounds, selectedItems[i + 1].boundsInParent);
}
// Move last node to first node's position
let lastNode = selectedItems[length - 1];
localBounds = {
x: lastNode.localBounds.x,
y: lastNode.localBounds.y
};
lastNode.placeInParentCoordinates(localBounds, firstNodeBounds);
}
module.exports = {
commands: {
swapPosition
}
};