Skip to content

Commit

Permalink
Blocks: Match blocks in the inserter using keywords from patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Dec 19, 2019
1 parent 01e2c05 commit b05a375
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/block-editor/src/components/inserter/search-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const searchItems = ( items, categories, searchTerm ) => {
return items;
}

return items.filter( ( { title, category, keywords = [] } ) => {
return items.filter( ( { title, category, keywords = [], patterns = [] } ) => {
let unmatchedTerms = removeMatchingTerms(
normalizedTerms,
title
Expand All @@ -81,6 +81,15 @@ export const searchItems = ( items, categories, searchTerm ) => {
get( find( categories, { slug: category } ), [ 'title' ] ),
);

if ( unmatchedTerms.length === 0 ) {
return true;
}

unmatchedTerms = removeMatchingTerms(
unmatchedTerms,
patterns.map( ( { label } ) => label ).join( ' ' ),
);

return unmatchedTerms.length === 0;
} );
};
1 change: 1 addition & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,7 @@ export const getInserterItems = createSelector(
icon: blockType.icon,
category: blockType.category,
keywords: blockType.keywords,
patterns: blockType.patterns,
isDisabled,
utility: calculateUtility( blockType.category, count, isContextual ),
frecency: calculateFrecency( time, count ),
Expand Down
10 changes: 9 additions & 1 deletion packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ const getNormalizedBlockType = ( state, nameOrType ) => (
* @return {Array} Block Types.
*/
export const getBlockTypes = createSelector(
( state ) => Object.values( state.blockTypes ),
( state ) => {
return Object.values( state.blockTypes ).map( ( blockType ) => {
return {
...blockType,
patterns: __experimentalGetBlockPatterns( state, blockType.name ),
};
} );
},
( state ) => [
state.blockTypes,
state.blockPatterns,
]
);

Expand Down

0 comments on commit b05a375

Please sign in to comment.