98 lines
7.7 KiB
JavaScript
98 lines
7.7 KiB
JavaScript
/*
|
|
* Copyright (C) 2026 Mateusz Gruszczyński @linuxiarz.pl
|
|
* Source-Available Code / Dual-Licensed.
|
|
*
|
|
* Free for non-commercial and evaluation use under terms of BSL/GPLv3.
|
|
* Commercial or production use requires a valid paid license.
|
|
* See LICENSE file in repository root for details.
|
|
*/
|
|
|
|
import { installGlobalDiagnostics, logInfo } from "@rustpad/logger";
|
|
installGlobalDiagnostics();
|
|
|
|
import { api } from "@rustpad/api";
|
|
import { copyText } from "@rustpad/clipboard";
|
|
import { alignPreviewLineNumbers, renderMarkdown, setMarkdownFiles } from "@rustpad/markdown";
|
|
import { toast } from "@rustpad/toast";
|
|
import { getTheme } from "@rustpad/theme";
|
|
import { loadHighlight, loadMediaPlayer, loadMermaid } from "@rustpad/vendor-libs";
|
|
|
|
const token = location.pathname.split("/").filter(Boolean)[1];
|
|
const content = document.querySelector("#public-content");
|
|
const lineNumbersToggle = document.querySelector("#public-line-numbers-toggle");
|
|
const fullWidthToggle = document.querySelector("#public-full-width-toggle");
|
|
const fullWidthStorageKey = "rustpad:public-full-width";
|
|
const passwordDialog = document.querySelector("#public-password-dialog"), passwordForm = document.querySelector("#public-password-form"), passwordInput = document.querySelector("#public-password"), passwordError = document.querySelector("#public-password-error");
|
|
let pagePassword = "";
|
|
let mermaidRenderVersion = 0;
|
|
function pageHeaders() { return pagePassword ? { "X-RustPad-Page-Password": pagePassword } : {}; }
|
|
async function renderMermaid() {
|
|
const renderVersion = ++mermaidRenderVersion;
|
|
const nodes = [...content.querySelectorAll(".mermaid")];
|
|
if (!nodes.length) return;
|
|
try {
|
|
const mermaid = await loadMermaid();
|
|
mermaid.initialize({ startOnLoad: false, theme: getTheme() === "dark" ? "dark" : "default", securityLevel: "strict" });
|
|
await mermaid.run({ nodes });
|
|
} catch {
|
|
if (renderVersion !== mermaidRenderVersion) return;
|
|
nodes.forEach(node => {
|
|
if (!node.isConnected || !content.contains(node) || !node.parentNode) return;
|
|
const message = document.createElement("p");
|
|
message.className = "error mermaid-error";
|
|
message.textContent = "Failed to load Mermaid.";
|
|
node.parentNode.insertBefore(message, node);
|
|
});
|
|
}
|
|
}
|
|
async function renderCodeHighlight() { const blocks = content.querySelectorAll('pre code[class^="language-"]'); if (!blocks.length) return; try { const hljs = await loadHighlight(); blocks.forEach(block => { const lines = block.querySelectorAll(".code-line"); if (!lines.length) { hljs.highlightElement(block); return; } const language = [...block.classList].find(name => name.startsWith("language-"))?.slice(9); lines.forEach(line => { try { line.innerHTML = hljs.highlight(line.textContent, { language, ignoreIllegals: true }).value; } catch { line.innerHTML = hljs.highlightAuto(line.textContent).value; } }); block.classList.add("hljs"); }); } catch { } }
|
|
async function renderMediaPlayers() { const nodes = content.querySelectorAll("[data-rustpad-player]"); if (!nodes.length) return; try { const { hydrateMediaPlayers } = await loadMediaPlayer(); hydrateMediaPlayers(content); } catch { } }
|
|
function lockPublicContent(allowTaskUpdates) {
|
|
content.querySelectorAll('[contenteditable]').forEach(node => node.removeAttribute('contenteditable'));
|
|
content.querySelectorAll('.preview-editable').forEach(node => node.classList.remove('preview-editable'));
|
|
content.querySelectorAll('.task-checkbox').forEach(box => { box.disabled = !allowTaskUpdates; box.title = allowTaskUpdates ? 'Update this task' : 'Task updates are disabled by the owner'; });
|
|
}
|
|
function scrollToPublicAnchor(hash, behavior = "auto") {
|
|
const rawId = String(hash || "").replace(/^#/, "");
|
|
if (!rawId) return false;
|
|
let id;
|
|
try { id = decodeURIComponent(rawId); } catch { id = rawId; }
|
|
const target = document.getElementById(id);
|
|
if (!target || !content.contains(target)) return false;
|
|
for (let details = target.closest("details"); details; details = details.parentElement?.closest("details")) details.open = true;
|
|
target.scrollIntoView({ behavior, block: "start" });
|
|
return true;
|
|
}
|
|
function setFullWidth(enabled, { persist = true } = {}) {
|
|
const active = Boolean(enabled);
|
|
document.body.classList.toggle("public-full-width", active);
|
|
fullWidthToggle.checked = active;
|
|
if (persist) {
|
|
try { localStorage.setItem(fullWidthStorageKey, active ? "1" : "0"); } catch { }
|
|
}
|
|
requestAnimationFrame(() => alignPreviewLineNumbers(content));
|
|
}
|
|
function restoreFullWidth() {
|
|
let enabled = false;
|
|
try { enabled = localStorage.getItem(fullWidthStorageKey) === "1"; } catch { }
|
|
setFullWidth(enabled, { persist: false });
|
|
}
|
|
async function initialize() { try { const page = await api(`/api/public/${encodeURIComponent(token)}`, { headers: pageHeaders() }); if (passwordDialog.open) passwordDialog.close(); passwordError.textContent = ""; document.querySelector("#public-title").textContent = page.title; document.querySelector("#public-meta").textContent = `Updated: ${new Date(page.updated_at).toLocaleString("en-US")}${page.allow_task_updates ? " · tasks can be updated" : ""}`; document.title = `${page.title} · RustPad`; setMarkdownFiles(page.files || []); content.innerHTML = renderMarkdown(page.content); alignPreviewLineNumbers(content); lockPublicContent(page.allow_task_updates); await Promise.all([renderMermaid(), renderCodeHighlight(), renderMediaPlayers()]); requestAnimationFrame(() => scrollToPublicAnchor(location.hash)); } catch (error) { if (error.status === 401 || error.status === 403) { passwordError.textContent = error.status === 403 ? "Sign in with an authorized account or enter the resource password." : "Enter the correct password."; if (!passwordDialog.open) passwordDialog.showModal(); passwordInput.focus(); return; } content.replaceChildren(); const message = document.createElement("p"); message.className = "error"; message.textContent = String(error.message); content.append(message); } }
|
|
content.addEventListener("click", event => {
|
|
const link = event.target.closest('a[href^="#"]');
|
|
if (!link || !content.contains(link)) return;
|
|
const hash = link.getAttribute("href");
|
|
event.preventDefault();
|
|
if (!hash || hash === "#" || !scrollToPublicAnchor(hash, "smooth")) return;
|
|
history.replaceState(null, "", `${location.pathname}${location.search}${hash}`);
|
|
});
|
|
window.addEventListener("hashchange", () => scrollToPublicAnchor(location.hash, "smooth"));
|
|
content.addEventListener("change", async event => { const box = event.target.closest(".task-checkbox"); if (!box || box.disabled) return; const previous = !box.checked; box.disabled = true; try { const page = await api(`/api/public/${encodeURIComponent(token)}/tasks`, { method: "POST", headers: pageHeaders(), body: JSON.stringify({ source_line: Number(box.dataset.sourceLine), checked: box.checked }) }); document.querySelector("#public-meta").textContent = `Updated: ${new Date(page.updated_at).toLocaleString("en-US")} · tasks can be updated`; toast("Task saved"); } catch (error) { box.checked = previous; toast(error.message); } finally { box.disabled = false; } });
|
|
lineNumbersToggle.addEventListener("change", () => { document.body.classList.toggle("hide-preview-line-numbers", !lineNumbersToggle.checked); });
|
|
fullWidthToggle.addEventListener("change", () => setFullWidth(fullWidthToggle.checked));
|
|
window.addEventListener("resize", () => requestAnimationFrame(() => alignPreviewLineNumbers(content)));
|
|
passwordForm.addEventListener("submit", async event => { event.preventDefault(); pagePassword = passwordInput.value; await initialize(); });
|
|
passwordDialog.addEventListener("cancel", event => event.preventDefault());
|
|
document.querySelector("#copy-public-link").addEventListener("click", async () => { try { await copyText(location.href); toast("Link copied"); } catch (error) { toast(error.message); } });
|
|
initialize();
|