diff --git a/lib/global-styles.php b/lib/global-styles.php index 1fd76c196e202..f02560893a61c 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -163,6 +163,22 @@ function gutenberg_experimental_global_styles_get_theme() { ); } +/** + * Given a block json, it returns an array of block selectors. + * + * @param array $block_json + * @return array + */ +function gutenberg_experimental_global_styles_extract_selectors( $block_json ) { + $selector = $block_json['selector']; + $block_name = $block_json['name']; + $block_selectors = array(); + foreach( $selector as $key => $value ) { + $block_selectors[ $block_name . '/' . $key ] = $value; + } + return $block_selectors; +} + /** * Takes a Global Styles tree and returns a CSS rule * that contains the corresponding CSS custom properties. @@ -181,6 +197,7 @@ function gutenberg_experimental_global_styles_resolver( $global_styles ) { // having to detect support by reading all core block's block.json. $blocks_supported = [ 'paragraph', + 'heading' ]; // The block library dir may not exist if working from a fresh clone => bail out early. $block_library_dir = dirname( __FILE__ ) . '/../build/block-library/blocks/'; @@ -189,8 +206,10 @@ function gutenberg_experimental_global_styles_resolver( $global_styles ) { $block_json_file = $block_library_dir . $block_dir . '/block.json'; if ( file_exists( $block_json_file ) ) { $block_json = json_decode( file_get_contents( $block_json_file ), true ); - if ( $block_json['selector'] ) { + if ( array_key_exists( 'selector', $block_json ) && is_string( $block_json['selector'] ) ) { $selectors[ $block_json['name'] ] = $block_json['selector']; + } else if ( array_key_exists( 'selector', $block_json) && is_array( $block_json['selector'] ) ) { + $selectors = array_merge( $selectors, gutenberg_experimental_global_styles_extract_selectors( $block_json ) ); } } } diff --git a/packages/block-library/src/heading/block.json b/packages/block-library/src/heading/block.json index 120bece3d39ae..f56aa7c7b8d49 100644 --- a/packages/block-library/src/heading/block.json +++ b/packages/block-library/src/heading/block.json @@ -1,6 +1,14 @@ { "name": "core/heading", "category": "common", + "selector": { + "h1": "h1", + "h2": "h2", + "h3": "h3", + "h4": "h4", + "h5": "h5", + "h6": "h6" + }, "attributes": { "align": { "type": "string"