Skip to content

Commit

Permalink
Improvements/revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Dec 13, 2017
1 parent b64945f commit d4b313e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
2 changes: 1 addition & 1 deletion blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { applyFilters } from '@wordpress/hooks';
import { getBlockType, getUnknownTypeHandlerName } from './registration';

/**
* Returns the block's name with common prefixes: 'core/' or 'core-' (in 'core-embed/') dropped
* Returns the block's name with common prefixes ('core/', 'core-') dropped
*
* @param {String} blockName The block name
* @return {string} Friendly name
Expand Down
55 changes: 21 additions & 34 deletions blocks/hooks/custom-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,51 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Component, getWrapperDisplayName } from '@wordpress/element';
import { Component } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';

/**
* Internal dependencies
*/
import { getBlockRandomAnchor, hasBlockSupport, getBlockType } from '../api';
import { getBlockRandomAnchor, hasBlockSupport } from '../api';

const CUSTOM_STYLE_ATTRIBUTES = [ 'backgroundColor', 'textColor' ];

const hasCustomStyles = ( attributes ) => (
const hasCustomStyleAttributes = ( attributes ) => (
CUSTOM_STYLE_ATTRIBUTES.some( attr => attributes[ attr ] )
);

class AddAnchorWhenNeeded extends Component {
componentWillReceiveProps( nextProps ) {
if ( ! nextProps.attributes.anchor && hasCustomStyles( nextProps.attributes ) ) {
nextProps.setAttributes( {
anchor: getBlockRandomAnchor( nextProps.name ),
} );
}
}

render() {
return null;
}
}

export function addAnchorWhenStylesNeed( BlockEdit ) {
const WrappedBlockEdit = ( props ) => {
let hasAnchor = false;
if ( hasBlockSupport( props.name, 'anchor' ) ) {
const blockType = getBlockType( props.name );
if ( blockType && hasCustomStyles( blockType.attributes ) ) {
hasAnchor = true;
function withCustomStylesAnchor( BlockEdit ) {
return class CustomStylesAnchor extends Component {
componentWillReceiveProps( nextProps ) {
if ( hasBlockSupport( nextProps.name, 'anchor' ) &&
hasBlockSupport( nextProps.name, 'customStyles' ) &&
! nextProps.attributes.anchor &&
hasCustomStyleAttributes( nextProps.attributes )
) {
nextProps.setAttributes( {
anchor: getBlockRandomAnchor( nextProps.name ),
} );
}
}

return [
<BlockEdit key="block-edit-add-anchor" { ...props } />,
hasAnchor && <AddAnchorWhenNeeded key="add-anchor" { ...props } />,
];
render() {
return (
<BlockEdit { ...this.props } />
);
}
};

WrappedBlockEdit.displayName = getWrapperDisplayName( BlockEdit, 'add-anchor' );

return WrappedBlockEdit;
}

export function addCustomStylesClass( extraProps, blockType, attributes ) {
if ( hasCustomStyles( attributes ) ) {
if ( hasBlockSupport( blockType.name, 'customStyles' ) && hasCustomStyleAttributes( attributes ) ) {
extraProps.className = classnames( extraProps.className, 'has-custom-styles' );
}

return extraProps;
}

export default function customAnchorStyles() {
addFilter( 'blocks.BlockEdit', 'core/custom-styles/inspector-control', addAnchorWhenStylesNeed );
addFilter( 'blocks.BlockEdit', 'core/custom-styles/inspector-control', withCustomStylesAnchor );
addFilter( 'blocks.getSaveContent.extraProps', 'core/custom-styles/save-props', addCustomStylesClass );
}
1 change: 1 addition & 0 deletions blocks/library/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ registerBlockType( 'core/paragraph', {
supports: {
className: false,
anchor: true,
customStyles: true,
},

attributes: {
Expand Down
4 changes: 2 additions & 2 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function gutenberg_render_block( $block ) {
*
* @return string CSS style block or empty string
*/
function compute_styles( $blocks ) {
function do_blocks_styles( $blocks ) {
$map_attrs_styles = array(
'textColor' => 'color',
'backgroundColor' => 'background-color',
Expand Down Expand Up @@ -215,7 +215,7 @@ function compute_styles( $blocks ) {
function do_blocks( $content ) {
$blocks = gutenberg_parse_blocks( $content );

$content_after_blocks = compute_styles( $blocks );
$content_after_blocks = do_blocks_styles( $blocks );
foreach ( $blocks as $block ) {
$content_after_blocks .= gutenberg_render_block( $block );
}
Expand Down

0 comments on commit d4b313e

Please sign in to comment.