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

Heading Block: Show default block name in list view when content is empty #59827

Merged
merged 4 commits into from
Mar 17, 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
7 changes: 4 additions & 3 deletions packages/block-library/src/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ export const settings = {
const { content, level } = attributes;

const customName = attributes?.metadata?.name;
const hasContent = content?.length > 0;
Copy link
Member

Choose a reason for hiding this comment

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

This will work for both undefined and RichTextData.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, that's handy! TIL the RichTextData class has a length getter 👍


// In the list view, use the block's content as the label.
// If the content is empty, fall back to the default label.
if ( context === 'list-view' && ( customName || content ) ) {
return attributes?.metadata?.name || content;
if ( context === 'list-view' && ( customName || hasContent ) ) {
return customName || content;
}

if ( context === 'accessibility' ) {
return ! content || content.length === 0
return ! hasContent
? sprintf(
/* translators: accessibility text. %s: heading level. */
__( 'Level %s. Empty.' ),
Expand Down
50 changes: 50 additions & 0 deletions test/e2e/specs/editor/blocks/heading.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,56 @@ test.describe( 'Heading', () => {
] );
} );

test( 'Should have proper label in the list view', async ( {
editor,
page,
} ) => {
await editor.insertBlock( { name: 'core/heading' } );

await editor.publishPost();
await page.reload();

await page
.getByRole( 'toolbar', { name: 'Document tools' } )
.getByRole( 'button', { name: 'Document Overview' } )
.click();

const listView = page.getByRole( 'treegrid', {
name: 'Block navigation structure',
} );

await expect(
listView.getByRole( 'link' ),
'should show default block name if the content is empty'
).toHaveText( 'Heading' );

await editor.canvas
.getByRole( 'document', {
name: 'Block: Heading',
} )
.fill( 'Heading content' );

await expect(
listView.getByRole( 'link' ),
'should show content'
).toHaveText( 'Heading content' );

await editor.openDocumentSettingsSidebar();

await page.getByRole( 'button', { name: 'Advanced' } ).click();

await page
.getByRole( 'textbox', {
name: 'Block name',
} )
.fill( 'My new name' );

await expect(
listView.getByRole( 'link' ),
'should show custom name'
).toHaveText( 'My new name' );
} );

test.describe( 'Block transforms', () => {
test.describe( 'FROM paragraph', () => {
test( 'should preserve the content', async ( { editor } ) => {
Expand Down
Loading