Skip to content

Commit c7686af

Browse files
committed
Now handling rotated layers again.
This fixes #10 and #14. And also introduces a new issue with non-pixel-aligned layers, see README for details.
1 parent 686a89d commit c7686af

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

Distributor.sketchplugin/Contents/Sketch/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
]
4141
},
4242
"identifier" : "com.betterthantomorrow.sketch.distributor",
43-
"version" : "1.0.3.1b",
43+
"version" : "1.0.4",
4444
"description" : "Distribute selected objects vertically or horizontally with exact spacing.",
4545
"authorEmail" : "[email protected]",
4646
"name" : "Distributor"

Distributor.sketchplugin/Contents/Sketch/script.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,16 @@ var Distributor = {
5353
return label;
5454
},
5555

56-
trimmedLayer: function(layer) {
57-
//return MSSliceTrimming.trimmedRectForLayerAncestry(MSImmutableLayerAncestry.ancestryWithMSLayer(layer));
58-
return [layer rect]
56+
offsetLayerY: function(layer, yOffset) {
57+
layer.frame().setY(layer.frame().y() + yOffset);
58+
},
59+
60+
offsetLayerX: function(layer, xOffset) {
61+
layer.frame().setX(layer.frame().x() + xOffset);
62+
},
63+
64+
trimmedRectForLayer: function(layer) {
65+
return MSSliceTrimming.trimmedRectForLayerAncestry(MSImmutableLayerAncestry.ancestryWithMSLayer(layer));
5966
},
6067

6168
createChoices: function(msg) {
@@ -99,28 +106,40 @@ var Distributor = {
99106
},
100107

101108
distribute: function(dimension, spacingString) {
102-
var sortedByLeft = this.sortedArray(this.selection, "frame.left"),
103-
sortedByTop = this.sortedArray(this.selection, "frame.top"),
104-
firstLeft = sortedByLeft[0],
105-
left = [[firstLeft frame] left],
106-
firstTop = sortedByTop[0],
107-
top = [[firstTop frame] top],
108-
formatter = [[NSNumberFormatter alloc] init],
109-
spacing = [formatter numberFromString:spacingString];
109+
var formatter = [[NSNumberFormatter alloc] init],
110+
spacing = [formatter numberFromString:spacingString];
110111

111112
if (spacing != null) {
112-
if (String(dimension) === "Vertically") {
113-
var loopV = [sortedByTop objectEnumerator];
114-
while (layer = [loopV nextObject]) {
115-
[[layer frame] setTop:(top + ([[layer frame] top] - CGRectGetMinY(Distributor.trimmedLayer(layer))))];
116-
top = CGRectGetMinY(Distributor.trimmedLayer(layer)) + CGRectGetHeight(Distributor.trimmedLayer(layer)) + spacing;
113+
if (String(dimension) == "Horizontally") {
114+
var sortedByLeft = this.sortedArray(this.selection, "frame.left"),
115+
loopH = [sortedByLeft objectEnumerator]
116+
firstH = [loopH nextObject],
117+
trimmedLayerRect = Distributor.trimmedRectForLayer(firstH),
118+
trimmedLeft = CGRectGetMinX(trimmedLayerRect),
119+
lastTrimmedRight = trimmedLeft + CGRectGetWidth(trimmedLayerRect);
120+
while (layer = [loopH nextObject]) {
121+
trimmedLayerRect = Distributor.trimmedRectForLayer(layer);
122+
trimmedLeft = CGRectGetMinX(trimmedLayerRect);
123+
Distributor.offsetLayerX(layer, lastTrimmedRight - trimmedLeft + spacing);
124+
trimmedLayerRect = Distributor.trimmedRectForLayer(layer);
125+
trimmedLeft = CGRectGetMinX(trimmedLayerRect);
126+
lastTrimmedRight = trimmedLeft + CGRectGetWidth(trimmedLayerRect);
117127
});
118128
}
119129
else {
120-
var loopH = [sortedByLeft objectEnumerator];
121-
while (layer = [loopH nextObject]) {
122-
[[layer frame] setLeft:(left + ([[layer frame] left] - CGRectGetMinX(Distributor.trimmedLayer(layer))))];
123-
left = CGRectGetMinX(Distributor.trimmedLayer(layer)) + CGRectGetWidth(Distributor.trimmedLayer(layer)) + spacing;
130+
var sortedByTop = this.sortedArray(this.selection, "frame.top"),
131+
loopV = [sortedByTop objectEnumerator]
132+
firstV = [loopV nextObject],
133+
trimmedLayerRect = Distributor.trimmedRectForLayer(firstV),
134+
trimmedTop = CGRectGetMinY(trimmedLayerRect),
135+
lastTrimmedBottom = trimmedTop + CGRectGetHeight(trimmedLayerRect);
136+
while (layer = [loopV nextObject]) {
137+
trimmedLayerRect = Distributor.trimmedRectForLayer(layer);
138+
trimmedTop = CGRectGetMinY(trimmedLayerRect);
139+
Distributor.offsetLayerY(layer, lastTrimmedBottom - trimmedTop + spacing);
140+
trimmedLayerRect = Distributor.trimmedRectForLayer(layer);
141+
trimmedTop = CGRectGetMinY(trimmedLayerRect);
142+
lastTrimmedBottom = trimmedTop + CGRectGetHeight(trimmedLayerRect);
124143
});
125144
}
126145
}

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ Note that you can use negative spacing to get overlap:
2525

2626
## Bugs / Comments / Questions / Suggestions
2727

28-
Is the plugin misbehaving? File an issue. Have a suggestion? File an issue. You can pretty much file an issue for whatever reason. There is also:
28+
Is the plugin misbehaving? File an issue. Have a suggestion? File an issue. You can pretty much file an issue for whatever reason. Please include the version of the plugin you are using if you think it could be relevant (it most often is). There is also:
2929

3030
* A low-traffic chat room over at [![Join the chat at https://gitter.im/PEZ/SketchDistributor](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/PEZ/SketchDistributor?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
3131
* Twitter: [@PappaPEZ](https://twitter.com/pappapez)
3232

33+
### Known Issues
34+
35+
The current version of the plugin will truncate any decimal points of the positions of the distributed objects. Say for instance that the leftmost layer in the selection is at X position `100.12` and you distribute horizontally with, say, `10` pixels. Then you would expect the following objects to be at `something.12` X positions after distribution, right? But they won't. They will be at `.0` X positions. I hope to be able to fix this issue soon. Most people use Sketch for pixel aligned stuff anyway, I think.
36+
3337
## Sketch Community Attention
3438

3539
This little plugin is surprisingly often present in various **Top X Sketch Productivity plugins** articles. Too many to list here, but here are a few that got me smiling and almost bursting:

0 commit comments

Comments
 (0)