Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor core blocks to have deprecated extracted to their own files (p.1) #14979

Merged
merged 1 commit into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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