Landing pages gain a Footer section (1 to 4 columns of title and text, a legal line under a divider) whose phone numbers, emails and web addresses show as links. The Form block and a hero with a form may carry a thank-you text shown after sign-up, with the same links. Full-width heroes get a lighter shade. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
183 lines
9.2 KiB
TypeScript
183 lines
9.2 KiB
TypeScript
"use client";
|
|
|
|
import { createContext, useContext, useLayoutEffect, useRef, type ElementType, type FormEvent, type KeyboardEvent, type MouseEvent, type ReactNode } from "react";
|
|
import dynamic from "next/dynamic";
|
|
import { ImagePlus } from "lucide-react";
|
|
import { BLOCK_LABELS, type CmsBlock } from "@/app/lib/cmsPages";
|
|
import { contactLinks } from "@/app/lib/contactLinks";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
|
|
// add-page-visual-editor: the landing page in edit mode, on the on-page editor's canvas. The public page and the
|
|
// preview never provide an edit context, so every part here renders exactly the public markup without one.
|
|
|
|
export interface PageEdit {
|
|
/** The selected block's index, or null. */
|
|
selected: number | null;
|
|
select(index: number): void;
|
|
/** A plain text field changed: `path` is "heading", "text", "buttonLabel", "cards.<i>.title|text" or "items.<i>.question|answer". */
|
|
text(index: number, path: string, value: string): void;
|
|
/** A rich text block's HTML changed. */
|
|
html(index: number, html: string): void;
|
|
/** An image spot was clicked: the editor opens its file picker. */
|
|
image(index: number): void;
|
|
/** A chosen image not yet uploaded, as a data URL. */
|
|
preview(index: number): string | undefined;
|
|
/** A Custom HTML block whose HTML has unsaved edits: its frame still shows the saved version. */
|
|
stale(index: number): boolean;
|
|
/** Bumped after each save, so Custom HTML frames reload the saved version. */
|
|
frameVersion: number;
|
|
/** The editor's own tools for a selected section (toolbar, add section). */
|
|
chrome(index: number): ReactNode;
|
|
}
|
|
|
|
export const EditContext = createContext<PageEdit | null>(null);
|
|
export const useEdit = () => useContext(EditContext);
|
|
|
|
/** The block with one plain text field set, by its path (see PageEdit.text). */
|
|
export function setField(block: CmsBlock, path: string, value: string): CmsBlock {
|
|
const [list, at, key] = path.split(".");
|
|
if (key === undefined) return { ...block, [list]: value };
|
|
const items = [...((block[list as "cards" | "items"] ?? []) as object[])];
|
|
items[Number(at)] = { ...items[Number(at)], [key]: value };
|
|
return { ...block, [list]: items };
|
|
}
|
|
|
|
/**
|
|
* A plain text field: the text as-is on the public page; in edit mode the same element, typed into in place. It is
|
|
* never re-rendered from its value while focused, so the caret stays where it is.
|
|
*/
|
|
export function Text({ as: Tag, index, path, value, max, code, n, className, multiline, links }: {
|
|
as: ElementType;
|
|
index: number;
|
|
path: string;
|
|
value: string | undefined;
|
|
max: number;
|
|
/** The field's label code, the editable's name; `n` fills its {number}. */
|
|
code: string;
|
|
n?: number;
|
|
className?: string;
|
|
multiline?: boolean;
|
|
/** Outside the editor, show contact details as links (add-landing-thanks-and-footer). */
|
|
links?: boolean;
|
|
}) {
|
|
const edit = useEdit();
|
|
if (!edit) return value ? <Tag className={className}>{links ? contactLinks(value) : value}</Tag> : null;
|
|
return <EditableText as={Tag} edit={edit} index={index} path={path} value={value ?? ""} max={max} code={code} n={n} className={className} multiline={multiline} />;
|
|
}
|
|
|
|
function EditableText({ as: Tag, edit, index, path, value, max, code, n, className, multiline }: {
|
|
as: ElementType; edit: PageEdit; index: number; path: string; value: string; max: number; code: string; n?: number; className?: string; multiline?: boolean;
|
|
}) {
|
|
const t = useT();
|
|
const ref = useRef<HTMLElement>(null);
|
|
useLayoutEffect(() => {
|
|
const el = ref.current;
|
|
if (el && el.ownerDocument.activeElement !== el && el.textContent !== value) el.textContent = value;
|
|
}, [value]);
|
|
const near = value.length > max * 0.8;
|
|
const name = t(code, n === undefined ? undefined : { number: n });
|
|
return (
|
|
<>
|
|
<Tag ref={ref} role="textbox" aria-label={name} aria-multiline={multiline || undefined} contentEditable="plaintext-only"
|
|
suppressContentEditableWarning data-placeholder={name}
|
|
className={`${className ?? ""} cursor-text rounded-sm outline-none hover:outline-dashed hover:outline-1 hover:outline-primary/50 focus:outline-2 focus:outline-primary empty:before:opacity-50 empty:before:content-[attr(data-placeholder)]`}
|
|
onInput={(e: FormEvent<HTMLElement>) => edit.text(index, path, e.currentTarget.textContent ?? "")}
|
|
onKeyDown={(e: KeyboardEvent) => { if (e.key === "Enter" && !multiline) e.preventDefault(); }} />
|
|
{near ? <span aria-live="polite" className={`text-xs tabular-nums ${value.length > max ? "text-destructive" : "opacity-60"}`}>{`${value.length}/${max}`}</span> : null}
|
|
</>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* An image of a block: in edit mode a button that opens the editor's file picker, showing a chosen image at once.
|
|
* A background image (`corner`) stays as it is, with a small button in the corner instead.
|
|
*/
|
|
export function ImageSpot({ index, src, className, corner }: { index: number; src: string | undefined; className: string; corner?: boolean }) {
|
|
const t = useT();
|
|
const edit = useEdit();
|
|
const shown = edit?.preview(index) ?? src;
|
|
// eslint-disable-next-line @next/next/no-img-element -- an uploaded image of unknown size
|
|
const img = shown ? <img src={shown} alt="" className={className} /> : null;
|
|
if (!edit) return img;
|
|
const open = (e: MouseEvent) => { e.stopPropagation(); edit.select(index); edit.image(index); };
|
|
if (corner) {
|
|
return (
|
|
<>
|
|
{img}
|
|
<button type="button" aria-label={t("PagesImage")} onClick={open}
|
|
className="absolute top-3 left-3 z-10 flex size-10 cursor-pointer items-center justify-center rounded-full bg-background/90 text-foreground shadow hover:bg-background">
|
|
<ImagePlus aria-hidden="true" className="size-5" />
|
|
</button>
|
|
</>
|
|
);
|
|
}
|
|
return (
|
|
<button type="button" aria-label={t("PagesImage")} onClick={open}
|
|
className={`group/img relative block w-full cursor-pointer ${shown ? "" : "flex min-h-40 items-center justify-center rounded-lg border-2 border-dashed border-current/30"}`}>
|
|
{img ?? <ImagePlus aria-hidden="true" className="size-8 opacity-60" />}
|
|
<span className="absolute inset-0 rounded-lg ring-primary group-hover/img:ring-2" />
|
|
</button>
|
|
);
|
|
}
|
|
|
|
/** In edit mode, the whole page: clicks never follow a link or submit. */
|
|
export function stopNavigation(e: MouseEvent) {
|
|
if ((e.target as Element).closest("a[href], button[type=submit], form")) e.preventDefault();
|
|
}
|
|
|
|
/** A section's frame in edit mode: selected on click, outlined when hovered or selected, with the editor's tools. */
|
|
export function EditSection({ index, children }: { index: number; children: ReactNode }) {
|
|
const edit = useEdit();
|
|
if (!edit) return <>{children}</>;
|
|
const selected = edit.selected === index;
|
|
return (
|
|
<div data-index={index} data-selected={selected || undefined} onClick={() => edit.select(index)}
|
|
className={`group/section relative outline-offset-2 hover:outline-dashed hover:outline-1 hover:outline-primary/60 ${selected ? "outline-2 outline-primary hover:outline-solid hover:outline-2" : ""}`}>
|
|
{children}
|
|
{edit.chrome(index)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** A Custom HTML block in edit mode: its saved version, covered so a click selects it, marked while unsaved. */
|
|
export function HtmlSpot({ index, children }: { index: number; children: ReactNode }) {
|
|
const t = useT();
|
|
const edit = useEdit();
|
|
if (!edit) return <>{children}</>;
|
|
return (
|
|
<EditSection index={index}>
|
|
<div className="relative">
|
|
{children}
|
|
<button type="button" aria-label={t(BLOCK_LABELS.HTML)} className="absolute inset-0 cursor-pointer" onClick={() => edit.select(index)} />
|
|
{edit.stale(index) ? (
|
|
<span className="absolute top-2 left-2 rounded-md border border-warning/40 bg-background px-2 py-1 text-xs font-medium text-warning shadow">{t("PagesSaveToUpdate")}</span>
|
|
) : null}
|
|
</div>
|
|
</EditSection>
|
|
);
|
|
}
|
|
|
|
/** The hero's text area: shaded over an image, including one chosen in the editor and not saved yet. */
|
|
export function HeroShade({ index, image, shaded, plain, children }: { index: number; image: boolean; shaded: string; plain: string; children: ReactNode }) {
|
|
const edit = useEdit();
|
|
return <div className={image || edit?.preview(index) ? shaded : plain}>{children}</div>;
|
|
}
|
|
|
|
// Loaded only when the canvas shows rich text: visitors' pages never download the editor.
|
|
const RichTextEditor = dynamic(() => import("./RichTextEditor").then((m) => m.RichTextEditor), { ssr: false });
|
|
|
|
/** A block's rich text: the sanitised HTML on the public page (`children`); in edit mode, edited in place. */
|
|
export function RichSpot({ index, html, className, children }: { index: number; html: string | undefined; className: string; children: ReactNode }) {
|
|
const t = useT();
|
|
const edit = useEdit();
|
|
if (!edit) return <>{children}</>;
|
|
return (
|
|
<div className={`${className} relative ${html ? "" : "[&_.ProseMirror]:min-h-24"}`}>
|
|
{/* An empty editor is a thin line under the section's Add section button: it gets room, and a faded cue like
|
|
the plain fields' marks the spot to type. */}
|
|
{html ? null : <p aria-hidden="true" className="pointer-events-none absolute top-0 left-0 opacity-50">{t("PagesContent")}</p>}
|
|
<RichTextEditor bare label={t("PagesContent")} value={html ?? ""} onChange={(value) => edit.html(index, value)} />
|
|
</div>
|
|
);
|
|
}
|