Skip to content

Commit

Permalink
Block library: Refactor Verse block to use class names for text align (
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Aug 29, 2019
1 parent feb2d0e commit e81e374
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 2.7.0 (2019-08-05)

### Enhancements

- Heading block uses `has-text-align-*` class names rather than inline style for text alignment.
- Verse block uses `has-text-align-*` class names rather than inline style for text alignment.

### Bug Fixes

- Fixed insertion of columns in the table block, which now inserts columns for all table sections ([#16410](https://github.com/WordPress/gutenberg/pull/16410))
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@
}

.has-text-align-left {
/*rtl:ignore*/
text-align: left;
}

.has-text-align-right {
/*rtl:ignore*/
text-align: right;
}

Expand Down
35 changes: 35 additions & 0 deletions packages/block-library/src/verse/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';

const blockAttributes = {
content: {
type: 'string',
source: 'html',
selector: 'pre',
default: '',
},
textAlign: {
type: 'string',
},
};

const deprecated = [
{
attributes: blockAttributes,
save( { attributes } ) {
const { textAlign, content } = attributes;

return (
<RichText.Content
tagName="pre"
style={ { textAlign } }
value={ content }
/>
);
},
},
];

export default deprecated;
9 changes: 8 additions & 1 deletion packages/block-library/src/verse/edit.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -29,9 +34,11 @@ export default function VerseEdit( { attributes, setAttributes, className, merge
content: nextContent,
} );
} }
style={ { textAlign } }
placeholder={ __( 'Write…' ) }
wrapperClassName={ className }
className={ classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ) }
onMerge={ mergeBlocks }
/>
</>
Expand Down
6 changes: 4 additions & 2 deletions packages/block-library/src/verse/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 icon from './icon';
import metadata from './block.json';
Expand All @@ -22,11 +23,12 @@ export const settings = {
icon,
keywords: [ __( 'poetry' ) ],
transforms,
edit,
save,
deprecated,
merge( attributes, attributesToMerge ) {
return {
content: attributes.content + attributesToMerge.content,
};
},
edit,
save,
};
11 changes: 10 additions & 1 deletion packages/block-library/src/verse/save.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand All @@ -6,10 +11,14 @@ import { RichText } from '@wordpress/block-editor';
export default function save( { attributes } ) {
const { textAlign, content } = attributes;

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

return (
<RichText.Content
tagName="pre"
style={ { textAlign } }
className={ className }
value={ content }
/>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:verse {"textAlign":"left"} -->
<pre style="text-align:left" class="wp-block-verse">A <em>verse</em><br>And more!</pre>
<!-- /wp:core/verse -->
13 changes: 13 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__verse__deprecated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"clientId": "_clientId_0",
"name": "core/verse",
"isValid": true,
"attributes": {
"content": "A <em>verse</em>…<br>And more!",
"textAlign": "left"
},
"innerBlocks": [],
"originalContent": "<pre style=\"text-align:left\" class=\"wp-block-verse\">A <em>verse</em>…<br>And more!</pre>"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"blockName": "core/verse",
"attrs": {
"textAlign": "left"
},
"innerBlocks": [],
"innerHTML": "\n<pre style=\"text-align:left\" class=\"wp-block-verse\">A <em>verse</em>…<br>And more!</pre>\n",
"innerContent": [
"\n<pre style=\"text-align:left\" class=\"wp-block-verse\">A <em>verse</em>…<br>And more!</pre>\n"
]
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:verse {"textAlign":"left"} -->
<pre class="wp-block-verse has-text-align-left">A <em>verse</em><br>And more!</pre>
<!-- /wp:verse -->

0 comments on commit e81e374

Please sign in to comment.