Files
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

152 lines
3.6 KiB
JavaScript

let dirty = false
async function renderMarkdownPreview() {
const editor =
document.getElementById(
"editor",
)
const preview =
document.getElementById(
"preview",
)
if (!editor || !preview) {
return
}
const res = await fetch(
"/_/api/v1/render/markdown",
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
markdown:
editor.value,
path: window.PagesWikiCurrentPath || location.pathname,
projection: true,
}),
},
)
const data =
await res.json()
preview.innerHTML =
data.html
}
async function restoreRevision(
revisionUUID,
) {
const res = await fetch(
"/_/api/v1/revisions/restore?revision="
+ encodeURIComponent(revisionUUID),
{ method: "POST" },
)
if (!res.ok) {
alert("restore failed")
return
}
location.reload()
}
async function openRevisionDialog() {
const objectUUID = document.getElementById("object-uuid")?.value
const root = document.getElementById("revision-list")
const dialog = document.getElementById("revision-dialog")
if (!objectUUID || !root || !dialog) return
const ja = document.documentElement.lang.startsWith("ja")
const res = await fetch("/_/api/v1/revisions/list?uuid=" + encodeURIComponent(objectUUID))
if (!res.ok) {
root.textContent = ja ? "履歴を取得できませんでした。" : "Revision history is unavailable."
dialog.showModal()
return
}
const revisions = await res.json()
root.replaceChildren()
revisions.forEach((revision) => {
const item = document.createElement("div")
item.className = "revision-item"
const id = document.createElement("code")
id.textContent = revision.uuid
const time = document.createElement("div")
time.textContent = new Date(revision.created_at * 1000).toLocaleString()
const message = document.createElement("p")
message.textContent = revision.message
const restore = document.createElement("button")
restore.type = "button"
restore.textContent = ja ? "復元" : "Restore"
restore.addEventListener("click", () => restoreRevision(revision.uuid))
item.append(id, time, message, restore)
root.append(item)
})
if (res.headers.get("X-Next-Cursor")) {
const more = document.createElement("a")
more.href = "/r/history/" + encodeURIComponent(objectUUID)
more.textContent = ja ? "すべての履歴を表示" : "Browse revision history"
root.append(more)
}
dialog.showModal()
}
window.addEventListener(
"beforeunload",
(ev) => {
if (!dirty) {
return
}
ev.preventDefault()
},
)
document.addEventListener(
"DOMContentLoaded",
() => {
const editor =
document.getElementById(
"editor",
)
if (editor) {
renderMarkdownPreview()
editor.addEventListener(
"input",
() => {
dirty = true
renderMarkdownPreview()
},
)
setInterval(
() => {
if (!dirty) {
return
}
saveRevision()
dirty = false
},
30000,
)
}
},
)