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