1
- import { Uri , workspace , window , MessageOptions , MessageItem , ProgressLocation , Range } from 'vscode' ;
1
+ import { Uri , workspace , window , MessageOptions , MessageItem , ProgressLocation , Range , WorkspaceEdit } from 'vscode' ;
2
2
import { ImageInformation , MarkdownImagesExtractor } from '../services/images-extractor.service' ;
3
3
4
4
type ExtractOption = MessageItem & Partial < Pick < MarkdownImagesExtractor , 'imageType' > > ;
@@ -38,13 +38,13 @@ export const extractImages = async (
38
38
...extractOptions
39
39
) ;
40
40
const editor = window . visibleTextEditors . find ( x => x . document . fileName === arg . fsPath ) ;
41
+ const textDocument = editor ?. document ?? workspace . textDocuments . find ( x => x . fileName === arg . fsPath ) ;
41
42
42
- if ( result && result . imageType && editor ) {
43
- extractor . imageType = result . imageType ;
43
+ if ( result && result . imageType && textDocument ) {
44
44
if ( extractor . findImages ( ) . length <= 0 ) return warnNoImages ( ) ;
45
+ extractor . imageType = result . imageType ;
45
46
46
- const document = editor . document ;
47
- await document . save ( ) ;
47
+ await textDocument . save ( ) ;
48
48
const failedImages = await window . withProgress (
49
49
{ title : '提取图片' , location : ProgressLocation . Notification } ,
50
50
async progress => {
@@ -59,8 +59,9 @@ export const extractImages = async (
59
59
const extractResults = await extractor . extract ( ) ;
60
60
const idx = 0 ;
61
61
const total = extractResults . length ;
62
- await editor . edit ( editBuilder => {
63
- for ( const [ range , , extractedImage ] of extractResults
62
+
63
+ await workspace . applyEdit (
64
+ extractResults
64
65
. filter ( ( x ) : x is [ source : ImageInformation , result : ImageInformation ] => x [ 1 ] != null )
65
66
. map (
66
67
( [ sourceImage , result ] ) : [
@@ -70,30 +71,36 @@ export const extractImages = async (
70
71
] => {
71
72
if ( sourceImage . index == null ) return [ null , sourceImage , result ] ;
72
73
73
- const endPos = document . positionAt (
74
+ const endPos = textDocument . positionAt (
74
75
sourceImage . index + sourceImage . symbol . length - 1
75
76
) ;
76
77
return [
77
78
new Range (
78
- document . positionAt ( sourceImage . index ) ,
79
+ textDocument . positionAt ( sourceImage . index ) ,
79
80
endPos . with ( { character : endPos . character + 1 } )
80
81
) ,
81
82
sourceImage ,
82
83
result ,
83
84
] ;
84
85
}
85
- ) ) {
86
- if ( range == null ) continue ;
86
+ )
87
+ . reduce ( ( workspaceEdit , [ range , , extractedImage ] ) => {
88
+ if ( range ) {
89
+ progress . report ( {
90
+ increment : ( idx / total ) * 20 + 80 ,
91
+ message : `[${ idx + 1 } / ${ total } ] 正在替换图片链接 ${ extractedImage . symbol } ` ,
92
+ } ) ;
93
+ workspaceEdit . replace ( textDocument . uri , range , extractedImage . symbol , {
94
+ needsConfirmation : false ,
95
+ label : extractedImage . symbol ,
96
+ } ) ;
97
+ }
87
98
88
- progress . report ( {
89
- increment : ( idx / total ) * 20 + 80 ,
90
- message : `[${ idx + 1 } / ${ total } ] 执行替换 ${ extractedImage . symbol } ` ,
91
- } ) ;
99
+ return workspaceEdit ;
100
+ } , new WorkspaceEdit ( ) )
101
+ ) ;
92
102
93
- editBuilder . replace ( range , extractedImage . symbol ) ;
94
- }
95
- } ) ;
96
- await document . save ( ) ;
103
+ await textDocument . save ( ) ;
97
104
return extractResults . filter ( x => x [ 1 ] === null ) . map ( x => x [ 0 ] ) ;
98
105
}
99
106
) ;
0 commit comments