feat(pages): add reusable page block editor and renderer baseline
This commit is contained in:
@@ -10,6 +10,7 @@ import Link from "next/link"
|
||||
import { redirect } from "next/navigation"
|
||||
|
||||
import { AdminShell } from "@/components/admin-shell"
|
||||
import { PageBlockEditor } from "@/components/pages/page-block-editor"
|
||||
import { requirePermissionForRoute } from "@/lib/route-guards"
|
||||
|
||||
export const dynamic = "force-dynamic"
|
||||
@@ -242,16 +243,7 @@ export default async function PageEditorPage({ params, searchParams }: PageProps
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label className="space-y-1">
|
||||
<span className="text-xs text-neutral-600">Content</span>
|
||||
<textarea
|
||||
name="content"
|
||||
rows={10}
|
||||
defaultValue={page.content}
|
||||
required
|
||||
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
</label>
|
||||
<PageBlockEditor name="content" initialContent={page.content} />
|
||||
|
||||
<div className="grid gap-3 md:grid-cols-2">
|
||||
<label className="space-y-1">
|
||||
@@ -368,16 +360,11 @@ export default async function PageEditorPage({ params, searchParams }: PageProps
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label className="space-y-1">
|
||||
<span className="text-xs text-neutral-600">Content</span>
|
||||
<textarea
|
||||
name="content"
|
||||
rows={8}
|
||||
defaultValue={selectedTranslation?.content ?? page.content}
|
||||
required
|
||||
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
</label>
|
||||
<PageBlockEditor
|
||||
name="content"
|
||||
initialContent={selectedTranslation?.content ?? page.content}
|
||||
label="Translation Blocks"
|
||||
/>
|
||||
|
||||
<div className="grid gap-3 md:grid-cols-2">
|
||||
<label className="space-y-1">
|
||||
|
||||
@@ -11,7 +11,9 @@ describe("CreatePageForm", () => {
|
||||
|
||||
expect((screen.getByLabelText("Title") as HTMLInputElement).name).toBe("title")
|
||||
expect((screen.getByLabelText("Slug") as HTMLInputElement).name).toBe("slug")
|
||||
expect((screen.getByLabelText("Content") as HTMLTextAreaElement).name).toBe("content")
|
||||
const contentField = document.querySelector('input[name="content"]') as HTMLInputElement | null
|
||||
expect(contentField).not.toBeNull()
|
||||
expect(contentField?.value.startsWith("[")).toBe(true)
|
||||
|
||||
const status = screen.getByLabelText("Status") as HTMLSelectElement
|
||||
expect(status.value).toBe("draft")
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { serializePageBlocks } from "@cms/content"
|
||||
import { Button } from "@cms/ui/button"
|
||||
|
||||
import { PageBlockEditor } from "./page-block-editor"
|
||||
|
||||
type CreatePageFormProps = {
|
||||
action: (formData: FormData) => void | Promise<void>
|
||||
}
|
||||
@@ -46,15 +49,16 @@ export function CreatePageForm({ action }: CreatePageFormProps) {
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label className="space-y-1">
|
||||
<span className="text-xs text-neutral-600">Content</span>
|
||||
<textarea
|
||||
name="content"
|
||||
rows={6}
|
||||
required
|
||||
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
</label>
|
||||
<PageBlockEditor
|
||||
name="content"
|
||||
initialContent={serializePageBlocks([
|
||||
{
|
||||
id: "initial-rich-text",
|
||||
type: "rich_text",
|
||||
body: "",
|
||||
},
|
||||
])}
|
||||
/>
|
||||
|
||||
<div className="grid gap-3 md:grid-cols-2">
|
||||
<label className="space-y-1">
|
||||
|
||||
292
apps/admin/src/components/pages/page-block-editor.tsx
Normal file
292
apps/admin/src/components/pages/page-block-editor.tsx
Normal file
@@ -0,0 +1,292 @@
|
||||
"use client"
|
||||
|
||||
import { type PageBlock, type PageBlocks, parsePageBlocks, serializePageBlocks } from "@cms/content"
|
||||
import { useMemo, useState } from "react"
|
||||
|
||||
type PageBlockEditorProps = {
|
||||
name: string
|
||||
initialContent: string
|
||||
label?: string
|
||||
}
|
||||
|
||||
function randomId(prefix: string): string {
|
||||
return `${prefix}-${Math.random().toString(36).slice(2, 10)}`
|
||||
}
|
||||
|
||||
function normalizeInitialBlocks(initialContent: string): PageBlocks {
|
||||
if (!initialContent.trim()) {
|
||||
return [
|
||||
{
|
||||
id: randomId("rich"),
|
||||
type: "rich_text",
|
||||
body: "",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
try {
|
||||
return parsePageBlocks(initialContent)
|
||||
} catch {
|
||||
return [
|
||||
{
|
||||
id: randomId("rich"),
|
||||
type: "rich_text",
|
||||
body: initialContent,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
function updateBlock(blocks: PageBlocks, blockId: string, next: Partial<PageBlock>): PageBlocks {
|
||||
return blocks.map((block) =>
|
||||
block.id === blockId ? ({ ...block, ...next } as PageBlock) : block,
|
||||
)
|
||||
}
|
||||
|
||||
export function PageBlockEditor({
|
||||
name,
|
||||
initialContent,
|
||||
label = "Page Blocks",
|
||||
}: PageBlockEditorProps) {
|
||||
const [blocks, setBlocks] = useState<PageBlocks>(() => normalizeInitialBlocks(initialContent))
|
||||
|
||||
const serialized = useMemo(() => serializePageBlocks(blocks), [blocks])
|
||||
|
||||
function addBlock(type: PageBlock["type"]) {
|
||||
if (type === "hero") {
|
||||
setBlocks((prev) => [
|
||||
...prev,
|
||||
{
|
||||
id: randomId("hero"),
|
||||
type,
|
||||
heading: "Hero heading",
|
||||
subheading: null,
|
||||
ctaHref: null,
|
||||
ctaLabel: null,
|
||||
},
|
||||
])
|
||||
return
|
||||
}
|
||||
|
||||
if (type === "rich_text") {
|
||||
setBlocks((prev) => [...prev, { id: randomId("rich"), type, body: "" }])
|
||||
return
|
||||
}
|
||||
|
||||
if (type === "gallery") {
|
||||
setBlocks((prev) => [...prev, { id: randomId("gallery"), type, title: null, imageIds: [] }])
|
||||
return
|
||||
}
|
||||
|
||||
if (type === "cta") {
|
||||
setBlocks((prev) => [
|
||||
...prev,
|
||||
{ id: randomId("cta"), type, label: "Open", href: "/", variant: "primary" },
|
||||
])
|
||||
return
|
||||
}
|
||||
|
||||
if (type === "form") {
|
||||
setBlocks((prev) => [
|
||||
...prev,
|
||||
{ id: randomId("form"), type, formKey: "contact", title: null, description: null },
|
||||
])
|
||||
return
|
||||
}
|
||||
|
||||
setBlocks((prev) => [
|
||||
...prev,
|
||||
{ id: randomId("price"), type: "price_cards", title: null, cards: [] },
|
||||
])
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3 rounded border border-neutral-200 p-3">
|
||||
<input type="hidden" name={name} value={serialized} />
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span className="text-xs text-neutral-600">{label}</span>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded border px-2 py-1 text-xs"
|
||||
onClick={() => addBlock("hero")}
|
||||
>
|
||||
+ Hero
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded border px-2 py-1 text-xs"
|
||||
onClick={() => addBlock("rich_text")}
|
||||
>
|
||||
+ Rich text
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded border px-2 py-1 text-xs"
|
||||
onClick={() => addBlock("gallery")}
|
||||
>
|
||||
+ Gallery
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded border px-2 py-1 text-xs"
|
||||
onClick={() => addBlock("cta")}
|
||||
>
|
||||
+ CTA
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded border px-2 py-1 text-xs"
|
||||
onClick={() => addBlock("form")}
|
||||
>
|
||||
+ Form
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded border px-2 py-1 text-xs"
|
||||
onClick={() => addBlock("price_cards")}
|
||||
>
|
||||
+ Price cards
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-3">
|
||||
{blocks.map((block, index) => (
|
||||
<article key={block.id} className="space-y-2 rounded border border-neutral-200 p-3">
|
||||
<div className="flex items-center justify-between text-xs text-neutral-600">
|
||||
<span>
|
||||
#{index + 1} {block.type}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded border px-2 py-1"
|
||||
onClick={() => setBlocks((prev) => prev.filter((entry) => entry.id !== block.id))}
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{block.type === "hero" ? (
|
||||
<div className="grid gap-2 md:grid-cols-2">
|
||||
<input
|
||||
value={block.heading}
|
||||
onChange={(event) =>
|
||||
setBlocks((prev) =>
|
||||
updateBlock(prev, block.id, { heading: event.target.value }),
|
||||
)
|
||||
}
|
||||
placeholder="Heading"
|
||||
className="rounded border border-neutral-300 px-2 py-1 text-sm"
|
||||
/>
|
||||
<input
|
||||
value={block.subheading ?? ""}
|
||||
onChange={(event) =>
|
||||
setBlocks((prev) =>
|
||||
updateBlock(prev, block.id, { subheading: event.target.value || null }),
|
||||
)
|
||||
}
|
||||
placeholder="Subheading"
|
||||
className="rounded border border-neutral-300 px-2 py-1 text-sm"
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{block.type === "rich_text" ? (
|
||||
<textarea
|
||||
rows={5}
|
||||
value={block.body}
|
||||
onChange={(event) =>
|
||||
setBlocks((prev) => updateBlock(prev, block.id, { body: event.target.value }))
|
||||
}
|
||||
placeholder="Text"
|
||||
className="w-full rounded border border-neutral-300 px-2 py-1 text-sm"
|
||||
/>
|
||||
) : null}
|
||||
|
||||
{block.type === "gallery" ? (
|
||||
<textarea
|
||||
rows={3}
|
||||
value={block.imageIds.join(",")}
|
||||
onChange={(event) =>
|
||||
setBlocks((prev) =>
|
||||
updateBlock(prev, block.id, {
|
||||
imageIds: event.target.value
|
||||
.split(",")
|
||||
.map((entry) => entry.trim())
|
||||
.filter((entry) => entry.length > 0),
|
||||
}),
|
||||
)
|
||||
}
|
||||
placeholder="Media asset IDs (comma separated UUIDs)"
|
||||
className="w-full rounded border border-neutral-300 px-2 py-1 text-sm"
|
||||
/>
|
||||
) : null}
|
||||
|
||||
{block.type === "cta" ? (
|
||||
<div className="grid gap-2 md:grid-cols-2">
|
||||
<input
|
||||
value={block.label}
|
||||
onChange={(event) =>
|
||||
setBlocks((prev) => updateBlock(prev, block.id, { label: event.target.value }))
|
||||
}
|
||||
placeholder="Button label"
|
||||
className="rounded border border-neutral-300 px-2 py-1 text-sm"
|
||||
/>
|
||||
<input
|
||||
value={block.href}
|
||||
onChange={(event) =>
|
||||
setBlocks((prev) => updateBlock(prev, block.id, { href: event.target.value }))
|
||||
}
|
||||
placeholder="Link href"
|
||||
className="rounded border border-neutral-300 px-2 py-1 text-sm"
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{block.type === "form" ? (
|
||||
<input
|
||||
value={block.formKey}
|
||||
onChange={(event) =>
|
||||
setBlocks((prev) => updateBlock(prev, block.id, { formKey: event.target.value }))
|
||||
}
|
||||
placeholder="Form key (e.g. contact, commission)"
|
||||
className="w-full rounded border border-neutral-300 px-2 py-1 text-sm"
|
||||
/>
|
||||
) : null}
|
||||
|
||||
{block.type === "price_cards" ? (
|
||||
<textarea
|
||||
rows={4}
|
||||
value={block.cards
|
||||
.map((card) => [card.name, card.price ?? "", card.description ?? ""].join("|"))
|
||||
.join("\n")}
|
||||
onChange={(event) =>
|
||||
setBlocks((prev) =>
|
||||
updateBlock(prev, block.id, {
|
||||
cards: event.target.value
|
||||
.split("\n")
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.map((line, lineIndex) => {
|
||||
const [name, price, description] = line
|
||||
.split("|")
|
||||
.map((entry) => entry.trim())
|
||||
return {
|
||||
id: `card-${lineIndex}`,
|
||||
name: name || `Card ${lineIndex + 1}`,
|
||||
price: price || null,
|
||||
description: description || null,
|
||||
}
|
||||
}),
|
||||
}),
|
||||
)
|
||||
}
|
||||
placeholder="One card per line: Name|Price|Description"
|
||||
className="w-full rounded border border-neutral-300 px-2 py-1 text-sm"
|
||||
/>
|
||||
) : null}
|
||||
</article>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
import { parsePageBlocks } from "@cms/content"
|
||||
import Image from "next/image"
|
||||
|
||||
type PageEntity = {
|
||||
title: string
|
||||
status: string
|
||||
@@ -10,6 +13,20 @@ type PublicPageViewProps = {
|
||||
}
|
||||
|
||||
export function PublicPageView({ page }: PublicPageViewProps) {
|
||||
const blocks = (() => {
|
||||
try {
|
||||
return parsePageBlocks(page.content)
|
||||
} catch {
|
||||
return [
|
||||
{
|
||||
id: "fallback-rich-text",
|
||||
type: "rich_text" as const,
|
||||
body: page.content,
|
||||
},
|
||||
]
|
||||
}
|
||||
})()
|
||||
|
||||
return (
|
||||
<article className="mx-auto flex w-full max-w-4xl flex-col gap-6 px-6 py-16">
|
||||
<header className="space-y-3">
|
||||
@@ -18,8 +35,105 @@ export function PublicPageView({ page }: PublicPageViewProps) {
|
||||
{page.summary ? <p className="text-neutral-600">{page.summary}</p> : null}
|
||||
</header>
|
||||
|
||||
<section className="prose prose-neutral max-w-none whitespace-pre-wrap rounded-xl border border-neutral-200 bg-white p-6 text-neutral-800">
|
||||
{page.content}
|
||||
<section className="space-y-4 rounded-xl border border-neutral-200 bg-white p-6 text-neutral-800">
|
||||
{blocks.map((block) => {
|
||||
if (block.type === "hero") {
|
||||
return (
|
||||
<section key={block.id} className="space-y-2 rounded border border-neutral-200 p-4">
|
||||
<h2 className="text-2xl font-semibold">{block.heading}</h2>
|
||||
{block.subheading ? <p className="text-neutral-600">{block.subheading}</p> : null}
|
||||
{block.ctaLabel && block.ctaHref ? (
|
||||
<a
|
||||
href={block.ctaHref}
|
||||
className="inline-flex rounded bg-neutral-900 px-3 py-1.5 text-sm text-white"
|
||||
>
|
||||
{block.ctaLabel}
|
||||
</a>
|
||||
) : null}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
if (block.type === "rich_text") {
|
||||
return (
|
||||
<section
|
||||
key={block.id}
|
||||
className="prose prose-neutral max-w-none whitespace-pre-wrap"
|
||||
>
|
||||
{block.body}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
if (block.type === "gallery") {
|
||||
return (
|
||||
<section key={block.id} className="space-y-3">
|
||||
{block.title ? <h3 className="text-lg font-medium">{block.title}</h3> : null}
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
{block.imageIds.length === 0 ? (
|
||||
<p className="text-sm text-neutral-500">No media linked yet.</p>
|
||||
) : (
|
||||
block.imageIds.map((imageId) => (
|
||||
<Image
|
||||
key={imageId}
|
||||
src={`/api/media/file/${imageId}`}
|
||||
alt=""
|
||||
width={1200}
|
||||
height={800}
|
||||
className="h-48 w-full rounded border border-neutral-200 object-cover"
|
||||
/>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
if (block.type === "cta") {
|
||||
return (
|
||||
<a
|
||||
key={block.id}
|
||||
href={block.href}
|
||||
className={`inline-flex rounded px-3 py-2 text-sm ${
|
||||
block.variant === "secondary"
|
||||
? "border border-neutral-300 text-neutral-800"
|
||||
: "bg-neutral-900 text-white"
|
||||
}`}
|
||||
>
|
||||
{block.label}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
|
||||
if (block.type === "form") {
|
||||
return (
|
||||
<section key={block.id} className="space-y-2 rounded border border-neutral-200 p-4">
|
||||
<h3 className="text-lg font-medium">{block.title || "Form block"}</h3>
|
||||
<p className="text-sm text-neutral-600">
|
||||
{block.description || "Form integration pending."}
|
||||
</p>
|
||||
<p className="text-xs text-neutral-500">formKey: {block.formKey}</p>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<section key={block.id} className="space-y-2 rounded border border-neutral-200 p-4">
|
||||
{block.title ? <h3 className="text-lg font-medium">{block.title}</h3> : null}
|
||||
<div className="grid gap-3 md:grid-cols-2">
|
||||
{block.cards.map((card) => (
|
||||
<article key={card.id} className="rounded border border-neutral-200 p-3">
|
||||
<h4 className="font-medium">{card.name}</h4>
|
||||
{card.price ? <p className="text-sm text-neutral-700">{card.price}</p> : null}
|
||||
{card.description ? (
|
||||
<p className="text-sm text-neutral-600">{card.description}</p>
|
||||
) : null}
|
||||
</article>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
)
|
||||
})}
|
||||
</section>
|
||||
</article>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user