From 7448e78aa7201b49ea3e23d1465251083a99b143 Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Thu, 12 Oct 2023 10:52:54 -0400 Subject: [PATCH] Implement registerProductEditorBlockType --- packages/js/product-editor/src/utils/index.ts | 1 + .../js/product-editor/src/utils/init-block.ts | 10 ++++-- .../register-product-editor-block-type.ts | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 packages/js/product-editor/src/utils/register-product-editor-block-type.ts diff --git a/packages/js/product-editor/src/utils/index.ts b/packages/js/product-editor/src/utils/index.ts index 5b33ea896e9..472530f0533 100644 --- a/packages/js/product-editor/src/utils/index.ts +++ b/packages/js/product-editor/src/utils/index.ts @@ -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'; diff --git a/packages/js/product-editor/src/utils/init-block.ts b/packages/js/product-editor/src/utils/init-block.ts index ba8f2b0aaf2..a0833892444 100644 --- a/packages/js/product-editor/src/utils/init-block.ts +++ b/packages/js/product-editor/src/utils/init-block.ts @@ -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 ); } diff --git a/packages/js/product-editor/src/utils/register-product-editor-block-type.ts b/packages/js/product-editor/src/utils/register-product-editor-block-type.ts new file mode 100644 index 00000000000..58f6ca53119 --- /dev/null +++ b/packages/js/product-editor/src/utils/register-product-editor-block-type.ts @@ -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, + } ); +}