Skip to content

Commit

Permalink
Added Group Support
Browse files Browse the repository at this point in the history
Users can now select a layer group, rather than individual elements
  • Loading branch information
LukeFinch committed Apr 5, 2018
1 parent 3702a3f commit 538436c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .appcast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
<enclosure url="https://github.com/lukefinch/quickstyles/releases/download/v0.2.0/QuickStyles.sketchplugin.zip" sparkle:version="0.2.0"/>
</item>
</channel>
</rss>
</rss>
Binary file modified QuickStyles.sketchplugin.zip
Binary file not shown.
115 changes: 63 additions & 52 deletions QuickStyles.sketchplugin/Contents/Sketch/my-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,67 +348,78 @@ module.exports = function toArray(object) {

/* WEBPACK VAR INJECTION */(function(console) {sketch = context.api();
doc = context.document;
sel = context.selection;
currentPage = doc.currentPage();
font = [];
colours = [];

function addArtboard(page, name) {
var artboard = MSArtboardGroup["new"]();
frame = artboard.frame();
frame.setWidth(4000);
frame.setHeight(4000);
frame.setConstrainProportions(false);
page.addLayers([artboard]);
artboard.setName(name);
return artboard;
var artboard = MSArtboardGroup["new"]();
frame = artboard.frame();
frame.setWidth(4000);
frame.setHeight(4000);
frame.setConstrainProportions(false);
page.addLayers([artboard]);
artboard.setName(name);
return artboard;
}

if (sel == 0) {
doc.showMessage('You need to select some text and colours!');
if (context.api().selectedDocument.selectedLayers.length == 0) {
context.document.showMessage('You need to select some text and colours!');
} else {
var addTextLayer = function addTextLayer(target, label, font, colour, i, j) {
var textLayer = MSTextLayer["new"]();
console.log(textLayer);
textLayer.setStringValue("Example text, im a pretty unicorn");
textLayer.setName(label);
textLayer.setFont(font);
textLayer.setTextColor(colour);
textLayer.setTextBehaviour(1);
textLayer.absoluteRect().setWidth(500);
textLayer.absoluteRect().setHeight(200);
textLayer.absoluteRect().setRulerX(i * 600);
textLayer.absoluteRect().setRulerY(j * (textLayer.frame().height() + 50));
target.addLayers([textLayer]);
return textLayer;
};

var board = new addArtboard(currentPage, "Quick Text Styles");

;

context.api().selectedDocument.selectedLayers.iterate(function (layer) {

if (layer['_object'].isKindOfClass(MSTextLayer)) {
font.push({
"font": layer['_object'].font(),
"text": layer['_object'].name()
});
}
if (layer['_object'].isKindOfClass(MSShapeGroup)) {
colours.push({
"colour": layer['_object'].style().fills()[0].color(),
"name": layer['_object'].name()
});
}
});

for (var i = 0; i < colours.length; i++) {
for (var j = 0; j < font.length; j++) {
addTextLayer(board, font[j].text + "/" + colours[i].name, font[j].font, colours[i].colour, i, j);
}
var addTextLayer = function addTextLayer(target, label, font, colour, i, j) {
var textLayer = MSTextLayer["new"]();
console.log(textLayer);
textLayer.setStringValue("Example text, im a pretty unicorn");
textLayer.setName(label);
textLayer.setFont(font);
textLayer.setTextColor(colour);
textLayer.setTextBehaviour(1);
textLayer.absoluteRect().setWidth(500);
textLayer.absoluteRect().setHeight(200);
textLayer.absoluteRect().setRulerX(i * 600);
textLayer.absoluteRect().setRulerY(j * (textLayer.frame().height() + 50));
target.addLayers([textLayer]);
return textLayer;
};

var board = new addArtboard(currentPage, "Quick Text Styles");

;

context.api().selectedDocument.selectedLayers.iterate(function (layer) {

//If the selection contains groups, this **should** loop through them, hopefully.

if (layer['_object'].isKindOfClass(MSLayerGroup)) {
for (var child = 0; child < layer['_object'].children().length; child++) {

pushLayer(layer['_object'].children()[child]);
}
} else pushLayer(layer['_object']);

function pushLayer(l) {

if (l.isKindOfClass(MSTextLayer)) {
font.push({
"font": l.font(),
"text": l.name()
});
}
if (l.isKindOfClass(MSShapeGroup)) {
colours.push({
"colour": l.style().fills()[0].color(),
"name": l.name()
});
}
}
});

for (var i = 0; i < colours.length; i++) {
for (var j = 0; j < font.length; j++) {
addTextLayer(board, font[j].text + "/" + colours[i].name, font[j].font, colours[i].colour, i, j);
}
board.resizeToFitChildren();
}
board.resizeToFitChildren();
} //end of else
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(0)))

Expand Down
40 changes: 28 additions & 12 deletions src/my-command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
sketch = context.api()
doc = context.document;
sel = context.selection;
currentPage = doc.currentPage();
font = [];
colours = [];
Expand All @@ -16,7 +15,7 @@ function addArtboard(page, name) {
return artboard;
}

if(sel == 0){doc.showMessage('You need to select some text and colours!')}
if(context.api().selectedDocument.selectedLayers.length == 0){context.document.showMessage('You need to select some text and colours!')}
else{
var board = new addArtboard(currentPage, "Quick Text Styles")

Expand All @@ -36,26 +35,43 @@ function addTextLayer (target, label, font, colour,i, j) {
return textLayer;
};



context.api().selectedDocument.selectedLayers.iterate(layer => {

if(layer['_object'].isKindOfClass(MSTextLayer)){
font.push({
"font": layer['_object'].font(),
"text": layer['_object'].name(),
}
)}
if(layer['_object'].isKindOfClass(MSShapeGroup)){

//If the selection contains groups, this **should** loop through them, hopefully.

if(layer['_object'].isKindOfClass(MSLayerGroup)){
for(var child = 0; child < layer['_object'].children().length; child++){

pushLayer(layer['_object'].children()[child])
}
}
else(pushLayer(layer['_object']))


function pushLayer(l){

if(l.isKindOfClass(MSTextLayer)){
font.push({
"font": l.font(),
"text": l.name(),
})
}
if(l.isKindOfClass(MSShapeGroup)){
colours.push({
"colour":layer['_object'].style().fills()[0].color(),
"name":layer['_object'].name(),
"colour":l.style().fills()[0].color(),
"name":l.name(),
}
)}
}

})

for(var i = 0; i < colours.length; i++){
for(var j = 0; j < font.length; j++){
addTextLayer(board, font[j].text + "/" + colours[i].name, font[j].font, colours[i].colour, i, j)
}}
board.resizeToFitChildren();

} //end of else

0 comments on commit 538436c

Please sign in to comment.