1- import { Uri , workspace , window , MessageOptions , MessageItem , ProgressLocation , Range } from 'vscode' ;
1+ import { Uri , workspace , window , MessageOptions , MessageItem , ProgressLocation , Range , WorkspaceEdit } from 'vscode' ;
22import { ImageInformation , MarkdownImagesExtractor } from '../services/images-extractor.service' ;
33
44type ExtractOption = MessageItem & Partial < Pick < MarkdownImagesExtractor , 'imageType' > > ;
@@ -38,13 +38,13 @@ export const extractImages = async (
3838 ...extractOptions
3939 ) ;
4040 const editor = window . visibleTextEditors . find ( x => x . document . fileName === arg . fsPath ) ;
41+ const textDocument = editor ?. document ?? workspace . textDocuments . find ( x => x . fileName === arg . fsPath ) ;
4142
42- if ( result && result . imageType && editor ) {
43- extractor . imageType = result . imageType ;
43+ if ( result && result . imageType && textDocument ) {
4444 if ( extractor . findImages ( ) . length <= 0 ) return warnNoImages ( ) ;
45+ extractor . imageType = result . imageType ;
4546
46- const document = editor . document ;
47- await document . save ( ) ;
47+ await textDocument . save ( ) ;
4848 const failedImages = await window . withProgress (
4949 { title : '提取图片' , location : ProgressLocation . Notification } ,
5050 async progress => {
@@ -59,8 +59,9 @@ export const extractImages = async (
5959 const extractResults = await extractor . extract ( ) ;
6060 const idx = 0 ;
6161 const total = extractResults . length ;
62- await editor . edit ( editBuilder => {
63- for ( const [ range , , extractedImage ] of extractResults
62+
63+ await workspace . applyEdit (
64+ extractResults
6465 . filter ( ( x ) : x is [ source : ImageInformation , result : ImageInformation ] => x [ 1 ] != null )
6566 . map (
6667 ( [ sourceImage , result ] ) : [
@@ -70,30 +71,36 @@ export const extractImages = async (
7071 ] => {
7172 if ( sourceImage . index == null ) return [ null , sourceImage , result ] ;
7273
73- const endPos = document . positionAt (
74+ const endPos = textDocument . positionAt (
7475 sourceImage . index + sourceImage . symbol . length - 1
7576 ) ;
7677 return [
7778 new Range (
78- document . positionAt ( sourceImage . index ) ,
79+ textDocument . positionAt ( sourceImage . index ) ,
7980 endPos . with ( { character : endPos . character + 1 } )
8081 ) ,
8182 sourceImage ,
8283 result ,
8384 ] ;
8485 }
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+ }
8798
88- progress . report ( {
89- increment : ( idx / total ) * 20 + 80 ,
90- message : `[${ idx + 1 } / ${ total } ] 执行替换 ${ extractedImage . symbol } ` ,
91- } ) ;
99+ return workspaceEdit ;
100+ } , new WorkspaceEdit ( ) )
101+ ) ;
92102
93- editBuilder . replace ( range , extractedImage . symbol ) ;
94- }
95- } ) ;
96- await document . save ( ) ;
103+ await textDocument . save ( ) ;
97104 return extractResults . filter ( x => x [ 1 ] === null ) . map ( x => x [ 0 ] ) ;
98105 }
99106 ) ;
0 commit comments