-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
170 lines (144 loc) · 5.41 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');
const { basicValidate, clipboardUploadValidate, getImageUrls} = require("./src/utils/common");
const { compressImage } = require("./src/utils/compress");
const { saveClipboardImage } = require("./src/utils/clipboard");
const { lskyUpload } = require("./src/utils/lsky");
const { getNewUrl } = require("./src/utils/replace");
const { tempPath, tinyKeys, domainList } = require("./src/extension/config");
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let uploadDisposable = vscode.commands.registerCommand('lsky-upload', function () {
vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: '图片上传',
cancellable: true
}, async (progress) => {
try{
await upload(progress);
vscode.window.showInformationMessage("图片上传成功!");
}catch(e){
console.log(e);
vscode.window.showErrorMessage(e.message);
}
});
});
context.subscriptions.push(uploadDisposable);
let batchUploadDisposable = vscode.commands.registerCommand('lsky-batch-upload', () => {
vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: '图片批量上传',
cancellable: true
}, async (progress) => {
try{
const { totalImages, replacedCount, skipCount, failedReplacements } = await batchUpload(progress);
let message = `共 ${totalImages} 张图片,成功:${replacedCount},跳过:${skipCount},失败:${failedReplacements.length}. `;
if (failedReplacements.length > 0) {
failedReplacements.forEach(failedUrl => {
message += `\n\n${failedUrl}`;
});
}
vscode.window.showInformationMessage(message);
}catch(e){
console.error(e);
vscode.window.showErrorMessage(e.message);
}
});
});
context.subscriptions.push(batchUploadDisposable);
}
async function upload(progress) {
return new Promise(async (resolve, reject) => {
try {
console.log(`剪贴板上传图片...`);
// 1. 验证
let editor = clipboardUploadValidate(vscode);
progress.report({ increment: 0, message: '准备上传图片...' });
// 2. 将图片保存至本地
let imagePath = await saveClipboardImage(tempPath, editor.document.getText(editor.selection));
console.log('图片地址:' + imagePath);
// 3. 压缩图片
if (tinyKeys) {
progress.report({ increment: 30, message: '正在使用 Tinypng 压缩图片...' });
await compressImage(imagePath);
}
// 4. 上传图片
progress.report({ increment: 70, message: '图片正在上传到图床...' });
let file = fs.createReadStream(imagePath);
let res = await lskyUpload(file);
editor.edit(textEditorEdit => {
textEditorEdit.insert(editor.selection.active, res.data.data.links.markdown);
});
progress.report({ increment: 100, message: '上传成功!' });
resolve();
} catch (error) {
reject(error);
}
});
}
async function batchUpload(progress) {
return new Promise(async (resolve, reject) => {
try {
console.log(`批量上传图片...`);
// 1. 验证
basicValidate(vscode);
// 2. 获取原始图片链接
const document = vscode.window.activeTextEditor?.document;
const imageUrls = getImageUrls(document.getText());
const totalImages = imageUrls.length;
progress.report({ increment: 0, message: '准备替换 ' + totalImages + ' 张图片...' });
// 3. 遍历替换图片链接
let replacedCount = 0;
let skipCount = 0;
let failedReplacements = [];
for (let i = 0; i < totalImages; i++) {
const imageUrl = imageUrls[i];
if (domainList.includes(new URL(imageUrl).hostname)) {
skipCount++;
console.log(`图片 ${imageUrl} 在 domainList 中,跳过`);
continue;
}
try {
await replaceImage(document, imageUrl);
replacedCount++;
} catch (error) {
failedReplacements.push(imageUrl);
console.error(`替换失败: ${imageUrl}. 错误: ${error}`);
}
progress.report({ increment: (replacedCount / totalImages) * 100, message: '共 ' + totalImages + ' 张图片,已替换 ' + replacedCount + ' 张图片' });
}
resolve({ totalImages, replacedCount, skipCount, failedReplacements });
} catch (error) {
reject(error);
}
});
}
// 替换指定的图片链接
async function replaceImage(document, imageUrl) {
const editor = vscode.window.activeTextEditor;
if (editor) {
const newUrl = await getNewUrl(imageUrl);
let text = document.getText();
let newText = text.replace(imageUrl, newUrl);
const fullRange = new vscode.Range(
document.positionAt(0),
document.positionAt(Math.max(text.length, newText.length))
);
editor.edit(editBuilder => {
editBuilder.replace(fullRange, newText);
});
}
}
// 扩展被禁用,会调用此方法
function deactivate() {}
module.exports = {
activate,
deactivate
}