handle when files are moved or deleted

This commit is contained in:
Ell 2023-08-17 17:51:11 +02:00
parent b22540c772
commit 0bb5ae69b0
4 changed files with 58 additions and 29 deletions

View file

@ -25,7 +25,7 @@ export default class JustSharePleasePlugin extends Plugin {
this.registerEvent(this.app.workspace.on("file-menu", async (m, f) => { this.registerEvent(this.app.workspace.on("file-menu", async (m, f) => {
if (f instanceof TFile && f.extension == "md") { if (f instanceof TFile && f.extension == "md") {
let shared = this.getSharedItem(f); let shared = this.getSharedItem(f.path);
if (!shared) { if (!shared) {
m.addItem(i => { m.addItem(i => {
i.setTitle("Share to JSP"); 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({ this.addCommand({
id: "share", id: "share",
name: "Share current file to JSP", name: "Share current file to JSP",
editorCheckCallback: (checking, _, ctx) => { editorCheckCallback: (checking, _, ctx) => {
if (!this.getSharedItem(ctx.file)) { if (!this.getSharedItem(ctx.file.path)) {
if (!checking) if (!checking)
this.shareFile(ctx.file); this.shareFile(ctx.file);
return true; return true;
@ -68,7 +89,7 @@ export default class JustSharePleasePlugin extends Plugin {
id: "copy", id: "copy",
name: "Copy current file's JSP link", name: "Copy current file's JSP link",
editorCheckCallback: (checking, _, ctx) => { editorCheckCallback: (checking, _, ctx) => {
let shared = this.getSharedItem(ctx.file); let shared = this.getSharedItem(ctx.file.path);
if (shared) { if (shared) {
if (!checking) if (!checking)
this.copyShareLink(shared); this.copyShareLink(shared);
@ -81,7 +102,7 @@ export default class JustSharePleasePlugin extends Plugin {
id: "update", id: "update",
name: "Update current file in JSP", name: "Update current file in JSP",
editorCheckCallback: (checking, _, ctx) => { editorCheckCallback: (checking, _, ctx) => {
let shared = this.getSharedItem(ctx.file); let shared = this.getSharedItem(ctx.file.path);
if (shared) { if (shared) {
if (!checking) if (!checking)
this.updateFile(shared, ctx.file); this.updateFile(shared, ctx.file);
@ -94,7 +115,7 @@ export default class JustSharePleasePlugin extends Plugin {
id: "delete", id: "delete",
name: "Delete current file from JSP", name: "Delete current file from JSP",
editorCheckCallback: (checking, _, ctx) => { editorCheckCallback: (checking, _, ctx) => {
let shared = this.getSharedItem(ctx.file); let shared = this.getSharedItem(ctx.file.path);
if (shared) { if (shared) {
if (!checking) if (!checking)
this.deleteFile(shared); this.deleteFile(shared);
@ -114,8 +135,8 @@ export default class JustSharePleasePlugin extends Plugin {
await this.saveData(this.settings); await this.saveData(this.settings);
} }
getSharedItem(file: TFile): SharedItem { getSharedItem(path: string): SharedItem {
return this.settings.shared.find(f => f.path == file.path); return this.settings.shared.find(f => f.path == path);
} }
async shareFile(file: TFile): Promise<SharedItem> { async shareFile(file: TFile): Promise<SharedItem> {
@ -128,13 +149,12 @@ export default class JustSharePleasePlugin extends Plugin {
let shared = response.json as SharedItem; let shared = response.json as SharedItem;
shared.path = file.path; 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); this.settings.shared.push(shared);
await this.saveSettings(); await this.saveSettings();
this.refreshAllViews(); this.refreshAllViews();
await this.copyShareLink(shared, false);
new Notice(`Successfully shared ${file.basename} and copied link to clipboard`);
return shared; return shared;
} catch (e) { } catch (e) {
new Notice(createFragment(f => { new Notice(createFragment(f => {
@ -153,6 +173,7 @@ export default class JustSharePleasePlugin extends Plugin {
headers: {"Password": item.password}, headers: {"Password": item.password},
body: JSON.stringify({content: await this.preProcessMarkdown(file)}) body: JSON.stringify({content: await this.preProcessMarkdown(file)})
}); });
if (notice)
new Notice(`Successfully updated ${file.basename} on JSP`); new Notice(`Successfully updated ${file.basename} on JSP`);
return true; return true;
} catch (e) { } catch (e) {
@ -167,7 +188,7 @@ export default class JustSharePleasePlugin extends Plugin {
} }
async deleteFile(item: SharedItem): Promise<boolean> { async deleteFile(item: SharedItem, notice = true): Promise<boolean> {
let name = basename(item.path, extname(item.path)); let name = basename(item.path, extname(item.path));
try { try {
await requestUrl({ await requestUrl({
@ -175,18 +196,21 @@ export default class JustSharePleasePlugin extends Plugin {
method: "DELETE", method: "DELETE",
headers: {"Password": item.password} headers: {"Password": item.password}
}); });
new Notice(`Successfully deleted ${name} from JSP`);
this.settings.shared.remove(item); this.settings.shared.remove(item);
await this.saveSettings(); await this.saveSettings();
this.refreshAllViews(); this.refreshAllViews();
if (notice)
new Notice(`Successfully deleted ${name} from JSP`);
return true; return true;
} catch (e) { } catch (e) {
if (notice) {
new Notice(createFragment(f => { new Notice(createFragment(f => {
f.createSpan({text: `There was an error deleting ${name}: `}); f.createSpan({text: `There was an error deleting ${name}: `});
f.createEl("code", {text: e}); f.createEl("code", {text: e});
}), 10000); }), 10000);
}
console.log(e); console.log(e);
} }
} }
@ -230,7 +254,6 @@ export default class JustSharePleasePlugin extends Plugin {
return text; return text;
} }
// TODO refresh when a file is moved or deleted in Obsidian
refreshAllViews(): void { refreshAllViews(): void {
for (let leaf of this.app.workspace.getLeavesOfType(JSPView.type)) { for (let leaf of this.app.workspace.getLeavesOfType(JSPView.type)) {
if (leaf.view instanceof JSPView) if (leaf.view instanceof JSPView)

View file

@ -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("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("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"}) this.containerEl.createEl("a", {href: "https://ellpeck.de/support"})

View file

@ -2,17 +2,18 @@ export const defaultSettings: JSPSettings = {
url: "https://jsp.ellpeck.de", url: "https://jsp.ellpeck.de",
shared: [], shared: [],
stripFrontmatter: true, stripFrontmatter: true,
includeNoteName: true includeNoteName: true,
unshareDeletedFiles: true
}; };
// TODO add a setting for auto-refreshing uploads when saving // 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 { export interface JSPSettings {
url: string; url: string;
shared: SharedItem[]; shared: SharedItem[];
stripFrontmatter: boolean; stripFrontmatter: boolean;
includeNoteName: boolean; includeNoteName: boolean;
unshareDeletedFiles: boolean;
} }
@ -20,7 +21,6 @@ export interface SharedItem {
id: string; id: string;
password: string; password: string;
// TODO auto-update path when file is moved
path: string; path: string;
} }

View file

@ -1,12 +1,7 @@
{ {
"url": "http://localhost:8080", "url": "http://localhost:8080",
"shared": [ "shared": [],
{
"id": "6ba272db",
"password": "6bf48896857373ff766f44052cbc8c38",
"path": "Cool Test Note.md"
}
],
"stripFrontmatter": true, "stripFrontmatter": true,
"includeNoteName": true "includeNoteName": true,
"unshareDeletedFiles": false
} }