diff --git a/docs/updating-data-in-code.md b/docs/updating-data-in-code.md index 7798117..e7e3e1d 100644 --- a/docs/updating-data-in-code.md +++ b/docs/updating-data-in-code.md @@ -285,6 +285,7 @@ The numbers band's "Number Source" dropdown works the same way: | Organization kind labels ("Region", "Group") | the Kind options in `src/lib/adminSchema/organizations.ts` (admin); `orgListHref` in `src/lib/hrefs.ts` (the public back link, "All regions") | | | Admin account roles | `src/lib/roles.ts` (labels, notes) and `ROLES` in `server/src/auth.js` | Adding a role is also a CHECK change on `admin_users.role`. | | Admin navigation | `src/pages/admin/adminNav.ts` | | +| Newsletter parts and their defaults (Tools → Newsletter) | `PARTS_SPEC`, `PART_TYPES` and `STARTER` in `src/pages/admin/AdminNewsletter.tsx`; `PICK_DEFAULTS`, `DEFAULT_TITLE` and `COLOR` in `src/lib/newsletter.ts` | A draft is one JSON list of parts (`newsletters.layout`, 060), so adding a part or a setting needs no migration: a field in `PARTS_SPEC` and its drawing in `newsletter.ts`. The color choices are `PALETTE`; `COLOR` holds the defaults as hex, since an inbox has no theme tokens. `STARTER` is what a new draft starts with. | | The live site's address (used by QR codes) | `SITE_ORIGIN` in `src/lib/site.ts` | | | Version shown on the admin home page | `src/lib/version.ts` | | | Image directories (where logos and photos are looked up) | `src/lib/media.ts` | | diff --git a/server/src/admin-schema/newsletters.js b/server/src/admin-schema/newsletters.js index be6d58d..c083221 100644 --- a/server/src/admin-schema/newsletters.js +++ b/server/src/admin-schema/newsletters.js @@ -1,11 +1,12 @@ -import { text, blocksChild } from "./columns.js"; +import { text } from "./columns.js"; -/* ── Newsletter drafts (057_newsletters.sql) ───────────────────── +/* ── Newsletter drafts (057, 060_newsletter_layout.sql) ───────── Saved from Tools → Newsletter (AdminNewsletter.tsx), which is their only editor: there is no list or form for them in Data. - `picks` is the builder's JSON of what's ticked. Deleting a draft - takes its blocks with it (newsletters_cleanup). */ + `layout` is the builder's JSON list of parts, in order + (src/lib/newsletter.ts); `title` is the title part's text, kept + apart for the list of drafts. */ export const newsletters = { key: "newsletters", @@ -21,7 +22,5 @@ export const newsletters = { order: "updated_at DESC, id DESC", }, - columns: [text("title"), text("intro"), text("footer"), text("picks")], - - children: [blocksChild("newsletter")], + columns: [text("title"), text("layout", { required: true })], }; diff --git a/server/src/migrations/060_newsletter_layout.sql b/server/src/migrations/060_newsletter_layout.sql new file mode 100644 index 0000000..d6f9286 --- /dev/null +++ b/server/src/migrations/060_newsletter_layout.sql @@ -0,0 +1,47 @@ +-- ═══════════════════════════════════════════════════════════════ +-- 060 NEWSLETTER LAYOUT +-- +-- A newsletter is one ordered list of parts now, each movable and +-- each with its own settings (src/lib/newsletter.ts reads them): +-- the header band, the title, any email block, a section of picked +-- events, posts or front page cards, and the footer. It's kept as +-- JSON in `layout`, the builder's only editor. +-- +-- That replaces 057's fixed fields (intro, footer, picks) and the +-- drafts' content_blocks. 057 never reached a live database, so +-- there are no drafts to carry over: the table is rebuilt keeping +-- each row's id, title and dates, and its blocks are deleted. +-- `title` stays as its own column, for the list of drafts. +-- ═══════════════════════════════════════════════════════════════ + +DELETE FROM content_blocks WHERE owner_kind = 'newsletter'; + +DROP TRIGGER newsletters_touch; +DROP TRIGGER newsletters_cleanup; + +CREATE TABLE newsletters_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT, + layout TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(layout) AND json_type(layout) = 'array'), + created_at TEXT NOT NULL DEFAULT (datetime('now')), + updated_at TEXT NOT NULL DEFAULT (datetime('now')) +) STRICT; + +INSERT INTO newsletters_new (id, title, created_at, updated_at) +SELECT id, title, created_at, updated_at FROM newsletters; + +-- content_blocks_owner_exists names this table. +PRAGMA legacy_alter_table = ON; +DROP TABLE newsletters; +ALTER TABLE newsletters_new RENAME TO newsletters; +PRAGMA legacy_alter_table = OFF; + + +-- ── Triggers last (see CLAUDE.md, Migrations) ── + +CREATE TRIGGER newsletters_touch +AFTER UPDATE ON newsletters +FOR EACH ROW WHEN new.updated_at = old.updated_at +BEGIN + UPDATE newsletters SET updated_at = datetime('now') WHERE id = new.id; +END; diff --git a/src/lib/newsletter.ts b/src/lib/newsletter.ts index 91c9681..3d07759 100644 --- a/src/lib/newsletter.ts +++ b/src/lib/newsletter.ts @@ -1,18 +1,27 @@ /* ═══════════════════════════════════════════════════════════════ - NEWSLETTER: email-ready HTML and plain text from picked items + NEWSLETTER: email-ready HTML and plain text from a list of parts - The admin's Newsletter tool (AdminNewsletter.tsx) gathers events, - blog posts and featured cards, with content blocks written for the - issue; this turns them into one email to paste into Mailchimp, - Gmail or Church Center. Nothing is sent from here. + The admin's Newsletter tool (AdminNewsletter.tsx) edits a draft as + one ordered list of parts (060_newsletter_layout.sql); this turns + it into one email to paste into Mailchimp, Gmail or Church Center. + Nothing is sent from here. The parts, each aligned left or center: - Blocks draw as their email-safe equivalent: headings, paragraphs - (with **bold**, *italic* and [links](url)), callouts, quotes, - lists, link and button rows, images, cards and dividers; a video - is its still linked to the video. Schedules, tables and people, - which need the site, are left out. + header the band above the title: text, a logo, its color + (or no band); left out when the part is removed + title the email's heading + picks a section of picked events, blog posts or front page + cards: its heading and underline, each card's left + bar, and the button label + footer the closing lines (blank: the site's name and + address) and the rule above them + any block drawn as its email-safe equivalent: headings, + paragraphs (with **bold**, *italic* and [links](url)), + callouts, quotes, lists, link and button rows, images, + cards and dividers (with their color, thickness and + spacing); a video is its still linked to the video - No unsubscribe link: Mailchimp adds its own to every send. + Colors are the site's palette (PALETTE) or "none"; blank is the + default. No unsubscribe link: Mailchimp adds its own to every send. Email clients ignore stylesheets, CSS variables and most layout, so the HTML is tables with inline styles at 600px, images with @@ -21,6 +30,8 @@ plain-text copy goes alongside for clients that show no HTML. ═══════════════════════════════════════════════════════════════ */ +import { readableOn } from './color.ts' + export type NewsletterItem = { title: string /** A line under the title: dates and place, a byline. */ @@ -33,32 +44,41 @@ export type NewsletterItem = { cta: string } -export type NewsletterSection = { heading: string; items: NewsletterItem[] } +export type PickSource = 'events' | 'posts' | 'cards' -/* A content block as the block editor holds it. */ -export type NewsletterBlock = { +/* One part of the email, as the builder's editor holds it. Blocks + use the content block fields; the other parts add their own. */ +export type NewsletterPart = { type: string + align?: 'left' | 'center' | '' | null text?: string | null media?: string | null href?: string | null alt?: string | null cite?: string | null items?: Array<{ text?: string | null; detail?: string | null; url?: string | null; media?: string | null }> + /** The header's band, a divider's line. */ + color?: string | null + /** A divider's thickness and the space above it, in pixels. */ + size?: string | number | null + space?: string | number | null + /** A picks section: what it picks from, its picks in order, its button label. */ + source?: PickSource | '' | null + picked?: Array<{ ref?: string | null }> + cta?: string | null + /** A picks section's underline, the footer's rule. */ + rule?: string | null + /** The bar down the left of a picks section's cards. */ + bar?: string | null } export type Newsletter = { - title: string - intro?: string | null - /** Written for this issue, under the intro. */ - blocks?: NewsletterBlock[] - sections: NewsletterSection[] - /** The closing lines. Blank: the site's name and address. */ - footer?: string | null + parts: NewsletterPart[] + /** A picks part's items, ready to draw (picked, published, in order). */ + itemsFor: (part: NewsletterPart) => NewsletterItem[] /** The site's address, for the header link and footer. */ origin: string siteName: string - /** An absolute URL of the logo for the header. */ - logo?: string | null /** A site path or upload as an absolute address (blocks hold paths and bare filenames). */ absolute: (pathOrFile: string, kind: 'link' | 'image') => string } @@ -74,12 +94,38 @@ const COLOR = { rule: '#d6f5fa', } +/* What each picks section is called and says on its buttons, unless + the part says otherwise. */ +export const PICK_DEFAULTS: Record = { + events: { heading: 'Coming up', cta: 'Event details' }, + posts: { heading: 'From the blog', cta: 'Read the post' }, + cards: { heading: 'Featured', cta: 'Learn more' }, +} + +export const DEFAULT_TITLE = (siteName: string) => `News from ${siteName}` + const FONT = "'Poppins', Helvetica, Arial, sans-serif" const BODY_FONT = "'Montserrat', Helvetica, Arial, sans-serif" const escape = (value: string) => value.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"') +/* A part's color: blank is `fallback`, "none" is no color at all, + and anything but a hex color (it lands in a style attribute) is + the fallback too. */ +function colorOf(value: string | null | undefined, fallback: string): string | null { + if (value === 'none') return null + return value && /^#[0-9a-f]{6}$/i.test(value) ? value : fallback +} + +/* A whole number of pixels within bounds, or the fallback. */ +function px(value: string | number | null | undefined, fallback: number, min: number, max: number): number { + const n = Math.round(Number(value)) + return value === '' || value == null || !Number.isFinite(n) ? fallback : Math.min(max, Math.max(min, n)) +} + +const alignOf = (part: NewsletterPart) => (part.align === 'center' ? 'center' : 'left') + /* A link an email can follow: the site's own paths made absolute, anything but http, https and mailto refused. */ function safeHref(href: string, n: Newsletter): string | null { @@ -111,13 +157,18 @@ const paragraphs = (text: string, style: string, n?: Newsletter) => .join('') const TEXT = `margin:12px 0 0;font-family:${BODY_FONT};font-size:15px;line-height:24px;color:${COLOR.body};` -const buttonHtml = (href: string, label: string) => - `${escape(label)}` +const buttonHtml = (href: string, label: string, align: string) => + `${escape(label)}` -function blockHtml(block: NewsletterBlock, n: Newsletter): string { +/* A full-width picture; centered by its row when it's narrower. */ +const imageTag = (src: string, alt: string) => + `${escape(alt)}` + +function blockHtml(block: NewsletterPart, n: Newsletter): string { const text = block.text?.trim() ?? '' const href = block.href ? safeHref(block.href, n) : null const items = block.items ?? [] + const align = alignOf(block) switch (block.type) { case 'heading': return text @@ -139,7 +190,7 @@ function blockHtml(block: NewsletterBlock, n: Newsletter): string { : '' case 'list': return items.length - ? `