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
Prev Previous commit
Move context action and selector to private
  • Loading branch information
SantosGuillamot committed May 28, 2024
commit 16a87b64317c32a44f69310ab3208cfa64c42e00
26 changes: 0 additions & 26 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,6 @@ _Returns_

- `Object?`: Block attributes.

### getBlockContext

Returns the Block Context of a block, if any exist.

_Parameters_

- _state_ `Object`: Editor state.
- _clientId_ `?string`: Block client ID.

_Returns_

- `?Object`: Block context of the block if set.

### getBlockCount

Returns the number of blocks currently present in the post.
Expand Down Expand Up @@ -1852,19 +1839,6 @@ _Returns_

- `Object`: Action object.

### updateBlockContext

Action that changes the block context of the given block(s) in the store.

_Parameters_

- _clientId_ `string | SettingsByClientId`: Client ID of the block.
- _context_ `Object`: Object with the new context.

_Returns_

- `Object`: Action object

### updateBlockListSettings

Action that changes the nested settings of the given block(s).
Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/block-edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useContext, useLayoutEffect, useMemo } from '@wordpress/element';
*/
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 @@ -55,7 +56,7 @@ const EditWithGeneratedProps = ( props ) => {
const blockContext = useContext( BlockContext );

// Sync the block context with the block editor store.
const { updateBlockContext } = useDispatch( blockEditorStore );
const { updateBlockContext } = unlock( useDispatch( blockEditorStore ) );
useLayoutEffect( () => {
if ( blockContext && Object.keys( blockContext ).length > 0 ) {
updateBlockContext( clientId, blockContext );
Expand Down
16 changes: 0 additions & 16 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1529,22 +1529,6 @@ export function updateBlockListSettings( clientId, settings ) {
};
}

/**
* Action that changes the block context of the given block(s) in the store.
*
* @param {string | SettingsByClientId} 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,
};
}

/**
* Action that updates the block editor settings.
*
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 ];
}
12 changes: 0 additions & 12 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2503,18 +2503,6 @@ export function getBlockListSettings( state, clientId ) {
return state.blockListSettings[ clientId ];
}

/**
* 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 ];
}

/**
* Returns the editor settings.
*
Expand Down
21 changes: 0 additions & 21 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const {
updateBlock,
updateBlockAttributes,
updateBlockListSettings,
updateBlockContext,
updateSettings,
validateBlocksToTemplate,
registerInserterMediaCategory,
Expand Down Expand Up @@ -774,26 +773,6 @@ describe( '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' },
} );
} );
} );

describe( 'replaceInnerBlocks', () => {
const block = {
clientId: 'ribs',
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 );
} );
} );
} );
28 changes: 0 additions & 28 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ const {
getTemplate,
getTemplateLock,
getBlockListSettings,
getBlockContext,
__experimentalGetBlockListSettingsForBlocks,
__experimentalGetLastBlockAttributeChanges,
getLowestCommonAncestorWithSelectedBlock,
Expand Down Expand Up @@ -3823,33 +3822,6 @@ describe( '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 );
} );
} );

describe( '__experimentalGetBlockListSettingsForBlocks', () => {
it( 'should return the settings for a set of blocks', () => {
const state = {
Expand Down
Loading