From b25479fa1689597ab9d0605d17e3d042f9ea09c1 Mon Sep 17 00:00:00 2001 From: David Williames Date: Wed, 7 Mar 2018 22:16:16 +1100 Subject: [PATCH 1/2] I think I've fixed the issue of selected layers unecessarily --- Paddy.sketchplugin/Contents/Sketch/layers.js | 6 ++-- Paddy.sketchplugin/Contents/Sketch/main.js | 29 +++++++++++-------- .../Contents/Sketch/manifest.json | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Paddy.sketchplugin/Contents/Sketch/layers.js b/Paddy.sketchplugin/Contents/Sketch/layers.js index 15dba8e..648adf7 100644 --- a/Paddy.sketchplugin/Contents/Sketch/layers.js +++ b/Paddy.sketchplugin/Contents/Sketch/layers.js @@ -451,13 +451,13 @@ function resizeLayer(layer) { // A hack for resizing – just in case Craft's Duplicator is installed // Select a 'Fake' layer, so that when we 'resize', the selection is not empty var nullLayer = MSLayer.alloc().init() - nullLayer.name = "NULL LAYER" + nullLayer.name = "PADDY-NULL-LAYER" document.currentPage().addLayer(nullLayer) - nullLayer.select_byExtendingSelection(true, true) + nullLayer.select_byExpandingSelection(true, false) layer.layerDidEndResize() - nullLayer.select_byExtendingSelection(false, false) + nullLayer.select_byExpandingSelection(false, false) nullLayer.removeFromParent() } diff --git a/Paddy.sketchplugin/Contents/Sketch/main.js b/Paddy.sketchplugin/Contents/Sketch/main.js index be88244..5ef4ebd 100644 --- a/Paddy.sketchplugin/Contents/Sketch/main.js +++ b/Paddy.sketchplugin/Contents/Sketch/main.js @@ -316,6 +316,8 @@ var layers = [] function selectionChanged(context) { + var newSelection = context.actionContext.newSelection + if (!automated) { COScript.currentCOScript().setShouldKeepAround(false) return @@ -325,13 +327,24 @@ function selectionChanged(context) { COScript.currentCOScript().setShouldKeepAround(true) } - runInBackground(function() { - layers = [] startBenchmark() document = context.actionContext.document + if (newSelection.length == 1 && newSelection[0].name() == "PADDY-NULL-LAYER") { + // The selection was becuase of the 'Null layer' used to resolve Craft's stupid Duplicator conflict!!! + + log('Selected the NULL layer; will reselect', context.actionContext.oldSelection) + + context.actionContext.oldSelection.forEach(function(layer) { + layer.select_byExpandingSelection(true, true) + }) + + COScript.currentCOScript().setShouldKeepAround(false) + return + } + // Only include layers that had properties change // Particularly if their frame or position changed @@ -350,8 +363,7 @@ function selectionChanged(context) { if (!persistentLayers) persistentLayers = [] // Only run if nothing is now selected - if (context.actionContext.newSelection.length > 0) { - + if (newSelection.length > 0) { var previouslySelectedParentProps = initialParentProps @@ -359,7 +371,7 @@ function selectionChanged(context) { initialParentProps = {} persistentLayers = [] - context.actionContext.newSelection.forEach(function(layer) { + newSelection.forEach(function(layer) { var rect = layer.absoluteRect() //rectForLayer(layer) var props = { @@ -414,11 +426,6 @@ function selectionChanged(context) { saveValueWithKeyToDoc(initialParentProps, previousParentKey) saveValueWithKeyToDoc(persistentLayers, persistentLayersKey) - context.actionContext.newSelection.forEach(function(layer) { - layer.select_byExpandingSelection(true, true) - }) - - return } @@ -510,8 +517,6 @@ function selectionChanged(context) { COScript.currentCOScript().setShouldKeepAround(false) } - }) - } diff --git a/Paddy.sketchplugin/Contents/Sketch/manifest.json b/Paddy.sketchplugin/Contents/Sketch/manifest.json index c1255f7..d8120e0 100644 --- a/Paddy.sketchplugin/Contents/Sketch/manifest.json +++ b/Paddy.sketchplugin/Contents/Sketch/manifest.json @@ -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.5", + "version": "1.0.6", "identifier": "com.davidwilliames.sketch-plugin.paddy", "compatibleVersion": 47, "bundleVersion": 1, From 328c9219734ef1e16108689221950e1d2bd714e4 Mon Sep 17 00:00:00 2001 From: David Williames Date: Thu, 8 Mar 2018 08:18:10 +1100 Subject: [PATCH 2/2] Fixed some more stuff --- .appcast.xml | 5 +++++ Paddy.sketchplugin/Contents/Sketch/async.js | 1 + Paddy.sketchplugin/Contents/Sketch/layers.js | 17 ++++++++++++++--- Paddy.sketchplugin/Contents/Sketch/main.js | 8 ++++++-- .../Contents/Sketch/manifest.json | 2 +- Paddy.sketchplugin/Contents/Sketch/spacing.js | 2 +- README.md | 2 +- 7 files changed, 29 insertions(+), 8 deletions(-) diff --git a/.appcast.xml b/.appcast.xml index 95102ec..3a4b627 100644 --- a/.appcast.xml +++ b/.appcast.xml @@ -5,6 +5,11 @@ https://raw.githubusercontent.com/DWilliames/paddy-sketch-plugin/master/.appcast.xml Automated padding, spacing and alignment for your Sketch layers en + + Version 1.0.6 + Squashed some more bugs + + Version 1.0.5 More bug fixes diff --git a/Paddy.sketchplugin/Contents/Sketch/async.js b/Paddy.sketchplugin/Contents/Sketch/async.js index af0e9ad..966d43a 100644 --- a/Paddy.sketchplugin/Contents/Sketch/async.js +++ b/Paddy.sketchplugin/Contents/Sketch/async.js @@ -1,3 +1,4 @@ +// Work in progress function loadFramework(name, className) { if (NSClassFromString(className) == null) { diff --git a/Paddy.sketchplugin/Contents/Sketch/layers.js b/Paddy.sketchplugin/Contents/Sketch/layers.js index 648adf7..40974ee 100644 --- a/Paddy.sketchplugin/Contents/Sketch/layers.js +++ b/Paddy.sketchplugin/Contents/Sketch/layers.js @@ -17,7 +17,7 @@ function getContainerFrameForBGLayer(bg) { return false if (layer.isMemberOfClass(MSSliceLayer)) return false - if (layer.name().startsWith('-')) + if (shouldLayerBeIgnored(layer)) return false return true }) @@ -36,13 +36,24 @@ function getContainerFrameForBGLayer(bg) { } +// If the layer should be ignored when calculating the rect of a layer +function shouldLayerBeIgnored(layer) { + if (!layer) return + + if (layer.name().startsWith('-')) { + return true + } + + return false +} + // Whether to check for Stack Groups var takeIntoAccountStackViews = true // Return the rect for a layer as an MSRect function rectForLayer(layer, ignoreWithPrefix) { - if (!layer || (ignoreWithPrefix && layer.name().startsWith('-'))) { + if (!layer || (ignoreWithPrefix && shouldLayerBeIgnored(layer))) { return } @@ -161,7 +172,7 @@ function getBGCandidateFromLayers(layers) { var candidate var existingBackground = layers.find(function(layer) { - if (layer.name().startsWith('-')) + if (shouldLayerBeIgnored(layer)) return false if (canLayerHavePadding(layer)) { diff --git a/Paddy.sketchplugin/Contents/Sketch/main.js b/Paddy.sketchplugin/Contents/Sketch/main.js index 5ef4ebd..884e871 100644 --- a/Paddy.sketchplugin/Contents/Sketch/main.js +++ b/Paddy.sketchplugin/Contents/Sketch/main.js @@ -332,11 +332,15 @@ function selectionChanged(context) { startBenchmark() document = context.actionContext.document + // print('Selection changed - OLD/NEW') + // print(context.actionContext.oldSelection) + // print(context.actionContext.newSelection) + if (newSelection.length == 1 && newSelection[0].name() == "PADDY-NULL-LAYER") { // The selection was becuase of the 'Null layer' used to resolve Craft's stupid Duplicator conflict!!! log('Selected the NULL layer; will reselect', context.actionContext.oldSelection) - + context.actionContext.oldSelection.forEach(function(layer) { layer.select_byExpandingSelection(true, true) }) @@ -576,7 +580,7 @@ function updatePaddingAndSpacingForLayer(layer) { var updateInstances = true - if (total >= 5) { + if (total >= 10) { // if there are more than 10 to update... ask the user if this is what they want to do var iconImage = NSImage.alloc().initByReferencingFile(plugin.urlForResourceNamed("icon.png").path()) diff --git a/Paddy.sketchplugin/Contents/Sketch/manifest.json b/Paddy.sketchplugin/Contents/Sketch/manifest.json index d8120e0..d3b28c6 100644 --- a/Paddy.sketchplugin/Contents/Sketch/manifest.json +++ b/Paddy.sketchplugin/Contents/Sketch/manifest.json @@ -21,7 +21,7 @@ { "name": "Imply padding for selection", "identifier": "autoApplyPadding", - "shortcut": "command p", + "shortcut": "command shift p", "script": "main.js", "handler": "autoApplyPadding", "description": "Apply implied padding to the currently selected layers", diff --git a/Paddy.sketchplugin/Contents/Sketch/spacing.js b/Paddy.sketchplugin/Contents/Sketch/spacing.js index ac1a28b..f011e30 100644 --- a/Paddy.sketchplugin/Contents/Sketch/spacing.js +++ b/Paddy.sketchplugin/Contents/Sketch/spacing.js @@ -234,7 +234,7 @@ function applySpacingToGroup(spacing, groupLayer) { return false if (layer.isMemberOfClass(MSSliceLayer)) return false - if (layer.name().startsWith('-')) + if (shouldLayerBeIgnored(layer)) return false return true }) diff --git a/README.md b/README.md index 51dbdf3..2a0ab84 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ Examples: Run the plugin command **'Enter padding for selection'** or **`Control + Alt + p`** to bring up an input field, to save padding to all your selected layers easier. -If you don't want to be prompted to enter the specified padding, and instead want it to be automatically inferred from what it already is – run the command **'Imply padding for selection'** or **`Command + p`**. +If you don't want to be prompted to enter the specified padding, and instead want it to be automatically inferred from what it already is – run the command **'Imply padding for selection'** or **`Command + shift + p`**. ![Padding example](.images/GIFs/padding.gif)