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

Update the paragraph block to use the colors support key #21037

Merged
merged 4 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 0 additions & 12 deletions packages/block-library/src/paragraph/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@
"placeholder": {
"type": "string"
},
"textColor": {
"type": "string"
},
"customTextColor": {
"type": "string"
},
"backgroundColor": {
"type": "string"
},
"customBackgroundColor": {
"type": "string"
},
"fontSize": {
"type": "string"
},
Expand Down
120 changes: 105 additions & 15 deletions packages/block-library/src/paragraph/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@ const blockAttributes = {
textColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
backgroundColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
fontSize: {
type: 'string',
},
Expand All @@ -57,12 +51,103 @@ const blockAttributes = {
type: 'string',
enum: [ 'ltr', 'rtl' ],
},
style: {
type: 'object',
},
};

const migrateCustomColors = ( attributes ) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to share this migration function across our blocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I duplicated for now but yes it's always the same.

if ( ! attributes.customTextColor && ! attributes.customBackgroundColor ) {
return attributes;
}
const style = { color: {} };
if ( attributes.customTextColor ) {
style.color.text = attributes.customTextColor;
}
if ( attributes.customBackgroundColor ) {
style.color.background = attributes.customBackgroundColor;
}
return {
...attributes,
style,
};
};

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

const textClass = getColorClassName( 'color', textColor );
const backgroundClass = getColorClassName(
'background-color',
backgroundColor
);
const fontSizeClass = getFontSizeClass( fontSize );

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

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

return (
<RichText.Content
tagName="p"
style={ styles }
className={ className ? className : undefined }
value={ content }
dir={ direction }
/>
);
},
},
{
supports,
attributes: {
...omit( blockAttributes, [ 'style' ] ),
customTextColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
},
migrate: migrateCustomColors,
save( { attributes } ) {
const {
align,
Expand Down Expand Up @@ -116,11 +201,18 @@ const deprecated = [
{
supports,
attributes: {
...blockAttributes,
...omit( blockAttributes, [ 'style' ] ),
customTextColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
width: {
type: 'string',
},
},
migrate: migrateCustomColors,
save( { attributes } ) {
const {
width,
Expand Down Expand Up @@ -179,9 +271,7 @@ const deprecated = [
type: 'number',
},
},
'customFontSize',
'customTextColor',
'customBackgroundColor'
[ 'customFontSize', 'style' ]
),
save( { attributes } ) {
const {
Expand Down Expand Up @@ -215,8 +305,8 @@ const deprecated = [
);
},
migrate( attributes ) {
return omit(
{
return migrateCustomColors(
omit( {
...attributes,
customFontSize: isFinite( attributes.fontSize )
? attributes.fontSize
Expand All @@ -231,8 +321,8 @@ const deprecated = [
'#' === attributes.backgroundColor[ 0 ]
? attributes.backgroundColor
: undefined,
},
[ 'fontSize', 'textColor', 'backgroundColor' ]
} ),
[ 'fontSize', 'textColor', 'backgroundColor', 'style' ]
);
},
},
Expand Down
119 changes: 44 additions & 75 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
InspectorControls,
RichText,
withFontSizes,
__experimentalUseColors,
__experimentalBlock as Block,
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
Expand Down Expand Up @@ -87,27 +86,6 @@ function ParagraphBlock( {
const dropCapMinimumHeight = useDropCapMinimumHeight( dropCap, [
fontSize.size,
] );
const {
TextColor,
BackgroundColor,
InspectorControlsColorPanel,
} = __experimentalUseColors(
[
{ name: 'textColor', property: 'color' },
{ name: 'backgroundColor', className: 'has-background' },
],
{
contrastCheckers: [
{
backgroundColor: true,
textColor: true,
fontSize: fontSize.size,
},
],
colorDetector: { targetRef: ref },
},
[ fontSize.size ]
);

return (
<>
Expand Down Expand Up @@ -145,60 +123,51 @@ function ParagraphBlock( {
/>
</PanelBody>
</InspectorControls>
{ InspectorControlsColorPanel }
<BackgroundColor>
<TextColor>
<RichText
ref={ ref }
identifier="content"
tagName={ Block.p }
className={ classnames( {
'has-drop-cap': dropCap,
[ `has-text-align-${ align }` ]: align,
[ fontSize.class ]: fontSize.class,
} ) }
style={ {
fontSize: fontSize.size
? fontSize.size + 'px'
: undefined,
direction,
minHeight: dropCapMinimumHeight,
} }
value={ content }
onChange={ ( newContent ) =>
setAttributes( { content: newContent } )
}
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( name );
}
<RichText
ref={ ref }
identifier="content"
tagName={ Block.p }
className={ classnames( {
'has-drop-cap': dropCap,
[ `has-text-align-${ align }` ]: align,
[ fontSize.class ]: fontSize.class,
} ) }
style={ {
fontSize: fontSize.size ? fontSize.size + 'px' : undefined,
direction,
minHeight: dropCapMinimumHeight,
} }
value={ content }
onChange={ ( newContent ) =>
setAttributes( { content: newContent } )
}
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( name );
}

return createBlock( name, {
...attributes,
content: value,
} );
} }
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={
onReplace ? () => onReplace( [] ) : undefined
}
aria-label={
content
? __( 'Paragraph block' )
: __(
'Empty block; start writing or type forward slash to choose a block'
)
}
placeholder={
placeholder ||
__( 'Start writing or type / to choose a block' )
}
__unstableEmbedURLOnPaste
__unstableAllowPrefixTransformations
/>
</TextColor>
</BackgroundColor>
return createBlock( name, {
...attributes,
content: value,
} );
} }
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={ onReplace ? () => onReplace( [] ) : undefined }
aria-label={
content
? __( 'Paragraph block' )
: __(
'Empty block; start writing or type forward slash to choose a block'
)
}
placeholder={
placeholder ||
__( 'Start writing or type / to choose a block' )
}
__unstableEmbedURLOnPaste
__unstableAllowPrefixTransformations
/>
</>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const settings = {
className: false,
__unstablePasteTextInline: true,
lightBlockWrapper: true,
__experimentalColor: true,
},
__experimentalLabel( attributes, { context } ) {
if ( context === 'accessibility' ) {
Expand Down
21 changes: 1 addition & 20 deletions packages/block-library/src/paragraph/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,27 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
getColorClassName,
getFontSizeClass,
RichText,
} from '@wordpress/block-editor';
import { getFontSizeClass, RichText } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const {
align,
content,
dropCap,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor,
fontSize,
customFontSize,
direction,
} = attributes;

const textClass = getColorClassName( 'color', textColor );
const backgroundClass = getColorClassName(
'background-color',
backgroundColor
);
const fontSizeClass = getFontSizeClass( fontSize );

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

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

Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/paragraph/style.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
:root p {
background-color: var(--wp--color--background);
color: var(--wp--color--text);
}

.is-small-text {
font-size: 14px;
}
Expand Down