2025-07-20 02:49:01 -04:00
|
|
|
// theme preference
|
|
|
|
function media_theme_pref() {
|
|
|
|
document.documentElement.removeAttribute("class");
|
|
|
|
|
|
|
|
if (
|
|
|
|
window.matchMedia("(prefers-color-scheme: dark)").matches &&
|
2025-07-25 18:33:16 -04:00
|
|
|
(!window.localStorage.getItem("fluffle:theme") ||
|
|
|
|
window.localStorage.getItem("fluffle:theme") === "Auto")
|
2025-07-20 02:49:01 -04:00
|
|
|
) {
|
|
|
|
document.documentElement.classList.add("dark");
|
|
|
|
|
|
|
|
document.getElementById("switch_light").classList.add("hidden");
|
|
|
|
document.getElementById("switch_dark").classList.remove("hidden");
|
|
|
|
} else if (
|
|
|
|
window.matchMedia("(prefers-color-scheme: light)").matches &&
|
2025-07-25 18:33:16 -04:00
|
|
|
(!window.localStorage.getItem("fluffle:theme") ||
|
|
|
|
window.localStorage.getItem("fluffle:theme") === "Auto")
|
2025-07-20 02:49:01 -04:00
|
|
|
) {
|
|
|
|
document.documentElement.classList.remove("dark");
|
|
|
|
|
|
|
|
document.getElementById("switch_light").classList.remove("hidden");
|
|
|
|
document.getElementById("switch_dark").classList.add("hidden");
|
2025-07-25 18:33:16 -04:00
|
|
|
} else if (window.localStorage.getItem("fluffle:theme")) {
|
2025-07-20 02:49:01 -04:00
|
|
|
/* restore theme */
|
2025-07-25 18:33:16 -04:00
|
|
|
const current = window.localStorage.getItem("fluffle:theme");
|
2025-07-20 02:49:01 -04:00
|
|
|
document.documentElement.className = current.toLowerCase();
|
|
|
|
|
|
|
|
if (current === "Light") {
|
|
|
|
document.getElementById("switch_light").classList.remove("hidden");
|
|
|
|
document.getElementById("switch_dark").classList.add("hidden");
|
|
|
|
} else {
|
|
|
|
document.getElementById("switch_light").classList.add("hidden");
|
|
|
|
document.getElementById("switch_dark").classList.remove("hidden");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-21 02:11:23 -04:00
|
|
|
globalThis.temporary_set_theme = (theme) => {
|
|
|
|
document.documentElement.className = theme.toLowerCase();
|
|
|
|
|
|
|
|
if (theme === "Light") {
|
|
|
|
document.getElementById("switch_light").classList.remove("hidden");
|
|
|
|
document.getElementById("switch_dark").classList.add("hidden");
|
|
|
|
} else {
|
|
|
|
document.getElementById("switch_light").classList.add("hidden");
|
|
|
|
document.getElementById("switch_dark").classList.remove("hidden");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-07-20 02:49:01 -04:00
|
|
|
globalThis.set_theme = (theme) => {
|
2025-07-25 18:33:16 -04:00
|
|
|
window.localStorage.setItem("fluffle:theme", theme);
|
2025-07-20 02:49:01 -04:00
|
|
|
document.documentElement.className = theme;
|
|
|
|
media_theme_pref();
|
|
|
|
};
|
|
|
|
|
|
|
|
media_theme_pref();
|
|
|
|
|
|
|
|
// messages
|
|
|
|
function get_cookie(key) {
|
|
|
|
return (document.cookie.split(`${key}=`)[1] || "").split(";")[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_message() {
|
|
|
|
const element = document.getElementById("messages");
|
|
|
|
|
|
|
|
const message = get_cookie("Atto-Message");
|
|
|
|
const message_good = get_cookie("Atto-Message-Good") === "true";
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
element.style.marginBottom = "1rem";
|
|
|
|
element.style.paddingLeft = "1rem";
|
2025-07-21 22:28:43 -04:00
|
|
|
element.innerHTML = `<li class="${message_good ? "green" : "red"}">${message.replaceAll('"', "")}</li>`;
|
2025-07-20 02:49:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear cookies
|
|
|
|
for (cookie of document.cookie.split(";")) {
|
|
|
|
// biome-ignore lint/suspicious/noDocumentCookie: cookie store is barely supported
|
|
|
|
document.cookie = `${cookie.split("=")[0]}=; expires=${new Date(0).toUTCString()}; path=/`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-21 02:11:23 -04:00
|
|
|
globalThis.show_message = (message, message_good = true) => {
|
|
|
|
const element = document.getElementById("messages");
|
|
|
|
element.style.marginBottom = "1rem";
|
|
|
|
element.style.paddingLeft = "1rem";
|
2025-07-21 22:28:43 -04:00
|
|
|
element.innerHTML = `<li class="${message_good ? "green" : "red"}">${message.replaceAll('"', "")}</li>`;
|
2025-07-21 02:11:23 -04:00
|
|
|
};
|
|
|
|
|
2025-07-20 02:49:01 -04:00
|
|
|
check_message();
|
|
|
|
|
|
|
|
// editor
|
2025-07-21 02:11:23 -04:00
|
|
|
globalThis.init_editor = (
|
|
|
|
name = "editor",
|
|
|
|
mode = "markdown",
|
|
|
|
element = "editor_tab",
|
|
|
|
content_element = "editor_content",
|
|
|
|
) => {
|
|
|
|
globalThis[name] = CodeMirror(document.getElementById(element), {
|
|
|
|
value: (document.getElementById(content_element) || { innerHTML: "" })
|
2025-07-20 02:49:01 -04:00
|
|
|
.innerHTML,
|
2025-07-21 02:11:23 -04:00
|
|
|
mode,
|
2025-07-20 02:49:01 -04:00
|
|
|
lineWrapping: true,
|
|
|
|
lineNumbers: false,
|
|
|
|
autoCloseBrackets: true,
|
|
|
|
autofocus: true,
|
|
|
|
viewportMargin: Number.POSITIVE_INFINITY,
|
2025-07-25 18:36:01 -04:00
|
|
|
inputStyle: "contenteditable",
|
2025-07-20 02:49:01 -04:00
|
|
|
highlightFormatting: false,
|
|
|
|
fencedCodeBlockHighlighting: false,
|
|
|
|
xml: false,
|
2025-07-20 18:29:43 -04:00
|
|
|
smartIndent: false,
|
2025-07-20 02:49:01 -04:00
|
|
|
indentUnit: 4,
|
2025-07-20 18:29:43 -04:00
|
|
|
tabSize: 4,
|
|
|
|
indentWithTabs: false,
|
2025-07-20 02:49:01 -04:00
|
|
|
placeholder: "",
|
|
|
|
extraKeys: {
|
|
|
|
Home: "goLineLeft",
|
|
|
|
End: "goLineRight",
|
|
|
|
Enter: (cm) => {
|
|
|
|
cm.replaceSelection("\n");
|
|
|
|
},
|
2025-07-20 18:29:43 -04:00
|
|
|
Tab: "insertSoftTab",
|
2025-07-20 02:49:01 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2025-07-21 02:11:23 -04:00
|
|
|
if (name === "editor") {
|
|
|
|
window.addEventListener("beforeunload", (e) => {
|
|
|
|
if (!globalThis.ALLOW_LEAVE) {
|
|
|
|
e.preventDefault();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2025-07-20 02:49:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
globalThis.tab_editor = () => {
|
|
|
|
document.getElementById("editor_tab").classList.remove("hidden");
|
|
|
|
document.getElementById("preview_tab").classList.add("hidden");
|
2025-07-21 02:11:23 -04:00
|
|
|
document.getElementById("metadata_tab").classList.add("hidden");
|
2025-07-20 02:49:01 -04:00
|
|
|
|
|
|
|
document.getElementById("editor_tab_button").classList.remove("camo");
|
|
|
|
document.getElementById("preview_tab_button").classList.add("camo");
|
2025-07-21 02:11:23 -04:00
|
|
|
document.getElementById("metadata_tab_button").classList.add("camo");
|
2025-07-25 18:06:29 -04:00
|
|
|
|
|
|
|
if (document.getElementById("metadata_css")) {
|
|
|
|
document.getElementById("metadata_css").remove();
|
|
|
|
}
|
2025-07-20 02:49:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
globalThis.tab_preview = async () => {
|
2025-07-24 01:06:03 -04:00
|
|
|
if (
|
|
|
|
!document
|
|
|
|
.getElementById("preview_tab_button")
|
|
|
|
.classList.contains("camo")
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-07-20 02:49:01 -04:00
|
|
|
// render
|
|
|
|
const res = await (
|
|
|
|
await fetch("/api/v1/render", {
|
|
|
|
method: "POST",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify({
|
|
|
|
content: globalThis.editor.getValue(),
|
2025-07-24 01:06:03 -04:00
|
|
|
metadata: globalThis.metadata_editor.getValue(),
|
2025-07-20 02:49:01 -04:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
).text();
|
|
|
|
|
|
|
|
document.getElementById("preview_tab").innerHTML = res;
|
|
|
|
hljs.highlightAll();
|
|
|
|
|
|
|
|
// ...
|
|
|
|
document.getElementById("editor_tab").classList.add("hidden");
|
|
|
|
document.getElementById("preview_tab").classList.remove("hidden");
|
2025-07-21 02:11:23 -04:00
|
|
|
document.getElementById("metadata_tab").classList.add("hidden");
|
2025-07-20 02:49:01 -04:00
|
|
|
|
|
|
|
document.getElementById("editor_tab_button").classList.add("camo");
|
|
|
|
document.getElementById("preview_tab_button").classList.remove("camo");
|
2025-07-21 02:11:23 -04:00
|
|
|
document.getElementById("metadata_tab_button").classList.add("camo");
|
|
|
|
};
|
|
|
|
|
|
|
|
globalThis.first_time_on_metadata_tab = true;
|
|
|
|
globalThis.tab_metadata = () => {
|
|
|
|
document.getElementById("editor_tab").classList.add("hidden");
|
|
|
|
document.getElementById("preview_tab").classList.add("hidden");
|
|
|
|
document.getElementById("metadata_tab").classList.remove("hidden");
|
|
|
|
|
|
|
|
document.getElementById("editor_tab_button").classList.add("camo");
|
|
|
|
document.getElementById("preview_tab_button").classList.add("camo");
|
|
|
|
document.getElementById("metadata_tab_button").classList.remove("camo");
|
|
|
|
|
|
|
|
if (globalThis.first_time_on_metadata_tab) {
|
|
|
|
globalThis.metadata_editor.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
globalThis.first_time_on_metadata_tab = false;
|
2025-07-20 02:49:01 -04:00
|
|
|
};
|
2025-07-20 20:43:01 -04:00
|
|
|
|
|
|
|
let exists_timeout = null;
|
|
|
|
globalThis.check_exists_input = (e) => {
|
|
|
|
if (exists_timeout) {
|
|
|
|
clearTimeout(exists_timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
exists_timeout = setTimeout(async () => {
|
|
|
|
if (e.target.value.length < 2 || e.target.value.length > 32) {
|
|
|
|
e.target.setCustomValidity("");
|
|
|
|
e.target.removeAttribute("data-invalid");
|
|
|
|
e.target.reportValidity();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const exists = (
|
|
|
|
await (await fetch(`/api/v1/entries/${e.target.value}`)).json()
|
|
|
|
).payload;
|
|
|
|
|
|
|
|
console.log(exists);
|
|
|
|
if (exists) {
|
|
|
|
e.target.setCustomValidity("Slug is already in use");
|
|
|
|
e.target.setAttribute("data-invalid", "true");
|
|
|
|
} else {
|
|
|
|
e.target.setCustomValidity("");
|
|
|
|
e.target.removeAttribute("data-invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
e.target.reportValidity();
|
|
|
|
}, 1000);
|
|
|
|
};
|
2025-07-25 15:12:15 -04:00
|
|
|
|
|
|
|
// components
|
|
|
|
function close_dropdowns() {
|
|
|
|
for (const dropdown of Array.from(
|
|
|
|
document.querySelectorAll(".inner.open"),
|
|
|
|
)) {
|
|
|
|
dropdown.classList.remove("open");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
globalThis.open_dropdown = (event) => {
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
let target = event.target;
|
|
|
|
|
|
|
|
while (!target.matches(".dropdown")) {
|
|
|
|
target = target.parentElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close all others
|
|
|
|
close_dropdowns();
|
|
|
|
|
|
|
|
// open
|
|
|
|
setTimeout(() => {
|
|
|
|
for (const dropdown of Array.from(target.querySelectorAll(".inner"))) {
|
|
|
|
// check y
|
|
|
|
const box = target.getBoundingClientRect();
|
|
|
|
|
|
|
|
let parent = dropdown.parentElement;
|
|
|
|
|
|
|
|
while (!parent.matches("html, .window")) {
|
|
|
|
parent = parent.parentElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
let parent_height = parent.getBoundingClientRect().y;
|
|
|
|
|
|
|
|
if (parent.nodeName === "HTML") {
|
|
|
|
parent_height = window.screen.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
const scroll = window.scrollY;
|
|
|
|
const height = parent_height;
|
|
|
|
const y = box.y + scroll;
|
|
|
|
|
|
|
|
if (y > height - scroll - 375) {
|
|
|
|
dropdown.classList.add("top");
|
|
|
|
} else {
|
|
|
|
dropdown.classList.remove("top");
|
|
|
|
}
|
|
|
|
|
|
|
|
// open
|
|
|
|
dropdown.classList.add("open");
|
|
|
|
|
|
|
|
if (dropdown.classList.contains("open")) {
|
|
|
|
dropdown.removeAttribute("aria-hidden");
|
|
|
|
} else {
|
|
|
|
dropdown.setAttribute("aria-hidden", "true");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 5);
|
|
|
|
};
|
|
|
|
|
|
|
|
globalThis.init_dropdowns = (bind_to) => {
|
|
|
|
for (const dropdown of Array.from(document.querySelectorAll(".inner"))) {
|
|
|
|
dropdown.setAttribute("aria-hidden", "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
bind_to.addEventListener("click", (event) => {
|
|
|
|
if (
|
|
|
|
event.target.matches(".dropdown") ||
|
|
|
|
event.target.matches("[exclude=dropdown]")
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const dropdown of Array.from(
|
|
|
|
document.querySelectorAll(".inner.open"),
|
|
|
|
)) {
|
|
|
|
dropdown.classList.remove("open");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
globalThis.METADATA_CSS_ENABLED = true;
|
|
|
|
globalThis.toggle_metadata_css = (e) => {
|
|
|
|
e.target.classList.add("yellow");
|
|
|
|
|
|
|
|
METADATA_CSS_ENABLED = !METADATA_CSS_ENABLED;
|
|
|
|
if (!METADATA_CSS_ENABLED) {
|
|
|
|
media_theme_pref(); // user user theme
|
2025-07-25 19:12:16 -04:00
|
|
|
document.getElementById("metadata_css").remove(); // remove css
|
2025-07-25 15:12:15 -04:00
|
|
|
|
|
|
|
// reset colored text
|
|
|
|
for (const element of Array.from(
|
|
|
|
document.querySelectorAll(".color_block"),
|
|
|
|
)) {
|
|
|
|
element.removeAttribute("style");
|
|
|
|
element.classList.remove("color_block");
|
|
|
|
}
|
|
|
|
|
|
|
|
// strikethrough auto theme since it's disabled
|
|
|
|
document.getElementById("auto_theme").style.textDecoration =
|
|
|
|
"line-through";
|
|
|
|
} else {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
};
|