import { defaultMarkdownSerializer } from "prosemirror-markdown"; // @ts-ignore, doesn't matter. import default_md from "./default.md" with {type: "text"}; import { EditorState } from "prosemirror-state"; import type { Post } from "@/lib/types"; var data_element = document.querySelector('script#data'); // JSON {content: "# Markdown\n bla bla bla"} var editor_container = document.querySelector("#editor-container") as HTMLScriptElement; var title_input = document.querySelector("#title") as HTMLInputElement; if (!data_element) { let error = document.createElement("div"); error.className = "error"; error.innerHTML = "

It appears to be that the poor soul who programmed this page made a mistake, and the editor couldn't load your post. Please excuse him.

Try reloading the page, clearing the cache, or contacting the maintainers if none of that works.

" editor_container.appendChild(error); alert("It appears to be that the poor soul who programmed this page made a mistake, and the editor couldn't load your post. Please excuse him.\nTry reloading the page, clearing the cache, or contacting the maintainers if none of that works.") } let data = JSON.parse(data_element!.textContent); export const initial_title = title_input.value; export const initial_post = { ...data, title: initial_title, } export let last_update = { ...initial_post, ...(initial_post.published_at !== undefined && { published_at: !!initial_post.published_at }) }; const target = new URL(window.location.href).pathname; export async function publish_post() { let path = (last_update.published_at === false ? "publish" : "unpublish"); const request = await fetch(`${target}/${path}`, { method: "POST" }); const answer = await request.json(); if (answer.success) { last_update.published_at = (answer.published_at ? answer.published_at : false); alert(answer.message) return true; } else { alert(answer.error); return false; } } export async function update_post(state: EditorState) { let update = { content: defaultMarkdownSerializer.serialize(state.doc), title: title_input.value!, }; const changed: Partial = {}; (Object.keys(update) as (keyof typeof update)[]).forEach(key => { if (update[key] !== last_update[key]) { // @ts-ignore changed[key] = update[key], last_update[key] = update[key] } }); if (Object.keys(changed).length === 0 || !changed) { alert("No changes have been made.") console.log("No changes have been made.") return false; } console.log("You have made the following changes:"); console.log(changed); let result = await fetch(target, { method: "POST", body: JSON.stringify(changed), headers: { "Content-type": "application/json; charset=UTF-8" } }); let response = await result.json() as { success: false, errors: any[] } | { success: true; post: Post; }; if (!response.success) { let msg = "OH NO! :(\n"; for (let key in response.errors) { if (response.errors.hasOwnProperty(key)) { msg = msg.concat(`Error updating ${key}! ${response.errors[key]}\n`); } } alert(msg); return false; } last_update = { ...response.post, ...(response.post.published_at !== undefined && { publish: response.post.published_at ? true : false }) }; alert("Post saved SUCCESSFULLY! :D"); return true; } export async function delete_post() { const answer = prompt("Are you sure you want to delete this post?\nType DELETE to delete it.")?.trim(); if (answer === "DELETE") { const request = await fetch(`${target}/delete`, { method: "POST", }); const result = await request.json(); if (result.success) window.location.replace(`/dashboard?message=${result.message.name}`); else if (!result.success) alert(result.error); } }