Skip to content

Commit

Permalink
Updated deprecation to keep the colors
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Nov 4, 2019
1 parent 6775d92 commit 7f6eccf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
53 changes: 49 additions & 4 deletions packages/block-library/src/pullquote/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,28 @@ const blockAttributes = {
},
};

function parseBorderColor( styleString ) {
if ( ! styleString ) {
return;
}
const matches = styleString.match( /border-color:([^;]+)[;]?/ );
if ( matches && matches[ 1 ] ) {
return matches[ 1 ];
}
}

const deprecated = [
{
attributes: blockAttributes,
attributes: {
...blockAttributes,
// figureStyle is an attribute that never existed.
// We are using it as a way to access the styles previously applied to the figure.
figureStyle: {
source: 'attribute',
selector: 'figure',
attribute: 'style',
},
},
save( { attributes } ) {
const {
mainColor,
Expand All @@ -60,6 +79,7 @@ const deprecated = [
value,
citation,
className,
figureStyle,
} = attributes;

const isSolidColorStyle = includes( className, SOLID_COLOR_CLASS );
Expand All @@ -86,10 +106,14 @@ const deprecated = [
// 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 );
// Previously here we queried the color settings to know the color value
// of a named color. This made the save function impure and the block was refactored,
// because meanwhile a change in the editor made it impossible to query color settings in the save function.
// Here instead of querying the color settings to know the color value, we retrieve the value
// directly from the style previously serialized.
const borderColor = parseBorderColor( figureStyle );
figureStyles = {
borderColor: colorObject.color,
borderColor,
};
}

Expand All @@ -109,6 +133,27 @@ const deprecated = [
</figure>
);
},
migrate( { className, figureStyle, mainColor, ...attributes } ) {
const isSolidColorStyle = includes( className, SOLID_COLOR_CLASS );
// If is the default style, and a main color is set,
// migrate the main color value into a custom color.
// The custom color value is retrived by parsing the figure styles.
if ( ! isSolidColorStyle && mainColor && figureStyle ) {
const borderColor = parseBorderColor( figureStyle );
if ( borderColor ) {
return {
...attributes,
className,
customMainColor: borderColor,
};
}
}
return {
className,
mainColor,
...attributes,
};
},
},
{
attributes: blockAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{
"clientId": "_clientId_0",
"name": "core/pullquote",
"isValid": false,
"isValid": true,
"attributes": {
"value": "<p>pullquote</p>",
"citation": "",
"mainColor": "accent",
"customMainColor": "#cd2653",
"textColor": "secondary"
},
"innerBlocks": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:pullquote {"mainColor":"accent","textColor":"secondary"} -->
<!-- wp:pullquote {"customMainColor":"#cd2653","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 -->

0 comments on commit 7f6eccf

Please sign in to comment.