Skip to content

Commit

Permalink
v3.1 update
Browse files Browse the repository at this point in the history
- Added support for textDecoration
- Added support for textTransform
- Removed appcast URL from manifest.json to fix Sketch crashing issue
- Implemented code style linter corrections
  • Loading branch information
perrysmotors committed May 26, 2017
1 parent 510536c commit fd9c852
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 35 deletions.
88 changes: 55 additions & 33 deletions CopyFramerCode.sketchplugin/Contents/Sketch/copy-framer-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function onRun(context) {
return false;
}

selection.iterate(function(layer) {
selection.iterate(function (layer) {
processLayerRecursively(layer);
});

Expand All @@ -49,7 +49,7 @@ function onRun(context) {

////////////////////////////////////////////////////////////////////////////////

var processLayerRecursively = function(layer, parent) {
var processLayerRecursively = function (layer, parent) {

var sketchObject = layer.sketchObject;

Expand Down Expand Up @@ -88,7 +88,7 @@ var processLayerRecursively = function(layer, parent) {
Object.assign(framerObject, layerCode(sketchObject));
framerLayers.push(framerObject);

layer.iterate(function(layer) {
layer.iterate(function (layer) {
processLayerRecursively(layer, name);
});
}
Expand All @@ -99,6 +99,7 @@ var processLayerRecursively = function(layer, parent) {
} else {
Object.assign(framerObject, layerCode(sketchObject));
}

framerLayers.push(framerObject);

} else if (layer.isText) {
Expand All @@ -111,7 +112,7 @@ var processLayerRecursively = function(layer, parent) {
framerLayers.push(framerObject);
}
}
}
};

////////////////////////////////////////////////////////////////////////////////

Expand All @@ -129,12 +130,13 @@ function layerWithPropertiesCode(layer) {
framerObject.backgroundColor = rgbaCode(fill.color());
}

var borderRadius
var borderRadius;
if (isCircle(layer)) {
borderRadius = framerObject.width / 2;
} else {
borderRadius = layer.layers().firstObject().cornerRadiusFloat() * scale;
}

if (borderRadius != 0) {
framerObject.borderRadius = borderRadius;
}
Expand Down Expand Up @@ -181,6 +183,7 @@ function textLayerCode(layer) {
if (fontStyle.slope != "") {
framerObject.fontStyle = fontStyle.slope;
}

if (fontStyle.weight != "") {
framerObject.fontWeight = fontStyle.weight;
}
Expand All @@ -193,7 +196,7 @@ function textLayerCode(layer) {
framerObject.lineHeight = layer.lineHeight() * scale / framerObject.fontSize;
}

switch(layer.textAlignment()) {
switch (layer.textAlignment()) {
case 1:
framerObject.textAlign = '"right"';
break;
Expand All @@ -204,6 +207,22 @@ function textLayerCode(layer) {
framerObject.textAlign = '"left"';
}

if (layer.styleAttributes().NSStrikethrough == 1) {
framerObject.textDecoration = '"line-through"';
}

if (layer.styleAttributes().NSUnderline == 1) {
framerObject.textDecoration = '"underline"';
}

if (layer.styleAttributes().MSAttributedStringTextTransformAttribute == 1) {
framerObject.textTransform = '"uppercase"';
}

if (layer.styleAttributes().MSAttributedStringTextTransformAttribute == 2) {
framerObject.textTransform = '"lowercase"';
}

framerObject.color = rgbaCode(layer.textColor());

var shadow = topShadow(layer.style());
Expand Down Expand Up @@ -239,10 +258,11 @@ function layerCode(layer) {
var exportFormats = layer.exportOptions().exportFormats();

if (exportFormats.length != 0) {
var matchingExportFormat = exportFormats.find(findFormatMatchingScale));
var matchingExportFormat = exportFormats.find(findFormatMatchingScale);
if (matchingExportFormat == null) {
matchingExportFormat = exportFormats[0];
}

var imageExtn = matchingExportFormat.fileFormat();
if (matchingExportFormat.name() != null) {
if (matchingExportFormat.namingScheme() == 0) {
Expand All @@ -251,6 +271,7 @@ function layerCode(layer) {
imageFilename = matchingExportFormat.name() + imageFilename;
}
}

framerObject.image = '"images/' + imageFilename + '.' + imageExtn + '"';
}

Expand All @@ -265,18 +286,19 @@ function layerCode(layer) {
////////////////////////////////////////////////////////////////////////////////

function framerLayerProperties(object) {
var text
var text;
if (object.layerType == "TextLayer") {
text = object.name + ' = new TextLayer\n';
} else {
text = object.name + ' = new Layer\n';
}

Object.keys(object).forEach(function(key) {
Object.keys(object).forEach(function (key) {
if (key != "layerType" && key != "name") {
text = text + '\t' + key + ': ' + object[key] + '\n';
}
});

return text;
}

Expand All @@ -289,7 +311,7 @@ function topFill(style) {
for (i = 0, len = fills.length; i < len; i++) {
var fillType = fills[i].fillType();
if (fillType == 0) {
fill = fills[i]
fill = fills[i];
}
}

Expand All @@ -303,7 +325,7 @@ function topBorder(style) {
for (i = 0, len = borders.length; i < len; i++) {
var fillType = borders[i].fillType();
if (fillType == 0) {
border = borders[i]
border = borders[i];
}
}

Expand All @@ -317,7 +339,7 @@ function topShadow(style) {
if (len == 0) {
return null;
} else {
return shadows[len-1];
return shadows[len - 1];
}
}

Expand All @@ -327,7 +349,7 @@ function isRectangle(layer) {
var layerCount = layer.layers().count();
var layerClass = layer.layers()[0].class();

if (layerCount == 1 && layerClass== MSRectangleShape) {
if (layerCount == 1 && layerClass == MSRectangleShape) {
return true;
} else {
return false;
Expand All @@ -340,42 +362,42 @@ function isCircle(layer) {
var width = layer.frame().width();
var height = layer.frame().height();

if (layerCount == 1 && layerClass== MSOvalShape && width == height) {
if (layerCount == 1 && layerClass == MSOvalShape && width == height) {
return true;
} else {
return false;
}
}

function rgbaCode(colour) {
var red = Math.round(colour.red()*255);
var green = Math.round(colour.green()*255);
var blue = Math.round(colour.blue()*255);
var red = Math.round(colour.red() * 255);
var green = Math.round(colour.green() * 255);
var blue = Math.round(colour.blue() * 255);

return '"rgba(' + red + ',' + green + ',' + blue + ',' + colour.alpha() + ')"';
}

function getFontStyle(layer) {

var fontWeights = {
"thin": 100,
"extralight": 200,
"ultralight": 200,
"light": 300,
"book": 400,
"normal": 400,
"regular": 400,
"roman": 400,
"medium": 500,
"semibold": 600,
"demibold": 600,
"bold": 700,
"thin": 100,
"extralight": 200,
"ultralight": 200,
"light": 300,
"book": 400,
"normal": 400,
"regular": 400,
"roman": 400,
"medium": 500,
"semibold": 600,
"demibold": 600,
"bold": 700,
"boldmt": 700,
"psboldmt": 700,
"extrabold": 800,
"ultrabold": 800,
"black": 900,
"heavy": 900
"extrabold": 800,
"ultrabold": 800,
"black": 900,
"heavy": 900
}

var fontFamily = layer.font().familyName().replace(/ /g, "");
Expand Down
3 changes: 1 addition & 2 deletions CopyFramerCode.sketchplugin/Contents/Sketch/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"name" : "Copy Framer Code",
"identifier" : "com.gilesperry.copy-framer-code",
"version" : "3.0.1",
"appcastURL": "https://api.sketchpacks.com/v1/plugins/com.gilesperry.copy-framer-code/appcast",
"version" : "3.1",
"compatibleVersion": 44,
"description" : "Copy layers to the clipboard as Framer code.",
"homepage": "https://github.com/perrysmotors/copy-framer-code",
Expand Down

0 comments on commit fd9c852

Please sign in to comment.