Skip to content

Commit

Permalink
Fix: setting a preset color on pullquote default style makes the bloc…
Browse files Browse the repository at this point in the history
…k invalid.
  • Loading branch information
jorgefilipecosta committed Nov 1, 2019
1 parent 3b19415 commit 6775d92
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 15 deletions.
61 changes: 61 additions & 0 deletions packages/block-library/src/pullquote/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,67 @@ const blockAttributes = {
};

const deprecated = [
{
attributes: blockAttributes,
save( { attributes } ) {
const {
mainColor,
customMainColor,
textColor,
customTextColor,
value,
citation,
className,
} = attributes;

const isSolidColorStyle = includes( className, SOLID_COLOR_CLASS );

let figureClasses, figureStyles;

// Is solid color style
if ( isSolidColorStyle ) {
const backgroundClass = getColorClassName( 'background-color', mainColor );

figureClasses = classnames( {
'has-background': ( backgroundClass || customMainColor ),
[ backgroundClass ]: backgroundClass,
} );

figureStyles = {
backgroundColor: backgroundClass ? undefined : customMainColor,
};
// Is normal style and a custom color is being used ( we can set a style directly with its value)
} else if ( customMainColor ) {
figureStyles = {
borderColor: customMainColor,
};
// If normal style and a named color are being used, we need to retrieve the color value to set the style,
// as there is no expectation that themes create classes that set border colors.
} else if ( mainColor ) {
const colors = get( select( 'core/block-editor' ).getSettings(), [ 'colors' ], [] );
const colorObject = getColorObjectByAttributeValues( colors, mainColor );
figureStyles = {
borderColor: colorObject.color,
};
}

const blockquoteTextColorClass = getColorClassName( 'color', textColor );
const blockquoteClasses = ( textColor || customTextColor ) && classnames( 'has-text-color', {
[ blockquoteTextColorClass ]: blockquoteTextColorClass,
} );

const blockquoteStyles = blockquoteTextColorClass ? undefined : { color: customTextColor };

return (
<figure className={ figureClasses } style={ figureStyles }>
<blockquote className={ blockquoteClasses } style={ blockquoteStyles } >
<RichText.Content value={ value } multiline />
{ ! RichText.isEmpty( citation ) && <RichText.Content tagName="cite" value={ citation } /> }
</blockquote>
</figure>
);
},
},
{
attributes: blockAttributes,
save( { attributes } ) {
Expand Down
29 changes: 27 additions & 2 deletions packages/block-library/src/pullquote/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ class PullQuoteEdit extends Component {
}

pullQuoteMainColorSetter( colorValue ) {
const { colorUtils, textColor, setTextColor, setMainColor, className } = this.props;
const { colorUtils, textColor, setAttributes, setTextColor, setMainColor, className } = this.props;
const isSolidColorStyle = includes( className, SOLID_COLOR_CLASS );
const needTextColor = ! textColor.color || this.wasTextColorAutomaticallyComputed;
const shouldSetTextColor = isSolidColorStyle && needTextColor && colorValue;

setMainColor( colorValue );
if ( isSolidColorStyle ) {
// If we use the solid color style, set the color using the normal mechanism.
setMainColor( colorValue );
} else {
// If we use the default style, set the color as a custom color to force the usage of an inline style.
// Default style uses a border color for which classes are not available.
setAttributes( { customMainColor: colorValue } );
}

if ( shouldSetTextColor ) {
this.wasTextColorAutomaticallyComputed = true;
setTextColor( colorUtils.getMostReadableColor( colorValue ) );
Expand All @@ -50,6 +58,23 @@ class PullQuoteEdit extends Component {
this.wasTextColorAutomaticallyComputed = false;
}

componentDidUpdate( prevProps ) {
const {
attributes,
className,
mainColor,
setAttributes,
} = this.props;
// If the block includes a named color and we switched from the
// solid color style to the default style.
if ( attributes.mainColor && ! includes( className, SOLID_COLOR_CLASS ) && includes( prevProps.className, SOLID_COLOR_CLASS ) ) {
// Remove the named color, and set the color as a custom color.
// This is done because named colors use classes, in the default style we use a border color,
// and themes don't set classes for border colors.
setAttributes( { mainColor: undefined, customMainColor: mainColor.color } );
}
}

render() {
const {
attributes,
Expand Down
14 changes: 1 addition & 13 deletions packages/block-library/src/pullquote/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
* External dependencies
*/
import classnames from 'classnames';
import { get, includes } from 'lodash';
import { includes } from 'lodash';

/**
* WordPress dependencies
*/
import {
getColorClassName,
RichText,
getColorObjectByAttributeValues,
} from '@wordpress/block-editor';
import {
select,
} from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -53,14 +49,6 @@ export default function save( { attributes } ) {
figureStyles = {
borderColor: customMainColor,
};
// If normal style and a named color are being used, we need to retrieve the color value to set the style,
// as there is no expectation that themes create classes that set border colors.
} else if ( mainColor ) {
const colors = get( select( 'core/block-editor' ).getSettings(), [ 'colors' ], [] );
const colorObject = getColorObjectByAttributeValues( colors, mainColor );
figureStyles = {
borderColor: colorObject.color,
};
}

const blockquoteTextColorClass = getColorClassName( 'color', textColor );
Expand Down
7 changes: 7 additions & 0 deletions packages/e2e-tests/fixtures/block-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ export const EXPECTED_TRANSFORMS = {
'Group',
],
},
'core__pullquote__main-color': {
originalBlock: 'Pullquote',
availableTransforms: [
'Quote',
'Group',
],
},
'core__pullquote__multi-paragraph': {
originalBlock: 'Pullquote',
availableTransforms: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:pullquote {"mainColor":"accent","textColor":"secondary"} -->
<figure class="wp-block-pullquote" style="border-color:#cd2653"><blockquote class="has-text-color has-secondary-color"><p>pullquote</p></blockquote></figure>
<!-- /wp:pullquote -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"clientId": "_clientId_0",
"name": "core/pullquote",
"isValid": false,
"attributes": {
"value": "<p>pullquote</p>",
"citation": "",
"mainColor": "accent",
"textColor": "secondary"
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-pullquote\" style=\"border-color:#cd2653\"><blockquote class=\"has-text-color has-secondary-color\"><p>pullquote</p></blockquote></figure>"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"blockName": "core/pullquote",
"attrs": {
"mainColor": "accent",
"textColor": "secondary"
},
"innerBlocks": [],
"innerHTML": "\n<figure class=\"wp-block-pullquote\" style=\"border-color:#cd2653\"><blockquote class=\"has-text-color has-secondary-color\"><p>pullquote</p></blockquote></figure>\n",
"innerContent": [
"\n<figure class=\"wp-block-pullquote\" style=\"border-color:#cd2653\"><blockquote class=\"has-text-color has-secondary-color\"><p>pullquote</p></blockquote></figure>\n"
]
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:pullquote {"mainColor":"accent","textColor":"secondary"} -->
<figure class="wp-block-pullquote" style="border-color:#cd2653"><blockquote class="has-text-color has-secondary-color"><p>pullquote</p></blockquote></figure>
<!-- /wp:pullquote -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:pullquote {"customMainColor":"#2207d0","textColor":"subtle-background","className":"has-background is-style-default"} -->
<figure class="wp-block-pullquote has-background is-style-default" style="border-color:#2207d0"><blockquote class="has-text-color has-subtle-background-color"><p>Pullquote custom color</p><cite>my citation</cite></blockquote></figure>
<!-- /wp:pullquote -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"clientId": "_clientId_0",
"name": "core/pullquote",
"isValid": true,
"attributes": {
"value": "<p>Pullquote custom color</p>",
"citation": "my citation",
"customMainColor": "#2207d0",
"textColor": "subtle-background",
"className": "has-background is-style-default"
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-pullquote has-background is-style-default\" style=\"border-color:#2207d0\"><blockquote class=\"has-text-color has-subtle-background-color\"><p>Pullquote custom color</p><cite>my citation</cite></blockquote></figure>"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"blockName": "core/pullquote",
"attrs": {
"customMainColor": "#2207d0",
"textColor": "subtle-background",
"className": "has-background is-style-default"
},
"innerBlocks": [],
"innerHTML": "\n<figure class=\"wp-block-pullquote has-background is-style-default\" style=\"border-color:#2207d0\"><blockquote class=\"has-text-color has-subtle-background-color\"><p>Pullquote custom color</p><cite>my citation</cite></blockquote></figure>\n",
"innerContent": [
"\n<figure class=\"wp-block-pullquote has-background is-style-default\" style=\"border-color:#2207d0\"><blockquote class=\"has-text-color has-subtle-background-color\"><p>Pullquote custom color</p><cite>my citation</cite></blockquote></figure>\n"
]
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:pullquote {"customMainColor":"#2207d0","textColor":"subtle-background","className":"has-background is-style-default"} -->
<figure class="wp-block-pullquote has-background is-style-default" style="border-color:#2207d0"><blockquote class="has-text-color has-subtle-background-color"><p>Pullquote custom color</p><cite>my citation</cite></blockquote></figure>
<!-- /wp:pullquote -->
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ exports[`Block transforms correctly transform block Pullquote in fixture core__p
<!-- /wp:quote -->"
`;
exports[`Block transforms correctly transform block Pullquote in fixture core__pullquote__main-color into the Quote block 1`] = `
"<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p>Pullquote custom color</p><cite>my citation</cite></blockquote>
<!-- /wp:quote -->"
`;
exports[`Block transforms correctly transform block Pullquote in fixture core__pullquote__multi-paragraph into the Quote block 1`] = `
"<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p>Paragraph <strong>one</strong></p><p>Paragraph two</p><cite>by whomever</cite></blockquote>
Expand Down

0 comments on commit 6775d92

Please sign in to comment.