import m from '@garage44/mithril' import {Icon} from '@/lib/icons' import {type Inline, paragraphs, parseInline} from './markup' import {type PageData, type PageNode, type PropValue, type Resolved, isTextRef, registry, resolve, safeHref} from './tree' /** * The content pages' components, drawn from a page's tree (`docs/mockups/cms`). * Each view gets its props with every `$t` already resolved for the language * being drawn, and its children already drawn (`blocks`) or resolved (`text`). * What a component may be given is `registry.json`, which the simulator checks a * page against when it is saved; a view still treats a missing prop as absent * rather than trusting that, because a tree saved by an older registry can meet a * newer bundle. */ export interface Ctx { data: PageData } type Props = Record interface ViewArgs { ctx: Ctx props: Props /** `blocks` children, drawn. */ blocks: m.Children[] /** A `text` child, resolved. */ text: Resolved | null } /** The `lang` attribute a text needs: only when it fell back to another language. */ export function langOf(ctx: Ctx, r: Resolved | null | undefined): string | undefined { return r && r.lang !== ctx.data.lang ? r.lang : undefined } function drawInline(nodes: Inline[]): m.Children { return nodes.map((n): m.Children => { if (typeof n === 'string') return n if ('b' in n) return {drawInline(n.b)} if ('i' in n) return {drawInline(n.i)} // A link a text carries is only checked here: a bad one keeps its words. return safeHref(n.a) ? {drawInline(n.ch)} : drawInline(n.ch) }) as m.Children } /** A text inside one element (a heading, a button): its markup, its line breaks. */ export function inline(r: Resolved | null | undefined): m.Children { if (!r) return null const lines = r.text.split('\n') // Nested arrays are fragments to Mithril; its types only spell one level. return lines.map((line, i) => [i ?
: null, drawInline(parseInline(line))]) as unknown as m.Children } /** A text as paragraphs, each marked with its language when it fell back. */ export function paras(ctx: Ctx, r: Resolved | null | undefined): m.Children { if (!r) return null const lang = langOf(ctx, r) return paragraphs(r.text).map((p) =>

{inline({lang: r.lang, text: p})}

) } function str(v: Props[string]): string | undefined { return typeof v === 'string' ? v : undefined } function txt(v: Props[string]): Resolved | undefined { return typeof v === 'object' ? v : undefined } const VIEWS: Record m.Children> = { ButtonLink: ({ctx, props, text}) => { const href = str(props.href) if (!href || !safeHref(href)) return null const variant = str(props.variant) === 'secondary' ? 'secondary' : 'primary' return ( {inline(text)} ) }, Columns: ({blocks}) =>
{blocks}
, DownloadButtons: ({ctx}) => { const label = (key: string) => { const r = resolve(ctx.data, key) return {inline(r)} } return ( ) }, Feature: ({ctx, props, text}) => { const heading = txt(props.heading) const icon = str(props.icon) return (
{icon ? Icon(icon, 'cms-feature-icon') : null}

{inline(heading)}

{paras(ctx, text)}
) }, Hero: ({ctx, props, blocks}) => { const heading = txt(props.heading) const image = str(props.image) return (
{image && safeHref(image) ? : null}

{inline(heading)}

{blocks}
) }, Image: ({ctx, props}) => { const src = str(props.src) if (!src || !safeHref(src)) return null const alt = txt(props.alt) const caption = txt(props.caption) return (
{alt?.text {caption ?
{inline(caption)}
: null}
) }, Page: ({blocks}) =>
{blocks}
, PlaceLink: ({ctx, props, text}) => { const place = str(props.place) if (!place) return inline(text) return ( {Icon('location_on', 'cms-place-icon')} {inline(text)} ) }, Quote: ({ctx, props, text}) => { const by = txt(props.by) return (
{paras(ctx, text)} {by ?
— {inline(by)}
: null}
) }, Section: ({ctx, props, blocks}) => { const heading = txt(props.heading) return (
{heading ?

{inline(heading)}

: null} {blocks}
) }, Text: ({ctx, props, text}) =>
{paras(ctx, text)}
, } /** Every component the registry names has a view, and nothing else does (`cms.test.ts`). */ export const VIEW_NAMES = Object.keys(VIEWS) function prop(ctx: Ctx, v: PropValue): Props[string] { if (isTextRef(v)) return resolve(ctx.data, v.t) ?? undefined return v } /** Draw one node of a page's tree, and everything under it. */ export function draw(ctx: Ctx, node: PageNode): m.Children { const spec = registry.components[node.c] const view = VIEWS[node.c] if (!spec || !view) { // A tree from a newer registry than this bundle: leave the block out. console.warn(`content page: no component ${node.c}`) return null } const props: Props = {} for (const [name, value] of Object.entries(node.p ?? {})) props[name] = prop(ctx, value) const children = node.ch ?? [] const blocks = spec.children === 'blocks' ? children.map((c) => (isTextRef(c) ? null : draw(ctx, c))) : [] const first = children[0] const text = spec.children === 'text' && first && isTextRef(first) ? resolve(ctx.data, first.t) : null return view({blocks, ctx, props, text}) }