Skip to content

Commit

Permalink
Merge pull request #19 from DWilliames/v1.0.2
Browse files Browse the repository at this point in the history
V1.0.2
  • Loading branch information
DWilliames authored Feb 13, 2018
2 parents b39c148 + d5df224 commit c6f2ad0
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 161 deletions.
5 changes: 5 additions & 0 deletions .appcast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<link>https://raw.githubusercontent.com/DWilliames/paddy-sketch-plugin/master/.appcast.xml</link>
<description>Automated padding, spacing and alignment for your Sketch layers</description>
<language>en</language>
<item>
<title>Version 1.0.2</title>
<description>More bug fixes</description>
<enclosure url="https://github.com/DWilliames/paddy-sketch-plugin/releases/download/v1.0.2/Paddy.sketchplugin.zip" sparkle:version="1.0.2" />
</item>
<item>
<title>Version 1.0.1</title>
<description>Fixed some bugs</description>
Expand Down
27 changes: 26 additions & 1 deletion Paddy.sketchplugin/Contents/Sketch/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function dependentLayersOfLayerIgnoringLayers(layer, objectIDsToIgnore) {
var onTheLeft = (sFrame.minX() < lFrame.minX()) && layer.isMemberOfClass(MSTextLayer) && layer.textAlignment() == 1
var diff = onTheLeft ? (lFrame.minX() - sFrame.maxX()) : (sFrame.minX() - lFrame.maxX())


// As far as I can tell; this seems to be the specific dimensions for this
if (diff <= 20 && diff >= 0) {
objectIDsToIgnore.push(sibling.objectID())

Expand Down Expand Up @@ -269,6 +269,26 @@ function dependentLayersOfLayerIgnoringLayers(layer, objectIDsToIgnore) {
}


function getAllChildrenForGroup(group) {
var layers = []

group.layers().forEach(function(layer) {
if (layer.isMemberOfClass(MSShapePathLayer)) {
// Ignore Shape path layers
return
}

layers.push(layer)

if (layer.isMemberOfClass(MSLayerGroup)) {
layers = layers.concat(getAllChildrenForGroup(layer))
}
})

return layers
}


/**
*
*/
Expand All @@ -280,6 +300,11 @@ function buildTreeMap(layers) {
layers.forEach(function(layer) {
fullDepthMap.push(layer)

// Add all it's children, if the layer was a group
if (layer.isMemberOfClass(MSLayerGroup)) {
fullDepthMap = fullDepthMap.concat(getAllChildrenForGroup(layer))
}

var parent = layer.parentGroup()
while(parent) {
fullDepthMap.push(parent)
Expand Down
83 changes: 80 additions & 3 deletions Paddy.sketchplugin/Contents/Sketch/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var selection, document, plugin, app, iconImage
function onSetUp(context) {
document = context.document
plugin = context.plugin
coscript.setShouldKeepAround(true)
}


Expand Down Expand Up @@ -186,11 +187,47 @@ function textChanged(context) {
}



// SELECTION

// Store the properties of a layer when it is initially selected
// Then we can see if they changed once the user deselects everything
var initialSelectedProps = {}

function selectionChanged(context) {
startBenchmark()

// Only run if nothing is now selected
if (context.actionContext.newSelection.length > 0) return
if (context.actionContext.newSelection.length > 0) {

initialSelectedProps = {}

context.actionContext.newSelection.forEach(function(layer) {
if (layer.isMemberOfClass(MSLayerGroup)) {

var frame = layer.frame()
var size = frame.size()
var origin = frame.origin()

// Group
initialSelectedProps[layer.objectID()] = {
layer: layer,
width: size.width,
height: size.height,
x: origin.x,
y: origin.y,
name: layer.name(),
parent: layer.parentGroup()
}
} else if (layer.isMemberOfClass(MSSymbolInstance)) {
initialSelectedProps[layer.objectID()] = {
layer: layer,
overrides: layer.overrides()
}
}
})
return
}

log('RUN PLUGIN BECAUSE SELECTION CHANGED')
document = context.actionContext.document
Expand All @@ -204,8 +241,48 @@ function selectionChanged(context) {
var uniqueLayers = []
context.actionContext.oldSelection.forEach(function(layer) {
// Ignore unique siblings, if it is a Symbol instance, or a layer group
if (layer.isMemberOfClass(MSSymbolInstance) || layer.isMemberOfClass(MSLayerGroup)) {
uniqueLayers.push(layer)
if (layer.isMemberOfClass(MSSymbolInstance)) {
// Only add a symbol, if it actually changed props
var layerProps = initialSelectedProps[layer.objectID()]

if (layerProps) {
if (layerProps.overrides != layer.overrides()) {
uniqueLayers.push(layer)
}
} else {
uniqueLayers.push(layer)
}

} else if (layer.isMemberOfClass(MSLayerGroup)) {
// Only add a group, if it actually changed props
var layerProps = initialSelectedProps[layer.objectID()]

if (layerProps) {
var name = layerProps.name
var frame = layer.frame()

var sameWidth = (layerProps.width == frame.size().width)
var sameHeight = (layerProps.height == frame.size().height)

var sameOrigin = (layerProps.x == frame.origin().x && layerProps.y == frame.origin().y)

if (layerProps.parent && !layer.parentGroup()) {
// Doesn't have a parent anymore... must've been deleted
uniqueLayers.push(layerProps.parent)
} else if (name != layer.name() || !(sameHeight && sameWidth)) {
// Name or sizing changed
uniqueLayers.push(layer)
} else if (!sameOrigin) {
// Origin moved – then it's parent mmay need to update
uniqueLayers.push(layer.parentGroup())
} else {
// Props haven't changed
}
} else {
// Layer has no previous props
uniqueLayers.push(layer)
}

} else if (!doesArrayContainSibling(uniqueLayers, layer)) {
uniqueLayers.push(layer)
}
Expand Down
2 changes: 1 addition & 1 deletion Paddy.sketchplugin/Contents/Sketch/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "David Williames",
"homepage": "https://github.com/DWilliames/paddy-sketch-plugin",
"appcast": "https://raw.githubusercontent.com/DWilliames/paddy-sketch-plugin/master/.appcast.xml",
"version": "1.0.1",
"version": "1.0.2",
"identifier": "com.davidwilliames.sketch-plugin.paddy",
"compatibleVersion": 47,
"bundleVersion": 1,
Expand Down
Loading

0 comments on commit c6f2ad0

Please sign in to comment.