diff --git a/docs/reference-guides/data/data-core-edit-site.md b/docs/reference-guides/data/data-core-edit-site.md index b22fd2238f303..775dd66a821ef 100644 --- a/docs/reference-guides/data/data-core-edit-site.md +++ b/docs/reference-guides/data/data-core-edit-site.md @@ -24,6 +24,8 @@ _Returns_ ### getCurrentTemplateTemplateParts +> **Deprecated** + Returns the template parts and their blocks for the current edited template. _Parameters_ diff --git a/packages/edit-site/src/store/selectors.js b/packages/edit-site/src/store/selectors.js index 3980d08bbc27b..3f7bbea0be7d2 100644 --- a/packages/edit-site/src/store/selectors.js +++ b/packages/edit-site/src/store/selectors.js @@ -2,16 +2,19 @@ * WordPress dependencies */ import { store as coreDataStore } from '@wordpress/core-data'; -import { createRegistrySelector } from '@wordpress/data'; +import { createRegistrySelector, createSelector } from '@wordpress/data'; import deprecated from '@wordpress/deprecated'; import { Platform } from '@wordpress/element'; import { store as preferencesStore } from '@wordpress/preferences'; import { store as editorStore } from '@wordpress/editor'; +import { store as blockEditorStore } from '@wordpress/block-editor'; /** * Internal dependencies */ import { unlock } from '../lock-unlock'; +import { TEMPLATE_PART_POST_TYPE } from '../utils/constants'; +import getFilteredTemplatePartBlocks from '../utils/get-filtered-template-parts'; /** * @typedef {'template'|'template_type'} TemplateType Template type. @@ -240,18 +243,46 @@ export function isSaveViewOpened( state ) { return state.saveViewPanel; } +function getBlocksAndTemplateParts( select ) { + const templateParts = select( coreDataStore ).getEntityRecords( + 'postType', + TEMPLATE_PART_POST_TYPE, + { per_page: -1 } + ); + + const { getBlocksByName, getBlocksByClientId } = select( blockEditorStore ); + + const clientIds = getBlocksByName( 'core/template-part' ); + const blocks = getBlocksByClientId( clientIds ); + return [ blocks, templateParts ]; +} + /** * Returns the template parts and their blocks for the current edited template. * + * @deprecated * @param {Object} state Global application state. * @return {Array} Template parts and their blocks in an array. */ export const getCurrentTemplateTemplateParts = createRegistrySelector( - ( select ) => () => { - return unlock( - select( editorStore ) - ).getCurrentTemplateTemplateParts(); - } + ( select ) => + createSelector( + () => { + deprecated( + `select( 'core/edit-site' ).getCurrentTemplateTemplateParts()`, + { + since: '6.7', + version: '6.9', + alternative: `select( 'core/block-editor' ).getBlocksByName( 'core/template-part' )`, + } + ); + + return getFilteredTemplatePartBlocks( + ...getBlocksAndTemplateParts( select ) + ); + }, + () => getBlocksAndTemplateParts( select ) + ) ); /** diff --git a/packages/editor/src/store/utils/get-filtered-template-parts.js b/packages/edit-site/src/utils/get-filtered-template-parts.js similarity index 83% rename from packages/editor/src/store/utils/get-filtered-template-parts.js rename to packages/edit-site/src/utils/get-filtered-template-parts.js index af24684ace615..bba982c0717a7 100644 --- a/packages/editor/src/store/utils/get-filtered-template-parts.js +++ b/packages/edit-site/src/utils/get-filtered-template-parts.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import memoize from 'memize'; - /** * WordPress dependencies */ @@ -20,7 +15,10 @@ const EMPTY_ARRAY = []; * @param {?Array} templateParts Available template parts. * @return {Array} An array of template parts and their blocks. */ -function getFilteredTemplatePartBlocks( blocks = EMPTY_ARRAY, templateParts ) { +export default function getFilteredTemplatePartBlocks( + blocks = EMPTY_ARRAY, + templateParts +) { const templatePartsById = templateParts ? // Key template parts by their ID. templateParts.reduce( @@ -61,9 +59,3 @@ function getFilteredTemplatePartBlocks( blocks = EMPTY_ARRAY, templateParts ) { return result; } - -const memoizedGetFilteredTemplatePartBlocks = memoize( - getFilteredTemplatePartBlocks -); - -export { memoizedGetFilteredTemplatePartBlocks as getFilteredTemplatePartBlocks }; diff --git a/packages/editor/src/store/utils/test/get-filtered-template-parts.js b/packages/edit-site/src/utils/test/get-filtered-template-parts.js similarity index 55% rename from packages/editor/src/store/utils/test/get-filtered-template-parts.js rename to packages/edit-site/src/utils/test/get-filtered-template-parts.js index bf9fad5a1cf35..0d5c283633021 100644 --- a/packages/editor/src/store/utils/test/get-filtered-template-parts.js +++ b/packages/edit-site/src/utils/test/get-filtered-template-parts.js @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import { getFilteredTemplatePartBlocks } from '../get-filtered-template-parts'; +import getFilteredTemplatePartBlocks from '../get-filtered-template-parts'; const NESTED_BLOCKS = [ { @@ -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', @@ -134,56 +124,4 @@ describe( 'getFilteredTemplatePartBlocks', () => { ); expect( flattenedFilteredTemplateParts ).toEqual( FLATTENED_BLOCKS ); } ); - - 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 ); - expect( - getFilteredTemplatePartBlocks( NESTED_BLOCKS, TEMPLATE_PARTS ) - ).toEqual( FLATTENED_BLOCKS ); - - // The function has been called twice with the same params, so the cache size should be 1. - /** - * TODO what should be done about this? - * Can it be tested another way? - * Is it necessary? - */ - // const [ , , originalSize ] = - // getFilteredTemplatePartBlocks.getCache(); - // expect( originalSize ).toBe( 1 ); - - // Call the function again, with different params. - expect( - getFilteredTemplatePartBlocks( - [ SINGLE_TEMPLATE_PART_BLOCK ], - TEMPLATE_PARTS - ) - ).toEqual( [ - { - block: { - clientId: '1', - name: 'core/template-part', - attributes: { - slug: 'aside', - theme: 'my-theme', - }, - }, - templatePart: { - id: 'my-theme//aside', - slug: 'aside', - theme: 'my-theme', - }, - }, - ] ); - - // The function has been called with different params, so the cache size should now be 2. - /** - * TODO what should be done about this? - * Can it be tested another way? - * Is it necessary? - */ - // const [ , , finalSize ] = getFilteredTemplatePartBlocks.getCache(); - // expect( finalSize ).toBe( 2 ); - } ); } );