-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFit.sketchplugin
74 lines (64 loc) · 1.86 KB
/
Fit.sketchplugin
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
var prefix = "com.utom.fit.";
if(!selection.count()){
return false;
}
for (var i = 0; i < selection.count(); i++) {
var layer = selection[i],
parent = layer.parentGroup(),
pFrame = parent.absoluteRect(),
index = context.command.valueForKey_onLayer(prefix + "index", layer),
newFrame = {};
if(parent.class() === MSPage){
return false;
}
if(index === null){
var frame = layer.absoluteRect();
context.command.setValue_forKey_onLayer(JSON.stringify({
x: frame.x(),
y: frame.y(),
width: frame.width(),
height: frame.height()
}), prefix + "original", layer);
index = 0;
}
var frame = JSON.parse(context.command.valueForKey_onLayer(prefix + "original", layer));
if(index == 0){
newFrame.x = pFrame.x();
newFrame.y = frame.y;
newFrame.width = pFrame.width();
newFrame.height = frame.height;
}
else if(index == 1){
newFrame.x = frame.x;
newFrame.y = pFrame.y();
newFrame.width = frame.width;
newFrame.height = pFrame.height();
}
else if(index == 2){
newFrame.x = pFrame.x();
newFrame.y = pFrame.y();
newFrame.width = pFrame.width();
newFrame.height = pFrame.height();
}
else if(index == 3){
newFrame.x = frame.x;
newFrame.y = frame.y;
newFrame.width = frame.width;
newFrame.height = frame.height;
}
setFrame(layer, newFrame);
if(index == 3){
index = 0;
}
else{
index++
}
context.command.setValue_forKey_onLayer(index, prefix + "index", layer);
}
function setFrame(layer, newFrame){
var frame = layer.absoluteRect();
frame.setX(newFrame.x);
frame.setY(newFrame.y);
frame.setWidth(newFrame.width);
frame.setHeight(newFrame.height);
}