Skip to content

Commit

Permalink
Block library: Extract deprecated field to their own files (p.2) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo authored May 21, 2019
1 parent 35fc599 commit 9e6c041
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 235 deletions.
168 changes: 168 additions & 0 deletions packages/block-library/src/paragraph/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { isFinite, omit } from 'lodash';

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

const supports = {
className: false,
};

const blockAttributes = {
align: {
type: 'string',
},
content: {
type: 'string',
source: 'html',
selector: 'p',
default: '',
},
dropCap: {
type: 'boolean',
default: false,
},
placeholder: {
type: 'string',
},
textColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
backgroundColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
fontSize: {
type: 'string',
},
customFontSize: {
type: 'number',
},
direction: {
type: 'string',
enum: [ 'ltr', 'rtl' ],
},
};

const deprecated = [
{
supports,
attributes: {
...blockAttributes,
width: {
type: 'string',
},
},
save( { attributes } ) {
const {
width,
align,
content,
dropCap,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor,
fontSize,
customFontSize,
} = attributes;

const textClass = getColorClassName( 'color', textColor );
const backgroundClass = getColorClassName( 'background-color', backgroundColor );
const fontSizeClass = fontSize && `is-${ fontSize }-text`;

const className = classnames( {
[ `align${ width }` ]: width,
'has-background': backgroundColor || customBackgroundColor,
'has-drop-cap': dropCap,
[ fontSizeClass ]: fontSizeClass,
[ textClass ]: textClass,
[ backgroundClass ]: backgroundClass,
} );

const styles = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor,
fontSize: fontSizeClass ? undefined : customFontSize,
textAlign: align,
};

return (
<RichText.Content
tagName="p"
style={ styles }
className={ className ? className : undefined }
value={ content }
/>
);
},
},
{
supports,
attributes: omit( {
...blockAttributes,
fontSize: {
type: 'number',
},
}, 'customFontSize', 'customTextColor', 'customBackgroundColor' ),
save( { attributes } ) {
const { width, align, content, dropCap, backgroundColor, textColor, fontSize } = attributes;
const className = classnames( {
[ `align${ width }` ]: width,
'has-background': backgroundColor,
'has-drop-cap': dropCap,
} );
const styles = {
backgroundColor,
color: textColor,
fontSize,
textAlign: align,
};

return <p style={ styles } className={ className ? className : undefined }>{ content }</p>;
},
migrate( attributes ) {
return omit( {
...attributes,
customFontSize: isFinite( attributes.fontSize ) ? attributes.fontSize : undefined,
customTextColor: attributes.textColor && '#' === attributes.textColor[ 0 ] ? attributes.textColor : undefined,
customBackgroundColor: attributes.backgroundColor && '#' === attributes.backgroundColor[ 0 ] ? attributes.backgroundColor : undefined,
}, [ 'fontSize', 'textColor', 'backgroundColor' ] );
},
},
{
supports,
attributes: {
...blockAttributes,
content: {
type: 'string',
source: 'html',
default: '',
},
},
save( { attributes } ) {
return <RawHTML>{ attributes.content }</RawHTML>;
},
migrate( attributes ) {
return attributes;
},
},
];

export default deprecated;
129 changes: 6 additions & 123 deletions packages/block-library/src/paragraph/index.js
Original file line number Diff line number Diff line change
@@ -1,149 +1,32 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { isFinite, omit } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
RawHTML,
} from '@wordpress/element';
import {
getColorClassName,
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';
import transforms from './transforms';

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

export { metadata, name };

const supports = {
className: false,
};

export const settings = {
title: __( 'Paragraph' ),
description: __( 'Start with the building block of all narrative.' ),
icon,
keywords: [ __( 'text' ) ],
supports,
supports: {
className: false,
},
transforms,
deprecated: [
{
supports,
attributes: {
...schema,
width: {
type: 'string',
},
},
save( { attributes } ) {
const {
width,
align,
content,
dropCap,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor,
fontSize,
customFontSize,
} = attributes;

const textClass = getColorClassName( 'color', textColor );
const backgroundClass = getColorClassName( 'background-color', backgroundColor );
const fontSizeClass = fontSize && `is-${ fontSize }-text`;

const className = classnames( {
[ `align${ width }` ]: width,
'has-background': backgroundColor || customBackgroundColor,
'has-drop-cap': dropCap,
[ fontSizeClass ]: fontSizeClass,
[ textClass ]: textClass,
[ backgroundClass ]: backgroundClass,
} );

const styles = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor,
fontSize: fontSizeClass ? undefined : customFontSize,
textAlign: align,
};

return (
<RichText.Content
tagName="p"
style={ styles }
className={ className ? className : undefined }
value={ content }
/>
);
},
},
{
supports,
attributes: omit( {
...schema,
fontSize: {
type: 'number',
},
}, 'customFontSize', 'customTextColor', 'customBackgroundColor' ),
save( { attributes } ) {
const { width, align, content, dropCap, backgroundColor, textColor, fontSize } = attributes;
const className = classnames( {
[ `align${ width }` ]: width,
'has-background': backgroundColor,
'has-drop-cap': dropCap,
} );
const styles = {
backgroundColor,
color: textColor,
fontSize,
textAlign: align,
};

return <p style={ styles } className={ className ? className : undefined }>{ content }</p>;
},
migrate( attributes ) {
return omit( {
...attributes,
customFontSize: isFinite( attributes.fontSize ) ? attributes.fontSize : undefined,
customTextColor: attributes.textColor && '#' === attributes.textColor[ 0 ] ? attributes.textColor : undefined,
customBackgroundColor: attributes.backgroundColor && '#' === attributes.backgroundColor[ 0 ] ? attributes.backgroundColor : undefined,
}, [ 'fontSize', 'textColor', 'backgroundColor' ] );
},
},
{
supports,
attributes: {
...schema,
content: {
type: 'string',
source: 'html',
default: '',
},
},
save( { attributes } ) {
return <RawHTML>{ attributes.content }</RawHTML>;
},
migrate( attributes ) {
return attributes;
},
},
],
deprecated,
merge( attributes, attributesToMerge ) {
return {
content: ( attributes.content || '' ) + ( attributesToMerge.content || '' ),
Expand Down
74 changes: 74 additions & 0 deletions packages/block-library/src/pullquote/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';

const blockAttributes = {
value: {
type: 'string',
source: 'html',
selector: 'blockquote',
multiline: 'p',
},
citation: {
type: 'string',
source: 'html',
selector: 'cite',
default: '',
},
mainColor: {
type: 'string',
},
customMainColor: {
type: 'string',
},
textColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
};

const deprecated = [
{
attributes: {
...blockAttributes,
},
save( { attributes } ) {
const { value, citation } = attributes;
return (
<blockquote>
<RichText.Content value={ value } multiline />
{ ! RichText.isEmpty( citation ) && <RichText.Content tagName="cite" value={ citation } /> }
</blockquote>
);
},
}, {
attributes: {
...blockAttributes,
citation: {
type: 'string',
source: 'html',
selector: 'footer',
},
align: {
type: 'string',
default: 'none',
},
},

save( { attributes } ) {
const { value, citation, align } = attributes;

return (
<blockquote className={ `align${ align }` }>
<RichText.Content value={ value } multiline />
{ ! RichText.isEmpty( citation ) && <RichText.Content tagName="footer" value={ citation } /> }
</blockquote>
);
},
},
];

export default deprecated;
Loading

0 comments on commit 9e6c041

Please sign in to comment.