forked from ddwht/sketch-dynamic-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamic Button.sketchplugin
96 lines (83 loc) · 2.83 KB
/
Dynamic Button.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// just a comment (cmd j)
function alert(msg, title) {
title = title || "alert";
var app = [NSApplication sharedApplication];
[app displayDialog:msg withTitle:title];
}
function isLayerInGroup(name, group) {
var BGLayer;
var groupLayers = [group layers];
for (var i=0; i<[groupLayers count]; i++) {
var layer = [groupLayers objectAtIndex:i];
log([layer name]);
if ([layer name] == name) {
BGLayer = layer;
}
}
return BGLayer;
}
if ([selection count] === 0) {
alert('Nothing selected', 'Error');
}
else {
var currentLayer = selection[0];
var layerHeight = [[currentLayer frame] height];
var layerWidth = [[currentLayer frame] width];
var layerX = [[currentLayer frame] x];
var layerY = [[currentLayer frame] y];
var splitLayer = [];
var splitLayer = [currentLayer name].split(':');
var offsetTop,offsetRight,offsetRight,offsetLeft;
log(splitLayer.length);
switch(splitLayer.length) {
case 1:
currentLayer.name = '0:0:0:0';
offsetTop = offsetRight = offsetBottom = offsetLeft = 0;
break;
case 2:
offsetTop = offsetBottom = parseInt(splitLayer[0]) || 0;
offsetRight = offsetLeft = parseInt(splitLayer[1]) || 0;
break;
case 3:
offsetTop = parseInt(splitLayer[0]) || 0;
offsetRight = offsetLeft = parseInt(splitLayer[1]) || 0;
offsetBottom = parseInt(splitLayer[2]) || 0;
break;
case 4:
offsetTop = parseInt(splitLayer[0]) || 0;
offsetRight = parseInt(splitLayer[1]) || 0;
offsetBottom = parseInt(splitLayer[2]) || 0;
offsetLeft = parseInt(splitLayer[3]) || 0;
break;
default:
alert('Wrong format', 'Error');
}
var parentGroup = [currentLayer parentGroup];
var BG = isLayerInGroup('BG', parentGroup);
if (BG) {
[[BG frame] setHeight:(layerHeight + offsetTop + offsetBottom)];
[[BG frame] setWidth:(layerWidth + offsetLeft + offsetRight)];
[[BG frame] setX: 0];
[[BG frame] setY: 0];
[[currentLayer frame] setX: offsetLeft];
[[currentLayer frame] setY: offsetTop];
}
else {
var newGroup = [parentGroup addLayerOfType:'group'];
newGroup.name = "flex button";
[[newGroup frame] setHeight:(layerHeight + offsetTop + offsetBottom)];
[[newGroup frame] setWidth:(layerWidth + offsetLeft + offsetRight)];
var newRect = [newGroup addLayerOfType:'rectangle']
newRect.name = "BG";
[[newRect frame] setHeight:(layerHeight + offsetTop + offsetBottom)];
[[newRect frame] setWidth:(layerWidth + offsetLeft + offsetRight)];
[[newRect frame] setX: layerX - offsetLeft];
[[newRect frame] setY: layerY - offsetTop];
var style = [[newRect style] fills];
style.addNewStylePart();
newLayer = [currentLayer duplicate];
[parentGroup removeLayer:newLayer];
[newGroup addLayer: newLayer];
[parentGroup removeLayer: currentLayer];
}
}