Skip to content

Commit 814672a

Browse files
committed
code translated to es6
1 parent 950b2cd commit 814672a

File tree

6 files changed

+105
-132
lines changed

6 files changed

+105
-132
lines changed

README.md

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# qiniu-upload-image
22

3-
一个VS Code插件,写Markdown时可以快捷上传本地图片获取七牛图床外链
3+
一个 VS Code 插件,写 Markdown 时可以快捷上传本地图片获取七牛图床外链
44

55
## Features
66

77
![priview](https://raw.githubusercontent.com/yscoder/vscode-qiniu-upload-image/master/features/preview.gif)
88

9-
> Tip: 只有在编辑Markdown时插件才可使用,启动快捷键 `Ctrl+Q`
9+
> Tips: 只有在编辑 Markdown 时插件才可使用,启动快捷键 `Ctrl+Q`
1010
1111
## Install
1212

1313
`Ctrl+P` 输入命令:
1414

15-
```
15+
```bash
1616
ext install qiniu-upload-image
1717
```
1818

@@ -32,8 +32,10 @@ ext install qiniu-upload-image
3232
// 七牛图片上传空间
3333
"qiniu.bucket": "ysblog",
3434

35-
// 七牛图片上传路径,参数化命名,暂时支持 ${fileName}、${date}、${dateTime}
36-
// 如:${fileName}-${date} 生成格式为 picName-20160725.jpg
35+
// 七牛图片上传路径,参数化命名,暂时支持 ${fileName}、${mdFileName}、${date}、${dateTime}
36+
// 示例:
37+
// ${fileName}-${date} -> picName-20160725.jpg
38+
// ${mdFileName}-${dateTime} -> markdownName-20170412222810.jpg
3739
"qiniu.remotePath": "${fileName}",
3840

3941
// 七牛图床域名
@@ -45,11 +47,4 @@ ext install qiniu-upload-image
4547

4648
[https://github.com/yscoder/vscode-qiniu-upload-image](https://github.com/yscoder/vscode-qiniu-upload-image)
4749

48-
49-
50-
## For more information
51-
52-
* [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown)
53-
* [Markdown Syntax Reference](https://help.github.com/articles/markdown-basics/)
54-
5550
**Enjoy!**

extension.js

+31-53
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,47 @@
1-
// The module 'vscode' contains the VS Code extensibility API
2-
// Import the module and reference it with the alias vscode in your code below
3-
var vscode = require('vscode');
4-
var path = require('path');
5-
var qnUpload = require('./lib/upload');
1+
const { window, commands, workspace } = require('vscode')
2+
const path = require('path')
3+
const qnUpload = require('./lib/upload')
64

75
// this method is called when your extension is activated
8-
// your extension is activated the very first time the command is executed
9-
function activate(context) {
6+
exports.activate = context => {
107

11-
// Use the console to output diagnostic information (console.log) and errors (console.error)
12-
// This line of code will only be executed once when your extension is activated
13-
//console.log('Congratulations, your extension "qiniu-upload-image" is now active!');
14-
var window = vscode.window;
15-
var config = vscode.workspace.getConfiguration('qiniu');
16-
8+
console.log('qiniu-upload-image is active!')
179

18-
if(!config.enable) return;
10+
const config = workspace.getConfiguration('qiniu')
1911

20-
21-
// The command has been defined in the package.json file
22-
// Now provide the implementation of the command with registerCommand
23-
// The commandId parameter must match the command field in package.json
24-
var disposable = vscode.commands.registerCommand('extension.qiniu.upload', function () {
25-
// The code you place here will be executed every time your command is executed
26-
var editor = window.activeTextEditor;
27-
var mdFilePath = editor.document.fileName;
28-
var mdFileName = path.basename(mdFilePath, path.extname(mdFilePath));
12+
if (!config.enable) return
13+
14+
const disposable = commands.registerCommand('extension.qiniu.upload', () => {
15+
16+
const editor = window.activeTextEditor
17+
const mdFilePath = editor.document.fileName
18+
const mdFileName = path.basename(mdFilePath, path.extname(mdFilePath))
2919

3020
if (!editor) {
31-
window.showErrorMessage("没有打开编辑窗口");
32-
return;
21+
window.showErrorMessage('没有打开编辑窗口')
22+
return
3323
}
3424

35-
// Display a message box to the user
36-
//vscode.window.showInformationMessage('插件就绪!');
3725
window.showInputBox({
3826
placeHolder: '输入一个本地图片地址'
39-
}).then(function(path){
40-
41-
return qnUpload(config, path, mdFileName);
42-
43-
}, function(err){
44-
45-
window.showErrorMessage(err);
46-
47-
}).then(function(ret){
48-
49-
var img = '!['+ ret.name +']('+ ret.url +')';
50-
editor.edit(function(textEditorEdit) {
51-
textEditorEdit.insert(editor.selection.active, img);
27+
}).then(path => qnUpload(config, path, mdFileName)
28+
, err => {
29+
window.showErrorMessage(err)
30+
}
31+
).then(({ name, url }) => {
32+
console.log('Upload success!')
33+
34+
const img = `![${name}](${url})`
35+
editor.edit(textEditorEdit => {
36+
textEditorEdit.insert(editor.selection.active, img)
37+
})
38+
}, err => {
39+
window.showErrorMessage(err)
5240
})
41+
})
5342

54-
}, function(err){
55-
56-
window.showErrorMessage(err);
57-
58-
});
59-
60-
});
61-
62-
context.subscriptions.push(disposable);
43+
context.subscriptions.push(disposable)
6344
}
64-
exports.activate = activate;
6545

6646
// this method is called when your extension is deactivated
67-
function deactivate() {
68-
}
69-
exports.deactivate = deactivate;
47+
exports.deactivate = () => { }

jsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
4-
"target": "ES5",
3+
"module": "es2015",
4+
"target": "ES6",
55
"noLib": true
66
},
77
"exclude": [
88
"node_modules"
99
]
10-
}
10+
}

lib/upload.js

+53-55
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,82 @@
1-
var qiniu = require('qiniu');
2-
var path = require('path');
3-
var url = require('url');
4-
var paramExp = /\${(\w+)}/g;
1+
const qiniu = require('qiniu')
2+
const path = require('path')
3+
const url = require('url')
4+
5+
const PutPolicy = qiniu.rs.PutPolicy
6+
const PutExtra = qiniu.io.PutExtra
57

68
// 上传策略函数
7-
function uptoken(bucket, key) {
8-
var putPolicy = new qiniu.rs.PutPolicy(bucket + ':' + key);
9-
return putPolicy.token();
10-
}
9+
const uptoken = (bucket, key) => new PutPolicy(`${bucket}:${key}`).token()
1110

1211
// 默认参数
13-
function fileParam(file, mdFile){
14-
var dt = new Date();
15-
var y = dt.getFullYear();
16-
var m = dt.getMonth() + 1;
17-
var d = dt.getDate();
18-
var h = dt.getHours();
19-
var mm = dt.getMinutes();
20-
var s = dt.getSeconds();
21-
var ap = Array.prototype;
22-
23-
var date = y + '' + m + '' + d;
24-
var ext = path.extname(file);
12+
const formatParam = (file, mdFileName) => {
13+
const dt = new Date()
14+
const y = dt.getFullYear()
15+
const m = dt.getMonth() + 1
16+
const d = dt.getDate()
17+
const h = dt.getHours()
18+
const mm = dt.getMinutes()
19+
const s = dt.getSeconds()
20+
21+
const date = `${y}${m}${d}`
22+
var ext = path.extname(file)
2523

2624
return {
27-
date: date,
28-
dateTime: date + '' + h + '' + mm + '' + s,
25+
date,
26+
dateTime: `${date}${h}${mm}${s}`,
2927
fileName: path.basename(file, ext),
30-
ext: ext,
31-
mdFileName: mdFile
28+
ext,
29+
mdFileName
3230
}
3331
}
3432

35-
module.exports = function(config, file, mdFile) {
36-
qiniu.conf.ACCESS_KEY = config.access_key;
37-
qiniu.conf.SECRET_KEY = config.secret_key;
33+
const formatString = (tplString, data) => {
34+
const keys = Object.keys(data)
35+
const values = keys.map(k => data[k])
3836

39-
//要上传的空间
40-
var bucket = config.bucket;
37+
return new Function(keys.join(','), 'return `' + tplString + '`').apply(null, values)
38+
}
4139

42-
var domain = config.domain;
40+
module.exports = ({
41+
access_key,
42+
secret_key,
43+
bucket,
44+
domain,
45+
remotePath }, file, mdFile) => {
4346

44-
if (/^".+"$/.test(file)) {
45-
file = file.substring(1, file.length - 1);
46-
}
47-
//要上传文件的本地路径
48-
var localFile = file;
49-
50-
var param = fileParam(file, mdFile);
51-
52-
//上传到七牛后保存的文件名
53-
var remotePath = (config.remotePath + '${ext}').replace(paramExp, function(exp, prop){
54-
return param[prop] || '';
55-
});
47+
qiniu.conf.ACCESS_KEY = access_key
48+
qiniu.conf.SECRET_KEY = secret_key
5649

50+
let localFile = file
51+
if (/^".+"$/.test(localFile)) {
52+
localFile = file.substring(1, file.length - 1)
53+
}
5754

55+
// 预设参数值
56+
const param = formatParam(localFile, mdFile)
57+
//上传到七牛后保存的文件名
58+
const saveFile = formatString(remotePath + '${ext}', param)
5859
//生成上传 Token
59-
var token = uptoken(bucket, remotePath);
60+
const token = uptoken(bucket, saveFile)
6061

61-
return new Promise(function(resolve, reject) {
62+
return new Promise((resolve, reject) => {
6263

63-
var extra = new qiniu.io.PutExtra();
64+
const extra = new PutExtra()
6465

65-
qiniu.io.putFile(token, remotePath, localFile, extra, function(err, ret) {
66+
qiniu.io.putFile(token, saveFile, localFile, extra, (err, { key }) => {
6667

67-
if(!err) {
68+
if (!err) {
6869
// 上传成功, 处理返回值
6970
resolve({
70-
name: path.basename(ret.key, param.ext),
71-
url: url.resolve(domain, remotePath)
72-
});
73-
71+
name: path.basename(key, param.ext),
72+
url: url.resolve(domain, saveFile)
73+
})
7474
} else {
7575
// 上传失败, 处理返回代码
76-
reject(err);
76+
reject(err)
7777
}
78-
79-
});
78+
})
8079
})
81-
8280
}
8381

8482

package.json

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "qiniu-upload-image",
33
"displayName": "qiniu-upload-image",
44
"description": "Picture upload generate markdown link format.",
5-
"version": "0.0.7",
5+
"version": "0.1.0",
66
"publisher": "imys",
77
"engines": {
88
"vscode": "^1.0.0"
@@ -15,12 +15,14 @@
1515
],
1616
"main": "./extension",
1717
"contributes": {
18-
"keybindings": [{
19-
"command": "extension.qiniu.upload",
20-
"key": "ctrl+q",
21-
"mac": "ctrl+q",
22-
"when": "editorTextFocus && editorLangId == 'markdown'"
23-
}],
18+
"keybindings": [
19+
{
20+
"command": "extension.qiniu.upload",
21+
"key": "ctrl+q",
22+
"mac": "ctrl+q",
23+
"when": "editorTextFocus && editorLangId == 'markdown'"
24+
}
25+
],
2426
"configuration": {
2527
"type": "object",
2628
"title": "qiniu configuration",
@@ -62,7 +64,7 @@
6264
"postinstall": "node ./node_modules/vscode/bin/install"
6365
},
6466
"devDependencies": {
65-
"vscode": "^0.11.0"
67+
"vscode": "^1.1.0"
6668
},
6769
"dependencies": {
6870
"qiniu": "^6.1.11"
@@ -72,4 +74,4 @@
7274
"type": "git",
7375
"url": "git+https://github.com/yscoder/vscode-qiniu-upload-image.git"
7476
}
75-
}
77+
}
Binary file not shown.

0 commit comments

Comments
 (0)