Skip to content

Commit

Permalink
Fix allowed patterns check (#32376)
Browse files Browse the repository at this point in the history
Co-authored-by: ntsekouras <[email protected]>
  • Loading branch information
youknowriad and ntsekouras committed Jun 7, 2021
1 parent e7d6fe4 commit 5faf21e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,17 @@ function usePatternsSetup( clientId, blockName, filterPatternsFn ) {
const {
getBlockRootClientId,
__experimentalGetPatternsByBlockTypes,
__experimentalGetParsedPattern,
__experimentalGetAllowedPatterns,
} = select( blockEditorStore );
const rootClientId = getBlockRootClientId( clientId );
let patterns = [];
if ( filterPatternsFn ) {
patterns = __experimentalGetAllowedPatterns(
rootClientId
).filter( filterPatternsFn );
} else {
patterns = __experimentalGetPatternsByBlockTypes(
blockName,
rootClientId
return __experimentalGetAllowedPatterns( rootClientId ).filter(
filterPatternsFn
);
}
return patterns.map( ( { name } ) =>
__experimentalGetParsedPattern( name )
return __experimentalGetPatternsByBlockTypes(
blockName,
rootClientId
);
},
[ clientId, blockName, filterPatternsFn ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
*/
import { useMemo } from '@wordpress/element';
import { cloneBlock } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { getMatchingBlockByName, getRetainedBlockAttributes } from './utils';
import { store as blockEditorStore } from '../../store';

/**
* Mutate the matched block's attributes by getting
Expand Down Expand Up @@ -96,19 +94,9 @@ export const getPatternTransformedBlocks = (
*/
// TODO tests
const useTransformedPatterns = ( patterns, selectedBlocks ) => {
const parsedPatterns = useSelect(
( select ) =>
patterns.map( ( { name } ) =>
select( blockEditorStore ).__experimentalGetParsedPattern(
name
)
),
[ patterns ]
);

return useMemo(
() =>
parsedPatterns.reduce( ( accumulator, _pattern ) => {
patterns.reduce( ( accumulator, _pattern ) => {
const transformedBlocks = getPatternTransformedBlocks(
selectedBlocks,
_pattern.blocks
Expand All @@ -121,7 +109,7 @@ const useTransformedPatterns = ( patterns, selectedBlocks ) => {
}
return accumulator;
}, [] ),
[ parsedPatterns, selectedBlocks ]
[ patterns, selectedBlocks ]
);
};

Expand Down
24 changes: 9 additions & 15 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
} from '@wordpress/blocks';
import { SVG, Rect, G, Path } from '@wordpress/components';
import { Platform } from '@wordpress/element';
import { parse as parseBlocks } from '@wordpress/block-serialization-default-parser';

/**
* A block selection object.
Expand Down Expand Up @@ -1830,17 +1829,11 @@ const getAllAllowedPatterns = createSelector(
( state ) => {
const patterns = state.settings.__experimentalBlockPatterns;
const { allowedBlockTypes } = getSettings( state );
const parsedPatterns = patterns.map( ( pattern ) => ( {
...pattern,
// We only need the overall block structure of the pattern. So, for
// performance reasons, we can parse the pattern's content using
// the raw blocks parser, also known as the "stage I" block parser.
// This is about 250x faster than the full parse that the Block API
// offers.
blockNodes: parseBlocks( pattern.content ),
} ) );
const allowedPatterns = parsedPatterns.filter( ( { blockNodes } ) =>
checkAllowListRecursive( blockNodes, allowedBlockTypes )
const parsedPatterns = patterns.map( ( { name } ) =>
__experimentalGetParsedPattern( state, name )
);
const allowedPatterns = parsedPatterns.filter( ( { blocks } ) =>
checkAllowListRecursive( blocks, allowedBlockTypes )
);
return allowedPatterns;
},
Expand All @@ -1863,11 +1856,12 @@ export const __experimentalGetAllowedPatterns = createSelector(
const availableParsedPatterns = getAllAllowedPatterns( state );
const patternsAllowed = filter(
availableParsedPatterns,
( { blockNodes } ) =>
blockNodes.every( ( { blockName } ) =>
canInsertBlockType( state, blockName, rootClientId )
( { blocks } ) =>
blocks.every( ( { name } ) =>
canInsertBlockType( state, name, rootClientId )
)
);

return patternsAllowed;
},
( state, rootClientId ) => [
Expand Down

0 comments on commit 5faf21e

Please sign in to comment.