From 293b6df1d61f689daee2d887d909d125b3da4bd1 Mon Sep 17 00:00:00 2001 From: Ella Date: Wed, 17 Apr 2024 17:14:55 +0300 Subject: [PATCH] Polish --- .../src/hooks/use-bindings-attributes.js | 169 ++++++++---------- 1 file changed, 73 insertions(+), 96 deletions(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index 6ae1c606577f8f..5ca526b5749b74 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -58,122 +58,99 @@ export function canBindAttribute( blockName, attributeName ) { export const withBlockBindingSupport = createHigherOrderComponent( ( BlockEdit ) => ( props ) => { const registry = useRegistry(); - - const boundAttributes = useSelect( - ( select ) => { - const bindings = Object.fromEntries( - Object.entries( - props.attributes.metadata?.bindings || {} - ).filter( ( [ attrName ] ) => - canBindAttribute( props.name, attrName ) - ) - ); - - if ( ! Object.keys( bindings ).length > 0 ) { - return; + const sources = useSelect( ( select ) => + unlock( select( blocksStore ) ).getAllBlockBindingsSources() + ); + const bindings = props.attributes.metadata?.bindings; + const { name, clientId, context } = props; + const boundAttributes = useSelect( () => { + if ( ! bindings ) { + return; + } + + const attributes = {}; + + for ( const [ attributeName, boundAttribute ] of Object.entries( + bindings + ) ) { + const source = sources[ boundAttribute.source ]; + if ( + ! source?.getValue || + ! canBindAttribute( name, attributeName ) + ) { + continue; } - const blockBindingsSources = unlock( - select( blocksStore ) - ).getAllBlockBindingsSources(); - - return Object.entries( bindings ).reduce( - ( accu, [ attrName, boundAttribute ] ) => { - // Bail early if the block doesn't have a valid source handler. - const source = - blockBindingsSources[ boundAttribute.source ]; - - if ( ! source?.getValue ) { - return accu; - } - - const args = { - registry, - context: props.context, - clientId: props.clientId, - attributeName: attrName, - args: boundAttribute.args, - }; - - accu[ attrName ] = source.getValue( args ); - - if ( accu[ attrName ] === undefined ) { - if ( attrName === 'url' ) { - accu[ attrName ] = null; - } else { - accu[ attrName ] = - source.getPlaceholder?.( args ); - } - } + const args = { + registry, + context, + clientId, + attributeName, + args: boundAttribute.args, + }; + + attributes[ attributeName ] = source.getValue( args ); + + if ( attributes[ attributeName ] === undefined ) { + if ( attributeName === 'url' ) { + attributes[ attributeName ] = null; + } else { + attributes[ attributeName ] = + source.getPlaceholder?.( args ); + } + } + } - return accu; - }, - {} - ); - }, - [ - props.attributes.metadata?.bindings, - props.name, - props.context, - props.clientId, - registry, - ] - ); + return attributes; + }, [ bindings, name, clientId, context, registry, sources ] ); const { setAttributes } = props; const _setAttributes = useCallback( ( nextAttributes ) => { - const keptAttributes = { ...nextAttributes }; registry.batch( () => { - const bindings = Object.fromEntries( - Object.entries( - props.attributes.metadata?.bindings || {} - ).filter( ( [ attrName ] ) => - canBindAttribute( props.name, attrName ) - ) - ); - - if ( ! Object.keys( bindings ).length > 0 ) { + if ( ! bindings ) { return setAttributes( nextAttributes ); } - const blockBindingsSources = unlock( - registry.select( blocksStore ) - ).getAllBlockBindingsSources(); - - for ( const [ attributeKey, value ] of Object.entries( - nextAttributes - ) ) { - if ( bindings[ attributeKey ] ) { - const source = - blockBindingsSources[ - bindings[ attributeKey ].source - ]; - if ( source?.setValue ) { - source.setValue( { - registry, - context: props.context, - clientId: props.clientId, - attributeName: attributeKey, - value, - args: bindings[ attributeKey ].args, - } ); - delete keptAttributes[ attributeKey ]; - } + const keptAttributes = { ...nextAttributes }; + + for ( const [ + attributeName, + boundAttribute, + ] of Object.entries( bindings ) ) { + const source = sources[ boundAttribute.source ]; + if ( + ! source?.setValue || + ! canBindAttribute( name, attributeName ) + ) { + continue; } + + source.setValue( { + registry, + context, + clientId, + attributeName, + args: boundAttribute.args, + value: nextAttributes[ attributeName ], + } ); + delete keptAttributes[ attributeName ]; } - setAttributes( keptAttributes ); + if ( Object.keys( keptAttributes ).length ) { + setAttributes( keptAttributes ); + } } ); }, [ registry, - props.attributes.metadata?.bindings, - props.name, - props.context, - props.clientId, + bindings, + name, + clientId, + context, setAttributes, + sources, ] );