-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve bindings metadata in block transforms (#59179)
* Add metadata in heading transforms * Add metadata in button transforms * Add heading and button transform from/to paragraph * Revert "Add heading and button transform from/to paragraph" This reverts commit c3138e3. * Move tests to individual block specs * Explicitly transform metadata in button block * Explicitly transform metadata in heading block * Make bindings transform explicit in heading block * Use util to transform metadata * Use metadata util in code block to keep id and name * Move util to block-library package * Change code transforms util path * Inherit transform metadata props in the util * Add transform tests for code block * Return early if no supported properties * Use bindings callback for mapping attributes
- Loading branch information
1 parent
667797e
commit 6a8bf9d
Showing
7 changed files
with
498 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/block-library/src/utils/get-transformed-metadata.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
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 {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, | ||
newBlockName, | ||
bindingsCallback | ||
) { | ||
if ( ! metadata ) { | ||
return; | ||
} | ||
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', | ||
]; | ||
// The metadata properties that should be preserved after the transform. | ||
const transformSupportedProps = []; | ||
// 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. | ||
if ( supports.renaming !== false ) { | ||
transformSupportedProps.push( 'name' ); | ||
} | ||
|
||
// Return early if no supported properties. | ||
if ( ! transformSupportedProps.length ) { | ||
return; | ||
} | ||
|
||
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; | ||
}, | ||
{} | ||
); | ||
|
||
// Return undefined if object is empty. | ||
return Object.keys( newMetadata ).length ? newMetadata : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.