stuff
This commit is contained in:
parent
ec90d4a8d7
commit
c3a1ce74af
4 changed files with 88 additions and 6 deletions
|
|
@ -1,31 +1,63 @@
|
|||
const textarea = document.getElementById("editor");
|
||||
const status = document.getElementById("status");
|
||||
const error = document.getElementById("error");
|
||||
const currentUser = document.getElementById("username").innerHTML;
|
||||
const path = window.location.pathname;
|
||||
|
||||
fetch("/api/" + noteName)
|
||||
// === ROUTE PARSING ===
|
||||
let mode = "private";
|
||||
let fetchUrl = "";
|
||||
let editable = true;
|
||||
let last = "";
|
||||
|
||||
if (path.startsWith("/public/")) {
|
||||
const username = path.split("/")[2];
|
||||
fetchUrl = "/publicapi/" + username;
|
||||
editable = (currentUser === username);
|
||||
mode = "public";
|
||||
} else if (path === "/board") {
|
||||
fetchUrl = "/boardapi";
|
||||
mode = "shared";
|
||||
editable = true;
|
||||
} else if (path.startsWith("/n/") || path.startsWith("/notes/")) {
|
||||
const note = path.split("/").pop();
|
||||
fetchUrl = "/api/" + note;
|
||||
editable = true;
|
||||
} else {
|
||||
error.textContent = "Unsupported path: " + path;
|
||||
textarea.disabled = true;
|
||||
throw new Error("Unknown path: " + path);
|
||||
}
|
||||
|
||||
if (!editable) textarea.disabled = true;
|
||||
|
||||
// === FETCH INITIAL CONTENT ===
|
||||
fetch(fetchUrl)
|
||||
.then(res => {
|
||||
if (!res.ok) throw new Error("Failed to load note. Status: " + res.status);
|
||||
if (!res.ok) throw new Error("Failed to load. Status: " + res.status);
|
||||
return res.text();
|
||||
})
|
||||
.then(text => {
|
||||
textarea.value = text;
|
||||
last = text;
|
||||
status.textContent = "Loaded";
|
||||
})
|
||||
.catch(err => {
|
||||
error.textContent = "Error loading note: " + err.message;
|
||||
error.textContent = "Error loading content: " + err.message;
|
||||
status.textContent = "Load failed";
|
||||
});
|
||||
|
||||
// === AUTOSAVE LOGIC ===
|
||||
let timeout;
|
||||
let last = "";
|
||||
|
||||
textarea.addEventListener("input", () => {
|
||||
if (!editable) return;
|
||||
status.textContent = "Typing...";
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(() => {
|
||||
const text = textarea.value;
|
||||
if (text !== last) {
|
||||
fetch("/api/" + noteName, {
|
||||
fetch(fetchUrl, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ content: text })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue