import type { ComponentType } from 'react' import type { BlockComponentMap } from '../blocks/types.js' import type { BlockContent, BlockContext, BlockUi } from './context.js' import type { LibraryBlockSlug } from './types.js' import type { CMSLinkProps } from './ui/CMSLink.js' import type { PartClasses } from './ui/cx.js' import type { MediaProps } from './ui/Media.js' import type { SectionProps } from './ui/Section.js' import { ContactBlock } from './blocks/contact/Component.js' import { CtaBlock } from './blocks/cta/Component.js' import { EntryListBlock } from './blocks/entryList/Component.js' import { FaqBlock } from './blocks/faq/Component.js' import { FeatureGridBlock } from './blocks/featureGrid/Component.js' import { GalleryBlock } from './blocks/gallery/Component.js' import { HeroBlock } from './blocks/hero/Component.js' import { LogosBlock } from './blocks/logos/Component.js' import { PricingBlock } from './blocks/pricing/Component.js' import { RichTextBlock } from './blocks/richText/Component.js' import { StatsBlock } from './blocks/stats/Component.js' import { StepsBlock } from './blocks/steps/Component.js' import { TeamBlock } from './blocks/team/Component.js' import { TestimonialsBlock } from './blocks/testimonials/Component.js' import { TextMediaBlock } from './blocks/textMedia/Component.js' import { buttonClass } from './ui/Button.js' import { Card } from './ui/Card.js' import { CMSLink } from './ui/CMSLink.js' import { Container } from './ui/Container.js' import { Media } from './ui/Media.js' import { Section, SECTION_BACKGROUNDS } from './ui/Section.js' import { SectionHeader } from './ui/SectionHeader.js' /** The plugin's component for each library block, by default slug. */ export const LIBRARY_BLOCK_COMPONENTS = { contact: ContactBlock, cta: CtaBlock, entryList: EntryListBlock, faq: FaqBlock, featureGrid: FeatureGridBlock, gallery: GalleryBlock, hero: HeroBlock, logos: LogosBlock, pricing: PricingBlock, richText: RichTextBlock, stats: StatsBlock, steps: StepsBlock, team: TeamBlock, testimonials: TestimonialsBlock, textMedia: TextMediaBlock, } as const export type BlockRegistryOptions = { /** Section backgrounds (`settings.background` values) → classes, merged over the defaults. */ backgrounds?: Record /** Classes per block part, replacing that part's defaults: `{ faq: { item: 'border-b border-border' } }`. */ classNames?: Partial> /** Replace a block's component. It gets the same props (data, locale, isFirst, ui, ctx, classNames). */ components?: Partial>> /** The project's own blocks, in the same registry. */ extra?: BlockComponentMap /** Turn on only these library blocks. Default: all 15 (an unused entry costs nothing). */ only?: readonly LibraryBlockSlug[] /** Renamed slugs (BlockOptions `slug`), so the registry key matches the stored blockType. */ slugs?: Partial> /** Swap UI primitives for every library block (e.g. the project's own Media). */ ui?: Partial } & Omit & Partial> /** * The `blockType → component` map for RenderBlocks / createPageRoute, with the library * blocks bound to the project's lib/content and options: * * // src/blocks/registry.ts * export const blockRegistry = createBlockRegistry(content, { sendForm, extra: { timeline: Timeline } }) * * Everything is bound on the server, so no function crosses to the client. */ export function createBlockRegistry(content: BlockContent, options: BlockRegistryOptions = {}): BlockComponentMap { const { backgrounds, classNames = {}, components = {}, extra = {}, only, slugs = {}, ui: own = {}, ...rest } = options const ctx: BlockContext = { ...rest, content, lightbox: rest.lightbox ?? true } const ui = buildUi(ctx, own, backgrounds) const map: BlockComponentMap = {} for (const slug of only ?? (Object.keys(LIBRARY_BLOCK_COMPONENTS) as LibraryBlockSlug[])) { const Block = (components[slug] ?? LIBRARY_BLOCK_COMPONENTS[slug]) as ComponentType> const bound = (props: Record) => bound.displayName = `Library(${slug})` map[slugs[slug] ?? slug] = bound } return { ...map, ...extra } } /** The plugin's primitives, with lib/content's resolveLink and the project's buttons and backgrounds bound in. */ function buildUi(ctx: BlockContext, own: Partial, backgrounds?: Record): BlockUi { const buttons = own.buttonClass ?? buttonClass const sectionBackgrounds = { ...SECTION_BACKGROUNDS, ...backgrounds } const BoundLink = (props: CMSLinkProps) => const BoundMedia = (props: MediaProps) => const BoundSection = (props: SectionProps) =>
return { buttonClass: buttons, Card: own.Card ?? Card, CMSLink: own.CMSLink ?? BoundLink, Container: own.Container ?? Container, Media: own.Media ?? BoundMedia, Section: own.Section ?? BoundSection, SectionHeader: own.SectionHeader ?? SectionHeader, } }