|
1 | 1 | import { Uri, workspace, window, MessageOptions, MessageItem, ProgressLocation, Range } from 'vscode';
|
2 | 2 | import { MarkdownImage, MarkdownImagesExtractor } from '../services/images-extractor.service';
|
3 | 3 |
|
| 4 | +type ExtractOption = MessageItem & Partial<Pick<MarkdownImagesExtractor, 'imageType'>>; |
| 5 | +const extractOptions: readonly ExtractOption[] = [ |
| 6 | + { title: '提取本地图片', imageType: 'local' }, |
| 7 | + { title: '提取网络图片', imageType: 'web' }, |
| 8 | + { title: '提取全部', imageType: 'all' }, |
| 9 | + { title: '取消', imageType: undefined, isCloseAffordance: true }, |
| 10 | +]; |
| 11 | + |
4 | 12 | export const extractImages = async (
|
5 | 13 | arg: unknown,
|
6 | 14 | inputImageType: MarkdownImagesExtractor['imageType'] | null | undefined
|
7 |
| -) => { |
| 15 | +): Promise<void> => { |
8 | 16 | if (arg instanceof Uri && arg.scheme === 'file') {
|
| 17 | + const ignoreWarnings = inputImageType != null; |
9 | 18 | const markdown = new TextDecoder().decode(await workspace.fs.readFile(arg));
|
10 | 19 | const extractor = new MarkdownImagesExtractor(markdown, arg);
|
11 | 20 | const images = extractor.findImages();
|
12 | 21 | const availableWebImagesCount = images.filter(extractor.createImageTypeFilter('web')).length;
|
13 | 22 | const availableLocalImagesCount = images.filter(extractor.createImageTypeFilter('local')).length;
|
| 23 | + const warnNoImages = (): void => void (ignoreWarnings ? null : window.showWarningMessage('没有可以提取的图片')); |
14 | 24 | if (images.length <= 0) {
|
15 |
| - await window.showWarningMessage('没有可以提取的图片'); |
16 |
| - return; |
| 25 | + return warnNoImages(); |
17 | 26 | }
|
18 |
| - const messageItems: (MessageItem & Partial<Pick<MarkdownImagesExtractor, 'imageType'>>)[] = [ |
19 |
| - { title: '提取本地图片', imageType: 'local' }, |
20 |
| - { title: '提取网络图片', imageType: 'web' }, |
21 |
| - { title: '提取全部', imageType: 'all' }, |
22 |
| - { title: '取消', imageType: undefined, isCloseAffordance: true }, |
23 |
| - ]; |
24 |
| - let result = messageItems.find(x => inputImageType != null && x.imageType === inputImageType); |
| 27 | + |
| 28 | + let result = extractOptions.find(x => inputImageType != null && x.imageType === inputImageType); |
25 | 29 | result = result
|
26 | 30 | ? result
|
27 |
| - : await window.showInformationMessage<typeof messageItems extends [...infer U] ? U[0] : never>( |
| 31 | + : await window.showInformationMessage<ExtractOption>( |
28 | 32 | '请选择要提取哪些图片? 注意! 此操作会替换源文件中的图片链接!',
|
29 | 33 | {
|
30 | 34 | modal: true,
|
31 | 35 | detail:
|
32 | 36 | `共找到 ${availableWebImagesCount} 张可以提取的网络图片\n` +
|
33 | 37 | `${availableLocalImagesCount} 张可以提取的本地图片`,
|
34 | 38 | } as MessageOptions,
|
35 |
| - ...messageItems |
| 39 | + ...extractOptions |
36 | 40 | );
|
37 | 41 | const editor = window.visibleTextEditors.find(x => x.document.fileName === arg.fsPath);
|
38 | 42 |
|
39 | 43 | if (result && result.imageType && editor) {
|
40 | 44 | extractor.imageType = result.imageType;
|
41 | 45 | if (extractor.findImages().length <= 0) {
|
42 |
| - await window.showWarningMessage('没有可以提取的图片'); |
43 |
| - return; |
| 46 | + return warnNoImages(); |
44 | 47 | }
|
45 | 48 | const document = editor.document;
|
46 | 49 | await document.save();
|
|
0 commit comments