-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathextension.js
65 lines (49 loc) · 1.81 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { window, commands, workspace } = require('vscode')
const path = require('path')
const qnUpload = require('./lib/upload')
const upload = (config, fsPath) => {
if(!fsPath) return
const editor = window.activeTextEditor
const mdFilePath = editor.document.fileName
const mdFileName = path.win32.basename(mdFilePath, path.extname(mdFilePath))
return qnUpload(config, fsPath, mdFileName).then(({ name, url }) => {
console.log('Upload success!')
const img = ``
editor.edit(textEditorEdit => {
textEditorEdit.insert(editor.selection.active, img)
})
})
}
const error = err => {
window.showErrorMessage(err)
}
// this method is called when your extension is activated
exports.activate = context => {
console.log('qiniu-upload-image is active!')
const config = workspace.getConfiguration('qiniu')
if (!config.enable) return
const inputUpload = commands.registerCommand('extension.qiniu.upload', () => {
if (!window.activeTextEditor) {
window.showErrorMessage('没有打开编辑窗口')
return
}
window.showInputBox({
placeHolder: '输入一个图片地址'
})
.then(fsPath => upload(config, fsPath), error)
})
const selectUpload = commands.registerCommand('extension.qiniu.select', () => {
window.showOpenDialog({
filters: { 'Images': ['png', 'jpg', 'gif', 'bmp'] }
}).then(result => {
if (result) {
const { fsPath } = result[0]
return upload(config, fsPath)
}
}, error)
})
context.subscriptions.push(inputUpload)
context.subscriptions.push(selectUpload)
}
// this method is called when your extension is deactivated
exports.deactivate = () => { }