@@ -2,6 +2,28 @@ const { window, commands, workspace } = require('vscode')
22const path = require ( 'path' )
33const qnUpload = require ( './lib/upload' )
44
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+
527// this method is called when your extension is activated
628exports . activate = context => {
729
@@ -11,36 +33,32 @@ exports.activate = context => {
1133
1234 if ( ! config . enable ) return
1335
14- const disposable = commands . registerCommand ( 'extension.qiniu.upload' , ( ) => {
36+ const inputUpload = commands . registerCommand ( 'extension.qiniu.upload' , ( ) => {
1537
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 ) {
2139 window . showErrorMessage ( '没有打开编辑窗口' )
2240 return
2341 }
2442
2543 window . showInputBox ( {
2644 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 )
3056 }
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 )
4158 } )
4259
43- context . subscriptions . push ( disposable )
60+ context . subscriptions . push ( inputUpload )
61+ context . subscriptions . push ( selectUpload )
4462}
4563
4664// this method is called when your extension is deactivated
0 commit comments