UNPKG

decocms

Version:

Deco CMS — Self-hostable MCP Gateway for managing AI connections and tools

89 lines (88 loc) • 2.82 kB
const w = "__decoStudioRefresh"; const y = "deco-studio:html-swap", m = "deco-studio-no-transitions"; function g(t) { const e = () => { }; t.ready?.catch(e), t.updateCallbackDone?.catch(e), t.finished.catch(e); } function E(t, e) { const r = t.startViewTransition; if (typeof r != "function") return e(), Promise.resolve(); const i = r.call(t, e); return g(i), i.finished.catch(() => { }); } function l(t) { dispatchEvent( new CustomEvent(y, { detail: { phase: t } }) ); } function S(t, e) { const r = new DOMParser().parseFromString(e, "text/html"), i = t.documentElement; for (const { name: s } of Array.from(i.attributes)) r.documentElement.hasAttribute(s) || i.removeAttribute(s); for (const { name: s, value: a } of Array.from(r.documentElement.attributes)) i.setAttribute(s, a); const n = r.createElement("style"); n.id = m, n.textContent = "* { transition: none !important; }", r.head.append(n), i.innerHTML = r.documentElement.innerHTML; } function A(t) { const e = Reflect.get(t, w); return typeof e == "function" ? e : null; } const v = (t) => { let e = null, r = location.href; const i = async (n, s) => { e?.abort(); const a = new AbortController(); e = a; const c = () => e === a; t.send({ type: "html-swap:start", id: n }), l("start"); try { const o = new URL(s ?? r, location.href); if (o.origin !== location.origin) throw new Error(`Not this page's origin: ${o.origin}`); const f = A(window); if (f) { if (await f(o.href), !c()) return; r = o.href, e = null, l("end"), t.send({ type: "html-swap:end", id: n }); return; } const u = await fetch(o.href, { headers: { accept: "text/html" }, credentials: "same-origin", cache: "no-store", signal: a.signal }); if (!u.ok) throw new Error(`The page answered ${u.status}`); const h = await u.text(), p = new DOMParser().parseFromString(h, "text/html").head; if (await Promise.all( Array.from( p.querySelectorAll('link[rel="stylesheet"]'), (d) => d.href ? fetch(d.href, { signal: a.signal }).catch(() => { }) : null ) ), !c() || (await E(document, () => S(document, h)), r = o.href, document.getElementById(m)?.remove(), !c())) return; e = null, l("end"), t.send({ type: "html-swap:end", id: n }); } catch (o) { if (!c()) return; e = null, l("end"), t.send({ type: "html-swap:error", id: n, message: o instanceof Error ? o.message : String(o) }); } }; t.on((n) => { n.type === "html-swap:refresh" && typeof n.id == "number" && i( n.id, typeof n.url == "string" ? n.url : void 0 ); }); }; export { v as default };