From d846beb4eb63bfeb1ebecf30e895ca49236a7eb6 Mon Sep 17 00:00:00 2001 From: Derrick Koo Date: Thu, 9 Jul 2020 16:26:07 -0600 Subject: [PATCH] fix: handle deeply nested image blocks --- src/editor/blocks/posts-inserter/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/editor/blocks/posts-inserter/index.js b/src/editor/blocks/posts-inserter/index.js index dde54b0bf..c51f55bdb 100644 --- a/src/editor/blocks/posts-inserter/index.js +++ b/src/editor/blocks/posts-inserter/index.js @@ -46,13 +46,16 @@ const PostsInserterBlock = ( { removeBlock, } ) => { const [ isReady, setIsReady ] = useState( ! attributes.displayFeaturedImage ); + const stringifiedPostList = JSON.stringify( postList ); // Stringify added to minimize flicker. const templateBlocks = useMemo( () => getTemplateBlocks( postList, attributes ), [ - JSON.stringify( postList ), + stringifiedPostList, attributes, ] ); + const stringifiedTemplateBlocks = JSON.stringify( templateBlocks ); + useEffect(() => { const { isDisplayingSpecificPosts, specificPosts } = attributes; @@ -81,14 +84,14 @@ const PostsInserterBlock = ( { } // Wait for image blocks to be added to the BlockPreview. - const imageBlocks = templateBlocks.filter( block => 'core/image' === block.name ); + const imageBlocks = stringifiedTemplateBlocks.match( /\"name\":\"core\/image\"/g ) || []; // Preview is ready once all image blocks are accounted for. if ( imageBlocks.length === images.length ) { setIsReady( true ); } } - }, [ JSON.stringify( postList ), JSON.stringify( templateBlocks ) ]); + }, [ stringifiedPostList, stringifiedTemplateBlocks ]); const innerBlocksToInsert = templateBlocks.map( convertBlockSerializationFormat ); useEffect(() => {