Skip to content

Commit

Permalink
Merge pull request #474 from ellunium/master
Browse files Browse the repository at this point in the history
Fix for Sketch v52 and v52.1 - With Inspector / Mark Property (and more) working
  • Loading branch information
utom authored Oct 17, 2018
2 parents 9c2a5ff + 3ea02ff commit cbde414
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 70 deletions.
174 changes: 117 additions & 57 deletions Sketch Measure.sketchplugin/Contents/Sketch/library/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ SM.extend({
return MSLayerGroup.new();
},
addShape: function(){
var shape = MSRectangleShape.alloc().initWithFrame(NSMakeRect(0, 0, 100, 100));
return MSShapeGroup.shapeWithPath(shape);
return MSShapeGroup.shapeWithRect(NSMakeRect(0, 0, 100, 100));

},
addText: function(container){
var text = MSTextLayer.new();
Expand Down Expand Up @@ -357,7 +357,13 @@ SM.extend({
};
},
getRadius: function(layer){
return ( layer.layers && this.is(layer.layers().firstObject(), MSRectangleShape) ) ? layer.layers().firstObject().fixedRadius(): 0;
if(layer.layers && this.is(layer.layers().firstObject(), MSRectangleShape)){
return (layer.layers().firstObject().cornerRadiusString().split(';').map(Number).length == 1) ? layer.layers().firstObject().fixedRadius() : layer.layers().firstObject().cornerRadiusString().split(';').map(Number);
} else if(this.is(layer, MSRectangleShape)) {
return (layer.cornerRadiusString().split(';').map(Number).length == 1) ? layer.fixedRadius() : layer.cornerRadiusString().split(';').map(Number);
} else {
return 0;
}
},
getBorders: function(style) {
var bordersData = [],
Expand Down Expand Up @@ -441,19 +447,38 @@ SM.extend({
return style.contextSettings().opacity()
},
getStyleName: function(layer){
var styles = (this.is(layer, MSTextLayer))? this.document.documentData().layerTextStyles(): this.document.documentData().layerStyles(),
layerStyle = layer.style(),
sharedObjectID = layerStyle.sharedObjectID(),
style;

styles = styles.objectsSortedByName();
if(layer.sharedStyleID()){
var styles = (this.is(layer, MSTextLayer))? this.document.documentData().layerTextStyles(): this.document.documentData().layerStyles(),
layerStyle = layer.style(),
sharedObjectID = layer.sharedStyleID(),
style;

styles = styles.objectsSortedByName();

if(styles.count() > 0){
style = this.find({key: "(objectID != NULL) && (objectID == %@)", match: sharedObjectID}, styles);
}

if(!style){
var styles = (this.is(layer, MSTextLayer))? this.document.documentData().foreignTextStyles(): this.document.documentData().foreignLayerStyles(),
layerStyle = layer.style(),
sharedObjectID = layer.sharedStyleID(),
style;

styles.forEach(libraryStyle => {
if(String(libraryStyle.localSharedStyle().objectID()) == String(sharedObjectID)){
style = libraryStyle.localSharedStyle().name();
}
});

if(!style) return "";
return this.toJSString(style);
}
return this.toJSString(style.name());

if(styles.count() > 0){
style = this.find({key: "(objectID != NULL) && (objectID == %@)", match: sharedObjectID}, styles);
} else {
return "";
}

if(!style) return "";
return this.toJSString(style.name());
},
updateContext: function(){
this.context.document = NSDocumentController.sharedDocumentController().currentDocument();
Expand All @@ -469,26 +494,46 @@ SM.extend({
return Math.round( number / 2 );
},
convertUnit: function(length, isText, percentageType){
if(percentageType && this.artboard){
var artboardRect = this.getRect( this.artboard );
if (percentageType == "width") {
return Math.round((length / artboardRect.width) * 1000) / 10 + "%";

if(Array.isArray(length)){
var units = this.configs.unit.split("/"),
unit = units[0];

if( units.length > 1 && isText){
unit = units[1];
}
else if(percentageType == "height"){
return Math.round((length / artboardRect.height) * 1000) / 10 + "%";
}
}

var length = Math.round( length / this.configs.scale * 10 ) / 10,
units = this.configs.unit.split("/"),
unit = units[0];
var scale = this.configs.scale;
var tempLegth = [];

if( units.length > 1 && isText){
unit = units[1];
}
length.forEach(function(element) {
tempLegth.push(Math.round( element / scale * 10 ) / 10);
});

return tempLegth.join(unit + ' ') + unit;

return length + unit;
} else {

if(percentageType && this.artboard){
var artboardRect = this.getRect( this.artboard );
if (percentageType == "width") {
return Math.round((length / artboardRect.width) * 1000) / 10 + "%";
}
else if(percentageType == "height"){
return Math.round((length / artboardRect.height) * 1000) / 10 + "%";
}
}

var length = Math.round( length / this.configs.scale * 10 ) / 10,
units = this.configs.unit.split("/"),
unit = units[0];

if( units.length > 1 && isText){
unit = units[1];
}

return length + unit;
}

},
toHex:function(c) {
var hex = Math.round(c).toString(16).toUpperCase();
Expand Down Expand Up @@ -655,11 +700,12 @@ SM.extend({
border.position = 1;
}

const s = MSSharedStyle.alloc().initWithName_firstInstance(name, style);
const s = MSSharedStyle.alloc().initWithName_style(name, style);
sharedStyles.addSharedObject(s);
}

return (style.newInstance)? style.newInstance(): style;
var style = this.find({key: "(name != NULL) && (name == %@)", match: name}, sharedStyles);
return style;
},
sharedTextStyle: function(name, color, alignment){
var sharedStyles = this.document.documentData().layerTextStyles(),
Expand All @@ -679,12 +725,16 @@ SM.extend({
text.setTextAlignment(alignment);

style = text.style();
const s = MSSharedStyle.alloc().initWithName_firstInstance(name, style);

const s = MSSharedStyle.alloc().initWithName_style(name, style);

sharedStyles.addSharedObject(s);
this.removeLayer(text);
}

return (style.newInstance)? style.newInstance(): style;
var style = this.find({key: "(name != NULL) && (name == %@)", match: name}, sharedStyles);
return style;

}
});

Expand All @@ -704,10 +754,9 @@ SM.extend({
placement = options.placement,
shapeTemp = this.addShape();

if(styles){
shapeTemp.setStyle(styles.layer);
}
else{
if(styles){
shapeTemp.setSharedStyle(styles.layer);
} else {
shapeTemp.style().addStylePartOfType(0);
}

Expand Down Expand Up @@ -816,10 +865,9 @@ SM.extend({
textTemp = this.addText();

if(styles){
shapeTemp.setStyle(styles.layer);
textTemp.setStyle(styles.text);
}
else{
shapeTemp.setSharedStyle(styles.layer);
textTemp.setSharedStyle(styles.text);
} else {
shape.style().addStylePartOfType(0);
}

Expand Down Expand Up @@ -903,13 +951,11 @@ SM.extend({
boxRect.setWidth(textRect.width + 8);
boxRect.setHeight(textRect.height + 8);

arrow.setRotation(45);
arrow.flatten();
arrowRect.setWidth(8);
arrowRect.setHeight(8);

arrowRect.setX(arrowX);
arrowRect.setY(arrowY);
arrow.setRotation(45);

return {
element: box,
Expand Down Expand Up @@ -1366,7 +1412,7 @@ SM.extend({
temp.setStringValue(text);
temp.setTextBehaviour(1);
temp.setTextBehaviour(0);
temp.setStyle(styles.text);
temp.setSharedStyle(styles.text);

var tempRect = this.getRect(temp),
ruler = this.setRuler({
Expand Down Expand Up @@ -1512,7 +1558,6 @@ SM.extend({
}
});


SM.extend({
overlay: function(target){
var targetRect = this.getRect(target),
Expand All @@ -1530,7 +1575,7 @@ SM.extend({

container.addLayers([overlay]);

overlay.setStyle(overlayStyle);
overlay.setSharedStyle(overlayStyle);
overlay.setName("overlay");
overlayRect.setX(targetRect.x);
overlayRect.setY(targetRect.y);
Expand Down Expand Up @@ -1617,7 +1662,7 @@ SM.extend({
content.push("opacity: " + Math.round( targetStyle.contextSettings().opacity() * 100) + "%");
break;
case "radius":
if(self.is(target, MSShapeGroup) && self.is(target.layers().firstObject(), MSRectangleShape)){
if((self.is(target, MSShapeGroup) && self.is(target.layers().firstObject(), MSRectangleShape)) || self.is(target, MSRectangleShape)){
content.push("radius: " + self.convertUnit( self.getRadius(target) ) );
}
break;
Expand Down Expand Up @@ -1657,8 +1702,8 @@ SM.extend({
content.push("style-name: " + styleName);
}
break;
case "layer-name":
content.push("layer-name: " + target.name());
case "layer-name":
content.push("layer-name: " + target.name());
break;
default:
render = false;
Expand Down Expand Up @@ -1957,8 +2002,8 @@ SM.extend({
text.setStringValue(target.stringValue());
text.setTextBehaviour(1);
text.setTextBehaviour(0);
note.setStyle(noteStyle.layer);
text.setStyle(noteStyle.text);
note.setSharedStyle(noteStyle.layer);
text.setSharedStyle(noteStyle.text);

var noteRect = this.getRect(note),
textRect = this.getRect(text),
Expand Down Expand Up @@ -2353,6 +2398,12 @@ SM.extend({
isExportable: function(layer) {
return this.is(layer, MSTextLayer) ||
this.is(layer, MSShapeGroup) ||
this.is(layer, MSRectangleShape) ||
this.is(layer, MSOvalShape) ||
this.is(layer, MSShapePathLayer) ||
this.is(layer, MSTriangleShape) ||
this.is(layer, MSStarShape) ||
this.is(layer, MSPolygonShape) ||
this.is(layer, MSBitmapLayer) ||
this.is(layer, MSSliceLayer) ||
this.is(layer, MSSymbolInstance) ||
Expand Down Expand Up @@ -3065,7 +3116,9 @@ SM.extend({
layerStates = this.getStates(layer);

if(layer && this.is(layer, MSLayerGroup) && /NOTE\#/.exec(layer.name())){
var textLayer = layer.children()[3];
for (var i = 0; i < layer.children().count(); i++) {
if(this.is(layer.children()[i], MSTextLayer)) var textLayer = layer.children()[i];
}

data.notes.push({
rect: this.rectToJSON(textLayer.absoluteRect(), artboardRect),
Expand All @@ -3080,7 +3133,8 @@ SM.extend({
( layerStates.isLocked && !this.is(layer, MSSliceLayer) ) ||
layerStates.isEmpty ||
layerStates.hasSlice ||
layerStates.isMeasure
layerStates.isMeasure ||
layer.isMasked()
){
return this;
}
Expand Down Expand Up @@ -3144,12 +3198,18 @@ SM.extend({

var layerCSSAttributes = layer.CSSAttributes(),
css = [];

for(var i = 0; i < layerCSSAttributes.count(); i++) {
var c = layerCSSAttributes[i]
if(! /\/\*/.exec(c) ) css.push(this.toJSString(c));
}
if(css.length > 0) layerData.css = css;

if(css.length > 0 || layer.CSSAttributes().length > 0) {
layerData.css = css;
if(this.is(layer, MSRectangleShape) && !!layer.cornerRadiusString() && layer.cornerRadiusString() != 0 && !/border-radius/.exec(layer.CSSAttributes())){
layerData.css.push('border-radius: ' + layer.cornerRadiusString().replace(/;/g,'px ') + 'px;');
}
}

this.getMask(group, layer, layerData, layerStates);
this.getSlice(layer, layerData, symbolLayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
"identifier": "com.utom.measure",
"appcast": "https://raw.githubusercontent.com/utom/sketch-measure/master/appcast.xml",
"homepage": "http://utom.design/measure/",
"version": "2.7.5",
"version": "2.7.6",
"description" : "Make it a fun to create spec for developers and teammates",
"authorEmail" : "utombox@gmail.com",
"name" : "Sketch Measure"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
"identifier": "com.utom.measure",
"appcast": "https://raw.githubusercontent.com/utom/sketch-measure/master/appcast.xml",
"homepage": "http://utom.design/measure/",
"version": "2.7.5",
"version": "2.7.6",
"description" : "Make it a fun to create spec for developers and teammates",
"authorEmail" : "utombox@gmail.com",
"name" : "Sketch Measure"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
"identifier": "com.utom.measure",
"appcast": "https://raw.githubusercontent.com/utom/sketch-measure/master/appcast.xml",
"homepage": "http://utom.design/measure/",
"version": "2.7.5",
"version": "2.7.6",
"description" : "Make it a fun to create spec for developers and teammates",
"authorEmail" : "utombox@gmail.com",
"name" : "Sketch Measure"
Expand Down
Loading

0 comments on commit cbde414

Please sign in to comment.