Files
validation-lab-site/public/admin/preview.js
T
2026-11-21 22:08:31 +01:00

150 lines
7.7 KiB
JavaScript

// Custom preview template for Sveltia CMS: mirrors the site's
// Site.astro layout so the preview pane shows what the published page
// will look like (hero banner, cards, journey, capability checklist),
// instead of Sveltia's generic field dump.
//
// Runs in the browser as-is: the CMS bundle exposes the globals `CMS`
// and `html` (HTM, bound to the CMS's React copy). Markup elements can
// carry data-key-path so click-to-highlight keeps working — clicking
// a section in the preview focuses that field in the edit pane.
const css = `
:root { --ink:#16213a; --muted:#5b6478; --accent:#3a55d9; --accent-soft:#eef1fd; --line:#e3e7f2; --bg:#fbfcfe; }
* { box-sizing:border-box; }
body { margin:0; font-family:ui-sans-serif,system-ui,-apple-system,'Segoe UI',sans-serif; color:var(--ink); background:var(--bg); line-height:1.65; }
.pv-nav { display:flex; gap:0.75rem; align-items:center; padding:0.9rem 1.5rem; border-bottom:1px solid var(--line); background:#fff; color:var(--muted); font-size:0.85rem; }
.pv-nav strong { color:var(--ink); display:flex; gap:0.45rem; align-items:center; }
main { max-width:46rem; margin:0 auto; padding:0 1.25rem 3rem; }
.pv-hero { position:relative; overflow:hidden; margin:0 -1.25rem; padding:3rem 1.25rem 2.6rem;
background:radial-gradient(42rem 20rem at 85% -10%, rgba(135,87,216,0.12), transparent 60%),
linear-gradient(180deg, var(--accent-soft), var(--bg)); border-bottom:1px solid var(--line); }
.pv-hero h1 { font-size:2.2rem; line-height:1.15; margin:0 0 1rem; letter-spacing:-0.02em; max-width:26rem; }
.pv-hero p { font-size:1.1rem; color:var(--muted); max-width:34rem; margin:0; }
.pv-cta { margin-top:1.4rem; display:flex; gap:0.6rem; flex-wrap:wrap; }
.pv-cta span { background:var(--accent); color:#fff; border-radius:0.5rem; padding:0.55rem 1rem; font-weight:600; font-size:0.95rem; }
.pv-cta span.sec { background:#fff; color:var(--accent); border:1px solid var(--accent); }
h2 { font-size:1.35rem; margin:2.2rem 0 0.8rem; letter-spacing:-0.01em; }
.pv-cards { margin-top:1rem; display:grid; gap:1rem; grid-template-columns:repeat(auto-fit,minmax(14rem,1fr)); }
.pv-card { background:#fff; border:1px solid var(--line); border-radius:0.7rem; padding:1rem 1.1rem; }
.pv-card strong { display:block; margin-bottom:0.4rem; }
.pv-card small { color:var(--muted); font-size:0.93rem; line-height:1.5; }
.pv-journey ol { list-style:none; padding:0; margin:0.5rem 0 0; display:grid; gap:0.55rem; }
.pv-journey li { padding:0.55rem 0.9rem; background:#fff; border:1px solid var(--line); border-inline-start:3px solid var(--accent); border-radius:0.45rem; color:var(--muted); font-size:0.95rem; }
.pv-journey li b { color:var(--ink); }
.pv-cap { display:flex; gap:0.8rem; align-items:flex-start; background:var(--accent-soft); border:1px solid var(--line); border-radius:0.7rem; padding:0.85rem 1rem; margin-bottom:0.6rem; }
.pv-cap .tick { width:1.05rem; height:1.05rem; border:2px solid var(--accent); border-radius:0.3rem; flex:none; margin-top:0.2rem; }
.pv-cap strong { display:block; }
.pv-cap small { color:var(--muted); }
.pv-request { display:inline-block; background:var(--accent); color:#fff; border-radius:0.5rem; padding:0.55rem 1rem; font-weight:600; font-size:0.95rem; margin-top:0.6rem; }
.pv-body { margin-top:1.6rem; }
.pv-body h1, .pv-body h2, .pv-body h3 { margin-top:1.5rem; }
.pv-body img { max-width:100%; height:auto; border-radius:0.5rem; }
.pv-body ul { padding-inline-start:1.5rem; }
.pv-body blockquote { margin:1.2rem 0; padding:0.4rem 1rem; border-inline-start:3px solid var(--accent); color:var(--muted); }
.pv-empty { color:var(--muted); font-size:0.9rem; padding:1rem; text-align:center; }
`;
const items = (widgets) =>
(Array.isArray(widgets) ? widgets : []).map((w) => w.getIn(["data"]));
const PagePreview = ({ entry, widgetFor, widgetsFor }) => {
const d = (key) => entry.getIn(["data", key]);
const valueProps = items(widgetsFor("value_props"));
const journey = items(widgetsFor("journey"));
const caps = items(widgetsFor("capabilities"));
const hasHero = d("hero_headline") || d("hero_sub");
return html`
<div>
<div class="pv-nav">
<strong>
<svg width="18" height="18" viewBox="0 0 32 32" aria-hidden="true">
<defs>
<linearGradient id="pv-mark" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="#3a55d9" />
<stop offset="1" stop-color="#8757d8" />
</linearGradient>
</defs>
<rect width="32" height="32" rx="8" fill="url(#pv-mark)" />
<path
d="M8.5 20.5c1.4-9.6 6.6-10.7 7-10.7 2.3 0-1.6 8.6 1.2 8.6 1.7 0 3.9-4.5 6.8-6.9"
fill="none" stroke="#fff" stroke-width="2.4" stroke-linecap="round"
/>
</svg>
Validation Lab — preview
</strong>
</div>
<main>
${hasHero &&
html`
<section class="pv-hero">
<h1 data-key-path="hero_headline" tabindex="0">${d("hero_headline")}</h1>
${d("hero_sub") &&
html`<p data-key-path="hero_sub" tabindex="0">${d("hero_sub")}</p>`}
${(d("cta_founders") || d("cta_hubs")) &&
html`
<p class="pv-cta">
${d("cta_founders") && html`<span data-key-path="cta_founders">${d("cta_founders")}</span>`}
${d("cta_hubs") && html`<span class="sec" data-key-path="cta_hubs">${d("cta_hubs")}</span>`}
</p>
`}
</section>
`}
${valueProps.length > 0 &&
html`
<section>
<h2>${d("value_props_title") || "Cards"}</h2>
<div class="pv-cards">
${valueProps.map((v, i) =>
html`<div class="pv-card" data-key-path={`value_props.${i}.heading`} tabindex="0">
<strong>${v.get("heading")}</strong>
<small>${v.get("text")}</small>
</div>`,
)}
</div>
</section>
`}
${journey.length > 0 &&
html`
<section class="pv-journey">
<h2>${d("journey_title") || "The journey"}</h2>
${d("journey_note") &&
html`<p data-key-path="journey_note" tabindex="0">${d("journey_note")}</p>`}
<ol>
${journey.map((s, i) =>
html`<li data-key-path={`journey.${i}.title`} tabindex="0">
<b>${s.get("title")}</b>${s.get("text") ? html` — ${s.get("text")}` : null}
</li>`,
)}
</ol>
</section>
`}
${caps.length > 0 &&
html`
<section>
<h2>${d("capabilities_title")}</h2>
${d("capabilities_note") &&
html`<p data-key-path="capabilities_note" tabindex="0">${d("capabilities_note")}</p>`}
${caps.map((c, i) =>
html`<div class="pv-cap" data-key-path={`capabilities.${i}.heading`} tabindex="0">
<span class="tick"></span>
<span>
<strong>${c.get("heading")}</strong>
<small>${c.get("text")}</small>
</span>
</div>`,
)}
${d("capabilities_cta") && html`<span class="pv-request">${d("capabilities_cta")}</span>`}
</section>
`}
<div class="pv-body" data-key-path="body">${widgetFor("body")}</div>
${!hasHero && valueProps.length === 0 && journey.length === 0 && caps.length === 0 &&
html`<p class="pv-empty">No structured sections — the preview shows the body only.</p>`}
</main>
</div>
`;
};
CMS.registerPreviewStyle(css, { raw: true });
CMS.registerPreviewTemplate("pages", PagePreview);