import { start } from "/__devpage/design-stream.js";
import { Host, asked, configure, hosts } from "/__devpage/design-canvas.js";
let Applied, Design, Gpu, failure;
try {
const engine = await import("/__devpage/design-engine.js");
await engine.default({ module_or_path: "/__devpage/design-engine.wasm" });
({ Applied, Design, Gpu } = engine);
} catch (error) {
failure = error;
}
const print = !!document.querySelector("script[data-print]");
let gpu = null;
if (!print && !failure) {
try {
gpu = await Gpu.open();
} catch (error) {
console.info("devpage: no WebGPU, drawing designs on the CPU", error);
}
}
const started = new WeakSet();
const mount = document.documentElement.dataset.mount ?? "";
function address(link, base) {
const path = decodeURIComponent(new URL(link, base).pathname);
return (path.startsWith(`${mount}/`) ? path.slice(mount.length) : path).replace(/^\//, "");
}
function fail(canvas, message) {
canvas.dataset.failed = "true";
const note = document.createElement("p");
note.className = "design-failed";
note.textContent = message;
canvas.before(note);
}
export function scan(root = document) {
const carried = [];
for (const host of [...hosts]) {
if (host.canvas.isConnected || host.mode === "overlay") continue;
if (host.mode === "embed") {
carried.push({ src: host.canvas.getAttribute("src"), path: host.path, history: host.history });
}
host.close();
}
const page = document.body.classList.contains("design-page");
for (const canvas of root.querySelectorAll("canvas.design[src]")) {
if (started.has(canvas)) continue;
started.add(canvas);
canvas.dataset.started = "true";
if (failure) {
console.error("devpage: the design engine didn't load", failure);
fail(canvas, `This design can't be drawn: the design engine didn't load (${failure.message ?? failure}).`);
continue;
}
const base = canvas.closest("[data-source]")?.dataset.source;
const path = address(canvas.getAttribute("src"), new URL(base ?? location.pathname, location.origin));
const at = page ? -1 : carried.findIndex((state) => state.src === canvas.getAttribute("src"));
const state = at >= 0 ? carried.splice(at, 1)[0] : null;
const host = new Host(canvas, state?.path ?? path, { mode: page ? "page" : "embed" });
if (state) host.history = state.history;
if (page) {
addEventListener("popstate", () => {
host.closeOverlay();
host.open(address(location.pathname, location.origin));
});
}
}
}
addEventListener("resize", () => {
for (const host of hosts) host.paint();
});
new MutationObserver(() => {
for (const host of hosts) host.paint();
}).observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
if (document.body.classList.contains("design-page")) {
const system = matchMedia("(prefers-color-scheme: dark)");
const saved = () => {
try {
const theme = localStorage.getItem("devpage-theme");
return theme === "light" || theme === "dark" ? theme : null;
} catch {
return null;
}
};
let chosen = asked;
const apply = (theme = chosen ?? saved()) => {
document.documentElement.dataset.theme = theme || (system.matches ? "dark" : "light");
const label = document.documentElement.dataset.theme === "dark" ? "Switch to light" : "Switch to dark";
for (const button of document.querySelectorAll(".theme-toggle")) {
button.title = label;
button.setAttribute("aria-label", label);
}
};
apply();
system.addEventListener("change", () => apply());
document.addEventListener("click", (event) => {
if (!event.target.closest?.(".theme-toggle")) return;
const theme = document.documentElement.dataset.theme === "dark" ? "light" : "dark";
try {
localStorage.setItem("devpage-theme", theme);
} catch {
}
chosen = theme;
apply(theme);
});
}
configure({ Design, Applied, gpu, print, mount });
start({
mount,
print,
shown: () => [...hosts].filter((host) => host.canvas.isConnected).map((host) => host.path),
deliver: (path, message) => {
for (const host of hosts) {
if (host.path === path && host.canvas.isConnected) host.receive(message);
}
},
});
scan();