From 0717165809c7b38d7ad0a923dec2c8ecd1fd394c Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 16 Aug 2023 14:21:40 +0200 Subject: [PATCH] started on JSP view --- src/main.ts | 29 ++++++-- src/settings.ts | 3 + src/view.ts | 66 +++++++++++++++++++ styles.css | 14 ++++ test-vault/.obsidian/app.json | 1 + .../plugins/just-share-please/data.json | 9 +-- test-vault/Cool Test Note.md | 2 +- test-vault/Second note.md | 0 8 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 src/view.ts create mode 100644 test-vault/.obsidian/app.json delete mode 100644 test-vault/Second note.md diff --git a/src/main.ts b/src/main.ts index 6052da3..4c3603c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,19 +2,29 @@ import {arrayBufferToBase64, Notice, Plugin, requestUrl, TFile} from "obsidian"; import {defaultSettings, JSPSettings, SharedItem} from "./settings"; import {JSPSettingsTab} from "./settings-tab"; import {basename, extname} from "path"; +import {JSPView} from "./view"; export default class JustSharePleasePlugin extends Plugin { - // TODO panel that displays all shares, including ones for removed files, and allows unsharing or updating them - // TODO add a setting for auto-refreshing uploads when saving - settings: JSPSettings; + public settings: JSPSettings; async onload(): Promise { await this.loadSettings(); this.addSettingTab(new JSPSettingsTab(this.app, this)); + this.registerView(JSPView.type, l => new JSPView(this, l)); + this.addCommand({ + id: `open-${JSPView.type}`, + name: `Open Just Share Please view`, + callback: async () => { + if (!this.app.workspace.getLeavesOfType(JSPView.type).length) + await this.app.workspace.getRightLeaf(false).setViewState({type: JSPView.type, active: true}); + this.app.workspace.revealLeaf(this.app.workspace.getLeavesOfType(JSPView.type)[0]); + } + }); + this.registerEvent(this.app.workspace.on("file-menu", async (m, f) => { - if (f instanceof TFile) { + if (f instanceof TFile && f.extension == "md") { let shared = this.getSharedItem(f); if (!shared) { m.addItem(i => { @@ -93,6 +103,7 @@ export default class JustSharePleasePlugin extends Plugin { return false; } }); + } async loadSettings(): Promise { @@ -122,6 +133,7 @@ export default class JustSharePleasePlugin extends Plugin { this.settings.shared.push(shared); await this.saveSettings(); + this.refreshAllViews(); return shared; } catch (e) { @@ -167,6 +179,7 @@ export default class JustSharePleasePlugin extends Plugin { this.settings.shared.remove(item); await this.saveSettings(); + this.refreshAllViews(); return true; } catch (e) { @@ -212,4 +225,12 @@ 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) + leaf.view.refresh(); + } + } } diff --git a/src/settings.ts b/src/settings.ts index 91c7339..83b0e6d 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -4,6 +4,8 @@ export const defaultSettings: JSPSettings = { stripFrontmatter: 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; @@ -16,6 +18,7 @@ export interface SharedItem { id: string; password: string; + // TODO auto-update path when file is moved path: string; } diff --git a/src/view.ts b/src/view.ts new file mode 100644 index 0000000..ff64e8f --- /dev/null +++ b/src/view.ts @@ -0,0 +1,66 @@ +import {ButtonComponent, ItemView, TFile, WorkspaceLeaf} from "obsidian"; +import {basename, extname} from "path"; +import JustSharePleasePlugin from "./main"; + +export class JSPView extends ItemView { + + public static readonly type: string = "jsp-view"; + + private readonly plugin: JustSharePleasePlugin; + + constructor(plugin: JustSharePleasePlugin, leaf: WorkspaceLeaf) { + super(leaf); + this.plugin = plugin; + } + + public refresh(): void { + this.contentEl.empty(); + let content = this.contentEl.createDiv({cls: "just-share-please-view"}); + for (let shared of this.plugin.settings.shared) { + let file = this.plugin.app.vault.getAbstractFileByPath(shared.path) as TFile; + let div = content.createDiv({cls: "just-share-please-shared-item"}); + div.createSpan({cls: "just-share-please-shared-name", text: basename(shared.path, extname(shared.path))}); + new ButtonComponent(div) + .setClass("clickable-icon") + .setTooltip("Copy JSP link") + .setIcon("link") + .onClick(async () => this.plugin.copyShareLink(shared)); + if (file) { + new ButtonComponent(div) + .setClass("clickable-icon") + .setTooltip("Open in Obsidian") + .setIcon("edit") + .onClick(async () => { + // TODO open in obsidian + }); + new ButtonComponent(div) + .setClass("clickable-icon") + .setTooltip("Update in JSP") + .setIcon("share") + .onClick(async () => this.plugin.updateFile(shared, file)); + } + new ButtonComponent(div) + .setClass("clickable-icon") + .setTooltip("Delete from JSP") + .setIcon("trash") + .onClick(async () => this.plugin.deleteFile(shared)); + } + } + + public onload(): void { + this.refresh(); + } + + public getDisplayText(): string { + return "Just Share Please"; + } + + public getViewType(): string { + return JSPView.type; + } + + public getIcon(): string { + return "share"; + } + +} diff --git a/styles.css b/styles.css index af92d99..f7b5435 100644 --- a/styles.css +++ b/styles.css @@ -3,3 +3,17 @@ width: 400px; height: auto; } + +.just-share-please-view .clickable-icon { + display: inline; +} + +.just-share-please-shared-name { + display: block; + font-weight: bold; + padding-bottom: 5px; +} + +.just-share-please-shared-item { + margin-bottom: 20px; +} diff --git a/test-vault/.obsidian/app.json b/test-vault/.obsidian/app.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test-vault/.obsidian/app.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test-vault/.obsidian/plugins/just-share-please/data.json b/test-vault/.obsidian/plugins/just-share-please/data.json index f35e5b0..bff21d1 100644 --- a/test-vault/.obsidian/plugins/just-share-please/data.json +++ b/test-vault/.obsidian/plugins/just-share-please/data.json @@ -2,13 +2,8 @@ "url": "http://localhost:8080", "shared": [ { - "id": "7dd626d1", - "password": "6253e1ab4b813dc08d52ee7c82b0990b", - "path": "Second note.md" - }, - { - "id": "fa0cf7f7", - "password": "b82b04752742c61bd602b52f535cb484", + "id": "de56fe78", + "password": "2d276f4d3c0715be08ad1812faa97deb", "path": "Cool Test Note.md" } ], diff --git a/test-vault/Cool Test Note.md b/test-vault/Cool Test Note.md index bb95433..bb84d46 100644 --- a/test-vault/Cool Test Note.md +++ b/test-vault/Cool Test Note.md @@ -2,7 +2,7 @@ test: yes --- - +yo # Cool Test Note This is a cool test note, my friends! diff --git a/test-vault/Second note.md b/test-vault/Second note.md deleted file mode 100644 index e69de29..0000000