From e3ebbfc45456550c0e3779aa04d43f1e5bfca634 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 29 Aug 2024 17:25:39 -0300 Subject: [PATCH] Only show binding manager if there are bindings --- includes/manager/register-attribute-binder.js | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/includes/manager/register-attribute-binder.js b/includes/manager/register-attribute-binder.js index 20f9f74..1623d23 100644 --- a/includes/manager/register-attribute-binder.js +++ b/includes/manager/register-attribute-binder.js @@ -46,8 +46,10 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => { 'meta' ); - // Saving the fields as serialized JSON because I was tired of fighting the REST API. - const fields = meta?.fields ? JSON.parse( meta.fields ) : []; + const fields = useMemo( () => { + // Saving the fields as serialized JSON because I was tired of fighting the REST API. + return meta?.fields ? JSON.parse( meta.fields ) : []; + }, [ meta.fields ] ); const { attributes, setAttributes, name } = props; @@ -81,7 +83,7 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => { } ); setAttributes( newAttributes ); - }, [ attributes.metadata, setAttributes ] ); + }, [ attributes.metadata, setAttributes, fields, setMeta ] ); const selectedBlockType = getBlockType( name ); @@ -196,43 +198,40 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => { return ; } + const bindings = attributes?.metadata?.[ window.BINDINGS_KEY ]; + return ( <> - { ! editingBoundAttribute && - attributes?.metadata?.[ - window.BLOCK_VARIATION_NAME_ATTR - ] && ( - - { supportedAttributes.map( - ( attribute ) => ( - - - - { attribute } - + { ! editingBoundAttribute && bindings && ( + + { supportedAttributes.map( ( attribute ) => { + return ( + + + + { attribute } + + { bindings[ attribute ] && ( { - attributes - ?.metadata?.[ - window - .BINDINGS_KEY - ][ + bindings[ attribute ] } - - - ) - ) } - - ) } + ) } + + + ); + } ) } + + ) } { ! editingBoundAttribute && ( @@ -246,9 +245,7 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => { > { __( 'Manage Binding' ) } - { attributes?.metadata?.[ - window.BLOCK_VARIATION_NAME_ATTR - ] && ( + { bindings && (