Skip to content

Commit

Permalink
Stabilize reapplyBlockTypeFilters
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Aug 28, 2023
1 parent cefb1ea commit 11322b9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion docs/reference-guides/data/data-core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,10 @@ The actions in this package shouldn't be used directly. Instead, use the functio

<!-- START TOKEN(Autogenerated actions|../../../packages/blocks/src/store/actions.js) -->

Nothing to document.
### reapplyBlockTypeFilters

Signals that all block types should be computed again. It uses stored unprocessed block types and all the most recent list of registered filters.

It addresses the issue where third party block filters get registered after third party blocks. A sample sequence: 1. Filter A. 2. Block B. 3. Block C. 4. Filter D. 5. Filter E. 6. Block F. 7. Filter G. In this scenario some filters would not get applied for all blocks because they are registered too late.

<!-- END TOKEN(Autogenerated actions|../../../packages/blocks/src/store/actions.js) -->
2 changes: 1 addition & 1 deletion packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ describe( 'blocks', () => {
);

// reapply the block filters
dispatch( blocksStore ).__experimentalReapplyBlockTypeFilters();
dispatch( blocksStore ).reapplyBlockTypeFilters();

// check that block type has filtered values
expect( getBlockType( 'test/block' ).title ).toBe(
Expand Down
23 changes: 20 additions & 3 deletions packages/blocks/src/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -39,9 +44,8 @@ export function addBlockTypes( blockTypes ) {
* 7. Filter G.
* In this scenario some filters would not get applied for all blocks because they are registered too late.
*/
export const __experimentalReapplyBlockTypeFilters =
() =>
( { dispatch, select } ) => {
export function reapplyBlockTypeFilters() {
return ( { dispatch, select } ) => {
const processedBlockTypes = [];
for ( const [ name, settings ] of Object.entries(
select.getUnprocessedBlockTypes()
Expand All @@ -58,6 +62,19 @@ export const __experimentalReapplyBlockTypeFilters =

dispatch.addBlockTypes( processedBlockTypes );
};
}

export function __experimentalReapplyBlockFilters() {
deprecated(
'wp.data.dispatch( "core/blocks" ).__experimentalReapplyBlockFilters',
{
since: '6.4',
alternative: 'reapplyBlockFilters',
}
);

return reapplyBlockTypeFilters();
}

/**
* Returns an action object used to remove a registered block type.
Expand Down
2 changes: 1 addition & 1 deletion packages/customize-widgets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function initialize( editorName, blockEditorSettings ) {
welcomeGuide: true,
} );

dispatch( blocksStore ).__experimentalReapplyBlockTypeFilters();
dispatch( blocksStore ).reapplyBlockTypeFilters();
const coreBlocks = __experimentalGetCoreBlocks().filter( ( block ) => {
return ! (
DISABLED_BLOCKS.includes( block.name ) ||
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function initializeEditor(
welcomeGuideTemplate: true,
} );

dispatch( blocksStore ).__experimentalReapplyBlockTypeFilters();
dispatch( blocksStore ).reapplyBlockTypeFilters();

// Check if the block list view should be open by default.
// If `distractionFree` mode is enabled, the block list view should not be open.
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function initializeEditor( id, settings ) {
fetchLinkSuggestions( search, searchOptions, settings );
settings.__experimentalFetchRichUrlData = fetchUrlData;

dispatch( blocksStore ).__experimentalReapplyBlockTypeFilters();
dispatch( blocksStore ).reapplyBlockTypeFilters();
const coreBlocks = __experimentalGetCoreBlocks().filter(
( { name } ) => name !== 'core/freeform'
);
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-widgets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function initializeEditor( id, settings ) {
themeStyles: true,
} );

dispatch( blocksStore ).__experimentalReapplyBlockTypeFilters();
dispatch( blocksStore ).reapplyBlockTypeFilters();
registerCoreBlocks( coreBlocks );
registerLegacyWidgetBlock();
if ( process.env.IS_GUTENBERG_PLUGIN ) {
Expand Down

0 comments on commit 11322b9

Please sign in to comment.