Skip to content

Commit

Permalink
Reusable blocks: fix performance of __experimentalGetAllowedPatterns (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored and gutenbergplugin committed Aug 29, 2024
1 parent ac02098 commit c7f1e8e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 45 deletions.
10 changes: 5 additions & 5 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 } ) =>
Expand Down
49 changes: 9 additions & 40 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getBlockVariations,
hasBlockSupport,
getPossibleBlockTransformations,
parse,
switchToBlockType,
store as blocksStore,
} from '@wordpress/blocks';
Expand All @@ -27,6 +26,7 @@ import {
checkAllowList,
getAllPatternsDependants,
getInsertBlockTypeDependants,
getParsedPattern,
} from './utils';
import { orderBy } from '../utils/sorting';
import { STORE_NAME } from './constants';
Expand Down Expand Up @@ -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 ) => [
Expand All @@ -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 )
Expand Down
38 changes: 38 additions & 0 deletions packages/block-editor/src/store/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { parse } from '@wordpress/blocks';

/**
* Internal dependencies
*/
Expand All @@ -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;
Expand Down

0 comments on commit c7f1e8e

Please sign in to comment.