Skip to content

Commit

Permalink
Heading Block: Show default block name in list view when content is e…
Browse files Browse the repository at this point in the history
…mpty (#59827)


Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: getdave <get_dave@git.wordpress.org>
  • Loading branch information
5 people authored Mar 17, 2024
1 parent 9dc6483 commit ec86a82
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
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;

// 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

0 comments on commit ec86a82

Please sign in to comment.