Visual Studio Code不但跨平台,还有良好的扩展性。我们可以在
Visual Studio Marketplace
上找到各种各样的插件。这里分享下怎样制作一个简单的用于代码填充的插件。
自定义代码片段
键盘快捷键
Ctr+Shift+P
搜索关键字snippet:
选择HTML:
这个时候在
C:\Users\<user name>\AppData\Roaming\Code\User\snippets
下会自动创建一个
html.json
的模版文件:
现在可以在里面写一点东西了。这里是Dynamic Web TWAIN的代码:
{
"include": {
"prefix": "dwt include",
"body": [
"<script type=\"text/javascript\" src=\"https://www.dynamsoft.com/Demo/DWT/Resources/dynamsoft.webtwain.min.js\"> </script>"
],
"description": "Include Dynamic Web TWAIN JavaScript library."
},
"scan module": {
"prefix": "dwt scan module",
"body": [
"<input type=\"button\" value=\"Scan\" onclick=\"AcquireImage();\" />",
"<div id=\"dwtcontrolContainer\"></div>\n",
"<script type=\"text/javascript\">",
"function AcquireImage() {",
"\tvar DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');",
"\tDWObject.IfDisableSourceAfterAcquire = true;",
"\tvar bSelected = DWObject.SelectSource(); \n",
"\tif(bSelected) {",
"\t\tvar OnAcquireImageSuccess, OnAcquireImageFailure;",
"\t\tOnAcquireImageSuccess = OnAcquireImageFailure = function () {",
"\t\tDWObject.CloseSource();",
"\t};\n",
"\tDWObject.OpenSource();",
"\tDWObject.AcquireImage(OnAcquireImageSuccess, OnAcquireImageFailure); ",
"\t}",
"}",
"</script>"
],
"description": "A simple web scanning module."
},
"full sample": {
"prefix": "dwt full sample",
"body": [
"<!DOCTYPE html>\n<head>\n\t<title>Hello World</title>",
"\t<script type=\"text/javascript\" src=\"https://www.dynamsoft.com/Demo/DWT/Resources/dynamsoft.webtwain.min.js\"> </script>\n</head>\n\n<body>",
"\t<input type=\"button\" value=\"Scan\" onclick=\"AcquireImage();\" />",
"\t<div id=\"dwtcontrolContainer\"></div>\n",
"\t<script type=\"text/javascript\">",
"\tfunction AcquireImage() {",
"\t\tvar DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');",
"\t\tDWObject.IfDisableSourceAfterAcquire = true;",
"\t\tvar bSelected = DWObject.SelectSource(); \n",
"\t\tif(bSelected) {",
"\t\t\tvar OnAcquireImageSuccess, OnAcquireImageFailure;",
"\t\t\tOnAcquireImageSuccess = OnAcquireImageFailure = function () {",
"\t\t\tDWObject.CloseSource();",
"\t\t};\n",
"\t\tDWObject.OpenSource();",
"\t\tDWObject.AcquireImage(OnAcquireImageSuccess, OnAcquireImageFailure); ",
"\t\t}",
"\t}",
"\t</script>\n</body>\n</html>"
],
"description": "The full sample code - hello world."
}
}
现在创建一个新的html文件。在里面输入前缀
dwt
就会出现代码提示:
插件制作
接下来要做的事,就是把这个html.json文件放到插件里面。官方推荐使用
Yeoman
:
这里有几个选项,看起来我们应该选择最后一项。选择最后一项会提示你选择一个包含.tmSnippets或者 .sublime-snippets文件的目录。那没有的话怎么办呢?可以不需要yo,手动创建目录结构:
这里总共有3个文件:插件logo,代码片段,配置文件。要做的事情就是编写一下配置文件:
插件打包
接下来用vsce来打包:
这个时候会生成一个.visx的文件。我们可以直接安装:
插件被安装在
C:\Users\<user name>\.vscode\extensions\Dynamsoft.dwt-0.0.4
。安装之后可以在VS Code中测试下是否起作用。
发布插件
登录
Visual Studio Team Services
。选择account > Security > Add来创建
Personal Access Token
。
创建一个新的发布者:
使用刚才创建的Token来发布插件:
安装插件
这里是我做好的插件:
https://marketplace.visualstudio.com/items?itemName=Dynamsoft.dwt
VS Code中使用快捷键Ctrl+P安装插件:
安装之后重启一下VS Code就可以用了。
参考资料
源码
https://github.com/dynamsoft-dwt/vscode-extension