diff --git a/Sketch Measure.sketchplugin/Contents/Sketch/library/common.js b/Sketch Measure.sketchplugin/Contents/Sketch/library/common.js index 35f9113..db5b0ec 100755 --- a/Sketch Measure.sketchplugin/Contents/Sketch/library/common.js +++ b/Sketch Measure.sketchplugin/Contents/Sketch/library/common.js @@ -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(); @@ -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 = [], @@ -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(); @@ -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(); @@ -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(), @@ -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; + } }); @@ -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); } @@ -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); } @@ -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, @@ -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({ @@ -1512,7 +1558,6 @@ SM.extend({ } }); - SM.extend({ overlay: function(target){ var targetRect = this.getRect(target), @@ -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); @@ -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; @@ -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; @@ -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), @@ -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) || @@ -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), @@ -3080,7 +3133,8 @@ SM.extend({ ( layerStates.isLocked && !this.is(layer, MSSliceLayer) ) || layerStates.isEmpty || layerStates.hasSlice || - layerStates.isMeasure + layerStates.isMeasure || + layer.isMasked() ){ return this; } @@ -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); diff --git a/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-en.json b/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-en.json index dd97dd6..35ef517 100755 --- a/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-en.json +++ b/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-en.json @@ -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" diff --git a/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-zh-Hans.json b/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-zh-Hans.json index 7d70b34..a063287 100755 --- a/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-zh-Hans.json +++ b/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-zh-Hans.json @@ -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" diff --git a/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-zh-Hant.json b/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-zh-Hant.json index 9e041ae..9d3b94b 100755 --- a/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-zh-Hant.json +++ b/Sketch Measure.sketchplugin/Contents/Sketch/library/i18n/manifest-zh-Hant.json @@ -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" diff --git a/Sketch Measure.sketchplugin/Contents/Sketch/library/template.html b/Sketch Measure.sketchplugin/Contents/Sketch/library/template.html index 72e68a5..a2c746e 100755 --- a/Sketch Measure.sketchplugin/Contents/Sketch/library/template.html +++ b/Sketch Measure.sketchplugin/Contents/Sketch/library/template.html @@ -232,13 +232,35 @@ return (Math.round( size / size2 * 1000 ) / 10) + "%"; }, unitSize: function(length, isText){ - var length = Math.round( length / this.configs.scale * 100 ) / 100, - units = this.configs.unit.split("/"), - unit = units[0]; - if( units.length > 1 && isText){ - unit = units[1]; + if(Array.isArray(length)){ + + var units = this.configs.unit.split("/"), + unit = units[0]; + + if( units.length > 1 && isText){ + unit = units[1]; + } + + var scale = this.configs.scale; + var tempLegth = []; + + length.forEach(function(element) { + tempLegth.push(Math.round( element / scale * 10 ) / 10); + }); + + return tempLegth.join(unit + ' ') + unit; + + } else { + + 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; } - return length + unit; + }, scaleSize: function (length){ return Math.round( length / this.configs.scale * 10 ) / 10; @@ -886,7 +908,6 @@ radius = (layerData.radius)? [ '