From 41631a05d5b3cda7dd5658b5fac7fe80a817c8b7 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Sat, 3 Feb 2024 15:16:14 +0400 Subject: [PATCH] Pattern: Use the '__experimentalLabel' method to get a title --- .../src/components/block-title/test/index.js | 2 +- .../block-title/use-block-display-title.js | 17 ++--------------- packages/block-library/src/block/index.js | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/block-editor/src/components/block-title/test/index.js b/packages/block-editor/src/components/block-title/test/index.js index 8507d5c4c94ca..8a4d3c2f52fd7 100644 --- a/packages/block-editor/src/components/block-title/test/index.js +++ b/packages/block-editor/src/components/block-title/test/index.js @@ -27,6 +27,7 @@ const blockLabelMap = { 'Block With Long Label': 'This is a longer label than typical for blocks to have.', 'Block With Custom Label': 'A Custom Label like a Block Variation Label', + 'Reusable Block': 'Reuse me!', }; jest.mock( '@wordpress/blocks', () => { @@ -102,7 +103,6 @@ describe( 'BlockTitle', () => { getBlockName: () => 'reusable-block', getBlockType: ( name ) => blockTypeMap[ name ], getBlockAttributes: () => ( { ref: 1 } ), - __experimentalGetReusableBlockTitle: () => 'Reuse me!', } ) ) ); diff --git a/packages/block-editor/src/components/block-title/use-block-display-title.js b/packages/block-editor/src/components/block-title/use-block-display-title.js index 1e4578630b472..a51b336554a2a 100644 --- a/packages/block-editor/src/components/block-title/use-block-display-title.js +++ b/packages/block-editor/src/components/block-title/use-block-display-title.js @@ -3,7 +3,6 @@ */ import { useSelect } from '@wordpress/data'; import { - isReusableBlock, __experimentalGetBlockLabel as getBlockLabel, store as blocksStore, } from '@wordpress/blocks'; @@ -40,11 +39,8 @@ export default function useBlockDisplayTitle( { return null; } - const { - getBlockName, - getBlockAttributes, - __experimentalGetReusableBlockTitle, - } = select( blockEditorStore ); + const { getBlockName, getBlockAttributes } = + select( blockEditorStore ); const { getBlockType, getActiveBlockVariation } = select( blocksStore ); @@ -55,15 +51,6 @@ export default function useBlockDisplayTitle( { } const attributes = getBlockAttributes( clientId ); - const isReusable = isReusableBlock( blockType ); - const reusableBlockTitle = isReusable - ? __experimentalGetReusableBlockTitle( attributes.ref ) - : null; - - if ( reusableBlockTitle ) { - return reusableBlockTitle; - } - const label = getBlockLabel( blockType, attributes, context ); // If the label is defined we prioritize it over a possible block variation title match. if ( label !== blockType.title ) { diff --git a/packages/block-library/src/block/index.js b/packages/block-library/src/block/index.js index 95e090f0afd6a..4039ad8a4ccfa 100644 --- a/packages/block-library/src/block/index.js +++ b/packages/block-library/src/block/index.js @@ -2,6 +2,9 @@ * WordPress dependencies */ import { symbol as icon } from '@wordpress/icons'; +import { store as coreStore } from '@wordpress/core-data'; +import { select } from '@wordpress/data'; +import { decodeEntities } from '@wordpress/html-entities'; /** * Internal dependencies @@ -17,6 +20,22 @@ export { metadata, name }; export const settings = { edit, icon, + __experimentalLabel: ( { ref } ) => { + if ( ! ref ) { + return; + } + + const entity = select( coreStore ).getEditedEntityRecord( + 'postType', + 'wp_block', + ref + ); + if ( ! entity?.title ) { + return; + } + + return decodeEntities( entity.title ); + }, }; export const init = () => initBlock( { name, metadata, settings } );