Skip to content

Commit

Permalink
Use util to transform metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Feb 20, 2024
1 parent e334ecd commit bb3b325
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 65 deletions.
2 changes: 2 additions & 0 deletions packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { default as PrivateQuickInserter } from './components/inserter/quick-ins
import { PrivateListView } from './components/list-view';
import BlockInfo from './components/block-info-slot-fill';
import { useCanBlockToolbarBeFocused } from './utils/use-can-block-toolbar-be-focused';
import { getTransformedMetadata } from './utils/get-transformed-metadata';
import { cleanEmptyObject, useStyleOverride } from './hooks/utils';
import BlockQuickNavigation from './components/block-quick-navigation';
import { LayoutStyle } from './components/block-list/layout';
Expand Down Expand Up @@ -44,6 +45,7 @@ lock( privateApis, {
ResizableBoxPopover,
BlockInfo,
useCanBlockToolbarBeFocused,
getTransformedMetadata,
cleanEmptyObject,
useStyleOverride,
BlockQuickNavigation,
Expand Down
42 changes: 42 additions & 0 deletions packages/block-editor/src/utils/get-transformed-metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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} supportedProps Array with the metadata properties to keep after the transform.
* @param {Object} bindingsMappings Maps each bound attribute of the original block to its corresponding attribute in the result.
* @return {Object|undefined} New metadata object only with the relevant properties.
*/
export function getTransformedMetadata(
metadata,
supportedProps,
bindingsMappings
) {
if ( ! metadata ) {
return;
}
return Object.entries( metadata ).reduce( ( obj, [ prop, value ] ) => {
// If prop is not supported, don't add it to the new metadata object.
if ( ! supportedProps.includes( prop ) ) {
return obj;
}
// If prop is `bindings` and `bindingsMappings` is not defined, don't add it to the new metadata object.
if ( prop === 'bindings' && ! bindingsMappings ) {
return obj;
}
// Adapt bindings object if `bindingsMappings` is defined.
// The rest of properties are added as they are.
obj[ prop ] =
prop === 'bindings' && !! bindingsMappings
? Object.entries( bindingsMappings ).reduce(
( bindingsObj, [ originalKey, resultingKey ] ) => {
bindingsObj[ resultingKey ] = value[ originalKey ];
return bindingsObj;
},
{}
)
: value;

return obj;
}, {} );
}
31 changes: 12 additions & 19 deletions packages/block-library/src/buttons/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
*/
import { createBlock } from '@wordpress/blocks';
import { __unstableCreateElement as createElement } from '@wordpress/rich-text';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';

const { getTransformedMetadata } = unlock( blockEditorPrivateApis );

const transforms = {
from: [
Expand Down Expand Up @@ -40,29 +47,15 @@ const transforms = {
// Get first url.
const link = element.querySelector( 'a' );
const url = link?.getAttribute( 'href' );
// Transform metadata object for button block.
let buttonMetadata;
if ( metadata ) {
// Only transform these metadata props.
const supportedProps = [ 'id', 'name', 'bindings' ];
buttonMetadata = Object.entries( metadata ).reduce(
( obj, [ prop, value ] ) => {
if ( supportedProps.includes( prop ) ) {
obj[ prop ] =
prop === 'bindings'
? { text: value.content }
: value;
}
return obj;
},
{}
);
}
// Create singular button in the buttons block.
return createBlock( 'core/button', {
text,
url,
metadata: buttonMetadata,
metadata: getTransformedMetadata(
metadata,
[ 'id', 'name', 'bindings' ],
{ content: 'text' }
),
} );
} )
),
Expand Down
67 changes: 21 additions & 46 deletions packages/block-library/src/heading/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
* WordPress dependencies
*/
import { createBlock, getBlockAttributes } from '@wordpress/blocks';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { getLevelFromHeadingNodeName } from './shared';
import { unlock } from '../lock-unlock';

const { getTransformedMetadata } = unlock( blockEditorPrivateApis );

const transforms = {
from: [
Expand All @@ -16,32 +20,17 @@ const transforms = {
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
attributes.map(
( { content, anchor, align: textAlign, metadata } ) => {
// Transform metadata object.
let headingMetadata;
if ( metadata ) {
// Only transform these metadata props.
const supportedProps = [ 'id', 'name', 'bindings' ];
headingMetadata = Object.entries( metadata ).reduce(
( obj, [ prop, value ] ) => {
if ( supportedProps.includes( prop ) ) {
obj[ prop ] =
prop === 'bindings'
? { content: value.content }
: value;
}
return obj;
},
{}
);
}
return createBlock( 'core/heading', {
( { content, anchor, align: textAlign, metadata } ) =>
createBlock( 'core/heading', {
content,
anchor,
textAlign,
metadata: headingMetadata,
} );
}
metadata: getTransformedMetadata(
metadata,
[ 'id', 'name', 'bindings' ],
{ content: 'content' }
),
} )
),
},
{
Expand Down Expand Up @@ -103,31 +92,17 @@ const transforms = {
isMultiBlock: true,
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
attributes.map( ( { content, textAlign: align, metadata } ) => {
// Transform metadata object.
let headingMetadata;
if ( metadata ) {
// Only transform these metadata props.
const supportedProps = [ 'id', 'name', 'bindings' ];
headingMetadata = Object.entries( metadata ).reduce(
( obj, [ prop, value ] ) => {
if ( supportedProps.includes( prop ) ) {
obj[ prop ] =
prop === 'bindings'
? { content: value.content }
: value;
}
return obj;
},
{}
);
}
return createBlock( 'core/paragraph', {
attributes.map( ( { content, textAlign: align, metadata } ) =>
createBlock( 'core/paragraph', {
content,
align,
metadata: headingMetadata,
} );
} ),
metadata: getTransformedMetadata(
metadata,
[ 'id', 'name', 'bindings' ],
{ content: 'content' }
),
} )
),
},
],
};
Expand Down

0 comments on commit bb3b325

Please sign in to comment.