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
|
|
@ -4,10 +4,13 @@
|
|||
let {
|
||||
background = { type: 'none', color: '', imageUrl: '', attachment: 'scroll', overlay: false, overlayOpacity: 40 } satisfies BlockBackground,
|
||||
foreground = {} as BlockForeground,
|
||||
anchor = '',
|
||||
children
|
||||
} = $props<{
|
||||
background?: BlockBackground;
|
||||
foreground?: BlockForeground;
|
||||
/** Block setting: lets links like "#prices" jump here. */
|
||||
anchor?: string;
|
||||
children?: import('svelte').Snippet;
|
||||
}>();
|
||||
|
||||
|
|
@ -41,7 +44,8 @@
|
|||
}
|
||||
</script>
|
||||
|
||||
<div style={bgStyle() + ' ' + fgStyle()} class="relative">
|
||||
<!-- scroll-mt keeps an #anchor target clear of the sticky header. -->
|
||||
<div id={anchor || undefined} style={bgStyle() + ' ' + fgStyle()} class="relative scroll-mt-20">
|
||||
{#if background.type === 'image' && background.overlay && background.imageUrl}
|
||||
<div
|
||||
class="pointer-events-none absolute inset-0"
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@
|
|||
import UpcomingCalendarBlock from './UpcomingCalendarBlock.svelte';
|
||||
import ShopBlock from './ShopBlock.svelte';
|
||||
import CheckoutBlock from './CheckoutBlock.svelte';
|
||||
import CardsBlock from './CardsBlock.svelte';
|
||||
import FaqBlock from './FaqBlock.svelte';
|
||||
import HtmlBlock from './HtmlBlock.svelte';
|
||||
|
||||
let { blocks = [], calendarEvents = [], items = [] } = $props<{
|
||||
blocks: ContentBlock[];
|
||||
|
|
@ -28,7 +31,7 @@
|
|||
</script>
|
||||
|
||||
{#each blocks as block (block.id)}
|
||||
<BackgroundWrapper background={getBg(block)} foreground={getFg(block)}>
|
||||
<BackgroundWrapper background={getBg(block)} foreground={getFg(block)} anchor={block.data._anchor}>
|
||||
{#if block.type === 'text'}
|
||||
<TextBlock data={block.data} />
|
||||
{:else if block.type === 'image_text'}
|
||||
|
|
@ -49,6 +52,12 @@
|
|||
<ShopBlock data={block.data} {items} />
|
||||
{:else if block.type === 'checkout'}
|
||||
<CheckoutBlock data={block.data} />
|
||||
{:else if block.type === 'cards'}
|
||||
<CardsBlock data={block.data} />
|
||||
{:else if block.type === 'faq'}
|
||||
<FaqBlock data={block.data} />
|
||||
{:else if block.type === 'html'}
|
||||
<HtmlBlock data={block.data} />
|
||||
{/if}
|
||||
</BackgroundWrapper>
|
||||
{/each}
|
||||
|
|
|
|||
54
packages/shared/src/blocks/CardsBlock.svelte
Normal file
54
packages/shared/src/blocks/CardsBlock.svelte
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<script lang="ts">
|
||||
import { scrollReveal } from '$shared/scrollReveal';
|
||||
|
||||
type Card = { imageUrl?: string; imageAlt?: string; title?: string; subtitle?: string; text?: string; href?: string; linkLabel?: string };
|
||||
let { data } = $props<{
|
||||
data: { heading?: string; intro?: string; align?: 'left' | 'center'; style?: 'image' | 'numbered'; items?: Card[] };
|
||||
}>();
|
||||
|
||||
let numbered = $derived(data.style === 'numbered');
|
||||
</script>
|
||||
|
||||
<section use:scrollReveal class="reveal mx-auto max-w-3xl px-6 py-12 md:py-16">
|
||||
{#if data.heading || data.intro}
|
||||
<div class="prose-wellness text-lg text-fg-body/85" class:text-center={data.align === 'center'}>
|
||||
{#if data.heading}<h2>{data.heading}</h2>{/if}
|
||||
{#if data.intro}<p>{data.intro}</p>{/if}
|
||||
</div>
|
||||
{/if}
|
||||
<div class="mt-6 grid gap-6 grid-cols-[repeat(auto-fit,minmax(200px,1fr))]">
|
||||
{#each data.items || [] as card, i (i)}
|
||||
<!-- Cards are white whatever the block's colours, so they use the site's own text colour. -->
|
||||
<div class="relative overflow-hidden rounded-[1.25rem] bg-white text-[color-mix(in_srgb,var(--color-text,#2c2c2c)_85%,transparent)] shadow-[0_1px_6px_rgba(0,0,0,0.08)] transition-shadow {card.href ? 'hover:shadow-lg' : ''}">
|
||||
{#if !numbered && card.imageUrl}
|
||||
<img class="block aspect-[4/3] w-full object-cover" src={card.imageUrl} alt={card.imageAlt || ''} />
|
||||
{/if}
|
||||
<div class={numbered ? 'p-6' : 'px-5 pt-4 pb-5'}>
|
||||
{#if numbered}
|
||||
<span class="inline-grid size-9 place-items-center rounded-full bg-sage font-semibold text-[var(--color-primary-content,#fff)]">{i + 1}</span>
|
||||
{/if}
|
||||
<!-- Not an h3: the site's h1–h4 rule (serif, heading colour) would override these classes. -->
|
||||
<strong class="block text-[1.15rem]" class:mt-3={numbered}>
|
||||
<!-- The title's link covers the whole card (::after), so the card is one click target. -->
|
||||
{#if card.href}<a class="after:absolute after:inset-0" href={card.href}>{card.title}</a>{:else}{card.title}{/if}
|
||||
</strong>
|
||||
{#if card.subtitle}<p class="mt-1 text-[0.95rem] opacity-75">{card.subtitle}</p>{/if}
|
||||
{#if card.text}<div class="card-text mt-1 text-[0.95rem]">{@html card.text}</div>{/if}
|
||||
{#if card.href && card.linkLabel}
|
||||
<p class="mt-3 font-semibold text-sage-dark underline underline-offset-4">{card.linkLabel}</p>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<style>
|
||||
/* Card text comes from the visual editor; links inside it stay clickable above the card's own link. */
|
||||
.card-text :global(p) { margin: 0.35rem 0 0; }
|
||||
.card-text :global(ul) { list-style: disc; padding-left: 1.25rem; margin: 0.5rem 0; }
|
||||
.card-text :global(ol) { list-style: decimal; padding-left: 1.25rem; margin: 0.5rem 0; }
|
||||
.card-text :global(li > p) { margin: 0; }
|
||||
.card-text :global(a) { position: relative; z-index: 1; text-decoration: underline; }
|
||||
.card-text :global(strong) { font-weight: 700; }
|
||||
</style>
|
||||
21
packages/shared/src/blocks/FaqBlock.svelte
Normal file
21
packages/shared/src/blocks/FaqBlock.svelte
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<script lang="ts">
|
||||
import { scrollReveal } from '$shared/scrollReveal';
|
||||
|
||||
type Question = { question?: string; answer?: string };
|
||||
let { data } = $props<{ data: { heading?: string; items?: Question[] } }>();
|
||||
</script>
|
||||
|
||||
<section use:scrollReveal class="reveal mx-auto max-w-3xl px-6 py-12 md:py-16">
|
||||
<div class="prose-wellness text-lg text-fg-body/85">
|
||||
{#if data.heading}<h2>{data.heading}</h2>{/if}
|
||||
{#each data.items || [] as item, i (i)}
|
||||
<details class="group border-b border-current/15 py-3.5">
|
||||
<summary class="flex cursor-pointer list-none items-center justify-between gap-4 text-[1.1rem] font-semibold [&::-webkit-details-marker]:hidden">
|
||||
{item.question}
|
||||
<span class="shrink-0 text-2xl leading-none font-light transition-transform group-open:rotate-45" aria-hidden="true">+</span>
|
||||
</summary>
|
||||
{@html item.answer || ''}
|
||||
</details>
|
||||
{/each}
|
||||
</div>
|
||||
</section>
|
||||
14
packages/shared/src/blocks/HtmlBlock.svelte
Normal file
14
packages/shared/src/blocks/HtmlBlock.svelte
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<script lang="ts">
|
||||
import { scrollReveal } from '$shared/scrollReveal';
|
||||
|
||||
let { data } = $props<{ data: { html?: string } }>();
|
||||
</script>
|
||||
|
||||
<!-- Written by an admin as-is, for embeds and one-off layouts. Same frame and text styles as a
|
||||
Text block (old text blocks with layout HTML became HTML blocks and must look unchanged).
|
||||
Tailwind classes in it get no styles (only classes found in this repo are generated). -->
|
||||
<section use:scrollReveal class="reveal mx-auto max-w-3xl px-6 py-12 md:py-16">
|
||||
<div class="prose-wellness text-lg text-fg-body/85">
|
||||
{@html data.html || ''}
|
||||
</div>
|
||||
</section>
|
||||
|
|
@ -1,11 +1,15 @@
|
|||
<script lang="ts">
|
||||
import { scrollReveal } from '$shared/scrollReveal';
|
||||
|
||||
let { data } = $props<{ data: { content?: string } }>();
|
||||
let { data } = $props<{ data: { content?: string; align?: 'left' | 'center'; size?: 'normal' | 'small' } }>();
|
||||
</script>
|
||||
|
||||
<section use:scrollReveal class="reveal mx-auto max-w-3xl px-6 py-12 md:py-16">
|
||||
<div class="prose-wellness text-lg text-fg-body/85">
|
||||
<!-- Small text is a footnote to the block above (references, terms), so it sits right under it. -->
|
||||
<section use:scrollReveal class="reveal mx-auto max-w-3xl px-6 {data.size === 'small' ? 'pb-12 md:pb-16' : 'py-12 md:py-16'}">
|
||||
<div
|
||||
class="prose-wellness {data.size === 'small' ? 'text-base text-fg-body/75' : 'text-lg text-fg-body/85'}"
|
||||
class:text-center={data.align === 'center'}
|
||||
>
|
||||
{@html data.content || '<h2>Welcome</h2><p>Add warm, inviting copy in the CMS.</p>'}
|
||||
</div>
|
||||
</section>
|
||||
|
|
|
|||
46
packages/shared/src/richText.ts
Normal file
46
packages/shared/src/richText.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import type { ContentBlock } from './types';
|
||||
|
||||
// Text blocks hold the HTML the admin's visual editor (Tiptap, see
|
||||
// app-admin RichTextEditor.svelte) produces: these tags, and attributes only
|
||||
// on links. Anything else (layouts, inline styles, embeds) belongs in an HTML
|
||||
// block. Keep this list and the editor's extensions in step, and mirror changes
|
||||
// in pocketbase/pb_migrations/1793300000_text_blocks_visual_editor.js.
|
||||
const EDITOR_TAGS = new Set(['p', 'h2', 'h3', 'strong', 'b', 'em', 'i', 's', 'a', 'ul', 'ol', 'li', 'blockquote', 'br', 'hr']);
|
||||
const LINK_ATTRS = new Set(['href', 'target', 'rel']);
|
||||
|
||||
/** Whether `html` only uses what the visual editor can produce (so editing it loses nothing). */
|
||||
export function isEditorHtml(html: string): boolean {
|
||||
for (const [, name, attrs] of html.matchAll(/<\/?([a-zA-Z][a-zA-Z0-9]*)([^>]*)>/g)) {
|
||||
const tag = name.toLowerCase();
|
||||
if (!EDITOR_TAGS.has(tag)) return false;
|
||||
for (const [, attr] of attrs.matchAll(/([a-zA-Z-]+)\s*=/g)) {
|
||||
if (tag !== 'a' || !LINK_ATTRS.has(attr.toLowerCase())) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const CENTRED = /\s+style="text-align:\s*center;?"/gi;
|
||||
const BLOCK_START = /<(p|h2|h3|ul|ol|blockquote)\b/gi;
|
||||
|
||||
/**
|
||||
* Text blocks written before the visual editor (raw HTML) become what they can
|
||||
* be without losing anything: clean text (with `align: 'center'` when every
|
||||
* paragraph and heading was centred), or else an HTML block that renders them
|
||||
* exactly as before. Other blocks are returned unchanged.
|
||||
*/
|
||||
export function upgradeLegacyBlock(block: ContentBlock): ContentBlock {
|
||||
if (block.type !== 'text') return block;
|
||||
const { content = '', ...rest } = block.data;
|
||||
if (isEditorHtml(content)) return block;
|
||||
const centred = (content.match(CENTRED) || []).length;
|
||||
const stripped = content.replace(CENTRED, '');
|
||||
if (centred && centred === (stripped.match(BLOCK_START) || []).length && isEditorHtml(stripped)) {
|
||||
return { ...block, data: { ...rest, content: stripped, align: 'center' } };
|
||||
}
|
||||
return { ...block, type: 'html', data: { ...rest, html: content } };
|
||||
}
|
||||
|
||||
export function upgradeLegacyBlocks(blocks: ContentBlock[]): ContentBlock[] {
|
||||
return Array.isArray(blocks) ? blocks.map(upgradeLegacyBlock) : blocks;
|
||||
}
|
||||
|
|
@ -8,7 +8,10 @@ export type BlockType =
|
|||
| 'testimonials'
|
||||
| 'upcoming_calendar'
|
||||
| 'shop'
|
||||
| 'checkout';
|
||||
| 'checkout'
|
||||
| 'cards'
|
||||
| 'faq'
|
||||
| 'html';
|
||||
|
||||
export type BlockBackground = {
|
||||
type: 'none' | 'color' | 'image';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue