|
| 1 | +import { Uri, workspace, window, MessageOptions, MessageItem, ProgressLocation, Range } from 'vscode'; |
| 2 | +import { MarkdownImage, MarkdownImagesExtractor } from '../services/images-extractor.service'; |
| 3 | + |
| 4 | +export const extractImages = async ( |
| 5 | + arg: unknown, |
| 6 | + inputImageType: MarkdownImagesExtractor['imageType'] | null | undefined |
| 7 | +) => { |
| 8 | + if (arg instanceof Uri && arg.scheme === 'file') { |
| 9 | + const markdown = new TextDecoder().decode(await workspace.fs.readFile(arg)); |
| 10 | + const extractor = new MarkdownImagesExtractor(markdown, arg); |
| 11 | + const images = extractor.findImages(); |
| 12 | + const availableWebImagesCount = images.filter(extractor.createImageTypeFilter('web')).length; |
| 13 | + const availableLocalImagesCount = images.filter(extractor.createImageTypeFilter('local')).length; |
| 14 | + if (images.length <= 0) { |
| 15 | + await window.showWarningMessage('没有可以提取的图片'); |
| 16 | + return; |
| 17 | + } |
| 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); |
| 25 | + result = result |
| 26 | + ? result |
| 27 | + : await window.showInformationMessage<typeof messageItems extends [...infer U] ? U[0] : never>( |
| 28 | + '请选择要提取哪些图片? 注意! 此操作会替换源文件中的图片链接!', |
| 29 | + { |
| 30 | + modal: true, |
| 31 | + detail: |
| 32 | + `共找到 ${availableWebImagesCount} 张可以提取的网络图片\n` + |
| 33 | + `${availableLocalImagesCount} 张可以提取的本地图片`, |
| 34 | + } as MessageOptions, |
| 35 | + ...messageItems |
| 36 | + ); |
| 37 | + const editor = window.visibleTextEditors.find(x => x.document.fileName === arg.fsPath); |
| 38 | + |
| 39 | + if (result && result.imageType && editor) { |
| 40 | + extractor.imageType = result.imageType; |
| 41 | + if (extractor.findImages().length <= 0) { |
| 42 | + await window.showWarningMessage('没有可以提取的图片'); |
| 43 | + return; |
| 44 | + } |
| 45 | + const document = editor.document; |
| 46 | + await document.save(); |
| 47 | + const failedImages = await window.withProgress( |
| 48 | + { title: '提取图片', location: ProgressLocation.Notification }, |
| 49 | + async progress => { |
| 50 | + extractor.onProgress = (idx, images) => { |
| 51 | + const total = images.length; |
| 52 | + const image = images[idx]; |
| 53 | + progress.report({ |
| 54 | + increment: (idx / total) * 80, |
| 55 | + message: `[${idx + 1} / ${total}] 正在提取 ${image.symbol}`, |
| 56 | + }); |
| 57 | + }; |
| 58 | + const extractResults = await extractor.extract(); |
| 59 | + let idx = 0; |
| 60 | + const total = extractResults.length; |
| 61 | + await editor.edit(editBuilder => { |
| 62 | + for (let [range, , extractedImage] of extractResults |
| 63 | + .filter((x): x is [source: MarkdownImage, result: MarkdownImage] => x[1] != null) |
| 64 | + .map( |
| 65 | + ([sourceImage, result]): [ |
| 66 | + range: Range | null, |
| 67 | + sourceImage: MarkdownImage, |
| 68 | + extractedImage: MarkdownImage |
| 69 | + ] => { |
| 70 | + if (sourceImage.index == null) { |
| 71 | + return [null, sourceImage, result]; |
| 72 | + } |
| 73 | + |
| 74 | + const endPos = document.positionAt( |
| 75 | + sourceImage.index + sourceImage.symbol.length - 1 |
| 76 | + ); |
| 77 | + return [ |
| 78 | + new Range( |
| 79 | + document.positionAt(sourceImage.index), |
| 80 | + endPos.with({ character: endPos.character + 1 }) |
| 81 | + ), |
| 82 | + sourceImage, |
| 83 | + result, |
| 84 | + ]; |
| 85 | + } |
| 86 | + )) { |
| 87 | + if (range == null) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + progress.report({ |
| 91 | + increment: (idx / total) * 20 + 80, |
| 92 | + message: `[${idx + 1} / ${total}] 执行替换 ${extractedImage.symbol}`, |
| 93 | + }); |
| 94 | + |
| 95 | + editBuilder.replace(range, extractedImage.symbol); |
| 96 | + } |
| 97 | + }); |
| 98 | + await document.save(); |
| 99 | + return extractResults.filter(x => x[1] === null).map(x => x[0]); |
| 100 | + } |
| 101 | + ); |
| 102 | + if (failedImages && failedImages.length > 0) { |
| 103 | + await window.showErrorMessage( |
| 104 | + `${failedImages.length}张图片提取失败\n${failedImages |
| 105 | + .map(x => [x.symbol, extractor.errors.find(y => y[0] === x.symbol)?.[1] ?? ''].join(': ')) |
| 106 | + .join('\n')}` |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +}; |
0 commit comments