Skip to content

Commit

Permalink
Style Book: Fix critical error when heading block is not registered (#…
Browse files Browse the repository at this point in the history
…64047)

Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
  • Loading branch information
3 people committed Jul 29, 2024
1 parent 4b873d6 commit d4bf959
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions packages/edit-site/src/components/style-book/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import {
getCategories,
getBlockType,
getBlockTypes,
getBlockFromExample,
createBlock,
Expand Down Expand Up @@ -128,41 +129,7 @@ function isObjectEmpty( object ) {
}

function getExamples() {
// Use our own example for the Heading block so that we can show multiple
// heading levels.
const headingsExample = {
name: 'core/heading',
title: __( 'Headings' ),
category: 'text',
blocks: [
createBlock( 'core/heading', {
content: __( 'Heading 1' ),
level: 1,
} ),
createBlock( 'core/heading', {
content: __( 'Heading 2' ),
level: 2,
} ),
createBlock( 'core/heading', {
content: __( 'Heading 3' ),
level: 3,
} ),
createBlock( 'core/heading', {
content: __( 'Heading 4' ),
level: 4,
} ),
createBlock( 'core/heading', {
content: __( 'Heading 5' ),
level: 5,
} ),
createBlock( 'core/heading', {
content: __( 'Heading 6' ),
level: 6,
} ),
],
};

const otherExamples = getBlockTypes()
const nonHeadingBlockExamples = getBlockTypes()
.filter( ( blockType ) => {
const { name, example, supports } = blockType;
return (
Expand All @@ -178,7 +145,31 @@ function getExamples() {
blocks: getBlockFromExample( blockType.name, blockType.example ),
} ) );

return [ headingsExample, ...otherExamples ];
const isHeadingBlockRegistered = !! getBlockType( 'core/heading' );

if ( ! isHeadingBlockRegistered ) {
return nonHeadingBlockExamples;
}

// Use our own example for the Heading block so that we can show multiple
// heading levels.
const headingsExample = {
name: 'core/heading',
title: __( 'Headings' ),
category: 'text',
blocks: [ 1, 2, 3, 4, 5, 6 ].map( ( level ) => {
return createBlock( 'core/heading', {
content: sprintf(
// translators: %d: heading level e.g: "1", "2", "3"
__( 'Heading %d' ),
level
),
level,
} );
} ),
};

return [ headingsExample, ...nonHeadingBlockExamples ];
}

function StyleBook( {
Expand Down

0 comments on commit d4bf959

Please sign in to comment.