閒來無事,將以前寫的一個vscode插件翻出來寫個教程。此插件以前沒有上傳到vscode的插件庫上,這才順帶整理一下發布流程。css
很簡單就是,快速的在編輯器裏面插入css代碼,不須要本身手工去ps量尺寸,或者看圖片的尺寸啥的了。估計有大佬說着有啥用了,對於小小的只寫scss的小菜鳥來講仍是挺有必要的,能夠節約幾秒的。 這個功能,貌似less是有這個功能,以下:git
.test{
width:image-width("file.png");
height: image-height("file.png");
}
複製代碼
若是你是less愛好者能夠不用往下看啦,然而對於我只習慣寫sass來講貌似沒這個函數,或者有我不知道。因此我把幾個輪子合到了一塊兒,作了一個輪子搬運工。附上git地址 插件地址github
import {
workspace,
} from 'vscode';
/**
* 獲取配置
*/
export const getConfig = (str: string): any => {
return workspace.getConfiguration('imgstyle').get(str);
};
<!--對呀在vscode setting.json裏面 imgstyle.tpl,imgstyle.path-->
//獲取path
getConfig('path');
複製代碼
//上面獲取到的path 是一個過濾條件 默認是數組["src/**/*.{png,jpg,gif,webp}"] 根據我的喜愛設置,具體過濾條件能夠看globby的配置項
await globby("src/**/*.{png,jpg,gif,webp}")
//這樣就獲取到src下面全部圖片的path數組了
複製代碼
import {
window,
} from 'vscode';
// 轉換爲pick配置項
const quickPickArray = imgsArray.map((v: string) => {
return {
label: path.parse(v).base, //選擇項標籤
description: path.relative(workspace.rootPath || __dirname, v),//選擇項描述
src: v,
};
});
// 打開vscode的選擇器
const action = await window.showQuickPick(quickPickArray);
// 選擇完成後action就是一個object對象,主要用到src
複製代碼
const jimp = require('jimp');
// 讀取圖片
const imgInfo = await jimp.read(action.src);
// console.log('imgInfo', imgInfo.bitmap.width);
const {
width,
height
} = imgInfo.bitmap;
複製代碼
"imgstyle.tpl": "width: ${width}px;height: ${height}px;background-image:url(${src});",
。在這個";"是有用換行替換方便css對齊的,因此目前仍是隻在樣式裏支持而已。具體能夠看源碼,這裏很少說具體import * as _ from 'lodash';
const fn = _.template("width: ${width}px;height: ${height}px;background-image:url(${src});");
<!--轉換成字符串-->
fn({
width: 10,
height: 10,
src: 'xxx'
});
複製代碼
const {
activeTextEditor //當前編輯頁
} = window;
activeTextEditor.edit((editBuilder) => {
//獲取光標位置
var position = new Position(activeTextEditor.selection.active.line, activeTextEditor.selection.active.character);
//在光標位置插入字符串
editBuilder.insert(position, 'width:...');
});
複製代碼
// 註冊命令
let disposable = vscode.commands.registerCommand('linz.imgInsert', () => {
// The code you place here will be executed every time your command is executed
imgInsert();
// Display a message box to the user
// vscode.window.showInformationMessage('hellow');
});
複製代碼
"activationEvents": [
"onCommand:linz.imgInsert"
],
"contributes": {
"commands": [
{
"command": "linz.imgInsert",
"title": "image insert"
}
],
},
複製代碼
"keybindings": [{
"command": "linz.imgInsert",
"key": "ctrl+4",
"mac": "cmd+4",
"when": "editorTextFocus" // 編輯文本時能夠調用命令
}],
複製代碼
"configuration": {
"type": "object",
"title": "imgstyle configuration",
"properties": {
"imgstyle.tpl": {
"type": "string",
"default": "test",
"description": "imgstyle path settings"
},
"imgstyle.path": {
"type": "Array",
"default": [
"src/**/*.{png,jpg,gif,webp}"
],
"description": "imgstyle path settings"
}
}
}
複製代碼
npm install -g vsce
此包用於打包併發布vsce create-publisher (publisher name)
<!--會要求輸入用戶名 郵箱 touken-->
複製代碼
vsce publish
而後就能夠靜靜的發呆等發佈完成了,小夥伴們最好開發插件的使用用npm install
插件哦 ,不要用yarn
否則沒法編譯發佈哦,這vsce貌似還沒支持yarn;web