notes/static/note.js
i-am-called-glitchy a5c4ffe3e9 git init m8
2025-06-03 12:00:58 +00:00

38 lines
909 B
JavaScript

const textarea = document.getElementById("editor");
const status = document.getElementById("status");
fetch("/api/" + noteName)
.then(res => res.ok ? res.text() : "")
.then(text => {
textarea.value = text;
status.textContent = "Loaded";
});
let timeout;
let last = "";
textarea.addEventListener("input", () => {
status.textContent = "Typing...";
clearTimeout(timeout);
timeout = setTimeout(() => {
const text = textarea.value;
if (text !== last) {
fetch("/api/" + noteName, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content: text })
}).then(res => {
if (res.ok) {
last = text;
status.textContent = "Saved";
} else {
status.textContent = "Save failed";
}
});
} else {
status.textContent = "No changes";
}
}, 500);
});