Skip to content

Commit

Permalink
Address feedback from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Dec 6, 2024
1 parent da3988f commit f192c04
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions packages/block-editor/src/components/block-edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -179,9 +180,6 @@ const EditWithGeneratedProps = ( props ) => {
]
);

const hasPatternOverrides = hasPatternOverridesDefaultBinding(
attributes?.metadata?.bindings
);
const setBoundAttributes = useCallback(
( nextAttributes ) => {
if ( ! blockBindings ) {
Expand Down

0 comments on commit f192c04

Please sign in to comment.