Files
co-lite/solvway-admin-portal/app/lib/cmsPages.ts
T
ngqileandClaude Opus 5.5 92e62fb3fd fix(pages): reserve the reg- prefix for the sign-up form's field ids
A block named reg-email would have duplicated the form's input id, so its
label and #reg-email links would bind to the wrong element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-03 09:43:23 +08:00

149 lines
5.1 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", "HTML"];
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[];
/** Optional link name: the block's id on the page, the target of a #name link (add-page-anchor-links). */
anchor?: string;
/** 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;
hideHeader?: boolean;
blocks: CmsBlock[];
updatedAt?: string | null;
}
export interface CmsPageDraft {
id?: number;
companyId?: number;
slug: string;
slugTouched: boolean;
title: string;
metaDescription: string;
status: PageStatus;
/** Hide the company header and the page title (add-custom-html-block). */
hideHeader: boolean;
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", hideHeader: false, 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 };
case "HTML":
return { type, html: "" };
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,
hideHeader: draft.hideHeader,
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";
}
/** A block link name, as services-counter checks it. "reg-" is reserved: the sign-up form's fields use it as ids. */
export const ANCHOR = /^(?!reg-)[a-z0-9][a-z0-9-]{0,39}$/;
/** The link name when it is valid, for use as an element id. */
export const anchorId = (name: string | undefined) => (name && ANCHOR.test(name) ? name : undefined);
/** The counter's link rule: same-site path, https, mailto, tel or #name. 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 ANCHOR.test(url.slice(1));
if (url.startsWith("/")) return !url.startsWith("//");
return /^(https:\/\/|mailto:|tel:)/i.test(url);
}