Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusable blocks: fix performance of __experimentalGetAllowedPatterns #64871

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
ellatrix marked this conversation as resolved.
Show resolved Hide resolved
( 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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine now without memoization, it will always return the same ref.

}
);

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
Loading