From 0bb5ae69b0b610290c4dfd2615b7e7c50d007e29 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 17 Aug 2023 17:51:11 +0200 Subject: [PATCH] handle when files are moved or deleted --- src/main.ts | 59 +++++++++++++------ src/settings-tab.ts | 11 ++++ src/settings.ts | 6 +- .../plugins/just-share-please/data.json | 11 +--- 4 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/main.ts b/src/main.ts index fb62f54..8962ea6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -25,7 +25,7 @@ export default class JustSharePleasePlugin extends Plugin { this.registerEvent(this.app.workspace.on("file-menu", async (m, f) => { if (f instanceof TFile && f.extension == "md") { - let shared = this.getSharedItem(f); + let shared = this.getSharedItem(f.path); if (!shared) { m.addItem(i => { i.setTitle("Share to JSP"); @@ -51,12 +51,33 @@ export default class JustSharePleasePlugin extends Plugin { } } })); + this.registerEvent(this.app.vault.on("rename", (f, p) => { + if (f instanceof TFile) { + let shared = this.getSharedItem(p); + if (shared) { + shared.path = f.path; + this.refreshAllViews(); + } + } + })); + this.registerEvent(this.app.vault.on("delete", f => { + if (f instanceof TFile) { + let shared = this.getSharedItem(f.path); + if (shared) { + if (this.settings.unshareDeletedFiles) { + this.deleteFile(shared, false); + } else { + this.refreshAllViews(); + } + } + } + })); this.addCommand({ id: "share", name: "Share current file to JSP", editorCheckCallback: (checking, _, ctx) => { - if (!this.getSharedItem(ctx.file)) { + if (!this.getSharedItem(ctx.file.path)) { if (!checking) this.shareFile(ctx.file); return true; @@ -68,7 +89,7 @@ export default class JustSharePleasePlugin extends Plugin { id: "copy", name: "Copy current file's JSP link", editorCheckCallback: (checking, _, ctx) => { - let shared = this.getSharedItem(ctx.file); + let shared = this.getSharedItem(ctx.file.path); if (shared) { if (!checking) this.copyShareLink(shared); @@ -81,7 +102,7 @@ export default class JustSharePleasePlugin extends Plugin { id: "update", name: "Update current file in JSP", editorCheckCallback: (checking, _, ctx) => { - let shared = this.getSharedItem(ctx.file); + let shared = this.getSharedItem(ctx.file.path); if (shared) { if (!checking) this.updateFile(shared, ctx.file); @@ -94,7 +115,7 @@ export default class JustSharePleasePlugin extends Plugin { id: "delete", name: "Delete current file from JSP", editorCheckCallback: (checking, _, ctx) => { - let shared = this.getSharedItem(ctx.file); + let shared = this.getSharedItem(ctx.file.path); if (shared) { if (!checking) this.deleteFile(shared); @@ -114,8 +135,8 @@ export default class JustSharePleasePlugin extends Plugin { await this.saveData(this.settings); } - getSharedItem(file: TFile): SharedItem { - return this.settings.shared.find(f => f.path == file.path); + getSharedItem(path: string): SharedItem { + return this.settings.shared.find(f => f.path == path); } async shareFile(file: TFile): Promise { @@ -128,13 +149,12 @@ export default class JustSharePleasePlugin extends Plugin { let shared = response.json as SharedItem; shared.path = file.path; - await this.copyShareLink(shared, false); - new Notice(`Successfully shared ${file.basename} and copied link to clipboard`); - this.settings.shared.push(shared); await this.saveSettings(); this.refreshAllViews(); + await this.copyShareLink(shared, false); + new Notice(`Successfully shared ${file.basename} and copied link to clipboard`); return shared; } catch (e) { new Notice(createFragment(f => { @@ -153,7 +173,8 @@ export default class JustSharePleasePlugin extends Plugin { headers: {"Password": item.password}, body: JSON.stringify({content: await this.preProcessMarkdown(file)}) }); - new Notice(`Successfully updated ${file.basename} on JSP`); + if (notice) + new Notice(`Successfully updated ${file.basename} on JSP`); return true; } catch (e) { if (notice) { @@ -167,7 +188,7 @@ export default class JustSharePleasePlugin extends Plugin { } - async deleteFile(item: SharedItem): Promise { + async deleteFile(item: SharedItem, notice = true): Promise { let name = basename(item.path, extname(item.path)); try { await requestUrl({ @@ -175,18 +196,21 @@ export default class JustSharePleasePlugin extends Plugin { method: "DELETE", headers: {"Password": item.password} }); - new Notice(`Successfully deleted ${name} from JSP`); this.settings.shared.remove(item); await this.saveSettings(); this.refreshAllViews(); + if (notice) + new Notice(`Successfully deleted ${name} from JSP`); return true; } catch (e) { - new Notice(createFragment(f => { - f.createSpan({text: `There was an error deleting ${name}: `}); - f.createEl("code", {text: e}); - }), 10000); + if (notice) { + new Notice(createFragment(f => { + f.createSpan({text: `There was an error deleting ${name}: `}); + f.createEl("code", {text: e}); + }), 10000); + } console.log(e); } } @@ -230,7 +254,6 @@ export default class JustSharePleasePlugin extends Plugin { return text; } - // TODO refresh when a file is moved or deleted in Obsidian refreshAllViews(): void { for (let leaf of this.app.workspace.getLeavesOfType(JSPView.type)) { if (leaf.view instanceof JSPView) diff --git a/src/settings-tab.ts b/src/settings-tab.ts index b0d1791..e10432a 100644 --- a/src/settings-tab.ts +++ b/src/settings-tab.ts @@ -54,6 +54,17 @@ export class JSPSettingsTab extends PluginSettingTab { }); }); + new Setting(this.containerEl) + .setName("Unshare Deleted Files") + .setDesc("Whether shares of files should be removed automatically when they are deleted. Only supported when deleting from within Obsidian.") + .addToggle(t => { + t.setValue(this.plugin.settings.unshareDeletedFiles); + t.onChange(async v => { + this.plugin.settings.unshareDeletedFiles = v; + await this.plugin.saveSettings(); + }); + }); + this.containerEl.createEl("hr"); this.containerEl.createEl("p", {text: "If you like this plugin and want to support its development, you can do so through my website by clicking this fancy image!"}); this.containerEl.createEl("a", {href: "https://ellpeck.de/support"}) diff --git a/src/settings.ts b/src/settings.ts index 26aa3f9..e36d6e9 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -2,17 +2,18 @@ export const defaultSettings: JSPSettings = { url: "https://jsp.ellpeck.de", shared: [], stripFrontmatter: true, - includeNoteName: true + includeNoteName: true, + unshareDeletedFiles: true }; // TODO add a setting for auto-refreshing uploads when saving -// TODO add a setting for auto-removing JSP shares when the original file is deleted export interface JSPSettings { url: string; shared: SharedItem[]; stripFrontmatter: boolean; includeNoteName: boolean; + unshareDeletedFiles: boolean; } @@ -20,7 +21,6 @@ export interface SharedItem { id: string; password: string; - // TODO auto-update path when file is moved path: string; } diff --git a/test-vault/.obsidian/plugins/just-share-please/data.json b/test-vault/.obsidian/plugins/just-share-please/data.json index 1d9d758..233094f 100644 --- a/test-vault/.obsidian/plugins/just-share-please/data.json +++ b/test-vault/.obsidian/plugins/just-share-please/data.json @@ -1,12 +1,7 @@ { "url": "http://localhost:8080", - "shared": [ - { - "id": "6ba272db", - "password": "6bf48896857373ff766f44052cbc8c38", - "path": "Cool Test Note.md" - } - ], + "shared": [], "stripFrontmatter": true, - "includeNoteName": true + "includeNoteName": true, + "unshareDeletedFiles": false } \ No newline at end of file