mirror of
https://github.com/Ellpeck/ObsidianJustSharePlease.git
synced 2024-11-23 10:58:36 +01:00
started on JSP view
This commit is contained in:
parent
ad6b1a223f
commit
0717165809
8 changed files with 112 additions and 12 deletions
29
src/main.ts
29
src/main.ts
|
@ -2,19 +2,29 @@ import {arrayBufferToBase64, Notice, Plugin, requestUrl, TFile} from "obsidian";
|
||||||
import {defaultSettings, JSPSettings, SharedItem} from "./settings";
|
import {defaultSettings, JSPSettings, SharedItem} from "./settings";
|
||||||
import {JSPSettingsTab} from "./settings-tab";
|
import {JSPSettingsTab} from "./settings-tab";
|
||||||
import {basename, extname} from "path";
|
import {basename, extname} from "path";
|
||||||
|
import {JSPView} from "./view";
|
||||||
|
|
||||||
export default class JustSharePleasePlugin extends Plugin {
|
export default class JustSharePleasePlugin extends Plugin {
|
||||||
|
|
||||||
// TODO panel that displays all shares, including ones for removed files, and allows unsharing or updating them
|
public settings: JSPSettings;
|
||||||
// TODO add a setting for auto-refreshing uploads when saving
|
|
||||||
settings: JSPSettings;
|
|
||||||
|
|
||||||
async onload(): Promise<void> {
|
async onload(): Promise<void> {
|
||||||
await this.loadSettings();
|
await this.loadSettings();
|
||||||
this.addSettingTab(new JSPSettingsTab(this.app, this));
|
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) => {
|
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);
|
let shared = this.getSharedItem(f);
|
||||||
if (!shared) {
|
if (!shared) {
|
||||||
m.addItem(i => {
|
m.addItem(i => {
|
||||||
|
@ -93,6 +103,7 @@ export default class JustSharePleasePlugin extends Plugin {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadSettings(): Promise<void> {
|
async loadSettings(): Promise<void> {
|
||||||
|
@ -122,6 +133,7 @@ export default class JustSharePleasePlugin extends Plugin {
|
||||||
|
|
||||||
this.settings.shared.push(shared);
|
this.settings.shared.push(shared);
|
||||||
await this.saveSettings();
|
await this.saveSettings();
|
||||||
|
this.refreshAllViews();
|
||||||
|
|
||||||
return shared;
|
return shared;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -167,6 +179,7 @@ export default class JustSharePleasePlugin extends Plugin {
|
||||||
|
|
||||||
this.settings.shared.remove(item);
|
this.settings.shared.remove(item);
|
||||||
await this.saveSettings();
|
await this.saveSettings();
|
||||||
|
this.refreshAllViews();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -212,4 +225,12 @@ export default class JustSharePleasePlugin extends Plugin {
|
||||||
|
|
||||||
return text;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ export const defaultSettings: JSPSettings = {
|
||||||
stripFrontmatter: true
|
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 {
|
export interface JSPSettings {
|
||||||
|
|
||||||
url: string;
|
url: string;
|
||||||
|
@ -16,6 +18,7 @@ export interface SharedItem {
|
||||||
|
|
||||||
id: string;
|
id: string;
|
||||||
password: string;
|
password: string;
|
||||||
|
// TODO auto-update path when file is moved
|
||||||
path: string;
|
path: string;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
66
src/view.ts
Normal file
66
src/view.ts
Normal 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";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
styles.css
14
styles.css
|
@ -3,3 +3,17 @@
|
||||||
width: 400px;
|
width: 400px;
|
||||||
height: auto;
|
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
1
test-vault/.obsidian/app.json
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{}
|
|
@ -2,13 +2,8 @@
|
||||||
"url": "http://localhost:8080",
|
"url": "http://localhost:8080",
|
||||||
"shared": [
|
"shared": [
|
||||||
{
|
{
|
||||||
"id": "7dd626d1",
|
"id": "de56fe78",
|
||||||
"password": "6253e1ab4b813dc08d52ee7c82b0990b",
|
"password": "2d276f4d3c0715be08ad1812faa97deb",
|
||||||
"path": "Second note.md"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "fa0cf7f7",
|
|
||||||
"password": "b82b04752742c61bd602b52f535cb484",
|
|
||||||
"path": "Cool Test Note.md"
|
"path": "Cool Test Note.md"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
test: yes
|
test: yes
|
||||||
---
|
---
|
||||||
|
|
||||||
|
yo
|
||||||
# Cool Test Note
|
# Cool Test Note
|
||||||
|
|
||||||
This is a cool test note, my friends!
|
This is a cool test note, my friends!
|
||||||
|
|
Loading…
Reference in a new issue