Skip to content

Commit

Permalink
Add support for heading block
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Apr 7, 2020
1 parent 59277f1 commit 86e956b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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/';
Expand All @@ -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 ) );
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 86e956b

Please sign in to comment.