From f192c04c50a6f6764ef978c2085ca2f307be2e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Zi=C3=B3=C5=82kowski?= Date: Fri, 6 Dec 2024 09:38:37 +0100 Subject: [PATCH] Address feedback from code review --- .../src/components/block-edit/edit.js | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/packages/block-editor/src/components/block-edit/edit.js b/packages/block-editor/src/components/block-edit/edit.js index 4a405ec2a9e9ae..27d3650f3a0902 100644 --- a/packages/block-editor/src/components/block-edit/edit.js +++ b/packages/block-editor/src/components/block-edit/edit.js @@ -61,45 +61,46 @@ const EditWithGeneratedProps = ( props ) => { const registry = useRegistry(); const blockType = getBlockType( name ); const blockContext = useContext( BlockContext ); - const registeredSources = useSelect( ( select ) => - unlock( select( blocksStore ) ).getAllBlockBindingsSources() + const registeredSources = useSelect( + ( select ) => + unlock( select( blocksStore ) ).getAllBlockBindingsSources(), + [] ); - const blockBindings = useMemo( - () => - replacePatternOverridesDefaultBinding( - name, - attributes?.metadata?.bindings - ), - [ attributes?.metadata?.bindings, name ] - ); - - // Assign context values using the block type's declared context needs. - const context = useMemo( () => { - const _context = - blockType && blockType.usesContext - ? Object.fromEntries( - Object.entries( blockContext ).filter( ( [ key ] ) => - blockType.usesContext.includes( key ) - ) - ) - : DEFAULT_BLOCK_CONTEXT; - // Add block bindings context. - const blockBindingsContext = {}; + const { blockBindings, context, hasPatternOverrides } = useMemo( () => { + // Assign context values using the block type's declared context needs. + const computedContext = blockType?.usesContext + ? Object.fromEntries( + Object.entries( blockContext ).filter( ( [ key ] ) => + blockType.usesContext.includes( key ) + ) + ) + : DEFAULT_BLOCK_CONTEXT; + // Add context requested by Block Bindings sources. if ( attributes?.metadata?.bindings ) { Object.values( attributes?.metadata?.bindings || {} ).forEach( ( binding ) => { registeredSources[ binding?.source ]?.usesContext?.forEach( ( key ) => { - blockBindingsContext[ key ] = blockContext[ key ]; + computedContext[ key ] = blockContext[ key ]; } ); } ); } - return { ..._context, ...blockBindingsContext }; + return { + blockBindings: replacePatternOverridesDefaultBinding( + name, + attributes?.metadata?.bindings + ), + context: computedContext, + hasPatternOverrides: hasPatternOverridesDefaultBinding( + attributes?.metadata?.bindings + ), + }; }, [ - blockType, + name, + blockType?.usesContext, blockContext, attributes?.metadata?.bindings, registeredSources, @@ -179,9 +180,6 @@ const EditWithGeneratedProps = ( props ) => { ] ); - const hasPatternOverrides = hasPatternOverridesDefaultBinding( - attributes?.metadata?.bindings - ); const setBoundAttributes = useCallback( ( nextAttributes ) => { if ( ! blockBindings ) {