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