init
This commit is contained in:
commit
fa6a4c650f
93 files changed
+6555
No files matched your search
@@ -0,0 +1,315 @@
|
||||
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 () => {
|
||||
|
||||
if (
|
||||
editType.value
|
||||
===
|
||||
"html"
|
||||
) {
|
||||
|
||||
preview.innerHTML =
|
||||
editor.value
|
||||
|
||||
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) {
|
||||
|
||||
preview.innerText =
|
||||
editor.value
|
||||
|
||||
return editor.value
|
||||
}
|
||||
|
||||
const data =
|
||||
await res.json()
|
||||
|
||||
preview.innerHTML =
|
||||
data.html
|
||||
|
||||
return data.html
|
||||
}
|
||||
|
||||
editor.addEventListener(
|
||||
"input",
|
||||
async () => {
|
||||
|
||||
await renderPreview()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
await fetch(
|
||||
"/_/api/v1/revisions/save",
|
||||
{
|
||||
method: "POST",
|
||||
|
||||
headers: {
|
||||
"Content-Type":
|
||||
"application/json",
|
||||
},
|
||||
|
||||
body: JSON.stringify({
|
||||
object_uuid:
|
||||
objectUUID.value,
|
||||
|
||||
body:
|
||||
canonicalHTML,
|
||||
|
||||
message:
|
||||
"autosave",
|
||||
}),
|
||||
},
|
||||
)
|
||||
},
|
||||
|
||||
async saveNow() {
|
||||
|
||||
const objectUUID =
|
||||
document.getElementById(
|
||||
"object-uuid",
|
||||
)
|
||||
|
||||
if (
|
||||
!objectUUID
|
||||
||
|
||||
!objectUUID.value
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
const canonicalHTML =
|
||||
await this.getCanonicalHTML()
|
||||
|
||||
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",
|
||||
}),
|
||||
},
|
||||
)
|
||||
|
||||
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()
|
||||
},
|
||||
)
|
||||
},
|
||||
)
|
||||
Reference in new issue
Block a user