Last active
April 4, 2019 01:29
-
-
Save jacopocolo/7e4c6f32dea068414599b33fcd0f97b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// let's get a hold on the Sketch API | |
const sketch = require('sketch'); | |
//let's expose these globally | |
var document = sketch.fromNative(context.document); | |
var page = document.selectedPage; | |
var Rectangle = require('sketch/dom').Rectangle; | |
var Shape = require('sketch/dom').Shape; | |
var Style = require('sketch/dom').Style; | |
var Group = require('sketch/dom').Group; | |
var selectedArtboard; | |
if(context.selection.length === 1 ){ | |
const layer = context.selection.firstObject(); | |
if( layer && layer.isKindOfClass(MSArtboardGroup) ){ | |
selectedArtboard = layer; | |
generateSwatch(context); | |
} | |
} else { | |
console.log('Export failed, please select one artboard'); | |
} | |
// swatches = document.sharedLayerStyles; | |
// swatches.sort(dynamicSort("name")); | |
function dynamicSort(property) { | |
var sortOrder = 1; | |
if(property[0] === "-") { | |
sortOrder = -1; | |
property = property.substr(1); | |
} | |
return function (a,b) { | |
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; | |
return result * sortOrder; | |
} | |
} | |
function generateSwatch(context) { | |
var offset = 66; | |
var x = 0; | |
var y = 0; | |
for (i=0;i<=document.sharedLayerStyles.length;i++) { | |
const rectangle = new Shape({ | |
name: "rectangle", | |
parent: selectedArtboard, | |
frame: new Rectangle(x, y, 84, 38), | |
style: { | |
fills: [{enabled:true}], | |
borders: [{enabled:true}] | |
}, | |
sharedStyleId: document.sharedLayerStyles[i].id | |
}) | |
rectangle.style.syncWithSharedStyle(document.sharedLayerStyles[i]); | |
const text = new sketch.Text({ | |
parent: selectedArtboard, | |
text: document.sharedLayerStyles[i].name, | |
frame: new Rectangle(x, y+42), | |
name: document.sharedLayerStyles[i].name | |
}); | |
new Group(); | |
var group = new Group({ | |
name: 'Artboard_label', | |
parent: selectedArtboard, | |
locked: true | |
}) | |
group.frame.width = rectangle.frame.width; | |
group.frame.height = rectangle.frame.height; | |
group.layers.push(rectangle); | |
group.layers.push(text); | |
y=y+offset; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment