import { applyBlockPatch } from '@agent/lib/block-patch'; import { createBlock, getBlockType, parse, serialize } from '@wordpress/blocks'; // The add catalog the backend prompt offers — the plugin constrains it, not // the backend, so old fielded builds never see types they can't build. export const INSERTABLE_BLOCK_TYPES = [ 'core/button', 'core/heading', 'core/paragraph', 'core/list', 'core/quote', 'core/pullquote', 'core/code', 'core/preformatted', 'core/verse', 'core/group', 'core/column', 'core/spacer', 'core/separator', ]; // One level of explicit children, on containers only. const CHILD_BEARING_TYPES = ['core/group', 'core/column']; // The model authors `text`; each type stores it under its own attribute. const RICH_TEXT_ATTR = { 'core/button': 'text', 'core/heading': 'content', 'core/paragraph': 'content', 'core/pullquote': 'value', 'core/code': 'content', 'core/preformatted': 'content', 'core/verse': 'content', }; const paragraphChildren = (text) => text ? [createBlock('core/paragraph', { content: text })] : []; // A bare column is only valid inside core/columns, so it can't be a child. const childBlocks = (children) => (children ?? []) .filter( ({ blockType }) => blockType !== 'core/column' && INSERTABLE_BLOCK_TYPES.includes(blockType) && getBlockType(blockType), ) .map(({ blockType, text }) => blockType === 'core/button' ? createBlock('core/buttons', {}, [freshBlock('core/button', text)]) : freshBlock(blockType, text), ); const freshBlock = (blockType, text, children = []) => { if (blockType === 'core/list') { // The model sometimes authors