Skip to content

Commit

Permalink
Patterns: Show the default patterns icons for all pattern blocks in i…
Browse files Browse the repository at this point in the history
…nserter (#53208)
  • Loading branch information
glendaviesnz authored Aug 2, 2023
1 parent afc73ee commit 76faac8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 45 deletions.
45 changes: 1 addition & 44 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1962,51 +1962,8 @@ const buildBlockTypeItem =
*/
export const getInserterItems = createSelector(
( state, rootClientId = null ) => {
/*
* Matches block comment delimiters amid serialized content.
*
* @see `tokenizer` in `@wordpress/block-serialization-default-parser`
* package
*
* blockParserTokenizer differs from the original tokenizer in the
* following ways:
*
* - removed global flag (/g)
* - prepended ^\s*
*
*/
const blockParserTokenizer =
/^\s*<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/;

const buildReusableBlockInserterItem = ( reusableBlock ) => {
let icon = symbol;

/*
* Instead of always displaying a generic "symbol" icon for every
* reusable block, try to use an icon that represents the first
* outermost block contained in the reusable block. This requires
* scanning the serialized form of the reusable block to find its
* first block delimiter, then looking up the corresponding block
* type, if available.
*/
if ( Platform.OS === 'web' ) {
const content =
typeof reusableBlock.content.raw === 'string'
? reusableBlock.content.raw
: reusableBlock.content;
const rawBlockMatch = content.match( blockParserTokenizer );
if ( rawBlockMatch ) {
const [ , , namespace = 'core/', blockName ] =
rawBlockMatch;
const referencedBlockType = getBlockType(
namespace + blockName
);
if ( referencedBlockType ) {
icon = referencedBlockType.icon;
}
}
}

const icon = symbol;
const id = `core/block/${ reusableBlock.id }`;
const { time, count = 0 } = getInsertUsage( state, id ) || {};
const frecency = calculateFrecency( time, count );
Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
setFreeformContentHandlerName,
} from '@wordpress/blocks';
import { RawHTML } from '@wordpress/element';
import { symbol } from '@wordpress/icons';

/**
* Internal dependencies
Expand Down Expand Up @@ -3347,7 +3348,7 @@ describe( 'selectors', () => {
category: 'reusable',
content: '<!-- /wp:test-block-a -->',
frecency: 0,
icon: { src: 'test' },
icon: symbol,
id: 'core/block/1',
initialAttributes: { ref: 1 },
isDisabled: false,
Expand Down

1 comment on commit 76faac8

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 76faac8.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5733554092
📝 Reported issues:

Please sign in to comment.