Files
pages/static/js/editor.js
T
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

407 lines
10 KiB
JavaScript

window.PagesEditor = {
autosaveTimer: null,
init() {
const editor =
document.getElementById(
"editor",
)
const preview =
document.getElementById(
"preview",
)
const editType =
document.getElementById(
"edit-type",
)
if (
!editor
||
!preview
) {
return
}
const renderPreview =
async () => {
this.previewController?.abort()
const controller = new AbortController()
this.previewController = controller
const sequence = (this.previewSequence || 0) + 1
this.previewSequence = sequence
const source = editor.value
if (editType.value === "code") {
preview.classList.add("code-preview")
preview.textContent =
editor.value
return editor.value
}
try {
const res =
await fetch(
"/_/api/v1/render/markdown",
{
method: "POST",
signal: controller.signal,
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
...(editType.value === "html" ? {html: source} : {markdown: source}),
path: window.PagesWikiCurrentPath || location.pathname,
projection: true,
}),
},
)
if (controller.signal.aborted || sequence !== this.previewSequence) return
if (!res.ok) {
preview.classList.add("code-preview")
preview.innerText =
source
return editor.value
}
const data =
await res.json()
if (controller.signal.aborted || sequence !== this.previewSequence) return
preview.classList.remove("code-preview")
preview.innerHTML =
data.html
if (window.PagesWikiLoadChildren) {
window.PagesWikiLoadChildren(preview)
}
return data.html
} catch (error) {
if (controller.signal.aborted || sequence !== this.previewSequence) return
preview.classList.add("code-preview")
preview.textContent = source
return source
}
}
this.renderPreview =
renderPreview
document.querySelectorAll('[data-editor-action]').forEach(button => button.addEventListener('click', event => {
event.preventDefault()
switch (button.dataset.editorAction) {
case 'save': this.saveNow(); break
case 'rename': window.PagesPathActions.renameCurrentPath(); break
case 'delete': window.PagesPathActions.removeCurrentPath(); break
case 'html': window.switchEditorFormat('html'); break
case 'markdown': window.switchEditorFormat('markdown'); break
case 'preview': window.toggleEditorPreview(); break
}
}))
editor.addEventListener(
"input",
async () => {
clearTimeout(this.previewTimer)
this.previewController?.abort()
this.previewTimer = setTimeout(renderPreview, 150)
clearTimeout(
this.autosaveTimer,
)
this.autosaveTimer =
setTimeout(
() => {
this.autosave()
},
3000,
)
},
)
document.addEventListener(
"keydown",
(ev) => {
if (
(ev.ctrlKey || ev.metaKey)
&&
ev.key === "s"
) {
ev.preventDefault()
this.saveNow()
}
},
)
renderPreview()
},
async getCanonicalHTML() {
const editor =
document.getElementById(
"editor",
)
const editType =
document.getElementById(
"edit-type",
)
if (
editType.value
===
"html"
) {
return editor.value
}
if (editType.value === "code") {
return editor.value
}
const res =
await fetch(
"/_/api/v1/render/markdown",
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
markdown:
editor.value,
}),
},
)
if (!res.ok) {
return editor.value
}
const data =
await res.json()
return data.html
},
async autosave() {
const objectUUID =
document.getElementById(
"object-uuid",
)
if (
!objectUUID
||
!objectUUID.value
) {
return
}
const canonicalHTML =
await this.getCanonicalHTML()
const pageLanguage =
document.getElementById(
"page-language",
)
const pageContentType =
document.getElementById("page-content-type")
const pageFilename =
document.getElementById("page-filename")
await fetch(
"/_/api/v1/revisions/save",
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
object_uuid:
objectUUID.value,
body:
canonicalHTML,
message:
"autosave",
lang:
pageLanguage
? pageLanguage.value
: "und",
content_type:
pageContentType
? pageContentType.value
: "",
filename:
pageFilename
? pageFilename.value
: "",
}),
},
)
},
async saveNow() {
const objectUUID =
document.getElementById(
"object-uuid",
)
if (
!objectUUID
||
!objectUUID.value
) {
return
}
const canonicalHTML =
await this.getCanonicalHTML()
const pageLanguage =
document.getElementById(
"page-language",
)
const pageContentType =
document.getElementById("page-content-type")
const pageFilename =
document.getElementById("page-filename")
const res =
await fetch(
"/_/api/v1/revisions/save",
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
object_uuid:
objectUUID.value,
body:
canonicalHTML,
message:
"manual-save",
lang:
pageLanguage
? pageLanguage.value
: "und",
content_type:
pageContentType
? pageContentType.value
: "",
filename:
pageFilename
? pageFilename.value
: "",
}),
},
)
if (!res.ok) {
alert(
"save failed",
)
return
}
alert(
"saved",
)
},
}
document.addEventListener(
"DOMContentLoaded",
() => {
if (
document.body.classList.contains(
"mode-editor",
)
) {
PagesEditor.init()
}
},
)
document.addEventListener(
"DOMContentLoaded",
() => {
const sel =
document.getElementById(
"editor-type",
)
if (!sel) {
return
}
sel.addEventListener(
"change",
() => {
const u =
new URL(
location.href,
)
u.searchParams.set(
"edittype",
sel.value,
)
location.href =
u.toString()
},
)
},
)
// HTML/Markdown previews and diagram projections are produced by the common
// server boundary. Do not reinterpret authoring source in a second client pass.