Skip to content

Commit c146cc9

Browse files
committed
feature: 增加文件/图片的移除功能
1 parent 7a01a42 commit c146cc9

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-markdown-paste",
33
"displayName": "Markdown Paste (KJ)",
44
"description": "A smartly paste for markdown.",
5-
"version": "0.19.2",
5+
"version": "0.19.3",
66
"publisher": "telesoho",
77
"author": {
88
"name": "telesoho",
@@ -41,6 +41,7 @@
4141
"onCommand:telesoho.MarkdownPaste",
4242
"onCommand:telesoho.MarkdownRuby",
4343
"onCommand:telesoho.insertMathSymbol",
44+
"onCommand:telesoho.MarkdownPasteRemove",
4445
"onCommand:telesoho.MarkdownPasteCode"
4546
],
4647
"main": "./out/extension",
@@ -198,6 +199,10 @@
198199
"command": "telesoho.insertMathSymbol",
199200
"title": "Insert latex math symbol"
200201
},
202+
{
203+
"command": "telesoho.MarkdownPasteRemove",
204+
"title": "Markdown Paste Remove"
205+
},
201206
{
202207
"command": "telesoho.MarkdownPasteCode",
203208
"title": "Markdown Paste Code"
@@ -210,6 +215,11 @@
210215
"when": "editorLangId == markdown",
211216
"group": "markdown"
212217
},
218+
{
219+
"command": "telesoho.MarkdownPasteRemove",
220+
"when": "editorLangId == markdown",
221+
"group": "markdown"
222+
},
213223
{
214224
"command": "telesoho.MarkdownPasteCode",
215225
"when": "editorLangId == markdown",

read.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
vs code插件打包: ```vsce package```
1+
vs code 插件打包: `vsce package`
22

33
# new feature
4-
- 增加文件拷贝引用的功能,适配了mac/win/linux平台
4+
5+
- 增加文件拷贝引用的功能,适配了 mac/win/linux 平台
6+
- 增加文件/图片的移除功能

src/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export function activate(context: vscode.ExtensionContext) {
6868
Paster.Ruby();
6969
})
7070
);
71+
context.subscriptions.push(
72+
vscode.commands.registerCommand("telesoho.MarkdownPasteRemove", () => {
73+
Paster.pasteRemove();
74+
})
75+
);
7176
context.subscriptions.push(
7277
vscode.commands.registerCommand("telesoho.MarkdownPasteCode", () => {
7378
Paster.pasteCode();

src/paster.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,57 @@ class Paster {
142142
}
143143
}
144144

145+
/**
146+
* Paste file remove
147+
*/
148+
public static async pasteRemove() {
149+
let editor = vscode.window.activeTextEditor;
150+
if (!editor) return;
151+
const selection = editor.selection;
152+
if (editor.selection.isEmpty) return;
153+
154+
const selectText = editor.document.getText(selection);
155+
const tempMatch = selectText.match(/\]\([/]?(.*)\)/);
156+
if (!tempMatch || !tempMatch.length) {
157+
vscode.window.showErrorMessage("Not found filepath!");
158+
return;
159+
}
160+
const filepath = tempMatch && tempMatch.pop();
161+
162+
let fileUri = editor.document.uri;
163+
if (!fileUri) return;
164+
let basePath = path.dirname(fileUri.fsPath);
165+
166+
let targetFile = path.join(basePath, filepath).replace(/\\/g, "/");
167+
if (targetFile && targetFile.length !== targetFile.trim().length) {
168+
vscode.window.showErrorMessage(
169+
'The specified path is invalid: "' + targetFile + '"'
170+
);
171+
return;
172+
}
173+
174+
Logger.log("Remove File:", targetFile);
175+
176+
let options: vscode.InputBoxOptions = {
177+
prompt: `Confirm to remove this file: [${targetFile}] ?`,
178+
value: "y",
179+
};
180+
vscode.window.showInputBox(options).then((inputVal) => {
181+
if (inputVal === "y") {
182+
editor.edit((edit) => {
183+
edit.replace(editor.selection, "");
184+
});
185+
186+
fs.rm(targetFile, (err) => {
187+
err &&
188+
vscode.window.showErrorMessage(
189+
"Remove failed:" + targetFile + " " + err
190+
);
191+
});
192+
}
193+
});
194+
}
195+
145196
/**
146197
* Download url content in clipboard
147198
*/

vscode-markdown-paste-0.19.3.vsix

11 MB
Binary file not shown.

0 commit comments

Comments
 (0)