mirror of
https://github.com/Ellpeck/ObsidianJustSharePlease.git
synced 2024-11-23 02:48:34 +01:00
handle when files are moved or deleted
This commit is contained in:
parent
b22540c772
commit
0bb5ae69b0
4 changed files with 58 additions and 29 deletions
49
src/main.ts
49
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<SharedItem> {
|
||||
|
@ -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,6 +173,7 @@ export default class JustSharePleasePlugin extends Plugin {
|
|||
headers: {"Password": item.password},
|
||||
body: JSON.stringify({content: await this.preProcessMarkdown(file)})
|
||||
});
|
||||
if (notice)
|
||||
new Notice(`Successfully updated ${file.basename} on JSP`);
|
||||
return true;
|
||||
} 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));
|
||||
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) {
|
||||
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)
|
||||
|
|
|
@ -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"})
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue