Skip to content

Commit

Permalink
Move logic to BlockContextProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed May 23, 2024
1 parent f43825c commit c939061
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
7 changes: 7 additions & 0 deletions packages/block-editor/src/components/block-context/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { createContext, useContext, useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';

/** @typedef {import('react').ReactNode} ReactNode */

/**
Expand All @@ -29,6 +35,7 @@ export function BlockContextProvider( { value, children } ) {
() => ( { ...context, ...value } ),
[ context, value ]
);
useDispatch( blockEditorStore ).updateBlockContext( nextValue );

return <Context.Provider value={ nextValue } children={ children } />;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1537,10 +1537,9 @@ export function updateBlockListSettings( clientId, settings ) {
*
* @return {Object} Action object
*/
export function updateBlockContext( clientId, context ) {
export function updateBlockContext( context ) {
return {
type: 'UPDATE_BLOCK_CONTEXT',
clientId,
context,
};
}
Expand Down
14 changes: 1 addition & 13 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1797,19 +1797,7 @@ export const blockListSettings = ( state = {}, action ) => {
*/
export const blockContext = ( state = {}, action ) => {
if ( action.type === 'UPDATE_BLOCK_CONTEXT' ) {
const { clientId } = action;
if ( ! action.context ) {
return state;
}

if ( fastDeepEqual( state[ clientId ], action.context ) ) {
return state;
}

return {
...state,
[ clientId ]: action.context,
};
return action.context;
}
return state;
};
Expand Down
11 changes: 10 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2512,7 +2512,16 @@ export function getBlockListSettings( state, clientId ) {
* @return {?Object} Block context of the block if set.
*/
export function getBlockContext( state, clientId ) {
return state.blockContext[ clientId ];
let blockContext = { ...state.blockContext };
// TODO: Review if it's necessary to get the context from the parent blocks.
getBlockParents( state, clientId ).forEach( ( parent ) => {
const block = getBlock( state, parent );
const blockType = getBlockType( state, block.name );
if ( blockType?.providesContext ) {
blockContext = { ...blockContext, ...blockType?.providesContext };
}
} );
return blockContext;
}

/**
Expand Down

0 comments on commit c939061

Please sign in to comment.