Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
134 lines
4.4 KiB
TypeScript
134 lines
4.4 KiB
TypeScript
// Landing-page blocks and the editor's pure helpers (add-landing-pages), ported from babyboo's cmsPages.ts
|
|
// minus the product blocks. Images are page_image ids, served at /api/public/page-images/<id>.
|
|
|
|
export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FAQ" | "FORM" | "HTML";
|
|
|
|
export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FAQ", "FORM"];
|
|
|
|
export const BLOCK_LABELS: Record<BlockType, string> = {
|
|
HERO: "PagesBlockHero",
|
|
RICH_TEXT: "PagesBlockRichText",
|
|
IMAGE_TEXT: "PagesBlockImageText",
|
|
FAQ: "PagesBlockFaq",
|
|
FORM: "PagesBlockForm",
|
|
HTML: "PagesBlockHtml",
|
|
};
|
|
|
|
export interface CmsFaqItem {
|
|
question: string;
|
|
answer: string;
|
|
}
|
|
|
|
export interface CmsBlock {
|
|
type: BlockType;
|
|
imageId?: number | null;
|
|
heading?: string;
|
|
text?: string;
|
|
buttonLabel?: string;
|
|
buttonUrl?: string;
|
|
html?: string;
|
|
imagePosition?: "LEFT" | "RIGHT";
|
|
items?: CmsFaqItem[];
|
|
/** The FORM block's event (a Snowflake uuid, kept as a string). */
|
|
eventUuid?: string | null;
|
|
}
|
|
|
|
export type PageStatus = "DRAFT" | "PUBLISHED";
|
|
|
|
export interface CmsPage {
|
|
id: number;
|
|
companyId: number;
|
|
slug: string;
|
|
title: string;
|
|
metaDescription: string | null;
|
|
status: PageStatus;
|
|
blocks: CmsBlock[];
|
|
updatedAt?: string | null;
|
|
}
|
|
|
|
export interface CmsPageDraft {
|
|
id?: number;
|
|
companyId?: number;
|
|
slug: string;
|
|
slugTouched: boolean;
|
|
title: string;
|
|
metaDescription: string;
|
|
status: PageStatus;
|
|
blocks: CmsBlock[];
|
|
}
|
|
|
|
export const imageSrc = (id: number) => `/api/public/page-images/${id}`;
|
|
|
|
export const pageAddress = (companyCode: string, slug: string) => `/p/${companyCode}/${slug}`;
|
|
|
|
export function emptyPage(): CmsPageDraft {
|
|
return { slug: "", slugTouched: false, title: "", metaDescription: "", status: "DRAFT", blocks: [] };
|
|
}
|
|
|
|
export function emptyBlock(type: BlockType): CmsBlock {
|
|
switch (type) {
|
|
case "FAQ":
|
|
return { type, heading: "", items: [{ question: "", answer: "" }] };
|
|
case "IMAGE_TEXT":
|
|
return { type, imageId: null, heading: "", html: "", imagePosition: "LEFT", buttonLabel: "", buttonUrl: "" };
|
|
case "FORM":
|
|
return { type, heading: "", eventUuid: null };
|
|
default:
|
|
return { type };
|
|
}
|
|
}
|
|
|
|
/** Lower-case words joined by hyphens: the slug rule the counter enforces. */
|
|
export function slugify(title: string): string {
|
|
return title.toLowerCase().normalize("NFKD").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 80);
|
|
}
|
|
|
|
export function withTitle(draft: CmsPageDraft, title: string): CmsPageDraft {
|
|
return { ...draft, title, slug: draft.slugTouched ? draft.slug : slugify(title) };
|
|
}
|
|
|
|
const blank = (v: unknown) => v === "" || v === null || v === undefined;
|
|
|
|
/** The save body: trimmed strings, blank optional fields dropped, FAQ items trimmed. */
|
|
export function pageBody(draft: CmsPageDraft) {
|
|
return {
|
|
companyId: draft.companyId,
|
|
slug: draft.slug.trim(),
|
|
title: draft.title.trim(),
|
|
metaDescription: draft.metaDescription.trim() || null,
|
|
status: draft.status,
|
|
blocks: draft.blocks.map((b) => {
|
|
const out: Record<string, unknown> = { type: b.type };
|
|
for (const [key, val] of Object.entries(b)) {
|
|
if (key === "type") continue;
|
|
if (key === "items") {
|
|
out.items = (val as CmsFaqItem[]).map((it) => ({ question: it.question.trim(), answer: it.answer.trim() }));
|
|
continue;
|
|
}
|
|
const v = typeof val === "string" ? val.trim() : val;
|
|
if (!blank(v)) out[key] = v;
|
|
}
|
|
return out as unknown as CmsBlock;
|
|
}),
|
|
};
|
|
}
|
|
|
|
/** Black or white text, whichever contrasts more with a #RRGGBB background (WCAG relative luminance). */
|
|
export function contrastText(hex: string): "#000000" | "#FFFFFF" {
|
|
const m = /^#([0-9a-f]{6})$/i.exec(hex);
|
|
if (!m) return "#FFFFFF";
|
|
const [r, g, b] = [0, 2, 4].map((i) => {
|
|
const c = parseInt(m[1].slice(i, i + 2), 16) / 255;
|
|
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
});
|
|
const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
return (lum + 0.05) / 0.05 > 1.05 / (lum + 0.05) ? "#000000" : "#FFFFFF";
|
|
}
|
|
|
|
/** The counter's link rule: same-site path, https, mailto or tel. Checked again before rendering a button. */
|
|
export function isSafeLink(url: string | undefined): boolean {
|
|
if (!url || url.length > 1000 || /[\s\\\u0000-\u001f]/.test(url)) return false;
|
|
if (url.startsWith("/")) return !url.startsWith("//");
|
|
return /^(https:\/\/|mailto:|tel:)/i.test(url);
|
|
}
|