implemented plugin menu items

This commit is contained in:
Ell 2023-08-11 19:01:10 +02:00
parent 521b3fbfaf
commit a5ab364ff9
12 changed files with 270 additions and 20 deletions

View file

@ -65,6 +65,8 @@
<script>
let main = $("#main");
// TODO display nice message when there is no hash
$(window).on("hashchange", display);
display();

View file

@ -1,5 +1,7 @@
<?php
header("Access-Control-Allow-Origin: *");
switch ($_SERVER["REQUEST_METHOD"]) {
case "GET":
handle_get();
@ -13,12 +15,15 @@ switch ($_SERVER["REQUEST_METHOD"]) {
case "DELETE":
handle_delete();
break;
default:
http_response_code(405);
echo "Unsupported method";
}
function handle_get(): void {
parse_str($_SERVER["QUERY_STRING"], $query);
$content = file_get_contents(get_markdown_path($query["id"]));
if (!$content) {
if ($content === false) {
http_response_code(404);
echo "Not found";
return;
@ -28,7 +33,7 @@ function handle_get(): void {
function handle_post(): void {
$content = get_markdown_content();
if (!$content)
if ($content === null)
return;
try {
@ -43,7 +48,7 @@ function handle_post(): void {
$meta = json_encode([
"id" => $id,
"deletion_password" => $password
"password" => $password
]);
// store markdown and metadata in data path
@ -56,7 +61,7 @@ function handle_post(): void {
function handle_patch(): void {
$info = get_patch_delete_info();
$content = get_markdown_content();
if (!$info || !$content)
if (!$info || $content === null)
return;
[$id, $password] = $info;
if (!check_password($id, $password))
@ -80,7 +85,7 @@ function handle_delete(): void {
function check_password(string $id, string $password): bool {
$meta = json_decode(file_get_contents(get_meta_path($id)), true);
if ($password != $meta["deletion_password"]) {
if ($password != $meta["password"]) {
http_response_code(401);
echo "Unauthorized";
return false;
@ -100,14 +105,14 @@ function get_patch_delete_info(): ?array {
return [$id, $password];
}
function get_markdown_content(): string {
function get_markdown_content(): ?string {
$body = json_decode(file_get_contents("php://input"), true);
$content = $body["content"];
if (!$content) {
if (!array_key_exists("content", $body)) {
http_response_code(400);
echo "No content";
return null;
}
return $content;
return $body["content"];
}
function get_markdown_path(string $id): string {

View file

@ -1,11 +1,10 @@
import {Plugin} from "obsidian";
import {defaultSettings, JSPSettings} from "./settings";
import {Plugin, requestUrl, TFile} from "obsidian";
import {defaultSettings, JSPSettings, SharedItem} from "./settings";
import {JSPSettingsTab} from "./settings-tab";
export default class JustSharePleasePlugin extends Plugin {
// TODO when uploading, store server-returned Guid and deletion password of each file in plugin settings
// TODO when deleting, also allow deleting uploads whose local files have been deleted by querying the settings!
// TODO allow deleting uploads whose local files have been deleted (through command?)
// TODO add a setting for auto-refreshing uploads when saving
// TODO strip frontmatter before uploading? maybe optionally
settings: JSPSettings;
@ -13,6 +12,85 @@ export default class JustSharePleasePlugin extends Plugin {
async onload(): Promise<void> {
await this.loadSettings();
this.addSettingTab(new JSPSettingsTab(this.app, this));
this.registerEvent(this.app.workspace.on("file-menu", async (m, f) => {
if (f instanceof TFile) {
let shared = this.settings.shared.find(i => i.path == f.path);
if (!shared) {
// (newly) share a note
m.addItem(i => {
i.setTitle("Share to JSP");
i.setIcon("share");
i.onClick(async () => {
let response = await requestUrl({
url: `${this.settings.url}/share.php`,
method: "POST",
body: JSON.stringify({content: await this.app.vault.cachedRead(f)}),
throw: false
});
// TODO display message about status success/fail and copy URL to clipboard
console.log(response.status + " " + response.text);
if (response.status == 200) {
shared = response.json;
shared.path = f.path;
this.settings.shared.push(shared);
await this.saveSettings();
}
});
});
} else {
// copy note link
m.addItem(i => {
i.setTitle("Copy JSP link");
i.setIcon("link");
// TODO let people know this happened
i.onClick(() => navigator.clipboard.writeText(`${this.settings.url}#${shared.id}`));
});
// update
m.addItem(i => {
i.setTitle("Update in JSP");
i.setIcon("share");
i.onClick(async () => {
let response = await requestUrl({
url: `${this.settings.url}/share.php?id=${shared.id}`,
method: "PATCH",
headers: {"Password": shared.password},
body: JSON.stringify({content: await this.app.vault.cachedRead(f)}),
throw: false
});
// TODO display message about status success/fail after updating
console.log(response.status + " " + response.text);
});
});
// delete
m.addItem(i => {
i.setTitle("Delete from JSP");
i.setIcon("trash");
i.onClick(async () => {
let response = await requestUrl({
url: `${this.settings.url}/share.php?id=${shared.id}`,
method: "DELETE",
headers: {"Password": shared.password},
throw: false
});
// TODO display message about status success/fail when deleting
console.log(response.status);
if (response.status == 200) {
this.settings.shared.remove(shared);
await this.saveSettings();
}
});
});
}
}
}));
}
async loadSettings(): Promise<void> {

View file

@ -1,6 +1,19 @@
export const defaultSettings: JSPSettings = {};
export const defaultSettings: JSPSettings = {
url: "http://localhost:8080",
shared: []
};
export interface JSPSettings {
url: string;
shared: SharedItem[];
}
export interface SharedItem {
id: string;
password: string;
path: string;
}

View file

@ -1,3 +1,3 @@
[
"just-share-please"
]
]

View file

@ -25,5 +25,6 @@
"publish": false,
"sync": false,
"canvas": true,
"bookmarks": true
"bookmarks": true,
"properties": true
}

View file

@ -7,6 +7,7 @@
"canvas",
"outgoing-link",
"tag-pane",
"properties",
"page-preview",
"daily-notes",
"templates",

View file

@ -0,0 +1,4 @@
{
"url": "http://localhost:8080",
"shared": []
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,14 @@
# Cool Test Note
This is a cool test note, my friends!
> How are you?
```cs
public class VeryGood {
}
```
http://localhost:8080#ff3ebb34
cool!!

View file