Skip to content

Commit

Permalink
Block library: Refactor Heading block to use class names for text ali…
Browse files Browse the repository at this point in the history
…gn (#16035)

* Block library: Refactor Heading block to use class names for text align

* Fix Heading icon in the collapsed mode

* Add missing label for the level dropdown menu

* Block library: Revert attribute name change for text align

* Remove code which moves text alignment control to the block toolbar
  • Loading branch information
gziolo authored Jun 14, 2019
1 parent 4f5b472 commit 93e31d0
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 18 deletions.
79 changes: 79 additions & 0 deletions packages/block-library/src/heading/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* External dependencies
*/
import classnames from 'classnames';

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

const blockSupports = {
className: false,
anchor: true,
};

const blockAttributes = {
align: {
type: 'string',
},
content: {
type: 'string',
source: 'html',
selector: 'h1,h2,h3,h4,h5,h6',
default: '',
},
level: {
type: 'number',
default: 2,
},
placeholder: {
type: 'string',
},
textColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
};

const deprecated = [
{
supports: blockSupports,
attributes: blockAttributes,
save( { attributes } ) {
const {
align,
level,
content,
textColor,
customTextColor,
} = attributes;
const tagName = 'h' + level;

const textClass = getColorClassName( 'color', textColor );

const className = classnames( {
[ textClass ]: textClass,
} );

return (
<RichText.Content
className={ className ? className : undefined }
tagName={ tagName }
style={ {
textAlign: align,
color: textClass ? undefined : customTextColor,
} }
value={ content }
/>
);
},
},
];

export default deprecated;
2 changes: 1 addition & 1 deletion packages/block-library/src/heading/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ function HeadingEdit( {
onReplace={ onReplace }
onRemove={ () => onReplace( [] ) }
className={ classnames( className, {
[ `has-text-align-${ align }` ]: align,
'has-text-color': textColor.color,
[ textColor.class ]: textColor.class,
} ) }
placeholder={ placeholder || __( 'Write heading…' ) }
style={ {
color: textColor.color,
textAlign: align,
} }
/>
</>
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import deprecated from './deprecated';
import edit from './edit';
import metadata from './block.json';
import save from './save';
Expand All @@ -25,6 +26,7 @@ export const settings = {
anchor: true,
},
transforms,
deprecated,
merge( attributes, attributesToMerge ) {
return {
content: ( attributes.content || '' ) + ( attributesToMerge.content || '' ),
Expand Down
6 changes: 3 additions & 3 deletions packages/block-library/src/heading/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ import {
export default function save( { attributes } ) {
const {
align,
level,
content,
textColor,
customTextColor,
level,
textColor,
} = attributes;
const tagName = 'h' + level;

const textClass = getColorClassName( 'color', textColor );

const className = classnames( {
[ textClass ]: textClass,
[ `has-text-align-${ align }` ]: align,
} );

return (
<RichText.Content
className={ className ? className : undefined }
tagName={ tagName }
style={ {
textAlign: align,
color: textClass ? undefined : customTextColor,
} }
value={ content }
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@
font-size: 42px;
}

// Text alignments.
.has-text-align-center {
text-align: center;
}

.has-text-align-left {
text-align: left;
}

.has-text-align-right {
text-align: right;
}

/**
* Vanilla Block Styles
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/fixtures/block-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export const EXPECTED_TRANSFORMS = {
originalBlock: 'Group',
availableTransforms: [],
},
'core__heading__h2-em': {
'core__heading__h4-em': {
originalBlock: 'Heading',
availableTransforms: [
'Quote',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:core/heading {"align":"right","level":3} -->
<h3 style="text-align:right">A picture is worth a thousand words, or so the saying goes</h3>
<!-- /wp:core/heading -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"clientId": "_clientId_0",
"name": "core/heading",
"isValid": true,
"attributes": {
"align": "right",
"content": "A picture is worth a thousand words, or so the saying goes",
"level": 3
},
"innerBlocks": [],
"originalContent": "<h3 style=\"text-align:right\">A picture is worth a thousand words, or so the saying goes</h3>"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"blockName": "core/heading",
"attrs": {
"align": "right",
"level": 3
},
"innerBlocks": [],
"innerHTML": "\n<h3 style=\"text-align:right\">A picture is worth a thousand words, or so the saying goes</h3>\n",
"innerContent": [
"\n<h3 style=\"text-align:right\">A picture is worth a thousand words, or so the saying goes</h3>\n"
]
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:heading {"align":"right","level":3} -->
<h3 class="has-text-align-right">A picture is worth a thousand words, or so the saying goes</h3>
<!-- /wp:heading -->
3 changes: 0 additions & 3 deletions packages/e2e-tests/fixtures/blocks/core__heading__h2-em.html

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__heading__h4-em.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:core/heading {"level":4} -->
<h4>The <em>Inserter</em> Tool</h4>
<!-- /wp:core/heading -->
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"isValid": true,
"attributes": {
"content": "The <em>Inserter</em> Tool",
"level": 2
"level": 4
},
"innerBlocks": [],
"originalContent": "<h2>The <em>Inserter</em> Tool</h2>"
"originalContent": "<h4>The <em>Inserter</em> Tool</h4>"
}
]
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[
{
"blockName": "core/heading",
"attrs": {},
"attrs": {
"level": 4
},
"innerBlocks": [],
"innerHTML": "\n<h2>The <em>Inserter</em> Tool</h2>\n",
"innerHTML": "\n<h4>The <em>Inserter</em> Tool</h4>\n",
"innerContent": [
"\n<h2>The <em>Inserter</em> Tool</h2>\n"
"\n<h4>The <em>Inserter</em> Tool</h4>\n"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:heading {"level":4} -->
<h4>The <em>Inserter</em> Tool</h4>
<!-- /wp:heading -->
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ exports[`Block transforms correctly transform block Heading in fixture core__hea
<!-- /wp:quote -->"
`;
exports[`Block transforms correctly transform block Heading in fixture core__heading__h2-em into the Paragraph block 1`] = `
exports[`Block transforms correctly transform block Heading in fixture core__heading__h4-em into the Paragraph block 1`] = `
"<!-- wp:paragraph -->
<p>The <em>Inserter</em> Tool</p>
<!-- /wp:paragraph -->"
`;
exports[`Block transforms correctly transform block Heading in fixture core__heading__h2-em into the Quote block 1`] = `
exports[`Block transforms correctly transform block Heading in fixture core__heading__h4-em into the Quote block 1`] = `
"<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p>The <em>Inserter</em> Tool</p></blockquote>
<!-- /wp:quote -->"
Expand Down

0 comments on commit 93e31d0

Please sign in to comment.