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

Pattern: Use the '__experimentalLabel' method to get a title #58646

Merged
merged 1 commit into from
Feb 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -102,7 +103,6 @@ describe( 'BlockTitle', () => {
getBlockName: () => 'reusable-block',
getBlockType: ( name ) => blockTypeMap[ name ],
getBlockAttributes: () => ( { ref: 1 } ),
__experimentalGetReusableBlockTitle: () => 'Reuse me!',
} ) )
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { useSelect } from '@wordpress/data';
import {
isReusableBlock,
__experimentalGetBlockLabel as getBlockLabel,
store as blocksStore,
} from '@wordpress/blocks';
Expand Down Expand Up @@ -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 );

Expand All @@ -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 ) {
Expand Down
19 changes: 19 additions & 0 deletions packages/block-library/src/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 } );
Loading