Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try: Sync block context with store #61895

Closed
wants to merge 10 commits into from
15 changes: 13 additions & 2 deletions packages/block-editor/src/components/block-edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import {
hasBlockSupport,
getBlockType,
} from '@wordpress/blocks';
import { useContext, useMemo } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { useContext, useLayoutEffect, useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import BlockContext from '../block-context';
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';

/**
* Default value used for blocks which do not define their own context needs,
Expand Down Expand Up @@ -48,10 +51,18 @@ const Edit = ( props ) => {
const EditWithFilters = withFilters( 'editor.BlockEdit' )( Edit );

const EditWithGeneratedProps = ( props ) => {
const { attributes = {}, name } = props;
const { clientId, attributes = {}, name } = props;
const blockType = getBlockType( name );
const blockContext = useContext( BlockContext );

// Sync the block context with the block editor store.
const { updateBlockContext } = unlock( useDispatch( blockEditorStore ) );
useLayoutEffect( () => {
if ( blockContext && Object.keys( blockContext ).length > 0 ) {
updateBlockContext( clientId, blockContext );
}
}, [ clientId, blockContext, updateBlockContext ] );

// Assign context values using the block type's declared context needs.
const context = useMemo( () => {
return blockType && blockType.usesContext
Expand Down
16 changes: 16 additions & 0 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,19 @@ export const modifyContentLockBlock =
focusModeToRevert
);
};

/**
* Action that changes the block context of the given block in the store.
*
* @param {string } clientId Client ID of the block.
* @param {Object} context Object with the new context.
*
* @return {Object} Action object
*/
export function updateBlockContext( clientId, context ) {
return {
type: 'UPDATE_BLOCK_CONTEXT',
clientId,
context,
};
}
12 changes: 12 additions & 0 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,15 @@ export function getTemporarilyEditingAsBlocks( state ) {
export function getTemporarilyEditingFocusModeToRevert( state ) {
return state.temporarilyEditingFocusModeRevert;
}

/**
* Returns the Block Context of a block, if any exist.
*
* @param {Object} state Editor state.
* @param {?string} clientId Block client ID.
*
* @return {?Object} Block context of the block if set.
*/
export function getBlockContext( state, clientId ) {
return state.blockContext[ clientId ];
}
29 changes: 29 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,34 @@ export const blockListSettings = ( state = {}, action ) => {
return state;
};

/**
* Reducer returning an object where each key is a block client ID, its value
* representing the block context.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
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 state;
};

/**
* Reducer returning which mode is enabled.
*
Expand Down Expand Up @@ -2080,6 +2108,7 @@ const combinedReducers = combineReducers( {
initialPosition,
blocksMode,
blockListSettings,
blockContext,
insertionPoint,
template,
settings,
Expand Down
21 changes: 21 additions & 0 deletions packages/block-editor/src/store/test/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
setOpenedBlockSettingsMenu,
startDragging,
stopDragging,
updateBlockContext,
} from '../private-actions';

describe( 'private actions', () => {
Expand Down Expand Up @@ -123,4 +124,24 @@ describe( 'private actions', () => {
} );
} );
} );

describe( 'updateBlockContext', () => {
it( 'should return the UPDATE_BLOCK_CONTEXT with undefined context', () => {
expect( updateBlockContext( 'chicken' ) ).toEqual( {
type: 'UPDATE_BLOCK_CONTEXT',
clientId: 'chicken',
context: undefined,
} );
} );

it( 'should return the UPDATE_BLOCK_CONTEXT action with the passed context', () => {
expect(
updateBlockContext( 'chicken', { chicken: 'ribs' } )
).toEqual( {
type: 'UPDATE_BLOCK_CONTEXT',
clientId: 'chicken',
context: { chicken: 'ribs' },
} );
} );
} );
} );
28 changes: 28 additions & 0 deletions packages/block-editor/src/store/test/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getEnabledBlockParents,
getExpandedBlock,
isDragging,
getBlockContext,
} from '../private-selectors';
import { getBlockEditingMode } from '../selectors';

Expand Down Expand Up @@ -509,4 +510,31 @@ describe( 'private selectors', () => {
);
} );
} );

describe( 'getBlockContext', () => {
it( 'should return the context of a block', () => {
const state = {
blockContext: {
chicken: {
context1: false,
},
ribs: {
context2: true,
},
},
};

expect( getBlockContext( state, 'chicken' ) ).toEqual( {
context1: false,
} );
} );

it( 'should return undefined if context for the block doesn’t exist', () => {
const state = {
blockContext: {},
};

expect( getBlockContext( state, 'chicken' ) ).toBe( undefined );
} );
} );
} );
Loading