started on JSP view

This commit is contained in:
Ell 2023-08-16 14:21:40 +02:00
parent ad6b1a223f
commit 0717165809
8 changed files with 112 additions and 12 deletions

View file

@ -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<void> {
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<void> {
@ -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();
}
}
}

View file

@ -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;
}

66
src/view.ts Normal file
View file

@ -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";
}
}

View file

@ -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;
}

1
test-vault/.obsidian/app.json vendored Normal file
View file

@ -0,0 +1 @@
{}

View file

@ -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"
}
],

View file

@ -2,7 +2,7 @@
test: yes
---
yo
# Cool Test Note
This is a cool test note, my friends!