From 5d77b03d5bb26a4b31d9b38d50524be38205bf70 Mon Sep 17 00:00:00 2001 From: Nik Tsekouras Date: Wed, 17 Aug 2022 18:54:02 +0300 Subject: [PATCH] [Block Editor]: Fix block switcher label to take into account block variations (#43309) * [Block Editor]: Fix block switcher label to take into account block variations * fix for multiple selected blocks --- .../src/components/block-switcher/index.js | 18 +++++++----------- .../components/block-switcher/test/index.js | 1 + 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/block-editor/src/components/block-switcher/index.js b/packages/block-editor/src/components/block-switcher/index.js index 692cf9cfb6d42..3e5e175b90c76 100644 --- a/packages/block-editor/src/components/block-switcher/index.js +++ b/packages/block-editor/src/components/block-switcher/index.js @@ -28,10 +28,10 @@ import { copy } from '@wordpress/icons'; import { store as blockEditorStore } from '../../store'; import useBlockDisplayInformation from '../use-block-display-information'; import BlockIcon from '../block-icon'; -import BlockTitle from '../block-title'; import BlockTransformationsMenu from './block-transformations-menu'; import BlockStylesMenu from './block-styles-menu'; import PatternTransformationsMenu from './pattern-transformations-menu'; +import useBlockDisplayTitle from '../block-title/use-block-display-title'; export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => { const { replaceBlocks } = useDispatch( blockEditorStore ); @@ -41,7 +41,6 @@ export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => { canRemove, hasBlockStyles, icon, - blockTitle, patterns, } = useSelect( ( select ) => { @@ -79,7 +78,6 @@ export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => { canRemove: canRemoveBlocks( clientIds, rootClientId ), hasBlockStyles: !! styles?.length, icon: _icon, - blockTitle: getBlockType( firstBlockName )?.title, patterns: __experimentalGetPatternTransformItems( blocks, rootClientId @@ -89,6 +87,10 @@ export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => { [ clientIds, blocks, blockInformation?.icon ] ); + const blockTitle = useBlockDisplayTitle( { + clientId: Array.isArray( clientIds ) ? clientIds[ 0 ] : clientIds, + maximumLength: 35, + } ); const isReusable = blocks.length === 1 && isReusableBlock( blocks[ 0 ] ); const isTemplate = blocks.length === 1 && isTemplatePart( blocks[ 0 ] ); @@ -119,10 +121,7 @@ export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => { { ( isReusable || isTemplate ) && ( - + { blockTitle } ) } @@ -176,10 +175,7 @@ export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => { /> { ( isReusable || isTemplate ) && ( - + { blockTitle } ) } diff --git a/packages/block-editor/src/components/block-switcher/test/index.js b/packages/block-editor/src/components/block-switcher/test/index.js index 43811b2380403..3cdb022dc361f 100644 --- a/packages/block-editor/src/components/block-switcher/test/index.js +++ b/packages/block-editor/src/components/block-switcher/test/index.js @@ -18,6 +18,7 @@ import { copy } from '@wordpress/icons'; import { BlockSwitcher, BlockSwitcherDropdownMenu } from '../'; jest.mock( '@wordpress/data/src/components/use-select', () => jest.fn() ); +jest.mock( '../../block-title/use-block-display-title', () => jest.fn() ); describe( 'BlockSwitcher', () => { test( 'should not render block switcher without blocks', () => {