From 0b53f74eec3a08be851a4e43d28a11eb346fab13 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 5 Oct 2023 16:34:03 +0400 Subject: [PATCH 1/4] Site Editor: Fix template part area listing when a template has no edits --- packages/edit-site/src/store/selectors.js | 2 +- packages/edit-site/src/store/utils.js | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/edit-site/src/store/selectors.js b/packages/edit-site/src/store/selectors.js index 3f44ab57ba8072..ed44ba326e3bcb 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/utils.js b/packages/edit-site/src/store/utils.js index af24684ace615e..88e5721d0f7798 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( From 71a9e3d9e2a94cda4ccc2a02328e9a277d4520f9 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 5 Oct 2023 17:08:57 +0400 Subject: [PATCH 2/4] Unit tests --- packages/edit-site/src/store/test/utils.js | 45 +++++++++++++++------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/packages/edit-site/src/store/test/utils.js b/packages/edit-site/src/store/test/utils.js index f5552f81cd179b..d07c5e31ad395e 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 TEMAPLATE_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 ], + TEMAPLATE_SINGLE_BLOCK, TEMPLATE_PARTS ) ).toEqual( [ From f1115dede7b144221cf47a59dbcc8b8c19cff4cf Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 6 Oct 2023 08:54:47 +0400 Subject: [PATCH 3/4] Update packages/edit-site/src/store/test/utils.js Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com> --- packages/edit-site/src/store/test/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/store/test/utils.js b/packages/edit-site/src/store/test/utils.js index d07c5e31ad395e..ccf23286c744e5 100644 --- a/packages/edit-site/src/store/test/utils.js +++ b/packages/edit-site/src/store/test/utils.js @@ -120,7 +120,7 @@ const TEMPLATE_NESTED_BLOCKS = { blocks: NESTED_BLOCKS, }; -const TEMAPLATE_SINGLE_BLOCK = { +const TEMPLATE_SINGLE_BLOCK = { blocks: [ { clientId: '1', From 8785d65ebf7d44c8a961da116553987c1c158634 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 6 Oct 2023 09:08:18 +0400 Subject: [PATCH 4/4] Fix unit tests --- packages/edit-site/src/store/test/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/store/test/utils.js b/packages/edit-site/src/store/test/utils.js index ccf23286c744e5..52386dd06d8a83 100644 --- a/packages/edit-site/src/store/test/utils.js +++ b/packages/edit-site/src/store/test/utils.js @@ -174,7 +174,7 @@ describe( 'utils', () => { // Call the function again, with different params. expect( getFilteredTemplatePartBlocks( - TEMAPLATE_SINGLE_BLOCK, + TEMPLATE_SINGLE_BLOCK, TEMPLATE_PARTS ) ).toEqual( [