import { RegistryBlockDataProvider as ToolkitRegistryBlockDataProvider, type RegistryBlockDataValue, type RegistryBlockRenderResult, type RegistryBlockSideMapBlock, } from "@agent-native/toolkit/editor/RegistryBlockContext"; import { IconPencil } from "@tabler/icons-react"; import { useCallback, useMemo, type ReactNode } from "react"; import { blockEditSurface } from "./BlockView.js"; import { useOptionalBlockRegistry } from "./provider.js"; import { SchemaBlockEditor } from "./SchemaBlockEditor.js"; export function RegistryBlockDataProvider< TBlock extends RegistryBlockSideMapBlock = RegistryBlockSideMapBlock, >({ value, children, }: { value: RegistryBlockDataValue; children: ReactNode; }) { const registryValue = useOptionalBlockRegistry(); const renderRegisteredBlock = useCallback( ( block: TBlock, options: Parameters< NonNullable["renderRegisteredBlock"]> >[1], ): RegistryBlockRenderResult | null => { const spec = registryValue?.registry.get(options.blockType); if (!registryValue || !spec) return null; const blockType = options.blockType; const blockData = block.data; const Read = spec.Read; let body: ReactNode = ( ); let editSurface: ReactNode = null; const canEditBlock = options.editable && spec.placement.includes("block"); if (canEditBlock) { const Edit = spec.Edit; const editorNode = Edit ? ( ) : ( options.onChange(nextData)} schema={spec.schema} editable blockId={block.id} ctx={registryValue.ctx} /> ); const surface = blockEditSurface(spec); if (surface === "panel" && registryValue.ctx.renderEditSurface) { editSurface = registryValue.ctx.renderEditSurface({ title: spec.label, open: options.panelOpen, onOpenChange: options.setPanelOpen, blockId: block.id, blockType, blockTitle: block.title, blockSummary: block.summary, blockData, trigger: ( ), children: editorNode, }); } else if (surface === "panel") { editSurface = options.selected ? (
{editorNode}
) : null; } else if (surface !== "none") { body = editorNode; } } return { body, editSurface }; }, [registryValue], ); const resolvedValue = useMemo( () => ({ ...value, renderRegisteredBlock, renderEditSurface: registryValue?.ctx.renderEditSurface, }), [registryValue?.ctx.renderEditSurface, renderRegisteredBlock, value], ); return ( {children} ); }