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

Add block variations for individual template parts #42454

Merged
merged 10 commits into from
Jul 27, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createBlock,
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';
import { __experimentalTruncate as Truncate } from '@wordpress/components';
import { ENTER } from '@wordpress/keycodes';

/**
Expand Down Expand Up @@ -135,7 +136,9 @@ function InserterListItem( {
<BlockIcon icon={ item.icon } showColors />
</span>
<span className="block-editor-block-types-list__item-title">
{ item.title }
<Truncate numberOfLines={ 3 }>
{ item.title }
</Truncate>
</span>
</InserterListboxItem>
</div>
Expand Down
20 changes: 6 additions & 14 deletions packages/block-editor/src/components/inserter/preview-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
isReusableBlock,
createBlock,
getBlockFromExample,
getBlockType,
} from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';

Expand All @@ -16,31 +15,24 @@ import BlockCard from '../block-card';
import BlockPreview from '../block-preview';

function InserterPreviewPanel( { item } ) {
const { name, title, icon, description, initialAttributes } = item;
const hoveredItemBlockType = getBlockType( name );
const { name, title, icon, description, initialAttributes, example } = item;
const isReusable = isReusableBlock( item );
return (
<div className="block-editor-inserter__preview-container">
<div className="block-editor-inserter__preview">
{ isReusable || hoveredItemBlockType?.example ? (
Copy link
Member

Choose a reason for hiding this comment

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

I think we need this in case example is undefined, which is the case for block style previews.
Issue: #43836
PR #43883

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for spotting that.

I thought this component was only part of the Inserter, so didn't think to test that. Perhaps it should be renamed.

{ isReusable || example ? (
<div className="block-editor-inserter__preview-content">
<BlockPreview
__experimentalPadding={ 16 }
viewportWidth={
hoveredItemBlockType.example?.viewportWidth ??
500
}
viewportWidth={ example?.viewportWidth ?? 500 }
blocks={
hoveredItemBlockType.example
example
? getBlockFromExample( item.name, {
attributes: {
...hoveredItemBlockType.example
.attributes,
...example.attributes,
...initialAttributes,
},
innerBlocks:
hoveredItemBlockType.example
.innerBlocks,
innerBlocks: example.innerBlocks,
} )
: createBlock( name, initialAttributes )
}
Expand Down
58 changes: 56 additions & 2 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ function render_block_core_template_part( $attributes ) {
}

/**
* Returns an array of variation objects for the template part block.
* Returns an array of area variation objects for the template part block.
*
* @return array Array containing the block variation objects.
*/
function build_template_part_block_variations() {
function build_template_part_block_area_variations() {
$variations = array();
$defined_areas = get_allowed_block_template_part_areas();
foreach ( $defined_areas as $area ) {
Expand All @@ -183,6 +183,60 @@ function build_template_part_block_variations() {
return $variations;
}

/**
* Returns an array of instance variation objects for the template part block
*
* @return array Array containing the block variation objects.
*/
function build_template_part_block_instance_variations() {
$variations = array();
$template_parts = get_block_templates(
array(
'post_type' => 'wp_template_part',
),
'wp_template_part'
);

$defined_areas = get_allowed_block_template_part_areas();
$icon_by_area = array_combine( array_column( $defined_areas, 'area' ), array_column( $defined_areas, 'icon' ) );

foreach ( $template_parts as $template_part ) {
$variations[] = array(
'name' => sanitize_title( $template_part->slug ),
'title' => $template_part->title,
// If there's no description for the template part don't show the
// block description. This is a bit hacky, but prevent the fallback
// by using a non-breaking space so that the value of description
// isn't falsey.
'description' => $template_part->description || '&nbsp;',
'attributes' => array(
'slug' => $template_part->slug,
'theme' => $template_part->theme,
'area' => $template_part->area,
),
'scope' => array( 'inserter' ),
'icon' => $icon_by_area[ $template_part->area ],
'example' => array(
'attributes' => array(
'slug' => $template_part->slug,
'theme' => $template_part->theme,
'area' => $template_part->area,
),
),
);
}
return $variations;
}

/**
* Returns an array of all template part block variations.
*
* @return array Array containing the block variation objects.
*/
function build_template_part_block_variations() {
return array_merge( build_template_part_block_area_variations(), build_template_part_block_instance_variations() );
}

/**
* Registers the `core/template-part` block on the server.
*/
Expand Down