Skip to content

Commit

Permalink
Update lib/global-styles.php
Browse files Browse the repository at this point in the history
Co-authored-by: Andrés <nosolosw@users.noreply.github.com> (+1 squashed commit)
Squashed commits:
[3213429] Update: Make global styles shape consistent with local styles shape.
  • Loading branch information
jorgefilipecosta committed May 29, 2020
1 parent cfbce35 commit 4b505ea
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
35 changes: 34 additions & 1 deletion lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,36 @@ function gutenberg_experimental_global_styles_get_block_data() {
return $block_data;
}

/**
* Given an array contain the styles shape returns the css for this styles.
* A similar function exists on the client at /packages/block-editor/src/hooks/style.js.
*
*
* @param array $styles Array containing the styles shape from global styles.
*
* @return array Containing a set of css rules.
*/
function gutenberg_experimental_global_styles_flatten_styles_tree( $styles ) {
$mappings = array(
'line-height' => array( 'typography', 'lineHeight' ),
'font-size' => array( 'typography', 'fontSize' ),
'background' => array( 'color', 'gradient' ),
'background-color' => array( 'color', 'background' ),
'color' => array( 'color', 'text' ),
);

$result = array();

foreach ( $mappings as $key => $path ) {
$value = gutenberg_experimental_get( $styles, $path );
if ( null !== $value ) {
$result[ $key ] = $value;
}
}
return $result;

}

/**
* Takes a tree adhering to the theme.json schema and generates
* the corresponding stylesheet.
Expand Down Expand Up @@ -352,7 +382,10 @@ function gutenberg_experimental_global_styles_resolver( $tree ) {
$stylesheet .= gutenberg_experimental_global_styles_resolver_styles(
$block_data[ $block_name ]['selector'],
$block_data[ $block_name ]['supports'],
array_merge( $tree[ $block_name ]['styles'], $css_variables )
array_merge(
gutenberg_experimental_global_styles_flatten_styles_tree( $tree[ $block_name ]['styles'] ),
$css_variables
)
);
}
return $stylesheet;
Expand Down
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function gutenberg_is_experiment_enabled( $name ) {
}

require dirname( __FILE__ ) . '/compat.php';
require dirname( __FILE__ ) . '/utils.php';

require dirname( __FILE__ ) . '/blocks.php';
require dirname( __FILE__ ) . '/templates.php';
Expand Down
21 changes: 21 additions & 0 deletions lib/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* This function allows to easily access a part from a php array.
* It is equivalent to want lodash get provides for JavaScript and is useful to have something similar
* in php so functions that do the same thing on the client and sever can have identical code.
*
* @param array $array An array from where we want to retrieve some information from.
* @param array $path An array containing the path we want to retrieve.
*
* @return array Containing a set of css rules.
*/
function gutenberg_experimental_get( $array, $path ) {
$path_length = count( $path );
for ( $i = 0; $i < $path_length; ++$i ) {
if ( empty( $array[ $path[ $i ] ] ) ) {
return null;
}
$array = $array[ $path[ $i ] ];
}
return $array;
}

0 comments on commit 4b505ea

Please sign in to comment.