VsCode插件開(kāi)發(fā)之插件初步通信的方法步驟
參考了Egret Wing,想像Egret Wing那樣在上方titlebar最右邊上面增加一個(gè)menu(這個(gè)menu相對(duì)于一個(gè)按鈕,當(dāng)點(diǎn)擊這個(gè)按鈕時(shí)會(huì)出現(xiàn)一個(gè)window彈框,這個(gè)window彈框里就包含相關(guān)的表單信息以供登錄或者注冊(cè)使用。我是以這個(gè)作為參考模板的。但是目前進(jìn)展并不是很順。于是我通過(guò)插件的方式暫時(shí)性解決了這個(gè)問(wèn)題。但是覺(jué)得還不是想要的那樣。
Egret Wing是這樣的,如圖所示:
不得不承認(rèn)一點(diǎn)Egret Wing改造的挺不錯(cuò)的,不愧是對(duì)VsCode進(jìn)行魔改。
今天先說(shuō)一下通過(guò)插件通信。
我主要參考的是一個(gè)叫小茗同學(xué)的插件開(kāi)發(fā),并改造其插件來(lái)達(dá)到我的目的。
這個(gè)小茗同學(xué)我覺(jué)得他寫(xiě)的插件開(kāi)發(fā),我覺(jué)得不是特別詳細(xì)全面,當(dāng)然,參考意義還是有的。
他的插件開(kāi)發(fā)目錄如下:
他的插件github地址為:https://github.com/sxei/vscode-plugin-demo.git
他的插件可以在VsCode插件擴(kuò)展中搜到(搜到后安裝,然后直接在下載成功的插件的基礎(chǔ)上改造),例如:
那么說(shuō)說(shuō)我是如何改造它的呢?
我主要改造它這么幾個(gè)地方?
一個(gè)是圖標(biāo),另外一個(gè)修改它的html界面(主要是修改custom-welcome.html),同時(shí)我要和還改了package.json文件。
插件開(kāi)發(fā)可以用TypeScript,也可以用JavaScript。
如果是用TypeScript的話(huà),通常擴(kuò)展腳本文件是extension.ts形式存在,如果是JavaScript,則是以extension.js的形式存在。
在此我想強(qiáng)調(diào)的是改他人插件或者自己編寫(xiě)插件,以ts為例,主要把握也就兩個(gè)文件,一個(gè)是extension.ts,另一個(gè)就是package.json。
如何從0開(kāi)發(fā)以插件的相關(guān)視頻,感興趣的可以看看,感覺(jué)還是有一定的啟發(fā)的:https://v.qq.com/x/page/k08220bdz3s.htmlb
我改造后的插件代碼,放在我的個(gè)人github上,大家可以將其下載下來(lái)放入,如下兩個(gè)文件中(任意一個(gè)都行):
注意:
.vscode文件夾:官方插件下載好默認(rèn)放入的目錄。
.vscode-oss-dev:下載源碼,自己編譯,下載插件放置的目錄。
自己編譯的不知道由于什么原因不能直接聯(lián)網(wǎng)通信搜索一些應(yīng)用市場(chǎng)下載的插件。
通常情況下(以.vscode-oss-dev為例),git clone下來(lái)我的插件地址,然后將其移植到這個(gè)目錄就能看到對(duì)應(yīng)的效果,效果圖如下:
我的VsCode插件地址為:https://github.com/youcong1996/study_simple_demo/tree/vscode-plugin-communication
將其克隆下來(lái)放入.vscode或者.vscode-oss-dev中的extensions目錄下即可起作用。
另外有一點(diǎn)要強(qiáng)調(diào)的是,如果是vscode非自己編譯的,需要重啟一下vscode,如果是自己編譯的話(huà),監(jiān)聽(tīng)需要暫時(shí)中斷重新輸入(yarn watch)。
接下來(lái)說(shuō)說(shuō)我修改的三個(gè)地方。
1.修改package.json(包含圖標(biāo)一起說(shuō)了及其點(diǎn)擊登錄的同時(shí)展示對(duì)應(yīng)的左側(cè)欄sidebar)
{
"name": "vscode-plugin-demo",
"displayName": "vscode-plugin-demo",
"description": "VSCode插件demo",
"keywords": [
"vscode",
"plugin",
"demo"
],
"version": "1.0.3",
"publisher": "sxei",
"engines": {
"vscode": "^1.27.0"
},
"categories": [
"Other"
],
"icon": "images/icon.png",
"activationEvents": [
"*"
],
"main": "./src/extension",
"contributes": {
"configuration": {
"type": "object",
"title": "Code插件demo",
"properties": {
"vscodePluginDemo.yourName": {
"type": "string",
"default": "guest",
"description": "你的名字"
},
"vscodePluginDemo.showTip": {
"type": "boolean",
"default": true,
"description": "啟動(dòng)時(shí)顯示自定義歡迎頁(yè)"
}
}
},
"commands": [
{
"command": "extension.sayHello",
"title": "Hello,小茗同學(xué)"
},
{
"command": "extension.demo.getCurrentFilePath",
"title": "獲取當(dāng)前文件(夾)路徑"
},
{
"command": "extension.demo.testMenuShow",
"title": "這個(gè)菜單僅在JS文件中出現(xiàn)",
"icon": {
"light": "./images/tool-light.svg",
"dark": "./images/tool-light.svg"
}
},
{
"command": "extension.demo.openWebview",
"title": "打開(kāi)WebView"
},
{
"command": "extension.demo.showWelcome",
"title": "顯示自定義歡迎頁(yè)"
}
],
"keybindings": [
{
"command": "extension.sayHello",
"key": "ctrl+f10",
"mac": "cmd+f10",
"when": "editorTextFocus"
},
{
"command": "extension.demo.openWebview",
"key": "ctrl+f9",
"mac": "cmd+f9",
"when": "editorTextFocus"
}
],
"menus": {
"editor/context": [
{
"when": "editorFocus",
"command": "extension.sayHello",
"group": "navigation@6"
},
{
"when": "editorFocus",
"command": "extension.demo.getCurrentFilePath",
"group": "navigation@5"
},
{
"when": "editorFocus && resourceLangId == javascript",
"command": "extension.demo.testMenuShow",
"group": "z_commands"
},
{
"command": "extension.demo.openWebview",
"group": "navigation"
}
],
"editor/title": [
{
"when": "editorFocus && resourceLangId == javascript",
"command": "extension.demo.testMenuShow",
"group": "navigation"
}
],
"editor/title/context": [
{
"when": "resourceLangId == javascript",
"command": "extension.demo.testMenuShow",
"group": "navigation"
}
],
"explorer/context": [
{
"command": "extension.demo.getCurrentFilePath",
"group": "navigation"
},
{
"command": "extension.demo.openWebview",
"group": "navigation"
}
]
},
"snippets": [
{
"language": "javascript",
"path": "./snippets/javascript.json"
},
{
"language": "html",
"path": "./snippets/html.json"
}
],
"viewsContainers": {
"activitybar": [
{
"id": "beautifulGirl",
"title": "測(cè)試",
"icon": "images/beautifulGirl.svg"
}
]
},
"views": {
"beautifulGirl": [
{
"id": "測(cè)試001",
"name": "test"
},
{
"id": "測(cè)試002",
"name": "test"
},
{
"id": "測(cè)試003",
"name": "test"
}
]
},
"iconThemes": [
{
"id": "testIconTheme",
"label": "測(cè)試圖標(biāo)主題",
"path": "./theme/icon-theme.json"
}
]
},
"scripts": {
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"typescript": "^2.6.1",
"vscode": "^1.1.6",
"eslint": "^4.11.0",
"@types/node": "^7.0.43",
"@types/mocha": "^2.2.42"
},
"license": "SEE LICENSE IN LICENSE.txt",
"bugs": {
"url": "https://github.com/sxei/vscode-plugin-demo/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/sxei/vscode-plugin-demo"
},
"homepage": "https://github.com/sxei/vscode-plugin-demo/blob/master/README.md",
"__metadata": {
"id": "ac2b7b16-d87f-4e51-87a8-37011e8aa713",
"publisherId": "cdd0fc1d-3acf-4250-a09b-95545e29bdbc",
"publisherDisplayName": "小茗同學(xué)"
}
}
在package.json我也就修改了這么幾個(gè)地方,一個(gè)是views(這個(gè)view通常主要用于展示左側(cè)的sidebar視圖),一個(gè)是viewsContainers(我主要修改beautifulGirl.svg)。
修改后的效果分別如下所示:
2.通信(修改custom-welcome.html)
通信我目前采用最原始的javascript的ajax請(qǐng)求,其實(shí)jQuery及其vue.js的異步通信也是可以的。
這個(gè)custom-welcome.html你可以理解成它就是一個(gè)webview。
custom-welcome.html文件內(nèi)容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自定義歡迎頁(yè)</title>
<link rel="stylesheet" href="../../lib/bootstrap-3.3.1/css/bootstrap.min.css" rel="external nofollow" >
<link rel="stylesheet" href="../../lib/layui/css/layui.css" rel="external nofollow" >
<style>
html, body, #app {
height: 100%;
}
::-webkit-scrollbar {
width: 10px;
height: 10px
}
::-webkit-scrollbar-track {
border-radius: 10px;
background-color: #d8dce5
}
::-webkit-scrollbar-thumb {
border-radius: 5px;
background-color: #adadad
}
::-webkit-scrollbar-thumb:hover {
background-color: #929292
}
::-webkit-scrollbar-thumb:active {
background-color: #666363
}
::-webkit-scrollbar-corner {
background-color: #535353
}
::-webkit-scrollbar-resizer {
background-color: #ff6e00
}
.page-title {
margin-bottom: 20px;
}
.control-label {
font-weight: normal;
}
.btn-primary {
background-color: #1890ff;
border-color: #1890ff;
outline: none;
}
.btn-primary:focus,
.btn-primary:hover {
background-color: #40a9ff;
border-color: #40a9ff;
outline: none;
}
.btn-primary.active,
.btn-primary:active {
background-color: #096dd9;
border-color: #096dd9;
color: #fff;
outline: none;
}
</style>
</head>
<body>
<div id="app" class="container-fluid">
<h3 class="page-title">自定義歡迎頁(yè)</h3>
<p class="alert alert-success" style="width: 300px;">{{userName}},{{time}}好!
<span id="info"></span>
</p>
<form class="form-horizontal">
<div class="form-group">
<div class="col-sm-6">
<div id="form">
<form>
<p>用戶(hù)名:<input type="text" id="userName" style="color:black;"/></p>
<p>密 碼 :<input type="password" id="password" style="color:black;"/></p>
<p> <input type="button" style="color:black;" value="提交" onclick="test()"/>
<input type="button" value="打開(kāi)" onclick="openLogin()"/>
</form>
</div>
<div class="checkbox">
<label>
<input type="checkbox" v-model="show"> 啟動(dòng)時(shí)顯示自定義歡迎頁(yè)
<input type="button" onclick="register()" value="退出"/>
</label>
</div>
</div>
</div>
</form>
</div>
<script src="../../lib/jquery/jquery.min.js"></script>
<script src="../../lib/bootstrap-3.3.1/js/bootstrap.min.js"></script>
<script src="../../lib/vue-2.5.17/vue.js"></script>
<script src="../../src/view/custom-welcome.js"></script>
<script src="../../lib/layui/layui.js"></script>
<script src="../../lib/layer/layer-v3.1.1/layer/mobile/layer.js"></script>
<script>
function openLogin(){
layui.use('layer', function(){
var layer = layui.layer;
layer.open({
type: 2,
title: 'Login',
fix: false,
maxmin: true,
shadeClose: true,
shade: 0.8,
area: ['500px', '500px'],
content: 'login.html',
end: function () {
location.reload();
}
});
});
}
function test(){
var userName = document.getElementById("userName").value;
if(userName != null && userName != undefined){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
$("#info").html("登錄成功");
$("#form").hide();
console.log('test:'+xhr.status);
console.log(xhr.responseText);
} else {
console.log("請(qǐng)求成功:" + xhr.status);
}
}
};
xhr.open("POST", "http://www.test.com/test-web/sysUser/getUserCodeByInfo?userCode=2", true);
xhr.send(null);
}else{
layui.use('layer', function(){
var layer = layui.layer;
layer.msg('userName為必填項(xiàng)');
});
}
}
function register(){
$("#info").html("");
$("#form").show();
}
</script>
</body>
</html>
這個(gè)html在瀏覽器上看到的效果如下所示:
目前這僅僅是一個(gè)很初級(jí)的(蹩腳通信),后續(xù)我將會(huì)繼續(xù)補(bǔ)充對(duì)VsCode的源碼解析及其插件開(kāi)發(fā)相關(guān)的詳細(xì)說(shuō)明,由于目前掌握的比較分散不夠系統(tǒng),暫時(shí)延后講解。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:如何給asp.net core寫(xiě)個(gè)簡(jiǎn)單的健康檢查
欄 目:ASP.NET
下一篇:vscode extension插件開(kāi)發(fā)詳解
本文標(biāo)題:VsCode插件開(kāi)發(fā)之插件初步通信的方法步驟
本文地址:http://www.jygsgssxh.com/a1/ASP_NET/10935.html
您可能感興趣的文章
- 01-11vscode extension插件開(kāi)發(fā)詳解
- 01-11.NET開(kāi)發(fā)人員關(guān)于ML.NET的入門(mén)學(xué)習(xí)
- 01-11VsCode之使用WebView通信詳解
- 01-11VS2015+Qt5+OpenCV3開(kāi)發(fā)環(huán)境配置
- 01-11vs2015中mysql.h文件打不開(kāi)的解決辦法
- 01-11.Net微信網(wǎng)頁(yè)開(kāi)發(fā)解決用戶(hù)在不同公眾號(hào)或在公眾號(hào)、移動(dòng)應(yīng)用之
- 01-11.NET 開(kāi)發(fā)環(huán)境搭建圖文詳解
- 01-11ASP.NET MVC 開(kāi)發(fā)微信支付H5的實(shí)現(xiàn)示例(外置瀏覽器支付)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11vscode extension插件開(kāi)發(fā)詳解
- 01-11VsCode插件開(kāi)發(fā)之插件初步通信的方法
- 01-11如何給asp.net core寫(xiě)個(gè)簡(jiǎn)單的健康檢查
- 01-11.net core高吞吐遠(yuǎn)程方法如何調(diào)用組件
- 01-11淺析.Net Core中Json配置的自動(dòng)更新
- 01-11.NET開(kāi)發(fā)人員關(guān)于ML.NET的入門(mén)學(xué)習(xí)
- 01-11.NET Core 遷移躺坑記續(xù)集之Win下莫名其
- 01-11.net core webapi jwt 更為清爽的認(rèn)證詳解
- 01-11docker部署Asp.net core應(yīng)用的完整步驟
- 01-11ASP.NET Core靜態(tài)文件的使用方法
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文


