Skip to content

Commit 041557e

Browse files
authored
chore(extract-images): improve compatibility with some custom editor (#144)
1 parent 6ea3749 commit 041557e

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

src/commands/extract-images.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Uri, workspace, window, MessageOptions, MessageItem, ProgressLocation, Range } from 'vscode';
1+
import { Uri, workspace, window, MessageOptions, MessageItem, ProgressLocation, Range, WorkspaceEdit } from 'vscode';
22
import { ImageInformation, MarkdownImagesExtractor } from '../services/images-extractor.service';
33

44
type 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
);

src/commands/upload-image/upload-image.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AlertService } from '@/services/alert.service';
2-
import { MessageOptions, window } from 'vscode';
2+
import { window } from 'vscode';
33
import { uploadImageFromClipboard } from './upload-clipboard-image';
44
import { insertImageLinkToActiveEditor, showUploadSuccessModel } from './upload-image-utils';
55
import { uploadLocalDiskImage } from './upload-local-disk-image';
@@ -12,7 +12,7 @@ export const uploadImage = async (autoInsertToActiveEditor = true, from?: 'local
1212
{
1313
modal: true,
1414
detail: '选择图片来源',
15-
} as MessageOptions,
15+
},
1616
...options
1717
)
1818
: from;

0 commit comments

Comments
 (0)