Refactor client-side attribute augmentation

This commit is contained in:
Matt Sherman 2023-10-17 10:18:56 -04:00
parent afa2b768b5
commit a4002105a4
1 changed files with 26 additions and 19 deletions

View File

@ -63,6 +63,31 @@ function getEdit<
};
}
function augmentAttributes<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends Record< string, any > = Record< string, any >
>( attributes: T ) {
// Note: If you modify this function, also update the server-side
// Automattic\WooCommerce\Admin\Features\ProductBlockEditor\BlockRegistry::augment_attributes() function.
return {
...attributes,
...{
_templateBlockId: {
type: 'string',
__experimentalRole: 'content',
},
_templateBlockOrder: {
type: 'integer',
__experimentalRole: 'content',
},
_templateBlockHideConditions: {
type: 'array',
__experimentalRole: 'content',
},
},
};
}
/**
* Function to register an individual block.
*
@ -86,27 +111,9 @@ export function registerWooBlockType<
return;
}
const templateBlockAttributes = {
_templateBlockId: {
type: 'string',
__experimentalRole: 'content',
},
_templateBlockOrder: {
type: 'integer',
__experimentalRole: 'content',
},
_templateBlockHideConditions: {
type: 'array',
__experimentalRole: 'content',
},
};
const augmentedMetadata = {
...metadata,
attributes: {
...metadata.attributes,
...templateBlockAttributes,
},
attributes: augmentAttributes( metadata.attributes ),
};
return registerBlockType< T >(