Skip to content

Commit

Permalink
Block Directory: Bootstrap dependent blocks on installed block
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikeya-io committed Mar 6, 2025
1 parent 61c0ba2 commit 028263c
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions packages/block-directory/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const installBlockType =
'title',
'category',
'parent',
'ancestor',
'icon',
'description',
'keywords',
Expand All @@ -102,6 +103,8 @@ export const installBlockType =
'styles',
'example',
'variations',
'blockHooks',
'allowedBlocks',
];
await apiFetch( {
path: addQueryArgs( `/wp/v2/block-types/`, {
Expand All @@ -114,6 +117,7 @@ export const installBlockType =
if ( ! response && ! Array.isArray( response ) ) {
return;
}

const blockDefinitions = Object.fromEntries(
response.map( ( blockItem ) => [
blockItem.name,
Expand All @@ -126,9 +130,56 @@ export const installBlockType =
] )
);

// Bootstrap all retrieved block definitions
const installedDefinition = blockDefinitions[ name ];
if ( ! installedDefinition ) {
return;
}

const { allowedBlocks = [] } = installedDefinition;

let blocksToBootstrap = {};
if (
Array.isArray( allowedBlocks ) &&
allowedBlocks.length > 0
) {
blocksToBootstrap = allowedBlocks.reduce(
( acc, blockName ) => {
if (
! blockName.startsWith( 'core/' ) &&
blockDefinitions[ blockName ]
) {
acc[ blockName ] =
blockDefinitions[ blockName ];
}
return acc;
},
{}
);

blocksToBootstrap[ name ] = installedDefinition;
} else {
const childBlocks = Object.entries(
blockDefinitions
).filter( ( [ , def ] ) => {
function includesBlockName( defProp ) {
if ( Array.isArray( defProp ) ) {
return defProp.includes( name );
}
return false;
}

return (
includesBlockName( def.parent ) ||
includesBlockName( def.ancestor )
);
} );

blocksToBootstrap = Object.fromEntries( childBlocks );

blocksToBootstrap[ name ] = installedDefinition;
}
unstable__bootstrapServerSideBlockDefinitions(
blockDefinitions
blocksToBootstrap
);
} );

Expand Down

0 comments on commit 028263c

Please sign in to comment.