Skip to content

Commit

Permalink
handle group and text layers
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieudutour committed Oct 23, 2018
1 parent 9358c9f commit 9f83b17
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions src/my-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ function getPathFromLayer(layer) {
function getOptionsFromLayer(layer) {
let options = {}

if (!layer.style) {
options.fill = '#000000'
return
}

const fill = (layer.style.fills || []).filter(
f => f.sketchObject.isEnabled() && f.fill === sketch.Style.FillType.Color
)[0]
Expand All @@ -31,43 +36,60 @@ function getOptionsFromLayer(layer) {
options.stroke = '#00000000'
}

if (!fill) {
if (layer.type === 'Text') {
options.fill = sketch.Style.colorToString(layer.sketchObject.textColor())
} else if (!border) {
options.fill = '#000000'
}
}

return options
}

export default function(context) {
const document = sketch.getSelectedDocument()

const selection = document.selectedLayers

if (selection.isEmpty) {
function makeRough(layer) {
if (layer.type === 'Group') {
layer.layers.forEach(makeRough)
return
}

selection.forEach(layer => {
if (!layer.sketchObject.pathInFrameWithTransforms) {
return
}
if (!layer.sketchObject.pathInFrameWithTransforms) {
return
}

// override the wrapper to have a proper object
// override the wrapper to have a proper object
if (!layer.type) {
layer = sketch.Shape.fromNative(layer.sketchObject)
}

const rc = new RoughSketch(layer.parent.type === 'Page' ? layer : layer.parent)
const rc = new RoughSketch(layer.parent.type === 'Page' ? layer : layer.parent)

const newLayer = rc.path(
getPathFromLayer(layer),
getOptionsFromLayer(layer)
)
const newLayer = rc.path(
getPathFromLayer(layer),
getOptionsFromLayer(layer)
)

newLayer.name = 'Rough ' + layer.name
newLayer.name = 'Rough ' + layer.name

// add new layer to parent
newLayer.parent = layer.parent
// add new layer to parent
newLayer.parent = layer.parent

// hide previous layer
layer.hidden = true
layer.selected = false
// hide previous layer
layer.hidden = true
layer.selected = false

// select new one
newLayer.selected = true
}

export default function(context) {
const document = sketch.getSelectedDocument()

const selection = document.selectedLayers

if (selection.isEmpty) {
return
}

// select new one
newLayer.selected = true
})
selection.forEach(makeRough)
}

0 comments on commit 9f83b17

Please sign in to comment.