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:
parent
72f8898925
commit
f86c2251dc
17 changed files with 532 additions and 72 deletions
|
|
@ -109,6 +109,36 @@ h4 {
|
|||
padding-left: 1.5rem;
|
||||
}
|
||||
|
||||
/* The visual editor wraps list items in <p>. */
|
||||
.prose-wellness li > p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.prose-wellness ol {
|
||||
list-style: decimal;
|
||||
padding-left: 1.5rem;
|
||||
}
|
||||
|
||||
/* Pull quote: a "Quote" in the visual editor. */
|
||||
.prose-wellness blockquote {
|
||||
margin: 0.75rem 0;
|
||||
padding-left: 1.25rem;
|
||||
border-left: 3px solid var(--color-primary, #8a9a7b);
|
||||
font-family: 'Playfair Display', Georgia, serif;
|
||||
font-size: 1.35rem;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.prose-wellness.text-center blockquote {
|
||||
border-left: 0;
|
||||
padding-left: 0;
|
||||
}
|
||||
|
||||
.prose-wellness hr {
|
||||
margin: 2rem 0;
|
||||
border-color: color-mix(in srgb, currentColor 20%, transparent);
|
||||
}
|
||||
|
||||
.prose-wellness a {
|
||||
color: color-mix(in srgb, var(--color-primary, #8a9a7b) 90%, #000);
|
||||
text-decoration: underline;
|
||||
|
|
@ -147,3 +177,15 @@ h4 {
|
|||
html {
|
||||
scroll-behavior: smooth;
|
||||
}
|
||||
|
||||
/* Visual editor (RichTextEditor): roughly how the site shows text, sized for the sidebar. */
|
||||
.rich-text h2 { font-size: 1.5rem; font-weight: 700; margin: 0.75rem 0 0.25rem; }
|
||||
.rich-text h3 { font-size: 1.2rem; font-weight: 700; margin: 0.75rem 0 0.25rem; }
|
||||
.rich-text p { margin: 0.4rem 0; }
|
||||
.rich-text ul { list-style: disc; padding-left: 1.5rem; }
|
||||
.rich-text ol { list-style: decimal; padding-left: 1.5rem; }
|
||||
.rich-text blockquote { border-left: 3px solid var(--color-primary); padding-left: 1rem; font-style: italic; }
|
||||
.rich-text hr { margin: 1rem 0; border-color: var(--color-base-300); }
|
||||
.rich-text a { text-decoration: underline; }
|
||||
.rich-text > :first-child { margin-top: 0; }
|
||||
.rich-text li > p { margin: 0; }
|
||||
|
|
|
|||
|
|
@ -11,7 +11,10 @@ export const blockLabels: Record<BlockType, string> = {
|
|||
testimonials: 'Testimonials',
|
||||
upcoming_calendar: 'Upcoming Calendar',
|
||||
shop: 'Shop',
|
||||
checkout: 'Checkout'
|
||||
checkout: 'Checkout',
|
||||
cards: 'Cards',
|
||||
faq: 'FAQ',
|
||||
html: 'HTML'
|
||||
};
|
||||
|
||||
export const blockTypes = Object.keys(blockLabels) as BlockType[];
|
||||
|
|
@ -80,6 +83,24 @@ export function createBlock(type: BlockType): ContentBlock {
|
|||
notesLabel: 'Notes or special requests'
|
||||
};
|
||||
break;
|
||||
case 'cards':
|
||||
base.data = {
|
||||
heading: 'Section heading',
|
||||
intro: '',
|
||||
align: 'center',
|
||||
style: 'image',
|
||||
items: [1, 2, 3].map((n) => ({ imageUrl: '', imageAlt: '', title: `Card ${n}`, subtitle: '', text: '<p>A sentence or two.</p>', href: '', linkLabel: '' }))
|
||||
};
|
||||
break;
|
||||
case 'faq':
|
||||
base.data = {
|
||||
heading: 'Questions people ask',
|
||||
items: [{ question: 'What happens in the first session?', answer: '<p>Answer it here.</p>' }]
|
||||
};
|
||||
break;
|
||||
case 'html':
|
||||
base.data = { html: '' };
|
||||
break;
|
||||
}
|
||||
base.data._background = { type: 'none', color: '', attachment: 'scroll', overlay: false, overlayOpacity: 40 };
|
||||
return base;
|
||||
|
|
|
|||
|
|
@ -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'}
|
||||
|
|
|
|||
|
|
@ -1,78 +1,127 @@
|
|||
<script lang="ts">
|
||||
import { browser } from '$app/environment';
|
||||
import { onMount } from 'svelte';
|
||||
import { onMount, untrack } from 'svelte';
|
||||
import { Editor } from '@tiptap/core';
|
||||
import StarterKit from '@tiptap/starter-kit';
|
||||
|
||||
let { value = '', onChange = (html: string) => {} } = $props<{ value?: string; onChange?: (html: string) => void }>();
|
||||
let editorEl: HTMLDivElement | undefined = $state();
|
||||
let mounted = $state(false);
|
||||
// The visual editor for text in blocks and emails. It can only produce what
|
||||
// the toolbar offers (see EDITOR_TAGS in $shared/richText): no inline styles
|
||||
// or layout HTML, so pages stay consistent. Layouts are blocks (Cards, FAQ);
|
||||
// anything else goes in an HTML block.
|
||||
let {
|
||||
value = '',
|
||||
onChange = (html: string) => {},
|
||||
compact = false
|
||||
} = $props<{ value?: string; onChange?: (html: string) => void; compact?: boolean }>();
|
||||
|
||||
let element: HTMLDivElement | undefined = $state();
|
||||
let editor: Editor | undefined = $state();
|
||||
// Bumped on every editor transaction so the toolbar's active states re-render.
|
||||
let tick = $state(0);
|
||||
|
||||
onMount(() => {
|
||||
mounted = true;
|
||||
if (editorEl) {
|
||||
editorEl.innerHTML = value || '';
|
||||
}
|
||||
editor = new Editor({
|
||||
element,
|
||||
extensions: [
|
||||
StarterKit.configure({
|
||||
heading: { levels: [2, 3] },
|
||||
code: false,
|
||||
codeBlock: false,
|
||||
underline: false,
|
||||
// It appends an empty paragraph after a closing heading or list, which changed content just by
|
||||
// opening a block (and looped with the value sync below).
|
||||
trailingNode: false,
|
||||
// No target/rel: most links are to the site's own pages, which shouldn't open a new tab.
|
||||
link: { openOnClick: false, autolink: true, defaultProtocol: 'https', HTMLAttributes: { target: null, rel: null } }
|
||||
})
|
||||
],
|
||||
content: value || '',
|
||||
editorProps: { attributes: { class: 'rich-text min-h-32 p-4 outline-none', role: 'textbox', 'aria-multiline': 'true' } },
|
||||
onTransaction: () => tick++,
|
||||
onUpdate: ({ editor }) => onChange(editor.isEmpty ? '' : editor.getHTML())
|
||||
});
|
||||
return () => editor?.destroy();
|
||||
});
|
||||
|
||||
// Sync content from props when a different block is selected,
|
||||
// without overwriting content while the user is actively editing.
|
||||
// Another block selected: show its content (not echoed back, so nothing is marked unsaved).
|
||||
// Untracked: setContent runs onTransaction (tick++), which must not become a dependency here.
|
||||
$effect(() => {
|
||||
if (mounted && editorEl && value !== undefined) {
|
||||
if (editorEl.innerHTML !== value) {
|
||||
editorEl.innerHTML = value || '';
|
||||
}
|
||||
}
|
||||
const html = value || '';
|
||||
const current = editor;
|
||||
if (!current) return;
|
||||
untrack(() => {
|
||||
if (html !== (current.isEmpty ? '' : current.getHTML())) current.commands.setContent(html, { emitUpdate: false });
|
||||
});
|
||||
});
|
||||
|
||||
function exec(cmd: string, val?: string) {
|
||||
if (!browser) return;
|
||||
editorEl?.focus();
|
||||
document.execCommand(cmd, false, val);
|
||||
emit();
|
||||
}
|
||||
|
||||
function emit() {
|
||||
if (editorEl) onChange(editorEl.innerHTML);
|
||||
function active(name: string, attrs?: Record<string, unknown>) {
|
||||
tick;
|
||||
return editor?.isActive(name, attrs) ?? false;
|
||||
}
|
||||
|
||||
function setLink() {
|
||||
const url = prompt('Link URL', 'https://');
|
||||
if (url) exec('createLink', url);
|
||||
if (!editor) return;
|
||||
const current = editor.getAttributes('link').href || '';
|
||||
const href = prompt('Link address (/page, #anchor or https://…). Empty removes the link.', current);
|
||||
if (href === null) return;
|
||||
const chain = editor.chain().focus().extendMarkRange('link');
|
||||
if (href.trim()) chain.setLink({ href: href.trim() }).run();
|
||||
else chain.unsetLink().run();
|
||||
}
|
||||
|
||||
function clearFormatting() {
|
||||
if (!browser) return;
|
||||
editorEl?.focus();
|
||||
document.execCommand('formatBlock', false, '<p>');
|
||||
document.execCommand('removeFormat');
|
||||
emit();
|
||||
}
|
||||
|
||||
function handleInput() {
|
||||
emit();
|
||||
}
|
||||
type Tool = { label: string; title: string; run: () => void; on?: () => boolean; class?: string };
|
||||
let tools = $derived.by((): Tool[][] => {
|
||||
const c = () => editor!.chain().focus();
|
||||
// Compact (cards, FAQ answers): no headings, quotes or lines inside small text.
|
||||
return [
|
||||
compact ? [] : [
|
||||
{ label: 'Text', title: 'Paragraph', run: () => c().setParagraph().run(), on: () => active('paragraph') },
|
||||
{ label: 'H2', title: 'Heading', run: () => c().toggleHeading({ level: 2 }).run(), on: () => active('heading', { level: 2 }) },
|
||||
{ label: 'H3', title: 'Subheading', run: () => c().toggleHeading({ level: 3 }).run(), on: () => active('heading', { level: 3 }) }
|
||||
],
|
||||
[
|
||||
{ label: 'B', title: 'Bold', run: () => c().toggleBold().run(), on: () => active('bold'), class: 'font-bold' },
|
||||
{ label: 'I', title: 'Italic', run: () => c().toggleItalic().run(), on: () => active('italic'), class: 'italic' },
|
||||
{ label: 'S', title: 'Strikethrough', run: () => c().toggleStrike().run(), on: () => active('strike'), class: 'line-through' },
|
||||
{ label: 'Link', title: 'Link', run: setLink, on: () => active('link') }
|
||||
],
|
||||
[
|
||||
{ label: '• List', title: 'Bulleted list', run: () => c().toggleBulletList().run(), on: () => active('bulletList') },
|
||||
{ label: '1. List', title: 'Numbered list', run: () => c().toggleOrderedList().run(), on: () => active('orderedList') },
|
||||
...(compact
|
||||
? []
|
||||
: [
|
||||
{ label: 'Quote', title: 'Pull quote', run: () => c().toggleBlockquote().run(), on: () => active('blockquote') },
|
||||
{ label: '— Line', title: 'Divider line', run: () => c().setHorizontalRule().run() }
|
||||
])
|
||||
],
|
||||
[
|
||||
{ label: 'Clear', title: 'Remove formatting', run: () => c().unsetAllMarks().clearNodes().run() },
|
||||
{ label: '↶', title: 'Undo', run: () => c().undo().run() },
|
||||
{ label: '↷', title: 'Redo', run: () => c().redo().run() }
|
||||
]
|
||||
].filter((group) => group.length);
|
||||
});
|
||||
</script>
|
||||
|
||||
{#if mounted}
|
||||
<div class="rounded-box border border-base-300 bg-base-100">
|
||||
<div class="flex flex-wrap gap-2 border-b border-base-300 p-2">
|
||||
<button class="btn btn-xs" onclick={() => exec('bold')} type="button"><b>B</b></button>
|
||||
<button class="btn btn-xs" onclick={() => exec('italic')} type="button"><i>I</i></button>
|
||||
<button class="btn btn-xs" onclick={() => exec('formatBlock', '<h2>')} type="button">H2</button>
|
||||
<button class="btn btn-xs" onclick={() => exec('formatBlock', '<h3>')} type="button">H3</button>
|
||||
<button class="btn btn-xs" onclick={() => exec('insertUnorderedList')} type="button">List</button>
|
||||
<button class="btn btn-xs" onclick={setLink} type="button">Link</button>
|
||||
<button class="btn btn-xs" onclick={clearFormatting} type="button">Clear</button>
|
||||
</div>
|
||||
<div
|
||||
bind:this={editorEl}
|
||||
contenteditable
|
||||
role="textbox"
|
||||
aria-multiline="true"
|
||||
class="min-h-40 p-4 outline-none"
|
||||
oninput={handleInput}
|
||||
onkeydown={(e) => { if (e.key === 'Tab') { e.preventDefault(); exec('insertHTML', ' '); } }}
|
||||
></div>
|
||||
<div class="rounded-box border border-base-300 bg-base-100">
|
||||
<div class="flex flex-wrap gap-1 border-b border-base-300 p-2" role="toolbar" aria-label="Text formatting">
|
||||
{#each tools as group, g (g)}
|
||||
{#if g > 0}<span class="mx-1 w-px self-stretch bg-base-300" aria-hidden="true"></span>{/if}
|
||||
{#each group as tool (tool.title)}
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-xs {tool.class || ''}"
|
||||
class:btn-primary={tool.on?.()}
|
||||
aria-pressed={tool.on ? tool.on() : undefined}
|
||||
title={tool.title}
|
||||
disabled={!editor}
|
||||
onmousedown={(e) => e.preventDefault()}
|
||||
onclick={tool.run}>{tool.label}</button
|
||||
>
|
||||
{/each}
|
||||
{/each}
|
||||
</div>
|
||||
{:else}
|
||||
<div class="rounded-box border border-base-300 bg-base-100 p-4 text-base-content/50">Loading editor…</div>
|
||||
{/if}
|
||||
<div bind:this={element}>
|
||||
{#if !editor}<div class="p-4 text-base-content/50">Loading editor…</div>{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import {
|
|||
validateSiteExport,
|
||||
type SiteExport
|
||||
} from '$lib/exportImport';
|
||||
import { upgradeLegacyBlocks } from '$shared/richText';
|
||||
import type { ContentBlock } from '$lib/types';
|
||||
|
||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
let body: SiteExport;
|
||||
|
|
@ -54,7 +56,8 @@ export const POST: RequestHandler = async ({ request, locals }) => {
|
|||
// 2. Import pages
|
||||
for (const page of body.pages) {
|
||||
try {
|
||||
const blocks = restorePlaceholders(page.blocks, keyToUrl);
|
||||
// Files exported before the visual editor may hold raw HTML in text blocks.
|
||||
const blocks = upgradeLegacyBlocks(restorePlaceholders(page.blocks, keyToUrl) as ContentBlock[]);
|
||||
|
||||
// Try to find existing page by slug
|
||||
let existing;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue