-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
219 lines (208 loc) · 7.63 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
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
var {LinkedGraphic, SymbolInstance, BooleanGroup, RepeatGrid, Group, Text, Artboard} = require("scenegraph");
const { alert, error } = require("./lib/dialogs.js");
var commands = require("commands");
function swappCommand(selection, documentRoot) {
if(selection.items.length == 2) {
// get items references
var item1 = selection.items[0];
var item2 = selection.items[1];
// get their Z indices
var index1 = getIndex(item1);
var index2 = getIndex(item2);
// get their parents references
var parent1 = item1.parent;
var parent2 = item2.parent;
// if elements are on different artboards
if(parent1.guid != parent2.guid) {
// check beforehand if children are containers
var swapSupported = checkForSwapSupport(item1) && checkForSwapSupport(item2);
if(!swapSupported) {
showSwapAlert();
return;
} else {
if(item1 instanceof Group) {
selection.items = [item1];
var tmpItem1 = fosterGroup(selection, parent2);
if(tmpItem1 == null) {
showSwapAlert();
return;
} else {
item1 = tmpItem1;
}
selection.items = [item1, item2];
} else {
item1.removeFromParent();
parent2.addChild(item1);
}
if(item2 instanceof Group) {
selection.items = [item2];
var tmpItem2 = fosterGroup(selection, parent1);
if(tmpItem2 == null) {
showSwapAlert();
} else {
item2 = tmpItem2;
}
selection.items = [item1, item2];
} else {
item2.removeFromParent();
parent1.addChild(item2);
}
}
}
var artboardsNotInvolved = true;
if(item1 instanceof Artboard || item2 instanceof Artboard) {
artboardsNotInvolved = false;
}
if(artboardsNotInvolved) {
// store rotation and remove it for a while
var item1Rotation = item1.rotation;
var item2Rotation = item2.rotation;
item1.rotateAround(-item1Rotation, item1.localCenterPoint);
item2.rotateAround(-item2Rotation, item2.localCenterPoint);
}
let pos1 = {x:0, y:0};
let pos2 = {x:0, y:0};
let bounds1 = item1.boundsInParent;
let bounds2 = item2.boundsInParent;
var type = "Generic";
if(item1 instanceof Text && item2 instanceof Text) {
type = "Texts";
} else if(item1 instanceof Text || item2 instanceof Text) {
type = "Mixed";
}
var goToPos1 = {x:0, y:0};
var goToPos2 = {x:0, y:0};
var localBounds1 = item1.localBounds;
var localBounds2 = item2.localBounds;
var hOffset1 = (bounds2.width-bounds1.width)/2;
var hOffset2 = (bounds1.width-bounds2.width)/2;
var vOffset1 = (bounds2.height-bounds1.height)/2;
var vOffset2 = (bounds1.height-bounds2.height)/2;
switch(type) {
case "Generic":
break;
case "Texts":
// both texts are PointTexts
/*if(item1.areaBox == null && item2.areaBox == null) {
var constrainTo = "Center";
if(localBounds1.x == 0 && localBounds2.x == 0) {
constrainTo = "Left";
hOffset1 = bounds2.x-bounds1.x;
hOffset2 = bounds1.x-bounds2.x;
} else if(Math.abs(localBounds1.x) == localBounds1.width && Math.abs(localBounds2.x) == localBounds2.width) {
constrainTo = "Right";
hOffset1 = bounds1.x-bounds2.x;
hOffset2 = bounds2.x-bounds1.x;
}
trace(constrainTo);
}*/
break;
case "Mixed":
break;
}
goToPos1 = {x:bounds2.x + hOffset1 - localBounds1.x , y:bounds2.y + vOffset1 - localBounds1.y};
goToPos2 = {x:bounds1.x + hOffset2 - localBounds2.x, y:bounds1.y + vOffset2 - localBounds2.y};
item1.placeInParentCoordinates(pos2, goToPos1);
item2.placeInParentCoordinates(pos1, goToPos2);
if(artboardsNotInvolved) {
// restore rotation
item1.rotateAround(item1Rotation, item1.localCenterPoint);
item2.rotateAround(item2Rotation, item2.localCenterPoint);
}
// placing object at the right Z indexes
setIndex(item1, index2, selection);
setIndex(item2, index1, selection);
return;
} else {
showSelectionAlert();
}
}
function getIndex(item) {
var parent = item.parent;
var len = parent.children.length;
for(var i=0; i<len; i++) {
var childNode = parent.children.at(i);
if(childNode.guid == item.guid) {
return i;
}
}
return len-1;
}
function setIndex(item, index, selection) {
var maxLength = item.parent.children.length-1
if(index > maxLength) {
index = maxLength;
}
var currentIndex = getIndex(item);
var selected = selection.items;
selection.items = [item];
while(currentIndex != index) {
if(currentIndex < index) {
commands.bringForward();
} else if(currentIndex > index) {
commands.sendBackward();
}
currentIndex = getIndex(item);
}
selection.items = selected;
}
function checkForSwapSupport(item) {
// we can's swap nested Groups, BooleanGroups, SymbolInstances, LinkedGraphics and RepeatGrids that belong to different Artboards
if(item instanceof Group) {
var len = item.children.length;
for(var i=0; i<len; i++) {
var childNode = item.children.at(i);
if(childNode.isContainer == true) {
return false;
}
}
} else if( item instanceof BooleanGroup ||
item instanceof RepeatGrid ||
item instanceof SymbolInstance ||
item instanceof LinkedGraphic)
{
return false;
}
return true;
}
function fosterGroup(selection, p) {
commands.ungroup();
selection.items.forEach(function (childNode, i) {
childNode.removeFromParent();
p.addChild(childNode);
});
commands.group();
return selection.items[0];
}
function fosterGroupAttempt(selection, g, p) {
selection.items = null;
g.children.forEach(function (childNode, i) {
if(childNode.isContainer) {
p.addChild(fosterGroup(childNode));
} else {
childNode.removeFromParent();
p.addChild(childNode);
selection.items.push(childNode);
}
});
commands.group();
return selection.items[0];
}
function trace(msg) {
console.log(msg)
}
async function showSelectionAlert() {
await alert("Select exactly 2 items", //[1]
"In order to function properly, Swapper requires a selection of exactly 2 items.",
"Please select the items you want to swap and keep on swapping!"); //[2]
}
async function showSwapAlert() {
await alert("Selected items are too complex", //[1]
"Swapper currently can't swap one or both of the selected items.",
"Please try again with different items."); //[2]
}
module.exports = {
commands: {
swappCommand: swappCommand
}
};