diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2025-09-30 13:34:10 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2025-09-30 13:37:25 +0200 |
| commit | e75d9e371269a4e46bffa6b3dd1fc7d040e82750 (patch) | |
| tree | c1f2a9aa8022a2710af5e5a577578855584540df /internal/mdedit/res/app.js | |
| parent | 0bb27576a37de4bf76dab35c73fa8790fcfa706a (diff) | |
| download | gofu-f8e0b3d1a52bae7500f4871cfcc855230216c279.tar.gz | |
add mdeditv2025.1
Diffstat (limited to 'internal/mdedit/res/app.js')
| -rw-r--r-- | internal/mdedit/res/app.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/internal/mdedit/res/app.js b/internal/mdedit/res/app.js new file mode 100644 index 0000000..c934697 --- /dev/null +++ b/internal/mdedit/res/app.js @@ -0,0 +1,67 @@ +(() => { + const $ = selector => document.querySelector(selector); + + const handleResponse = (promise, action) => { + const error = promise.then(response => { + if (!response.ok) { + throw new Error(`HTTP error, status = ${response.status} from ${response.url}`); + } + const title = response.headers.get("X-Mdedit-Path"); + if (title !== null && title !== "") { + window.document.title = "mdedit: " + title; + $("span#title").innerText = title; + } + return response.text(); + }).then(body => { + action(body); + return null; + }).catch((error) => { + return error; + }).then((error) => { + if (error != null) { + console.log(error); + $("span#status").innerText = `Error: ${error.message}`; + } + }); + }; + + // handler for receiving a rendered HTML from the server + const receiveHTML = body => { + $("div#preview").innerHTML = body; + $("span#status").innerText = "Saved"; + }; + + // whenever the <textedit> changes, wait 3 seconds and then save all changes + let timeoutID = null; + const onTextChange = event => { + if (timeoutID !== null) { + window.clearTimeout(timeoutID); + } + timeoutID = window.setTimeout(uploadMarkdown, 1000); + $("span#status").innerText = "Changed"; + }; + const uploadMarkdown = () => { + window.clearTimeout(timeoutID); + timeoutID = null; + $("span#status").innerText = "Saving..."; + + const opts = { + method: "PUT", + body: $("textarea#editor").value, + }; + handleResponse(fetch("/data.md", opts), receiveHTML); + }; + + // load Markdown and rendered HTML on startup + handleResponse(fetch("/data.md"), body => { + const editor = $("textarea#editor"); + $("textarea#editor").value = body; + handleResponse(fetch("/data.html"), receiveHTML); + + // avoid a useless upload on startup by attaching this only after the initial load is done + for (const eventType of ["change", "input", "textInput"]) { + editor.addEventListener(eventType, onTextChange); + } + }); + +})(); |
