Improve content blocks: new types, formatting, and a better editor

Blocks (migration 032): video (click to play, sharing the featured
cards' player), button and callout types; quotes get an attribution
and images alt text separate from the caption. Paragraphs, callouts,
quotes and list items take **bold**, *italic*, [text](url), line
breaks and blank-line paragraphs, parsed into elements (never HTML)
with only safe link schemes. A list item's detail no longer uses an
em-dash.

Editor: each block type shows only its fields, with a named type and
a description, and every open block previews itself with the site's
renderer. Any repeater row can now be duplicated or have a new row
inserted below it; nested collections take `when`, and help text can
depend on the row.

src/seed-showcase.js writes a local test post using every block type
and option. It refuses the production database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-27 16:50:21 -05:00
parent c62dc84add
commit c5f12c769f
13 changed files with 723 additions and 169 deletions

View file

@ -32,6 +32,21 @@
can't keep a value for a choice it no longer makes: a featured
card switched from "record" to "link" drops its record.
A nested collection takes `when` the same way, and one that goes
hidden is emptied: a list block turned into a paragraph drops its
items.
`help` may be a function of the row, for help that depends on
another field (a content block's type says what its text is).
`preview` on a collection names an entry in PREVIEWS below; each
open row then shows how it will look on the site, drawn by the
site's own component.
Every row can be duplicated (a copy lands below it, ids stripped
so it saves as new) or have a blank row inserted below it, as
well as appended at the end.
`widget: "linkPreview"` is a URL box with a "Fetch preview" button
(LinkPreviewInput in MediaLibrary.tsx) that fills the row's empty
title, blurb and image through `onPatch`.
@ -57,8 +72,15 @@
import { useRef, useState } from "react";
import ContentBlocks from "../ContentBlocks.tsx";
import { ImageInput, LinkPreviewInput } from "./MediaLibrary.tsx";
/* Live previews a collection can name with `preview`. Each gets the
row as the form holds it, which is the API's shape. */
const PREVIEWS = {
contentBlock: (row) => <ContentBlocks blocks={[row]} />,
};
const input =
"w-full rounded-lg border border-[#4a6b72]/25 bg-white px-3 py-2 text-sm text-[#26454c] " +
"outline-none transition-colors focus:border-[#138ba0] focus:ring-2 focus:ring-[#138ba0]/25";
@ -104,17 +126,37 @@ export function fieldShown(field, row) {
/* A row after an edit, with any field the edit hid cleared. "" is
the engine's deliberate clear, so it saves as NULL. */
function withHiddenCleared(fields, row) {
function withHiddenCleared(spec, row) {
let next = row;
for (const field of fields) {
for (const field of spec.fields) {
const value = next[field.path];
if (!fieldShown(field, next) && value != null && value !== "") {
next = { ...next, [field.path]: "" };
}
}
for (const nested of spec.children ?? []) {
if (!fieldShown(nested, next) && next[nested.key]?.length) {
next = { ...next, [nested.key]: [] };
}
}
return next;
}
/* A copy of a row with every id removed, nested rows included, so
the server inserts it as new rather than colliding with the row
it was copied from. */
function withoutIds(value) {
if (Array.isArray(value)) return value.map(withoutIds);
if (!value || typeof value !== "object") return value;
const copy = {};
for (const [key, inner] of Object.entries(value)) {
if (key !== "id") copy[key] = withoutIds(inner);
}
return copy;
}
const helpText = (field, row) => (typeof field.help === "function" ? field.help(row) : field.help);
/* ── Field ───────────────────────────────────────────────────── */
export function Field({ field, value, row, options, error, onChange, onPatch }: any) {
@ -170,7 +212,7 @@ export function Field({ field, value, row, options, error, onChange, onPatch }:
onChange={(e) => onChange(e.target.checked ? 1 : 0)}
className="h-4 w-4 rounded border-[#4a6b72]/40 text-[#138ba0] focus:ring-[#138ba0]/40 disabled:opacity-50"
/>
{field.help ?? "Yes"}
{helpText(field, row) ?? "Yes"}
</label>
) : widget === "textarea" ? (
<textarea
@ -281,9 +323,9 @@ export function Field({ field, value, row, options, error, onChange, onPatch }:
This points at something that has been deleted. Pick a replacement before saving.
</p>
) : (
field.help &&
helpText(field, row) &&
widget !== "checkbox" && (
<p className="mt-1 text-xs text-[#4a6b72]">{field.help}</p>
<p className="mt-1 text-xs text-[#4a6b72]">{helpText(field, row)}</p>
)
)}
</div>
@ -340,7 +382,7 @@ export function Repeater({
const rowRefs = useRef<any[]>([]);
const update = (index, next) =>
onChange(list.map((row, i) => (i === index ? withHiddenCleared(spec.fields, next) : row)));
onChange(list.map((row, i) => (i === index ? withHiddenCleared(spec, next) : row)));
const move = (index, delta) => {
const target = index + delta;
@ -374,6 +416,13 @@ export function Repeater({
onChange([...list, { ...spec.blank }]);
};
/* A row placed at `at`, pushing everything from there down one,
and opened so it can be filled in straight away. */
const insertAt = (at, row) => {
setOpen((prev) => remapOpen(prev, (i) => (i >= at ? i + 1 : i)).add(at));
onChange([...list.slice(0, at), row, ...list.slice(at)]);
};
const removeAt = (index) => {
setOpen((prev) => remapOpen(prev, (i) => (i === index ? -1 : i > index ? i - 1 : i)));
onChange(list.filter((_, i) => i !== index));
@ -518,6 +567,18 @@ export function Repeater({
>
↓
</IconButton>
<IconButton
label="Insert a new one below"
onClick={() => insertAt(index + 1, { ...spec.blank })}
>
+
</IconButton>
<IconButton
label="Duplicate"
onClick={() => insertAt(index + 1, withoutIds(row))}
>
⧉
</IconButton>
<IconButton
label="Remove"
danger
@ -547,19 +608,30 @@ export function Repeater({
))}
</FieldGrid>
{(spec.children ?? []).map((nested) => (
<div key={nested.key} className="mt-4 border-t border-[#4a6b72]/15 pt-4">
<Repeater
spec={nested}
rows={row[nested.key]}
options={options}
errors={errors}
errorPrefix={`${errorPrefix}${index}.${nested.key}.`}
className=""
onChange={(value) => update(index, { ...row, [nested.key]: value })}
/>
{(spec.children ?? [])
.filter((nested) => fieldShown(nested, row))
.map((nested) => (
<div key={nested.key} className="mt-4 border-t border-[#4a6b72]/15 pt-4">
<Repeater
spec={nested}
rows={row[nested.key]}
options={options}
errors={errors}
errorPrefix={`${errorPrefix}${index}.${nested.key}.`}
className=""
onChange={(value) => update(index, { ...row, [nested.key]: value })}
/>
</div>
))}
{spec.preview && PREVIEWS[spec.preview] && (
<div className="mt-4 rounded-lg border border-dashed border-[#138ba0]/30 bg-white p-4">
<p className="mb-3 text-xs font-semibold uppercase tracking-wider text-[#4a6b72]/70">
Preview
</p>
{PREVIEWS[spec.preview](row)}
</div>
))}
)}
</>
)}
</div>