Implement registerProductEditorBlockType

This commit is contained in:
Matt Sherman 2023-10-12 10:52:54 -04:00
parent 20acfe5e37
commit 7448e78aa7
3 changed files with 41 additions and 3 deletions

View File

@ -23,6 +23,7 @@ import { isValidEmail } from './validate-email';
export * from './create-ordered-children';
export * from './sort-fills-by-order';
export * from './register-product-editor-block-type';
export * from './init-block';
export * from './product-apifetch-middleware';
export * from './sift';

View File

@ -3,7 +3,11 @@
*/
import { Block, BlockConfiguration } from '@wordpress/blocks';
import deprecated from '@wordpress/deprecated';
import { registerWooBlockType } from '@woocommerce/block-templates';
/**
* Internal dependencies
*/
import { registerProductEditorBlockType } from './register-product-editor-block-type';
interface BlockRepresentation< T extends Record< string, object > > {
name?: string;
@ -22,12 +26,12 @@ export function initBlock<
T extends Record< string, any > = Record< string, any >
>( block: BlockRepresentation< T > ): Block< T > | undefined {
deprecated( 'initBlock()', {
alternative: 'registerWooBlockType() from @woocommerce/block-templates',
alternative: 'registerProductEditorBlockType()',
} );
if ( ! block ) {
return;
}
return registerWooBlockType( block );
return registerProductEditorBlockType( block );
}

View File

@ -0,0 +1,33 @@
/**
* External dependencies
*/
import { Block, BlockConfiguration } from '@wordpress/blocks';
/**
* External dependencies
*/
import { registerWooBlockType } from '@woocommerce/block-templates';
interface BlockRepresentation< T extends Record< string, object > > {
name?: string;
metadata: BlockConfiguration< T >;
settings: Partial< BlockConfiguration< T > >;
}
export function registerProductEditorBlockType<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends Record< string, any > = Record< string, any >
>( block: BlockRepresentation< T > ): Block< T > | undefined {
const { metadata, settings, name } = block;
const augmentedMetadata = {
...metadata,
usesContext: [ ...( metadata.usesContext || [] ), 'editedProduct' ],
};
return registerWooBlockType( {
name,
metadata: augmentedMetadata,
settings,
} );
}