38 lines
909 B
JavaScript
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);
|
|
});
|
|
|