添加删除所有图片功能

This commit is contained in:
2025-04-12 20:01:37 +08:00
parent 10794dcdba
commit f3a3aba1fe
4 changed files with 44 additions and 362 deletions

39
main.ts
View File

@@ -1,3 +1,4 @@
import { error } from "console";
import COS from "cos-js-sdk-v5";
import { App, Editor, MarkdownView, Notice, Plugin, PluginSettingTab, Setting, TFile, } from "obsidian";
@@ -304,6 +305,44 @@ export default class CosPicbedPlugin extends Plugin {
})
);
// 右键菜单删除所有图片
this.registerEvent(
this.app.workspace.on("editor-menu", (menu, editor) => {
const self = this;
menu.addItem((item) => {
item.setTitle("Delete all images")
.setIcon("trash")
.onClick(async () => {
const allContent = editor.getValue();
const imageMatches = [...allContent.matchAll(/!\[.*?\]\((.*?)\)/g)];
if (imageMatches.length == 0) {
new Notice("No image found!");
return;
}
const imageNames = imageMatches.map(match => {
return match[1].split('/').pop()!;
}).filter(Boolean);
await Promise.all(
imageNames.map(name => {
self.uploader.deleteFile(name).catch(error => {
new Notice("Image delete failed:" + error.message);
})
})
);
const newContent = allContent.replace(/!\[.*?\]\((.*?)\)/g, "");
editor.setValue(newContent);
new Notice(`Deleted ${imageNames.length} images!`);
})
});
})
);
this.addSettingTab(new CosPicbedSettingTab(this.app, this, () => { }));
this.registerInterval(window.setInterval(() => { }, 5 * 60 * 1000));
}