Skip to content

Commit

Permalink
Use bindings callback for mapping attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Feb 21, 2024
1 parent 2d09640 commit 5e3eda0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 69 deletions.
6 changes: 4 additions & 2 deletions packages/block-library/src/buttons/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ const transforms = {
url,
metadata: getTransformedMetadata(
metadata,
'core/paragraph',
'core/button'
'core/button',
( { content: contentBinding } ) => ( {
text: contentBinding,
} )
),
} );
} )
Expand Down
13 changes: 2 additions & 11 deletions packages/block-library/src/code/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
} ),
},
{
Expand All @@ -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' ),
} );
},
},
Expand Down Expand Up @@ -73,7 +65,6 @@ const transforms = {
content,
metadata: getTransformedMetadata(
metadata,
'core/code',
'core/paragraph'
),
} ),
Expand Down
12 changes: 8 additions & 4 deletions packages/block-library/src/heading/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ const transforms = {
textAlign,
metadata: getTransformedMetadata(
metadata,
'core/paragraph',
'core/heading'
'core/heading',
( { content: contentBinding } ) => ( {
content: contentBinding,
} )
),
} )
),
Expand Down Expand Up @@ -95,8 +97,10 @@ const transforms = {
align,
metadata: getTransformedMetadata(
metadata,
'core/heading',
'core/paragraph'
'core/paragraph',
( { content: contentBinding } ) => ( {
content: contentBinding,
} )
),
} )
),
Expand Down
79 changes: 27 additions & 52 deletions packages/block-library/src/utils/get-transformed-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,34 @@ 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',
'core/heading',
'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.
Expand All @@ -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;
}

0 comments on commit 5e3eda0

Please sign in to comment.