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

Add __experimentalGetGlobalBlocksByName selector #39610

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,29 @@ export const getGlobalBlockCount = createSelector(
( state ) => [ state.blocks.order, state.blocks.byClientId ]
);

/**
* Returns all global blocks that match a blockName. Results include nested blocks.
*
* @param {Object} state Global application state.
* @param {?string} blockName Optional block name, if not specified, returns an empty array.
*
* @return {Array} Array of clientIds of blocks with name equal to blockName.
*/
export const __experimentalGetGlobalBlocksByName = createSelector(
( state, blockName ) => {
if ( ! blockName ) {
return EMPTY_ARRAY;
}
const clientIds = getClientIdsWithDescendants( state );
const foundBlocks = clientIds.filter( ( clientId ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just return the result of the filter? Do we need to use the EMPTY_ARRAY const?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filter will return a new array reference. Thus we use the reference to a constant object reference EMPTY_ARRAY so as to avoid returning a new object reference if the selector returns "nothing". It's a minor perf thing but it could make a big difference in certain components.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see Kerry even wrote a test to validate that. See the final it() in the tests.

const block = state.blocks.byClientId[ clientId ];
return block.name === blockName;
} );
return foundBlocks.length > 0 ? foundBlocks : EMPTY_ARRAY;
},
( state ) => [ state.blocks.order, state.blocks.byClientId ]
);

/**
* Given an array of block client IDs, returns the corresponding array of block
* objects.
Expand Down
63 changes: 63 additions & 0 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const {
__unstableGetClientIdsTree,
__experimentalGetPatternTransformItems,
wasBlockJustInserted,
__experimentalGetGlobalBlocksByName,
} = selectors;

describe( 'selectors', () => {
Expand Down Expand Up @@ -797,6 +798,68 @@ describe( 'selectors', () => {
} );
} );

describe( '__experimentalGetGlobalBlocksByName', () => {
const state = {
blocks: {
byClientId: {
123: { clientId: 123, name: 'core/heading' },
456: { clientId: 456, name: 'core/paragraph' },
789: { clientId: 789, name: 'core/paragraph' },
1011: { clientId: 1011, name: 'core/group' },
1213: { clientId: 1213, name: 'core/paragraph' },
1415: { clientId: 1213, name: 'core/paragraph' },
},
attributes: {
123: {},
456: {},
789: {},
1011: {},
1213: {},
1415: {},
},
order: {
'': [ 123, 456, 1011 ],
1011: [ 1415, 1213 ],
},
parents: {
123: '',
456: '',
1011: '',
1213: 1011,
1415: 1011,
},
},
};

it( 'should return the clientIds of blocks of a given type', () => {
expect(
__experimentalGetGlobalBlocksByName( state, 'core/heading' )
).toStrictEqual( [ 123 ] );
} );

it( 'should return the clientIds of blocks of a given type even if blocks are nested', () => {
expect(
__experimentalGetGlobalBlocksByName( state, 'core/paragraph' )
).toStrictEqual( [ 456, 1415, 1213 ] );
} );

it( 'Should return empty array if no blocks match. The empty array should be the same reference', () => {
const result = __experimentalGetGlobalBlocksByName(
state,
'test/missing'
);
expect(
__experimentalGetGlobalBlocksByName( state, 'test/missing' )
).toStrictEqual( [] );
expect(
__experimentalGetGlobalBlocksByName(
state,
'test/missing2'
) === result
).toBe( true );
} );
} );

describe( 'getSelectedBlockClientId', () => {
it( 'should return null if no block is selected', () => {
const state = {
Expand Down