Skip to content

Commit

Permalink
pick block data from straight props
Browse files Browse the repository at this point in the history
  • Loading branch information
retrofox committed Feb 27, 2024
1 parent db32d18 commit e55f6bc
Showing 1 changed file with 7 additions and 26 deletions.
33 changes: 7 additions & 26 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { RichTextData } from '@wordpress/rich-text';
*/
import { store as blockEditorStore } from '../store';
import { unlock } from '../lock-unlock';
import { useBlockEditContext } from '../components/block-edit/context';

/** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */
/** @typedef {import('@wordpress/blocks').WPBlockSettings} WPBlockSettings */
Expand Down Expand Up @@ -64,27 +63,19 @@ export function canBindAttribute( blockName, attributeName ) {
*
* @param {Object} props - The component props.
* @param {string} props.attrName - The attribute name.
* @param {any} props.attrValue - The attribute value.
* @param {string} props.blockName - The block name.
* @param {Object} props.blockProps - The block props with bound attribute.
* @param {Object} props.source - Source handler.
* @param {Object} props.args - The arguments to pass to the source.
* @return {null} This is a data-handling component. Render nothing.
*/
const BindingConnector = ( {
args,
attrName,
attrValue,
blockName,
blockProps,
source,
} ) => {
const BindingConnector = ( { args, attrName, blockProps, source } ) => {
const { placeholder, value: propValue } = source.useSource(
blockProps,
args
);

const setAttributes = blockProps.setAttributes;
const { setAttributes, name } = blockProps;
const attrValue = blockProps.attributes[ attrName ];

const { syncDerivedUpdates } = unlock( useDispatch( blockEditorStore ) );

Expand Down Expand Up @@ -136,7 +127,7 @@ const BindingConnector = ( {
* attributes and metadata fields types are improved and include `url`.
*/
const htmlAttribute =
getBlockType( blockName ).attributes[ attrName ].attribute;
getBlockType( name ).attributes[ attrName ].attribute;

if ( htmlAttribute === 'src' || htmlAttribute === 'href' ) {
updateBoundAttibute( null );
Expand All @@ -150,7 +141,7 @@ const BindingConnector = ( {
propValue,
attrValue,
placeholder,
blockName,
name,
attrName,
] );

Expand All @@ -164,13 +155,11 @@ const BindingConnector = ( {
* For this, it creates a BindingConnector for each bound attribute.
*
* @param {Object} props - The component props.
* @param {string} props.blockName - The block name.
* @param {Object} props.blockProps - The BlockEdit props object.
* @param {Object} props.bindings - The block bindings settings.
* @param {Object} props.attributes - The block attributes.
* @return {null} This is a data-handling component. Render nothing.
*/
function BlockBindingBridge( { blockName, blockProps, bindings, attributes } ) {
function BlockBindingBridge( { blockProps, bindings } ) {
const blockBindingsSources = unlock(
useSelect( blocksStore )
).getAllBlockBindingsSources();
Expand All @@ -189,9 +178,7 @@ function BlockBindingBridge( { blockName, blockProps, bindings, attributes } ) {
return (
<BindingConnector
key={ attrName }
blockName={ blockName }
attrName={ attrName }
attrValue={ attributes[ attrName ] }
source={ source }
blockProps={ blockProps }
args={ boundAttribute.args }
Expand All @@ -205,16 +192,12 @@ function BlockBindingBridge( { blockName, blockProps, bindings, attributes } ) {

const withBlockBindingSupport = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { clientId, name: blockName } = useBlockEditContext();
const { getBlockAttributes } = useSelect( blockEditorStore );

/*
* Create binding object filtering
* only the attributes that can be bound.
*/
const attributes = getBlockAttributes( clientId );
const bindings = Object.fromEntries(
Object.entries( attributes.metadata?.bindings || {} ).filter(
Object.entries( props.attributes.metadata?.bindings || {} ).filter(
( [ attrName ] ) => canBindAttribute( props.name, attrName )
)
);
Expand All @@ -224,9 +207,7 @@ const withBlockBindingSupport = createHigherOrderComponent(
{ Object.keys( bindings ).length > 0 && (
<BlockBindingBridge
blockProps={ props }
blockName={ blockName }
bindings={ bindings }
attributes={ attributes }
/>
) }
<BlockEdit { ...props } />
Expand Down

0 comments on commit e55f6bc

Please sign in to comment.