PluginProbe
Extendify / trunk
Extendify vtrunk
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Agent / workflows / block-selector / tools / get-block-schemas.js

get-block-schemas.js in Extendify trunk, at src/Agent/workflows/block-selector/tools/get-block-schemas.js

44 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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