Skip to content

Commit

Permalink
Add font style hook
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Sep 29, 2020
1 parent b9ff7ea commit 2774b2b
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 4 deletions.
28 changes: 27 additions & 1 deletion lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ function gutenberg_register_typography_support( $block_type ) {
$has_font_size_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalFontSize' ), false );
}

$has_font_style_support = false;
if ( property_exists( $block_type, 'supports' ) ) {
$has_font_style_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalFontStyle' ), false );
}

$has_line_height_support = false;
if ( property_exists( $block_type, 'supports' ) ) {
$has_line_height_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalLineHeight' ), false );
Expand All @@ -25,7 +30,7 @@ function gutenberg_register_typography_support( $block_type ) {
$block_type->attributes = array();
}

if ( ( $has_font_size_support || $has_line_height_support ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
if ( ( $has_font_size_support || $has_font_style_support || $has_line_height_support ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
$block_type->attributes['style'] = array(
'type' => 'object',
);
Expand All @@ -36,6 +41,12 @@ function gutenberg_register_typography_support( $block_type ) {
'type' => 'string',
);
}

if ( $has_font_style_support && ! array_key_exists( 'fontStyle', $block_type->attributes ) ) {
$block_type->attributes['fontStyle'] = array(
'type' => 'string',
);
}
}

/**
Expand All @@ -54,6 +65,11 @@ function gutenberg_apply_typography_support( $attributes, $block_attributes, $bl
$has_font_size_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalFontSize' ), false );
}

$has_font_style_support = false;
if ( property_exists( $block_type, 'supports' ) ) {
$has_font_style_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalFontStyle' ), false );
}

$has_line_height_support = false;
if ( property_exists( $block_type, 'supports' ) ) {
$has_line_height_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalLineHeight' ), false );
Expand All @@ -72,6 +88,16 @@ function gutenberg_apply_typography_support( $attributes, $block_attributes, $bl
}
}

// Font Style.
if ( $has_font_style_support ) {
$has_font_style = array_key_exists( 'fontStyle', $block_attributes );

// Apply required class or style.
if ( $has_font_style ) {
$attributes['css_classes'][] = sprintf( 'has-%s-font-style', $block_attributes['fontStyle'] );
}
}

// Line Height.
if ( $has_line_height_support ) {
$has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
Expand Down
12 changes: 12 additions & 0 deletions lib/experimental-default-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@
"slug": "huge",
"size": 42
}
],
"fontStyles": [
{
"name": "Normal",
"slug": "normal",
"style": "normal"
},
{
"name": "Italics",
"slug": "italics",
"style": "italic"
}
]
},
"spacing": {
Expand Down
14 changes: 11 additions & 3 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ function gutenberg_experimental_global_styles_get_css_property( $style_property
return 'background-color';
case 'fontSize':
return 'font-size';
case 'fontStyle':
return 'font-style';
case 'lineHeight':
return 'line-height';
default:
Expand All @@ -406,6 +408,7 @@ function gutenberg_experimental_global_styles_get_style_property() {
'backgroundColor' => array( 'color', 'background' ),
'color' => array( 'color', 'text' ),
'fontSize' => array( 'typography', 'fontSize' ),
'fontStyle' => array( 'typography', 'fontStyle' ),
'lineHeight' => array( 'typography', 'lineHeight' ),
);
}
Expand All @@ -422,6 +425,7 @@ function gutenberg_experimental_global_styles_get_support_keys() {
'backgroundColor' => array( '__experimentalColor' ),
'color' => array( '__experimentalColor' ),
'fontSize' => array( '__experimentalFontSize' ),
'fontStyle' => array( '__experimentalFontStyle' ),
'lineHeight' => array( '__experimentalLineHeight' ),
);
}
Expand All @@ -433,18 +437,22 @@ function gutenberg_experimental_global_styles_get_support_keys() {
*/
function gutenberg_experimental_global_styles_get_presets_structure() {
return array(
'color' => array(
'color' => array(
'path' => array( 'color', 'palette' ),
'key' => 'color',
),
'gradient' => array(
'gradient' => array(
'path' => array( 'color', 'gradients' ),
'key' => 'gradient',
),
'fontSize' => array(
'fontSize' => array(
'path' => array( 'typography', 'fontSizes' ),
'key' => 'size',
),
'fontStyle' => array(
'path' => array( 'typography', 'fontStyles' ),
'key' => 'style',
),
);
}

Expand Down
166 changes: 166 additions & 0 deletions packages/block-editor/src/hooks/font-style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/**
* WordPress dependencies
*/
import TokenList from '@wordpress/token-list';
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport } from '@wordpress/blocks';
import { SelectControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import useEditorFeature from '../components/use-editor-feature';
import { cleanEmptyObject } from './utils';

export const FONT_STYLE_SUPPORT_KEY = '__experimentalFontStyle';

/**
* Filters registered block settings, extending attributes to include
* `fontStyle`.
*
* @param {Object} settings Original block settings
* @return {Object} Filtered block settings
*/
function addAttributes( settings ) {
if ( ! hasBlockSupport( settings, FONT_STYLE_SUPPORT_KEY ) ) {
return settings;
}

// Allow blocks to specify a default value if needed.
if ( ! settings.attributes.fontStyle ) {
Object.assign( settings.attributes, { fontStyle: { type: 'string' } } );
}

return settings;
}

/**
* Override props assigned to save component to inject font style.
*
* @param {Object} props Additional props applied to save element
* @param {Object} blockType Block type
* @param {Object} attributes Block attributes
* @return {Object} Filtered props applied to save element
*/
function addSaveProps( props, blockType, attributes ) {
if ( ! hasBlockSupport( blockType, FONT_STYLE_SUPPORT_KEY ) ) {
return props;
}

// Use TokenList to de-dupe classes.
const classes = new TokenList( props.className );
classes.add( `has-${ attributes.fontStyle }-font-style` );
const newClassName = classes.value;
props.className = newClassName ? newClassName : undefined;

return props;
}

/**
* Filters registered block settings to expand the block edit wrapper
* by applying the desired classname.
*
* @param {Object} settings Original block settings
* @return {Object} Filtered block settings
*/
function addEditProps( settings ) {
if ( ! hasBlockSupport( settings, FONT_STYLE_SUPPORT_KEY ) ) {
return settings;
}

const existingGetEditWrapperProps = settings.getEditWrapperProps;
settings.getEditWrapperProps = ( attributes ) => {
let props = {};
if ( existingGetEditWrapperProps ) {
props = existingGetEditWrapperProps( attributes );
}
return addSaveProps( props, settings, attributes );
};

return settings;
}

/**
* Inspector control panel containing the font style option for italics.
*
* @param {Object} props
* @return {WPElement} Font style edit element.
*/
export function FontStyleEdit( props ) {
const {
attributes: { style },
setAttributes,
} = props;

const isDisabled = useIsFontStyleDisabled( props );

if ( isDisabled ) {
return null;
}

const onChange = ( fontStyle ) => {
const newStyle = {
...style,
typography: {
...style?.typography,
fontStyle: fontStyle ? fontStyle : undefined,
},
};

setAttributes( {
style: cleanEmptyObject( newStyle ),
fontStyle,
} );
};

const styleOptions = [
{
label: __( 'Normal' ),
value: '',
},
{
label: __( 'Italics' ),
value: 'italic',
},
];

return (
<SelectControl
options={ styleOptions }
value={ style?.typography?.fontStyle }
label={ __( 'Font Style' ) }
onChange={ onChange }
/>
);
}

/**
* Hook to check if font-style settings have been disabled.
*
* @param {string} name Name of the block.
* @return {boolean} Whether or not the setting is disabled.
*/
export function useIsFontStyleDisabled( { name: blockName } = {} ) {
const fontStyles = useEditorFeature( 'typography.fontStyles' );
const hasFontStyles = fontStyles.length;

return (
! hasBlockSupport( blockName, FONT_STYLE_SUPPORT_KEY ) ||
! hasFontStyles
);
}

addFilter(
'blocks.registerBlockType',
'core/font/addAttribute',
addAttributes
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/font/addSaveProps',
addSaveProps
);

addFilter( 'blocks.registerBlockType', 'core/font/addEditProps', addEditProps );
8 changes: 8 additions & 0 deletions packages/block-editor/src/hooks/typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ import {
FontSizeEdit,
useIsFontSizeDisabled,
} from './font-size';
import {
FONT_STYLE_SUPPORT_KEY,
FontStyleEdit,
useIsFontStyleDisabled,
} from './font-style';

export const TYPOGRAPHY_SUPPORT_KEYS = [
LINE_HEIGHT_SUPPORT_KEY,
FONT_SIZE_SUPPORT_KEY,
FONT_STYLE_SUPPORT_KEY,
];

export function TypographyPanel( props ) {
Expand All @@ -38,6 +44,7 @@ export function TypographyPanel( props ) {
<PanelBody title={ __( 'Typography' ) }>
<FontSizeEdit { ...props } />
<LineHeightEdit { ...props } />
<FontStyleEdit { ...props } />
</PanelBody>
</InspectorControls>
);
Expand All @@ -56,6 +63,7 @@ function useIsTypographyDisabled( props = {} ) {
const configs = [
useIsFontSizeDisabled( props ),
useIsLineHeightDisabled( props ),
useIsFontStyleDisabled( props ),
];

return configs.filter( Boolean ).length === configs.length;
Expand Down
9 changes: 9 additions & 0 deletions packages/block-library/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@
font-size: 42px;
}

// Font Styles.
.editor-styles-wrapper .has-normal-font-style {
font-style: normal;
}

.editor-styles-wrapper .has-italic-font-style {
font-style: italic;
}

/**
* Editor Normalization Styles
*
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@
"textColor": true,
"backgroundColor": true
}
"__experimentalFontStyle": true
}
}
9 changes: 9 additions & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
font-size: 2.625em;
}

// Font Styles.
.has-normal-font-style {
font-style: normal;
}

.has-italic-font-style {
font-style: italic;
}

// Text alignments.
.has-text-align-center {
text-align: center;
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
backgroundColor: [ 'color', 'background' ],
color: [ 'color', 'text' ],
fontSize: [ 'typography', 'fontSize' ],
fontStyle: [ 'typography', 'fontStyle' ],
lineHeight: [ 'typography', 'lineHeight' ],
paddingBottom: [ 'spacing', 'padding', 'bottom' ],
paddingLeft: [ 'spacing', 'padding', 'left' ],
Expand Down

0 comments on commit 2774b2b

Please sign in to comment.