/** The page's sections: count, the add menu, and one card per section with move and remove. */ import type { Page, Section } from "@cloudhal/core-contracts"; import { Badge, Button, DropdownMenu, Empty, InlineCopyText, SectionCard, Text, Tooltip, } from "@cloudhal/ui"; import { ArrowDown, ArrowUp, CaretDown, Plus, Rows, Trash } from "@phosphor-icons/react"; import type { ReactNode } from "react"; import { useT } from "../../i18n/index.tsx"; import { blockIcon, newBlockSection } from "../BlockSection.tsx"; import { BodyProposals } from "../BodyProposals.tsx"; import { SectionForm } from "./SectionForm.tsx"; import { KIND_ICON, KINDS, newSection, sectionId, sectionQueryIssues } from "./sections.ts"; import type { PageEditorState } from "./usePageEditor.ts"; export function SectionList({ ed, draft, siteId, readOnly, }: { ed: PageEditorState; draft: Page; siteId: string; readOnly: boolean; }) { const t = useT(); const { setDraft, blockTypes, blockTitle } = ed; const updateSection = (i: number, s: Section) => setDraft({ ...draft, sections: draft.sections.map((x, j) => (j === i ? s : x)) }); const move = (i: number, dir: -1 | 1) => { const sections = [...draft.sections]; const j = i + dir; if (j < 0 || j >= sections.length) return; [sections[i], sections[j]] = [sections[j] as Section, sections[i] as Section]; setDraft({ ...draft, sections }); }; const kindLabel = (k: Section["kind"]) => t(`editor.sectionKinds.${k}`); const sectionTitle = (s: Section) => s.kind === "component" ? blockTitle(s.type) : kindLabel(s.kind); const sectionIcon = (s: Section) => s.kind === "component" ? blockIcon(blockTypes.find((b) => b.name === s.type)) : KIND_ICON[s.kind]; const addMenu = ; const iconButton = (label: string, icon: ReactNode, onClick: () => void, disabled = false) => ( } /> ); return (
{t("editor.sections")} {draft.sections.length} {draft.sections.length > 0 && addMenu}
{draft.sections.length === 0 ? ( } title={t("editor.noSections")} description={t("editor.noSectionsDescription")} contents={addMenu} /> ) : ( draft.sections.map((s, i) => { const KindIcon = sectionIcon(s); return ( } actions={ <> {s.id} {iconButton(t("editor.moveUp"), , () => move(i, -1), i === 0)} {iconButton( t("editor.moveDown"), , () => move(i, 1), i === draft.sections.length - 1, )} {iconButton(t("editor.remove"), , () => ed.setRemoveIndex(i))} } > updateSection(i, next)} media={{ siteId, defaultLocale: ed.site.data?.defaultLocale ?? draft.locale, host: ed.host, nodeId: draft.id, locale: draft.locale, }} blocks={blockTypes} readOnly={readOnly} queryIssues={sectionQueryIssues(ed.save.error, i)} /> {s.kind === "richText" && s.format === "markdown" && ( ed.query.refetch()} /> )} ); }) )}
); } /** The built-in section kinds, then the theme's section block types (RFC-0015). */ function AddSectionMenu({ ed, draft }: { ed: PageEditorState; draft: Page }) { const t = useT(); const { setDraft, blockTypes, blockTitle } = ed; const sectionBlocks = blockTypes.filter((b) => b.where.includes("section")); return ( } />}> {t("editor.addSection")} {KINDS.map((k) => ( setDraft({ ...draft, sections: [...draft.sections, newSection(k, t("editor.heading"))], }) } > {t(`editor.sectionKinds.${k}`)} ))} {sectionBlocks.length > 0 && ( <> {t("editor.blocks.menu")} {sectionBlocks.map((b) => { const BlockIcon = blockIcon(b); return ( setDraft({ ...draft, sections: [...draft.sections, newBlockSection(sectionId(), b)], }) } > {blockTitle(b.name)} ); })} )} ); }