diff --git a/packages/block-library/src/buttons/transforms.js b/packages/block-library/src/buttons/transforms.js index c5281ed90c37e..9848299f3a99f 100644 --- a/packages/block-library/src/buttons/transforms.js +++ b/packages/block-library/src/buttons/transforms.js @@ -51,8 +51,10 @@ const transforms = { url, metadata: getTransformedMetadata( metadata, - 'core/paragraph', - 'core/button' + 'core/button', + ( { content: contentBinding } ) => ( { + text: contentBinding, + } ) ), } ); } ) diff --git a/packages/block-library/src/code/transforms.js b/packages/block-library/src/code/transforms.js index 0e17a98644d15..e537db342b8d5 100644 --- a/packages/block-library/src/code/transforms.js +++ b/packages/block-library/src/code/transforms.js @@ -22,11 +22,7 @@ const transforms = { transform: ( { content, metadata } ) => createBlock( 'core/code', { content, - metadata: getTransformedMetadata( - metadata, - 'core/paragraph', - 'core/code' - ), + metadata: getTransformedMetadata( metadata, 'core/code' ), } ), }, { @@ -37,11 +33,7 @@ const transforms = { // The HTML is plain text (with plain line breaks), so // convert it to rich text. content: toHTMLString( { value: create( { text } ) } ), - metadata: getTransformedMetadata( - metadata, - 'core/html', - 'core/code' - ), + metadata: getTransformedMetadata( metadata, 'core/code' ), } ); }, }, @@ -73,7 +65,6 @@ const transforms = { content, metadata: getTransformedMetadata( metadata, - 'core/code', 'core/paragraph' ), } ), diff --git a/packages/block-library/src/heading/transforms.js b/packages/block-library/src/heading/transforms.js index 848c4629d8a6f..f040ff06e37e8 100644 --- a/packages/block-library/src/heading/transforms.js +++ b/packages/block-library/src/heading/transforms.js @@ -24,8 +24,10 @@ const transforms = { textAlign, metadata: getTransformedMetadata( metadata, - 'core/paragraph', - 'core/heading' + 'core/heading', + ( { content: contentBinding } ) => ( { + content: contentBinding, + } ) ), } ) ), @@ -95,8 +97,10 @@ const transforms = { align, metadata: getTransformedMetadata( metadata, - 'core/heading', - 'core/paragraph' + 'core/paragraph', + ( { content: contentBinding } ) => ( { + content: contentBinding, + } ) ), } ) ), diff --git a/packages/block-library/src/utils/get-transformed-metadata.js b/packages/block-library/src/utils/get-transformed-metadata.js index b9fab2f8ed87c..53d79d3c1e42a 100644 --- a/packages/block-library/src/utils/get-transformed-metadata.js +++ b/packages/block-library/src/utils/get-transformed-metadata.js @@ -7,16 +7,20 @@ import { getBlockType } from '@wordpress/blocks'; * Transform the metadata attribute with only the values and bindings specified by each transform. * Returns `undefined` if the input metadata is falsy. * - * @param {Object} metadata Original metadata attribute from the block that is being transformed. - * @param {Array} fromBlockName Name of the original block that is being transformed. - * @param {Object} toBlockName Name of the final block after the transformation. + * @param {Object} metadata Original metadata attribute from the block that is being transformed. + * @param {Object} newBlockName Name of the final block after the transformation. + * @param {Function} bindingsCallback Optional callback to transform the `bindings` property object. * @return {Object|undefined} New metadata object only with the relevant properties. */ -export function getTransformedMetadata( metadata, fromBlockName, toBlockName ) { +export function getTransformedMetadata( + metadata, + newBlockName, + bindingsCallback +) { if ( ! metadata ) { return; } - const { supports } = getBlockType( toBlockName ); + const { supports } = getBlockType( newBlockName ); // Fixed until an opt-in mechanism is implemented. const BLOCK_BINDINGS_SUPPORTED_BLOCKS = [ 'core/paragraph', @@ -24,31 +28,13 @@ export function getTransformedMetadata( metadata, fromBlockName, toBlockName ) { 'core/image', 'core/button', ]; - // Fixed until a proper mechanism is defined. - const BINDINGS_ATTRIBUTES_MAPPING = { - 'core/paragraph': { - content: { - 'core/heading': 'content', - 'core/button': 'text', - }, - }, - 'core/heading': { - content: { - 'core/paragraph': 'content', - 'core/button': 'text', - }, - }, - 'core/button': { - text: { - 'core/paragraph': 'content', - 'core/heading': 'content', - }, - }, - }; // The metadata properties that should be preserved after the transform. const transformSupportedProps = []; - // If it support bindings, add the `id` and `bindings` properties. - if ( BLOCK_BINDINGS_SUPPORTED_BLOCKS.includes( toBlockName ) ) { + // If it support bindings, and there is a transform bindings callback, add the `id` and `bindings` properties. + if ( + BLOCK_BINDINGS_SUPPORTED_BLOCKS.includes( newBlockName ) && + bindingsCallback + ) { transformSupportedProps.push( 'id', 'bindings' ); } // If it support block naming (true by default), add the `name` property. @@ -61,30 +47,19 @@ export function getTransformedMetadata( metadata, fromBlockName, toBlockName ) { return; } - return Object.entries( metadata ).reduce( ( obj, [ prop, value ] ) => { - // If prop is not supported, don't add it to the new metadata object. - if ( ! transformSupportedProps.includes( prop ) ) { + const newMetadata = Object.entries( metadata ).reduce( + ( obj, [ prop, value ] ) => { + // If prop is not supported, don't add it to the new metadata object. + if ( ! transformSupportedProps.includes( prop ) ) { + return obj; + } + obj[ prop ] = + prop === 'bindings' ? bindingsCallback( value ) : value; return obj; - } - - if ( prop === 'bindings' ) { - // Adapt bindings object based on the mapping. - obj[ prop ] = Object.entries( value ).reduce( - ( transformedObj, [ originalKey, originalObj ] ) => { - const transformedKey = - BINDINGS_ATTRIBUTES_MAPPING[ fromBlockName ][ - originalKey - ][ toBlockName ]; - - transformedObj[ transformedKey ] = originalObj; - return transformedObj; - }, - {} - ); - } else { - obj[ prop ] = value; - } + }, + {} + ); - return obj; - }, {} ); + // Return undefined if object is empty. + return Object.keys( newMetadata ).length ? newMetadata : undefined; }