Skip to content

Commit

Permalink
Refactor core blocks to have deprecated extracted to their ownf files…
Browse files Browse the repository at this point in the history
… (p.1) (#14979)
  • Loading branch information
gziolo authored Apr 18, 2019
1 parent ea6cdb4 commit 0ccb047
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 274 deletions.
112 changes: 112 additions & 0 deletions packages/block-library/src/button/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* External dependencies
*/
import { omit } from 'lodash';

/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';

const colorsMigration = ( attributes ) => {
return omit( {
...attributes,
customTextColor: attributes.textColor && '#' === attributes.textColor[ 0 ] ? attributes.textColor : undefined,
customBackgroundColor: attributes.color && '#' === attributes.color[ 0 ] ? attributes.color : undefined,
}, [ 'color', 'textColor' ] );
};

const blockAttributes = {
url: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'href',
},
title: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'title',
},
text: {
type: 'string',
source: 'html',
selector: 'a',
},
};

const deprecated = [
{
attributes: {
...blockAttributes,
color: {
type: 'string',
},
textColor: {
type: 'string',
},
align: {
type: 'string',
default: 'none',
},
},
save( { attributes } ) {
const { url, text, title, align, color, textColor } = attributes;

const buttonStyle = {
backgroundColor: color,
color: textColor,
};

const linkClass = 'wp-block-button__link';

return (
<div className={ `align${ align }` }>
<RichText.Content
tagName="a"
className={ linkClass }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
/>
</div>
);
},
migrate: colorsMigration,
},
{
attributes: {
...blockAttributes,
color: {
type: 'string',
},
textColor: {
type: 'string',
},
align: {
type: 'string',
default: 'none',
},
},
save( { attributes } ) {
const { url, text, title, align, color, textColor } = attributes;

return (
<div className={ `align${ align }` } style={ { backgroundColor: color } }>
<RichText.Content
tagName="a"
href={ url }
title={ title }
style={ { color: textColor } }
value={ text }
/>
</div>
);
},
migrate: colorsMigration,
},
];

export default deprecated;
91 changes: 3 additions & 88 deletions packages/block-library/src/button/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
/**
* External dependencies
*/
import { omit, pick } from 'lodash';

/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { RichText } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import deprecated from './deprecated';
import edit from './edit';
import icon from './icon';
import metadata from './block.json';
import save from './save';

const { name, attributes: blockAttributes } = metadata;
const { name } = metadata;

export { metadata, name };

const colorsMigration = ( attributes ) => {
return omit( {
...attributes,
customTextColor: attributes.textColor && '#' === attributes.textColor[ 0 ] ? attributes.textColor : undefined,
customBackgroundColor: attributes.color && '#' === attributes.color[ 0 ] ? attributes.color : undefined,
}, [ 'color', 'textColor' ] );
};

export const settings = {
title: __( 'Button' ),
description: __( 'Prompt visitors to take action with a button-style link.' ),
Expand All @@ -45,77 +32,5 @@ export const settings = {
],
edit,
save,
deprecated: [ {
attributes: {
...pick( blockAttributes, [ 'url', 'title', 'text' ] ),
color: {
type: 'string',
},
textColor: {
type: 'string',
},
align: {
type: 'string',
default: 'none',
},
},

save( { attributes } ) {
const { url, text, title, align, color, textColor } = attributes;

const buttonStyle = {
backgroundColor: color,
color: textColor,
};

const linkClass = 'wp-block-button__link';

return (
<div className={ `align${ align }` }>
<RichText.Content
tagName="a"
className={ linkClass }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
/>
</div>
);
},
migrate: colorsMigration,
},
{
attributes: {
...pick( blockAttributes, [ 'url', 'title', 'text' ] ),
color: {
type: 'string',
},
textColor: {
type: 'string',
},
align: {
type: 'string',
default: 'none',
},
},

save( { attributes } ) {
const { url, text, title, align, color, textColor } = attributes;

return (
<div className={ `align${ align }` } style={ { backgroundColor: color } }>
<RichText.Content
tagName="a"
href={ url }
title={ title }
style={ { color: textColor } }
value={ text }
/>
</div>
);
},
migrate: colorsMigration,
},
],
deprecated,
};
Loading

0 comments on commit 0ccb047

Please sign in to comment.