import React, { useMemo } from "react"; import { observer } from "mobx-react-lite"; import { toJS } from "mobx"; import { type ContentClient, type Document, LayoutContentArea, type PreviewPage, withGlobalElementDefaults } from "@webiny/website-builder-sdk"; import { ElementRenderer } from "~/components/ElementRenderer.js"; import { ElementSlot } from "~/components/ElementSlot.js"; import type { ComponentProps } from "~/types.js"; import { DocumentStoreScope, useDocumentStore } from "~/components/DocumentStoreProvider.js"; import { useSeededDocumentStore } from "~/components/useSeededDocumentStore.js"; import { PageScope, ReadOnlyScope, usePageLayout } from "~/components/PageLayout.js"; import { SlotLabel } from "~/components/SlotLabel.js"; import { getLayoutAreaContent, LayoutAreasProvider, useLayoutAreas } from "~/components/LayoutAreas.js"; import { getContentAreaLabel } from "./LayoutContentLabel.js"; import { useContentClient } from "~/components/useContentClient.js"; const toPreviewDocument = (page: PreviewPage, client: ContentClient): Document => { const document: Document = { id: page.id, version: 1, state: {}, extensions: {}, metadata: {}, properties: page.properties ?? { id: page.id }, elements: page.elements ?? {}, bindings: page.bindings ?? {} }; // Globals the page has no stored instance of render from their manifest defaults, as on the live site. const withDefaults = withGlobalElementDefaults(document, client.getComponentManifests()); return { ...document, elements: withDefaults.elements, bindings: withDefaults.bindings }; }; /** * The page's content of one area. The main area is the page root, as on a page without a layout; the * others are slots of the page root. In the page editor, each is a place to drop content. */ const PageArea = observer(({ area }: { area: LayoutContentArea }) => { const documentStore = useDocumentStore(); const slot = area.slot(); // While an area is empty, its drop area in the page editor says which one it is. if (area.isMain()) { return ( ); } const ids = documentStore.getDocument()?.bindings.root?.inputs?.[slot]?.static; return ( ); }); const PreviewPageScope = ({ document, children }: { document: Document; children: React.ReactNode; }) => { const store = useSeededDocumentStore(document); return {children}; }; /** * In the layout editor, the page picked to preview the layout with: shown in place of the page content, * as on the live site. It can't be selected; it belongs to the page, not to the layout. */ const PreviewPageContent = ({ pageJson, area }: { pageJson: string; area: LayoutContentArea }) => { const client = useContentClient(); const document = useMemo( () => toPreviewDocument(JSON.parse(pageJson), client), [pageJson, client] ); // Keyed by page: picking another page renders it from scratch. Its elements share ids with the previous // page's ("root"), so they would otherwise keep what they had read from the previous page. return ( ); }; const Placeholder = ({ area }: { area: LayoutContentArea }) => { return (
{getContentAreaLabel(area)}
); }; const LayoutEditorContent = observer(({ area }: { area: LayoutContentArea }) => { const documentStore = useDocumentStore(); const previewPage = documentStore.getDocument()?.previewPage; if (previewPage) { return ; } return ; }); /** * A page content area: where a page renders its own content of that area inside a layout. In the layout * editor, there is no page: it shows a placeholder, or the page picked to preview the layout with. */ const ContentArea = ({ area }: { area: LayoutContentArea }) => { const client = useContentClient(); const pageLayout = usePageLayout(); const layoutAreas = useLayoutAreas(); // Content from code rendered in the layout (see ). It's not part of the layout, so // nothing of the layout is around it any more. if (layoutAreas) { return ( {getLayoutAreaContent(layoutAreas, area)} ); } if (pageLayout) { return ( ); } if (!client.isEditing()) { return null; } return ; }; export const MainContentComponent = ({ element }: ComponentProps) => { return ; }; export const NamedContentComponent = ({ inputs, element }: ComponentProps<{ area?: string }>) => { return ; };