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

Revert "Add block variations for individual template parts" #42740

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
createBlock,
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';
import { __experimentalTruncate as Truncate } from '@wordpress/components';
import { ENTER } from '@wordpress/keycodes';

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

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

function InserterPreviewPanel( { item } ) {
const { name, title, icon, description, initialAttributes, example } = item;
const { name, title, icon, description, initialAttributes } = item;
const hoveredItemBlockType = getBlockType( name );
const isReusable = isReusableBlock( item );
return (
<div className="block-editor-inserter__preview-container">
<div className="block-editor-inserter__preview">
{ isReusable || example ? (
{ isReusable || hoveredItemBlockType?.example ? (
<div className="block-editor-inserter__preview-content">
<BlockPreview
__experimentalPadding={ 16 }
viewportWidth={ example?.viewportWidth ?? 500 }
viewportWidth={
hoveredItemBlockType.example?.viewportWidth ??
500
}
blocks={
example
hoveredItemBlockType.example
? getBlockFromExample( item.name, {
attributes: {
...example.attributes,
...hoveredItemBlockType.example
.attributes,
...initialAttributes,
},
innerBlocks: example.innerBlocks,
innerBlocks:
hoveredItemBlockType.example
.innerBlocks,
} )
: createBlock( name, initialAttributes )
}
Expand Down
58 changes: 2 additions & 56 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 area variation objects for the template part block.
* Returns an array of variation objects for the template part block.
*
* @return array Array containing the block variation objects.
*/
function build_template_part_block_area_variations() {
function build_template_part_block_variations() {
$variations = array();
$defined_areas = get_allowed_block_template_part_areas();
foreach ( $defined_areas as $area ) {
Expand All @@ -183,60 +183,6 @@ function build_template_part_block_area_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