This commit is contained in:
Anya Tokugawa committed 2026-05-19 21:30:42 +09:00
commit fa6a4c650f
93 files changed
+6555

No files matched your search

+163
View File
@@ -0,0 +1,163 @@
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,
}),
},
)
const data =
await res.json()
preview.innerHTML =
data.html
}
async function restoreRevision(
revisionUUID,
) {
const res = await fetch(
"/_/api/v1/revisions/restore?revision="
+ revisionUUID,
)
if (!res.ok) {
alert("restore failed")
return
}
location.reload()
}
async function openRevisionDialog() {
const objectUUID =
document.getElementById(
"object-uuid",
).value
const res = await fetch(
"/_/api/v1/revisions/list?uuid="
+ objectUUID,
)
const revisions =
await res.json()
const root =
document.getElementById(
"revision-list",
)
root.innerHTML = ""
revisions.forEach((r) => {
const div =
document.createElement("div")
div.className =
"revision-item"
const ts =
new Date(
r.created_at * 1000,
)
div.innerHTML =
"<code>"
+ r.uuid
+ "</code><br>"
+ ts.toLocaleString()
+ "<br>"
+ r.message
+ "<br><button onclick=\"restoreRevision('"
+ r.uuid
+ "')\">Restore</button>"
root.appendChild(div)
})
document.getElementById(
"revision-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,
)
}
},
)
+315
View File
@@ -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()
},
)
},
)
+50
View File
@@ -0,0 +1,50 @@
console.log("navigation.js loaded")
function createChildPage() {
const name = prompt("child page name")
if (!name) return
let path = location.pathname
if (!path.endsWith("/")) path += "/"
location.href = "/e" + path + name
}
function selectObject(uuid) {
const input = document.getElementById("link-object-uuid")
if (input) input.value = uuid
}
async function bindPath() {
const objectUUID = document.getElementById("link-object-uuid")?.value
const path = document.getElementById("link-path")?.value
const res = await fetch("/_/api/v1/paths/bind", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({object_uuid: objectUUID, path: path}),
})
if (!res.ok) {
alert("bind failed")
return
}
location.href = path
}
document.addEventListener("keydown", (ev) => {
if (ev.target && ["TEXTAREA", "INPUT"].includes(ev.target.tagName)) return
if (ev.key === "c") location.href = "/new"
if (ev.key === "e" && !location.pathname.startsWith("/e/")) {
location.href = "/e" + location.pathname
}
if (ev.key === "^") {
const parts = location.pathname.split("/").filter(Boolean)
parts.pop()
location.href = "/" + parts.join("/")
}
})
+105
View File
@@ -0,0 +1,105 @@
window.PagesPathActions = {
async renameCurrentPath() {
const currentPath =
location.pathname.replace(
/^\/e/,
"",
)
const nextPath =
prompt(
"Rename path:",
currentPath,
)
if (
!nextPath
||
nextPath === currentPath
) {
return
}
const res =
await fetch(
"/_/api/v1/path/rename",
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
old_path:
currentPath,
new_path:
nextPath,
}),
},
)
if (!res.ok) {
alert(
"rename failed",
)
return
}
location.href =
"/e" + nextPath
},
async removeCurrentPath() {
const currentPath =
location.pathname.replace(
/^\/e/,
"",
)
if (
!confirm(
"Remove path?\n\n" +
currentPath,
)
) {
return
}
const res =
await fetch(
"/_/api/v1/path/remove",
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
path:
currentPath,
}),
},
)
if (!res.ok) {
alert(
"remove failed",
)
return
}
location.href = "/"
},
}
+44
View File
@@ -0,0 +1,44 @@
console.log("revisions.js loaded")
async function restoreRevision(
revisionUUID,
) {
if (
!confirm(
"restore revision?"
)
) {
return
}
const res = await fetch(
"/_/api/v1/revisions/restore",
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
revision_uuid:
revisionUUID,
}),
},
)
if (!res.ok) {
alert(
"restore failed",
)
return
}
alert(
"revision restored",
)
location.reload()
}
+1
View File
@@ -0,0 +1 @@
console.log("search.js loaded")