diff --git a/packages/edit-site/src/store/selectors.js b/packages/edit-site/src/store/selectors.js index 3f44ab57ba807..ed44ba326e3bc 100644 --- a/packages/edit-site/src/store/selectors.js +++ b/packages/edit-site/src/store/selectors.js @@ -307,7 +307,7 @@ export const getCurrentTemplateTemplateParts = createRegistrySelector( { per_page: -1 } ); - return getFilteredTemplatePartBlocks( template.blocks, templateParts ); + return getFilteredTemplatePartBlocks( template, templateParts ); } ); diff --git a/packages/edit-site/src/store/test/utils.js b/packages/edit-site/src/store/test/utils.js index f5552f81cd179..52386dd06d8a8 100644 --- a/packages/edit-site/src/store/test/utils.js +++ b/packages/edit-site/src/store/test/utils.js @@ -98,16 +98,6 @@ const FLATTENED_BLOCKS = [ }, ]; -const SINGLE_TEMPLATE_PART_BLOCK = { - clientId: '1', - name: 'core/template-part', - innerBlocks: [], - attributes: { - slug: 'aside', - theme: 'my-theme', - }, -}; - const TEMPLATE_PARTS = [ { id: 'my-theme//header', @@ -126,11 +116,32 @@ const TEMPLATE_PARTS = [ }, ]; +const TEMPLATE_NESTED_BLOCKS = { + blocks: NESTED_BLOCKS, +}; + +const TEMPLATE_SINGLE_BLOCK = { + blocks: [ + { + clientId: '1', + name: 'core/template-part', + innerBlocks: [], + attributes: { + slug: 'aside', + theme: 'my-theme', + }, + }, + ], +}; + describe( 'utils', () => { describe( 'getFilteredTemplatePartBlocks', () => { it( 'returns a flattened list of filtered template parts preserving a depth-first order', () => { const flattenedFilteredTemplateParts = - getFilteredTemplatePartBlocks( NESTED_BLOCKS, TEMPLATE_PARTS ); + getFilteredTemplatePartBlocks( + TEMPLATE_NESTED_BLOCKS, + TEMPLATE_PARTS + ); expect( flattenedFilteredTemplateParts ).toEqual( FLATTENED_BLOCKS ); @@ -139,9 +150,15 @@ describe( 'utils', () => { it( 'returns a cached result when passed the same params', () => { // Clear the cache and call the function twice. getFilteredTemplatePartBlocks.clear(); - getFilteredTemplatePartBlocks( NESTED_BLOCKS, TEMPLATE_PARTS ); + getFilteredTemplatePartBlocks( + TEMPLATE_NESTED_BLOCKS, + TEMPLATE_PARTS + ); expect( - getFilteredTemplatePartBlocks( NESTED_BLOCKS, TEMPLATE_PARTS ) + getFilteredTemplatePartBlocks( + TEMPLATE_NESTED_BLOCKS, + TEMPLATE_PARTS + ) ).toEqual( FLATTENED_BLOCKS ); // The function has been called twice with the same params, so the cache size should be 1. @@ -157,7 +174,7 @@ describe( 'utils', () => { // Call the function again, with different params. expect( getFilteredTemplatePartBlocks( - [ SINGLE_TEMPLATE_PART_BLOCK ], + TEMPLATE_SINGLE_BLOCK, TEMPLATE_PARTS ) ).toEqual( [ diff --git a/packages/edit-site/src/store/utils.js b/packages/edit-site/src/store/utils.js index af24684ace615..88e5721d0f779 100644 --- a/packages/edit-site/src/store/utils.js +++ b/packages/edit-site/src/store/utils.js @@ -6,21 +6,30 @@ import memoize from 'memize'; /** * WordPress dependencies */ -import { isTemplatePart } from '@wordpress/blocks'; +import { isTemplatePart, parse } from '@wordpress/blocks'; -const EMPTY_ARRAY = []; +function getBlocksFromRecord( record ) { + if ( record?.blocks ) { + return record?.blocks; + } + + return record?.content && typeof record.content !== 'function' + ? parse( record.content ) + : []; +} /** * Get a flattened and filtered list of template parts and the matching block for that template part. * - * Takes a list of blocks defined within a template, and a list of template parts, and returns a + * Takes a template, and a list of template parts, and returns a * flattened list of template parts and the matching block for that template part. * - * @param {Array} blocks Blocks to flatten. + * @param {Object} template Current template. * @param {?Array} templateParts Available template parts. * @return {Array} An array of template parts and their blocks. */ -function getFilteredTemplatePartBlocks( blocks = EMPTY_ARRAY, templateParts ) { +function getFilteredTemplatePartBlocks( template, templateParts ) { + const blocks = getBlocksFromRecord( template ); const templatePartsById = templateParts ? // Key template parts by their ID. templateParts.reduce(