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

Patterns: Show the default patterns icons for all pattern blocks in inserter #53208

Merged
merged 2 commits into from
Aug 2, 2023
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
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
Loading