From c7686af24a13567dc1a350733194a8cb06cf135c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Str=C3=B6mberg?= Date: Sat, 11 Feb 2017 18:22:38 +0100 Subject: [PATCH] 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. --- .../Contents/Sketch/manifest.json | 2 +- .../Contents/Sketch/script.js | 59 ++++++++++++------- README.md | 6 +- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/Distributor.sketchplugin/Contents/Sketch/manifest.json b/Distributor.sketchplugin/Contents/Sketch/manifest.json index 7789d06..2d9dcc7 100644 --- a/Distributor.sketchplugin/Contents/Sketch/manifest.json +++ b/Distributor.sketchplugin/Contents/Sketch/manifest.json @@ -40,7 +40,7 @@ ] }, "identifier" : "com.betterthantomorrow.sketch.distributor", - "version" : "1.0.3.1b", + "version" : "1.0.4", "description" : "Distribute selected objects vertically or horizontally with exact spacing.", "authorEmail" : "pez@pezius.com", "name" : "Distributor" diff --git a/Distributor.sketchplugin/Contents/Sketch/script.js b/Distributor.sketchplugin/Contents/Sketch/script.js index 4b7e607..d15c092 100644 --- a/Distributor.sketchplugin/Contents/Sketch/script.js +++ b/Distributor.sketchplugin/Contents/Sketch/script.js @@ -53,9 +53,16 @@ var Distributor = { return label; }, - trimmedLayer: function(layer) { - //return MSSliceTrimming.trimmedRectForLayerAncestry(MSImmutableLayerAncestry.ancestryWithMSLayer(layer)); - return [layer rect] + offsetLayerY: function(layer, yOffset) { + layer.frame().setY(layer.frame().y() + yOffset); + }, + + offsetLayerX: function(layer, xOffset) { + layer.frame().setX(layer.frame().x() + xOffset); + }, + + trimmedRectForLayer: function(layer) { + return MSSliceTrimming.trimmedRectForLayerAncestry(MSImmutableLayerAncestry.ancestryWithMSLayer(layer)); }, createChoices: function(msg) { @@ -99,28 +106,40 @@ var Distributor = { }, distribute: function(dimension, spacingString) { - var sortedByLeft = this.sortedArray(this.selection, "frame.left"), - sortedByTop = this.sortedArray(this.selection, "frame.top"), - firstLeft = sortedByLeft[0], - left = [[firstLeft frame] left], - firstTop = sortedByTop[0], - top = [[firstTop frame] top], - formatter = [[NSNumberFormatter alloc] init], - spacing = [formatter numberFromString:spacingString]; + var formatter = [[NSNumberFormatter alloc] init], + spacing = [formatter numberFromString:spacingString]; if (spacing != null) { - if (String(dimension) === "Vertically") { - var loopV = [sortedByTop objectEnumerator]; - while (layer = [loopV nextObject]) { - [[layer frame] setTop:(top + ([[layer frame] top] - CGRectGetMinY(Distributor.trimmedLayer(layer))))]; - top = CGRectGetMinY(Distributor.trimmedLayer(layer)) + CGRectGetHeight(Distributor.trimmedLayer(layer)) + spacing; + if (String(dimension) == "Horizontally") { + var sortedByLeft = this.sortedArray(this.selection, "frame.left"), + loopH = [sortedByLeft objectEnumerator] + firstH = [loopH nextObject], + trimmedLayerRect = Distributor.trimmedRectForLayer(firstH), + trimmedLeft = CGRectGetMinX(trimmedLayerRect), + lastTrimmedRight = trimmedLeft + CGRectGetWidth(trimmedLayerRect); + while (layer = [loopH nextObject]) { + trimmedLayerRect = Distributor.trimmedRectForLayer(layer); + trimmedLeft = CGRectGetMinX(trimmedLayerRect); + Distributor.offsetLayerX(layer, lastTrimmedRight - trimmedLeft + spacing); + trimmedLayerRect = Distributor.trimmedRectForLayer(layer); + trimmedLeft = CGRectGetMinX(trimmedLayerRect); + lastTrimmedRight = trimmedLeft + CGRectGetWidth(trimmedLayerRect); }); } else { - var loopH = [sortedByLeft objectEnumerator]; - while (layer = [loopH nextObject]) { - [[layer frame] setLeft:(left + ([[layer frame] left] - CGRectGetMinX(Distributor.trimmedLayer(layer))))]; - left = CGRectGetMinX(Distributor.trimmedLayer(layer)) + CGRectGetWidth(Distributor.trimmedLayer(layer)) + spacing; + var sortedByTop = this.sortedArray(this.selection, "frame.top"), + loopV = [sortedByTop objectEnumerator] + firstV = [loopV nextObject], + trimmedLayerRect = Distributor.trimmedRectForLayer(firstV), + trimmedTop = CGRectGetMinY(trimmedLayerRect), + lastTrimmedBottom = trimmedTop + CGRectGetHeight(trimmedLayerRect); + while (layer = [loopV nextObject]) { + trimmedLayerRect = Distributor.trimmedRectForLayer(layer); + trimmedTop = CGRectGetMinY(trimmedLayerRect); + Distributor.offsetLayerY(layer, lastTrimmedBottom - trimmedTop + spacing); + trimmedLayerRect = Distributor.trimmedRectForLayer(layer); + trimmedTop = CGRectGetMinY(trimmedLayerRect); + lastTrimmedBottom = trimmedTop + CGRectGetHeight(trimmedLayerRect); }); } } diff --git a/README.md b/README.md index c8d3e85..282a137 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,15 @@ Note that you can use negative spacing to get overlap: ## Bugs / Comments / Questions / Suggestions -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: +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: * 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) * Twitter: [@PappaPEZ](https://twitter.com/pappapez) +### Known Issues + +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. + ## Sketch Community Attention 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: