diff --git a/packages/block-editor/src/store/private-selectors.js b/packages/block-editor/src/store/private-selectors.js index 3a6dcdd4e19e6b..9d3c5f27a7cc98 100644 --- a/packages/block-editor/src/store/private-selectors.js +++ b/packages/block-editor/src/store/private-selectors.js @@ -20,6 +20,7 @@ import { checkAllowListRecursive, getAllPatternsDependants, getInsertBlockTypeDependants, + getParsedPattern, } from './utils'; import { INSERTER_PATTERN_TYPES } from '../components/inserter/block-patterns-tab/utils'; import { STORE_NAME } from './constants'; @@ -291,16 +292,15 @@ export const getInserterMediaCategories = createSelector( export const hasAllowedPatterns = createRegistrySelector( ( select ) => createSelector( ( state, rootClientId = null ) => { - const { getAllPatterns, __experimentalGetParsedPattern } = unlock( - select( STORE_NAME ) - ); + const { getAllPatterns } = unlock( select( STORE_NAME ) ); const patterns = getAllPatterns(); const { allowedBlockTypes } = getSettings( state ); - return patterns.some( ( { name, inserter = true } ) => { + return patterns.some( ( pattern ) => { + const { inserter = true } = pattern; if ( ! inserter ) { return false; } - const { blocks } = __experimentalGetParsedPattern( name ); + const { blocks } = getParsedPattern( pattern ); return ( checkAllowListRecursive( blocks, allowedBlockTypes ) && blocks.every( ( { name: blockName } ) => diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index f06293dadf233c..32fa081b390ae9 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -7,7 +7,6 @@ import { getBlockVariations, hasBlockSupport, getPossibleBlockTransformations, - parse, switchToBlockType, store as blocksStore, } from '@wordpress/blocks'; @@ -27,6 +26,7 @@ import { checkAllowList, getAllPatternsDependants, getInsertBlockTypeDependants, + getParsedPattern, } from './utils'; import { orderBy } from '../utils/sorting'; import { STORE_NAME } from './constants'; @@ -2349,40 +2349,12 @@ export function __experimentalGetDirectInsertBlock( } export const __experimentalGetParsedPattern = createRegistrySelector( - ( select ) => - createSelector( - ( state, patternName ) => { - const pattern = unlock( select( STORE_NAME ) ).getPatternBySlug( - patternName - ); - if ( ! pattern ) { - return null; - } - const blocks = parse( pattern.content, { - __unstableSkipMigrationLogs: true, - } ); - if ( blocks.length === 1 ) { - blocks[ 0 ].attributes = { - ...blocks[ 0 ].attributes, - metadata: { - ...( blocks[ 0 ].attributes.metadata || {} ), - categories: pattern.categories, - patternName: pattern.name, - name: - blocks[ 0 ].attributes.metadata?.name || - pattern.title, - }, - }; - } - return { - ...pattern, - blocks, - }; - }, - ( state, patternName ) => [ - unlock( select( STORE_NAME ) ).getPatternBySlug( patternName ), - ] - ) + ( select ) => ( state, patternName ) => { + const pattern = unlock( select( STORE_NAME ) ).getPatternBySlug( + patternName + ); + return pattern ? getParsedPattern( pattern ) : null; + } ); const getAllowedPatternsDependants = ( select ) => ( state, rootClientId ) => [ @@ -2401,16 +2373,13 @@ const getAllowedPatternsDependants = ( select ) => ( state, rootClientId ) => [ export const __experimentalGetAllowedPatterns = createRegistrySelector( ( select ) => { return createSelector( ( state, rootClientId = null ) => { - const { - getAllPatterns, - __experimentalGetParsedPattern: getParsedPattern, - } = unlock( select( STORE_NAME ) ); + const { getAllPatterns } = unlock( select( STORE_NAME ) ); const patterns = getAllPatterns(); const { allowedBlockTypes } = getSettings( state ); const parsedPatterns = patterns .filter( ( { inserter = true } ) => !! inserter ) - .map( ( { name } ) => getParsedPattern( name ) ); + .map( getParsedPattern ); const availableParsedPatterns = parsedPatterns.filter( ( { blocks } ) => checkAllowListRecursive( blocks, allowedBlockTypes ) diff --git a/packages/block-editor/src/store/utils.js b/packages/block-editor/src/store/utils.js index c94453e99c60a4..71c5ac44f45b83 100644 --- a/packages/block-editor/src/store/utils.js +++ b/packages/block-editor/src/store/utils.js @@ -1,3 +1,8 @@ +/** + * WordPress dependencies + */ +import { parse } from '@wordpress/blocks'; + /** * Internal dependencies */ @@ -7,6 +12,39 @@ import { STORE_NAME } from './constants'; export const withRootClientIdOptionKey = Symbol( 'withRootClientId' ); +const parsedPatternCache = new WeakMap(); + +function parsePattern( pattern ) { + const blocks = parse( pattern.content, { + __unstableSkipMigrationLogs: true, + } ); + if ( blocks.length === 1 ) { + blocks[ 0 ].attributes = { + ...blocks[ 0 ].attributes, + metadata: { + ...( blocks[ 0 ].attributes.metadata || {} ), + categories: pattern.categories, + patternName: pattern.name, + name: blocks[ 0 ].attributes.metadata?.name || pattern.title, + }, + }; + } + return { + ...pattern, + blocks, + }; +} + +export function getParsedPattern( pattern ) { + let parsedPattern = parsedPatternCache.get( pattern ); + if ( parsedPattern ) { + return parsedPattern; + } + parsedPattern = parsePattern( pattern ); + parsedPatternCache.set( pattern, parsedPattern ); + return parsedPattern; +} + export const checkAllowList = ( list, item, defaultResult = null ) => { if ( typeof list === 'boolean' ) { return list;