Visual text editor; Cards, FAQ and HTML blocks; block anchors

The text editor (Tiptap) only produces simple HTML: headings, bold,
italic, strikethrough, links, lists, pull quotes, lines. Layouts that
were hand-written HTML in text blocks get their own blocks (Cards with
pictures or numbered steps, FAQ), one-offs and embeds go in an HTML
block, and every block can have an anchor for #links. Text blocks get
alignment and a small (footnote) size.

Existing text blocks the editor can't represent become HTML blocks that
render exactly as before (migration, and backup import for old files);
text that is only centred becomes centred text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
This commit is contained in:
agent 2026-10-26 12:58:15 +00:00
parent 72f8898925
commit f86c2251dc
17 changed files with 532 additions and 72 deletions

View file

@ -38,6 +38,18 @@
updateData({ [key]: (block.data[key] || []).filter((_: unknown, i: number) => i !== index) });
}
function moveArrayItem(key: string, index: number, by: number) {
if (!block) return;
const items = [...(block.data[key] || [])];
const to = index + by;
if (to < 0 || to >= items.length) return;
[items[index], items[to]] = [items[to], items[index]];
updateData({ [key]: items });
}
// "#prices" links jump to the block with anchor "prices"; keep it to characters that work in an address.
const toAnchor = (value: string) => value.trim().toLowerCase().replace(/^#/, '').replace(/[^a-z0-9_-]+/g, '-').replace(/^-+|-+$/g, '');
function toggleCategory(category: string) {
if (!block) return;
const categories = block.data.categories || [];
@ -76,6 +88,10 @@
{#if block.type === 'text'}
<RichTextEditor value={block.data.content} onChange={(content) => updateData({ content })} />
<div class="mt-4 space-y-3">
<SegmentedControl label="Alignment" options={['left', 'center']} value={block.data.align === 'center' ? 'center' : 'left'} onChange={(v) => updateData({ align: v })} />
<SegmentedControl label="Text size" options={['normal', 'small']} value={block.data.size === 'small' ? 'small' : 'normal'} onChange={(v) => updateData({ size: v })} formatLabel={(v) => (v === 'small' ? 'Small (footnote)' : 'Normal')} />
</div>
{:else if block.type === 'image_text'}
<div class="space-y-4">
<ImageUploader url={block.data.url} alt={block.data.alt} onChange={(image) => updateData(image)} />
@ -163,9 +179,77 @@
<ShopBlockEditor {block} {items} {onChange} />
{:else if block.type === 'checkout'}
<CheckoutBlockEditor {block} {onChange} />
{:else if block.type === 'cards'}
{@render field('Heading', block.data.heading || '', (heading) => updateData({ heading }), 'Optional')}
<label class="fieldset">
<span class="fieldset-legend">Intro</span>
<textarea class="textarea w-full" rows="2" placeholder="Optional" value={block.data.intro || ''} oninput={(e) => updateData({ intro: e.currentTarget.value })}></textarea>
</label>
<div class="mt-2 space-y-3">
<SegmentedControl label="Heading" options={['left', 'center']} value={block.data.align === 'center' ? 'center' : 'left'} onChange={(v) => updateData({ align: v })} />
<SegmentedControl label="Cards show" options={['image', 'numbered']} value={block.data.style === 'numbered' ? 'numbered' : 'image'} onChange={(v) => updateData({ style: v })} formatLabel={(v) => (v === 'image' ? 'A picture' : 'A number')} />
</div>
<div class="mt-4 space-y-3">
{#each block.data.items || [] as card, i}
<div class="rounded-box border border-base-300 p-3">
<div class="mb-2 flex items-center gap-1">
<span class="flex-1 text-sm font-semibold text-base-content/60">Card {i + 1}</span>
<button class="btn btn-ghost btn-xs" type="button" aria-label="Move card up" disabled={i === 0} onclick={() => moveArrayItem('items', i, -1)}>↑</button>
<button class="btn btn-ghost btn-xs" type="button" aria-label="Move card down" disabled={i === block.data.items.length - 1} onclick={() => moveArrayItem('items', i, 1)}>↓</button>
<button class="btn btn-ghost btn-xs text-error" type="button" onclick={() => removeArrayItem('items', i)}>Remove</button>
</div>
{#if block.data.style !== 'numbered'}
<ImageUploader url={card.imageUrl} alt={card.imageAlt} onChange={(image) => updateArray('items', i, { imageUrl: image.url, imageAlt: image.alt })} />
{/if}
<div class="grid gap-x-3 md:grid-cols-2">
{@render field('Title', card.title || '', (title) => updateArray('items', i, { title }))}
{@render field('Subtitle', card.subtitle || '', (subtitle) => updateArray('items', i, { subtitle }), 'Optional, shown lighter')}
</div>
<span class="fieldset-legend">Text</span>
<RichTextEditor compact value={card.text} onChange={(text) => updateArray('items', i, { text })} />
<div class="grid gap-x-3 md:grid-cols-2">
{@render field('Link', card.href || '', (href) => updateArray('items', i, { href }), '/page, #anchor or https://…')}
{@render field('Link text', card.linkLabel || '', (linkLabel) => updateArray('items', i, { linkLabel }), 'Optional, e.g. Read more')}
</div>
</div>
{/each}
<button class="btn btn-sm" type="button" onclick={() => addArrayItem('items', { imageUrl: '', imageAlt: '', title: '', subtitle: '', text: '', href: '', linkLabel: '' })}>Add card</button>
<p class="text-sm text-base-content/60">With a link, the whole card is clickable.</p>
</div>
{:else if block.type === 'faq'}
{@render field('Heading', block.data.heading || '', (heading) => updateData({ heading }), 'Optional')}
<div class="mt-3 space-y-3">
{#each block.data.items || [] as item, i}
<div class="rounded-box border border-base-300 p-3">
<div class="mb-2 flex items-center gap-1">
<span class="flex-1 text-sm font-semibold text-base-content/60">Question {i + 1}</span>
<button class="btn btn-ghost btn-xs" type="button" aria-label="Move question up" disabled={i === 0} onclick={() => moveArrayItem('items', i, -1)}>↑</button>
<button class="btn btn-ghost btn-xs" type="button" aria-label="Move question down" disabled={i === block.data.items.length - 1} onclick={() => moveArrayItem('items', i, 1)}>↓</button>
<button class="btn btn-ghost btn-xs text-error" type="button" onclick={() => removeArrayItem('items', i)}>Remove</button>
</div>
<input class="input mb-2 w-full" aria-label="Question" placeholder="Question" value={item.question} oninput={(e) => updateArray('items', i, { question: e.currentTarget.value })} />
<RichTextEditor compact value={item.answer} onChange={(answer) => updateArray('items', i, { answer })} />
</div>
{/each}
<button class="btn btn-sm" type="button" onclick={() => addArrayItem('items', { question: '', answer: '' })}>Add question</button>
<p class="text-sm text-base-content/60">Visitors click a question to show its answer.</p>
</div>
{:else if block.type === 'html'}
<label class="fieldset">
<span class="fieldset-legend">HTML</span>
<textarea class="textarea w-full font-mono text-xs" rows="14" spellcheck="false" value={block.data.html || ''} oninput={(e) => updateData({ html: e.currentTarget.value })}></textarea>
<span class="label">For embeds (a map, a video) and one-offs. Use inline styles: CSS classes from elsewhere don't apply here. Scripts don't run. Anything loaded from another site (an embed, an image) sends visitors' IP addresses there, so it may need mentioning in the privacy policy.</span>
</label>
{/if}
<div class="mt-6 space-y-3 rounded-box border border-base-300 p-4">
<label class="flex items-center gap-3">
<span class="w-28 shrink-0 text-sm font-bold">Anchor</span>
<input class="input input-sm flex-1" placeholder="e.g. prices — then link to #prices" value={block.data._anchor || ''} onchange={(e) => updateData({ _anchor: toAnchor(e.currentTarget.value) })} />
</label>
<hr class="border-base-300" />
<SegmentedControl label="Background" strong options={['none', 'color', 'image']} value={block.data._background?.type || 'none'} onChange={(type) => updateBackground({ type })} />
{#if block.data._background?.type === 'color'}