| 1 |
import { fetchBlockCodeById } from '@agent/lib/block-code'; |
| 2 |
import { buildBlockSchema } from '@agent/lib/block-schema'; |
| 3 |
import { INSERTABLE_BLOCK_TYPES } from '@agent/lib/insertable-blocks'; |
| 4 |
import { ensureCoreBlocksRegistered } from '@agent/lib/register-blocks'; |
| 5 |
import { useQuickEditStore } from '@quick-edit/state/store'; |
| 6 |
import { getBlockType, parse } from '@wordpress/blocks'; |
| 7 |
|
| 8 |
// Each picked block's code + per-type schemas, the same shape the single path |
| 9 |
// ships up front; `blockTypes` fetches by name for adds not yet on the page. |
| 10 |
export default async ({ blockIds = [], blockTypes = [] }) => { |
| 11 |
await ensureCoreBlocksRegistered(); |
| 12 |
const { agentBlock } = useQuickEditStore.getState(); |
| 13 |
const postId = window.extAgentData?.context?.postId; |
| 14 |
|
| 15 |
const blocks = ( |
| 16 |
await Promise.all( |
| 17 |
blockIds.map(async (blockId) => { |
| 18 |
const code = await fetchBlockCodeById( |
| 19 |
blockId, |
| 20 |
agentBlock?.source, |
| 21 |
postId, |
| 22 |
); |
| 23 |
return { blockId, type: parse(code)[0]?.name, code }; |
| 24 |
}), |
| 25 |
) |
| 26 |
).filter(({ type }) => type); |
| 27 |
|
| 28 |
const schemaByType = {}; |
| 29 |
for (const { type } of blocks) { |
| 30 |
schemaByType[type] ??= buildBlockSchema(getBlockType(type)); |
| 31 |
} |
| 32 |
for (const type of blockTypes) { |
| 33 |
if (!INSERTABLE_BLOCK_TYPES.includes(type)) continue; |
| 34 |
schemaByType[type] ??= buildBlockSchema(getBlockType(type)); |
| 35 |
} |
| 36 |
// Typed so each patch validates against its own block's fields. |
| 37 |
return { |
| 38 |
blocks, |
| 39 |
blockSchemas: Object.entries(schemaByType) |
| 40 |
.filter(([, schema]) => schema) |
| 41 |
.map(([type, schema]) => ({ type, schema })), |
| 42 |
}; |
| 43 |
}; |
| 44 |
|