1
1
import { MessageItem , MessageOptions , ProgressLocation , Range , Uri , window , workspace , WorkspaceEdit } from 'vscode'
2
- import { ImageSrc , MarkdownImagesExtractor , ImageInfo , newImageSrcFilter } from '@/services/images -extractor.service'
2
+ import { ImageInfo , ImageSrc , MkdImgExtractor , newImageSrcFilter } from '@/services/mkd-img -extractor.service'
3
3
4
4
type ExtractOption = MessageItem & Partial < { imageSrc : ImageSrc } >
5
- const extractOptions : readonly ExtractOption [ ] = [
6
- { title : '提取本地图片' , imageSrc : ImageSrc . local } ,
7
- { title : '提取网络图片' , imageSrc : ImageSrc . web } ,
8
- { title : '提取全部' , imageSrc : ImageSrc . any } ,
9
- { title : '取消' , imageSrc : undefined , isCloseAffordance : true } ,
10
- ]
11
-
12
- export async function extractImages ( arg : unknown , inputImageSrc : ImageSrc | undefined ) {
5
+
6
+ export async function extractImages ( arg : unknown , inputImageSrc ?: ImageSrc ) {
13
7
if ( ! ( arg instanceof Uri && arg . scheme === 'file' ) ) return
14
8
15
9
const editor = window . visibleTextEditors . find ( x => x . document . fileName === arg . fsPath )
@@ -19,40 +13,60 @@ export async function extractImages(arg: unknown, inputImageSrc: ImageSrc | unde
19
13
await textDocument . save ( )
20
14
21
15
const markdown = ( await workspace . fs . readFile ( arg ) ) . toString ( )
22
- const extractor = new MarkdownImagesExtractor ( markdown , arg )
16
+ const extractor = new MkdImgExtractor ( markdown , arg )
23
17
24
18
const images = extractor . findImages ( )
25
- if ( images . length <= 0 )
26
- void ( ! inputImageSrc != null ? window . showWarningMessage ( '没有找到可以提取的图片' ) : undefined )
27
-
28
- const availableWebImagesCount = images . filter ( newImageSrcFilter ( ImageSrc . web ) ) . length
29
- const availableLocalImagesCount = images . filter ( newImageSrcFilter ( ImageSrc . local ) ) . length
30
- const result =
31
- extractOptions . find ( x => inputImageSrc != null && x . imageSrc === inputImageSrc ) ??
32
- ( await window . showInformationMessage < ExtractOption > (
19
+ if ( images . length <= 0 ) {
20
+ void ( ! inputImageSrc !== undefined ? window . showWarningMessage ( '没有找到可以提取的图片' ) : undefined )
21
+ return
22
+ }
23
+
24
+ const getExtractOption = ( ) => {
25
+ const webImgCount = images . filter ( newImageSrcFilter ( ImageSrc . web ) ) . length
26
+ const dataUrlImgCount = images . filter ( newImageSrcFilter ( ImageSrc . dataUrl ) ) . length
27
+ const fsImgCount = images . filter ( newImageSrcFilter ( ImageSrc . fs ) ) . length
28
+
29
+ const displayOptions : ExtractOption [ ] = [
30
+ { title : '提取全部' , imageSrc : ImageSrc . any } ,
31
+ { title : '提取网络图片' , imageSrc : ImageSrc . web } ,
32
+ { title : '提取 Data Url 图片' , imageSrc : ImageSrc . dataUrl } ,
33
+ { title : '提取本地图片' , imageSrc : ImageSrc . fs } ,
34
+ { title : '取消' , imageSrc : undefined , isCloseAffordance : true } ,
35
+ ]
36
+
37
+ if ( inputImageSrc !== undefined )
38
+ return Promise . resolve ( displayOptions . find ( ent => ent . imageSrc === inputImageSrc ) )
39
+
40
+ // if src is not specified:
41
+ return window . showInformationMessage < ExtractOption > (
33
42
'要提取哪些图片? 此操作会替换源文件中的图片链接!' ,
34
43
{
35
44
modal : true ,
36
45
detail :
37
- `共找到 ${ availableWebImagesCount } 张可以提取的网络图片\n` +
38
- `${ availableLocalImagesCount } 张可以提取的本地图片` ,
46
+ '共找到:\n' +
47
+ `${ webImgCount } 张可以提取的网络图片\n` +
48
+ `${ dataUrlImgCount } 张可以提取的 Data Url 图片\n` +
49
+ `${ fsImgCount } 张可以提取的本地图片` ,
39
50
} as MessageOptions ,
40
- ...extractOptions
41
- ) )
51
+ ...displayOptions
52
+ )
53
+ }
54
+
55
+ const extractImageSrc = ( await getExtractOption ( ) ) ?. imageSrc
42
56
43
- if ( ! ( result && result . imageSrc !== undefined ) ) return
57
+ if ( extractImageSrc === undefined ) return
44
58
45
- extractor . imageSrc = result . imageSrc
59
+ extractor . imageSrc = extractImageSrc
46
60
47
61
const failedImages = await window . withProgress (
48
- { title : '提取图片 ' , location : ProgressLocation . Notification } ,
62
+ { title : '正在提取图片 ' , location : ProgressLocation . Notification } ,
49
63
async progress => {
50
- extractor . onProgress = ( idx , images ) => {
51
- const total = images . length
52
- const image = images [ idx ]
64
+ extractor . onProgress = ( count , info ) => {
65
+ const total = info . length
66
+ const image = info [ count ]
53
67
progress . report ( {
54
- increment : ( idx / total ) * 80 ,
55
- message : `[${ idx + 1 } / ${ total } ] 正在提取 ${ image . link } ` ,
68
+ increment : ( count / total ) * 80 ,
69
+ message : `[${ count + 1 } / ${ total } ] 正在提取 ${ image . data } ` ,
56
70
} )
57
71
}
58
72
@@ -65,25 +79,26 @@ export async function extractImages(arg: unknown, inputImageSrc: ImageSrc | unde
65
79
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
66
80
. map ( ( [ src , dst ] ) => [ src , dst ! ] )
67
81
. map ( ( [ src , dst ] ) => {
68
- const startPos = textDocument . positionAt ( src . startOffset )
69
- const endPos = textDocument . positionAt (
70
- src . startOffset + src . prefix . length + src . link . length + src . postfix . length
82
+ const posL = textDocument . positionAt ( src . startOffset )
83
+ const posR = textDocument . positionAt (
84
+ src . startOffset + src . prefix . length + src . data . length + src . postfix . length
71
85
)
72
- const range = new Range ( startPos , endPos )
86
+ const range = new Range ( posL , posR )
73
87
88
+ // just for ts type inferring
74
89
const ret : [ Range , ImageInfo ] = [ range , dst ]
75
90
return ret
76
91
} )
77
92
. reduce ( ( we , [ range , dst ] ) => {
78
93
if ( range ) {
79
94
progress . report ( {
80
95
increment : ( idx / extractedLen ) * 20 + 80 ,
81
- message : `[${ idx + 1 } / ${ extractedLen } ] 正在替换图片链接 ${ dst . link } ` ,
96
+ message : `[${ idx + 1 } / ${ extractedLen } ] 正在替换图片链接 ${ dst . data } ` ,
82
97
} )
83
- const newText = dst . prefix + dst . link + dst . postfix
98
+ const newText = dst . prefix + dst . data + dst . postfix
84
99
we . replace ( textDocument . uri , range , newText , {
85
100
needsConfirmation : false ,
86
- label : dst . link ,
101
+ label : dst . data ,
87
102
} )
88
103
}
89
104
@@ -96,10 +111,10 @@ export async function extractImages(arg: unknown, inputImageSrc: ImageSrc | unde
96
111
}
97
112
)
98
113
99
- if ( failedImages && failedImages . length > 0 ) {
114
+ if ( failedImages . length > 0 ) {
100
115
const info = failedImages
101
- . map ( x => [ x . link , extractor . errors . find ( ( [ link ] ) => link === x . link ) ?. [ 1 ] ?? '' ] . join ( ',' ) )
116
+ . map ( info => [ info . data , extractor . errors . find ( ( [ link ] ) => link === info . data ) ?. [ 1 ] ?? '' ] . join ( ',' ) )
102
117
. join ( '\n' )
103
- window . showErrorMessage ( `${ failedImages . length } 张图片提取失败: ${ info } ` ) . then ( undefined , console . warn )
118
+ window . showErrorMessage ( `${ failedImages . length } 张图片提取失败: ${ info } ` ) . then ( undefined , console . warn )
104
119
}
105
120
}
0 commit comments