Skip to content

Commit

Permalink
传一个测试功能,symbol代码复用
Browse files Browse the repository at this point in the history
  • Loading branch information
bugluo committed Oct 10, 2017
1 parent 7d5df57 commit f2abb00
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 1 deletion.
18 changes: 18 additions & 0 deletions WeSketch.sketchplugin/Contents/Sketch/i18n/manifest-enUS.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,22 @@
"istool":"true",
"handler" : "onRun"
},
{
"name" : "Get Symbol Code",
"identifier" : "getSymbolCode",
"script" : "symbolCode.js",
"shortcut": "command shift y",
"istool":"",
"handler" : "getSymbolCode"
},
{
"name" : "Set Symbol Code",
"identifier" : "setSymbolCode",
"script" : "symbolCode.js",
"shortcut": "command shift u",
"istool":"",
"handler" : "setSymbolCode"
},
{
"name" : " └─ Settings",
"identifier" : "codeSetting",
Expand Down Expand Up @@ -387,6 +403,8 @@
"codeColor",
"exportSlice",
"codeStyle",
"setSymbolCode",
"getSymbolCode",
"codeSetting",
"-",
"toolbar",
Expand Down
18 changes: 18 additions & 0 deletions WeSketch.sketchplugin/Contents/Sketch/i18n/manifest-zhCN.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,22 @@
"istool":"true",
"handler" : "onRun"
},
{
"name" : "获取代码片段",
"identifier" : "getSymbolCode",
"script" : "symbolCode.js",
"shortcut": "command shift y",
"istool":"",
"handler" : "getSymbolCode"
},
{
"name" : "设置代码片段",
"identifier" : "setSymbolCode",
"script" : "symbolCode.js",
"shortcut": "command shift u",
"istool":"",
"handler" : "setSymbolCode"
},
{
"name" : " └─ 设置",
"identifier" : "codeSetting",
Expand Down Expand Up @@ -387,6 +403,8 @@
"codeColor",
"exportSlice",
"codeStyle",
"setSymbolCode",
"getSymbolCode",
"codeSetting",
"-",
"toolbar",
Expand Down
79 changes: 79 additions & 0 deletions WeSketch.sketchplugin/Contents/Sketch/library/panel/setCode.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript" src="js/jquery.js"></script>
<link href="css/normalize.css" rel="stylesheet">
<style type="text/css">
body,
html {
height: 100%;
background-color: #ececec;
font-size: 12px;
}

.wrap {
padding: 10px 15px 20px 15px;
}

.savebutton {
color: white;
background: #3485f0;
border: 1px solid #237fff;
border-radius: 4px;
padding: 0 15px;
margin-right: 35px;
float: right;
cursor: pointer;
}

.textarea{
width: 100%;
resize:none;
height:330px;
}

.savebutton:active {
background: linear-gradient(0deg, #4c98fe 0%, #0466e3 100%);
}

.buttonwrap {
left: 0;
bottom: 0;
padding: 10px 0;
width: 100%;
border-top: 1px solid #bebebe;
background: #f1f1f1;
position: fixed;
box-sizing: border-box;
}

</style>
</head>

<body>
<div class="wrap">
<textarea name="" class="textarea" id="textarea" ></textarea>
<div class="buttonwrap">
<div class="savebutton">确定</div>
</div>
</div>


<script type="text/javascript">

var init = function (data) {
$('.savebutton').on('click',function(){
var obj = {data:$('#textarea').val()};
SMAction('submit', obj);
SMAction('close');
})
}
</script>
</body>

</html>
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@
}
})
}
init();
</script>
</body>

Expand Down
18 changes: 18 additions & 0 deletions WeSketch.sketchplugin/Contents/Sketch/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,22 @@
"istool":"true",
"handler" : "onRun"
},
{
"name" : "Get Symbol Code",
"identifier" : "getSymbolCode",
"script" : "symbolCode.js",
"shortcut": "command shift y",
"istool":"",
"handler" : "getSymbolCode"
},
{
"name" : "Set Symbol Code",
"identifier" : "setSymbolCode",
"script" : "symbolCode.js",
"shortcut": "command shift u",
"istool":"",
"handler" : "setSymbolCode"
},
{
"name" : " └─ Settings",
"identifier" : "codeSetting",
Expand Down Expand Up @@ -388,6 +404,8 @@
"codeColor",
"exportSlice",
"codeStyle",
"setSymbolCode",
"getSymbolCode",
"codeSetting",
"-",
"toolbar",
Expand Down
58 changes: 58 additions & 0 deletions WeSketch.sketchplugin/Contents/Sketch/symbolCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@import "common.js"

var symbolCodeKey = "com.sketchplugins.wechat.symbolCode";

function getSymbolCode(context) {
if(context.selection.length == 0){
return;
}
var selection = context.selection[0];
if(selection.className() == 'MSSymbolInstance'){
var code = decodeURIComponent(encodeURIComponent(context.command.valueForKey_onLayer_forPluginIdentifier("symbolKey", context.selection[0].symbolMaster(), symbolCodeKey)));
for (var key in selection.overrides()) {
for(var i = 0;i<selection.symbolMaster().children().length;i++){
if(key == selection.symbolMaster().children()[i].objectID()){
code = code.replace(selection.symbolMaster().children()[i].stringValue(),selection.overrides()[key]);
break;
}
}
}
}
paste(code);
context.document.showMessage('代码已经放入剪贴板');
var ga = new Analytics(context);
if (ga) ga.sendEvent('code', 'get');
}

function setSymbolCode(context) {
if(context.selection[0].className() == 'MSSymbolMaster'){
var pluginSketch = context.plugin.url().URLByAppendingPathComponent("Contents").URLByAppendingPathComponent("Sketch").URLByAppendingPathComponent("library").path();
SMPanel({
url: pluginSketch + "/panel/setCode.html",
width: 400,
height: 400,
data: {

},
hiddenClose: false,
floatWindow: false,
identifier: "setCode",
callback: function (data) {
context.command.setValue_forKey_onLayer_forPluginIdentifier(data.data, "symbolKey", context.selection[0], symbolCodeKey);
context.document.showMessage('设置成功');
},
closeCallback: function () {

}
});
var ga = new Analytics(context);
if (ga) ga.sendEvent('code', 'set');
}


}


var onRun = function (context) {
setSymbolCode(context);
}

0 comments on commit f2abb00

Please sign in to comment.