闲来无事,将以前写的一个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