Skip to content

Commit

Permalink
Fix articles block fail to get all inner blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Sep 10, 2024
1 parent ad718db commit ac6914a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/blocks/homepage-articles/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { addQueryArgs } from '@wordpress/url';
* Internal dependencies
*/
import metadata from './block.json';
import { getBlockQueries, sanitizePostList } from './utils';
import { getBlockQueries, sanitizePostList, recursivelyGetBlocks } from './utils';

const { name } = metadata;
export const STORE_NAMESPACE = `newspack-blocks/${ name }`;
Expand Down Expand Up @@ -138,15 +138,7 @@ const createFetchPostsSaga = blockNames => {

yield put( { type: 'DISABLE_UI' } );

// Ensure innerBlocks are populated for widget area blocks.
// See https://github.com/WordPress/gutenberg/issues/32607#issuecomment-890728216.
const blocks = getBlocks().map( block => {
const innerBlocks = select( 'core/block-editor' ).getBlocks( block.clientId );
return {
...block,
innerBlocks,
};
} );
const blocks = recursivelyGetBlocks( getBlocks );

const blockQueries = getBlockQueries( blocks, blockNames );

Expand Down
26 changes: 26 additions & 0 deletions src/blocks/homepage-articles/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import { dispatch as wpDataDispatch } from '@wordpress/data';

import { STORE_NAMESPACE } from './store';

// A shimmed type for a Block.
type Block = {
clientId: string;
innerBlocks: Block[];
};

/**
* Based global WP.com blog_public option, checks whether current blog is
* private or not.
Expand Down Expand Up @@ -271,3 +277,23 @@ export const postsBlockDispatch = (
triggerReflow: isEditorBlock ? dispatch( STORE_NAMESPACE ).reflow : () => undefined,
};
};

// Ensure innerBlocks are populated for some blocks (e.g. `widget-area` and `post-content`).
// See https://github.com/WordPress/gutenberg/issues/32607#issuecomment-890728216.
// See https://github.com/Automattic/wp-calypso/issues/91839.
export const recursivelyGetBlocks = (
getBlocks: ( clientId?: string ) => Block[],
blocks: Block[] = getBlocks()
) => {
return blocks.map( ( block ) => {
let innerBlocks =
block.innerBlocks.length === 0
? getBlocks( block.clientId )
: block.innerBlocks;
innerBlocks = recursivelyGetBlocks( getBlocks, innerBlocks );
return {
...block,
innerBlocks,
};
});
};

0 comments on commit ac6914a

Please sign in to comment.