Skip to content

Commit

Permalink
- No div wrapper for text block
Browse files Browse the repository at this point in the history
- Do not split when hitting Enter on the first row
- Fix the nullable value error
  • Loading branch information
youknowriad committed Apr 18, 2017
1 parent 49a169e commit de3265a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
7 changes: 5 additions & 2 deletions blocks/components/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ export default class Editable extends wp.element.Component {

// Wait for the event to propagate
setTimeout( () => {
if ( ! this.editor ) {
return;
}
// Getting the content before and after the cursor
this.editor.selection.getStart();
const childNodes = Array.from( this.editor.getBody().childNodes );
const splitIndex = childNodes.indexOf( this.editor.selection.getStart() );
const getHtml = ( nodes ) => nodes.reduce( ( memo, node ) => memo + node.outerHTML, '' );
Expand All @@ -78,6 +80,7 @@ export default class Editable extends wp.element.Component {
// Avoid splitting on single enter
if (
! lastNodeBeforeCursor ||
beforeNodes.length < 2 ||
!! lastNodeBeforeCursor.textContent
) {
return;
Expand All @@ -86,7 +89,7 @@ export default class Editable extends wp.element.Component {
const after = getHtml( childNodes.slice( splitIndex ) );

// Splitting into two blocks
this.editor.setContent( this.props.value );
this.editor.setContent( this.props.value || '' );
const hasAfter = !! childNodes.slice( splitIndex )
.reduce( ( memo, node ) => memo + node.textContent, '' );
this.props.onSplit( before, hasAfter ? after : '' );
Expand Down
34 changes: 22 additions & 12 deletions blocks/library/text/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/**
* Internal dependencies
*/
import { registerBlock, query } from 'api';
import { registerBlock, query as hpq } from 'api';
import Editable from 'components/editable';

const { html, prop } = query;
const { html, parse, query } = hpq;

const fromValueToParagraphs = ( value ) => value ? value.map( ( paragraph ) => `<p>${ paragraph }</p>` ).join( '' ) : '';
const fromParagraphsToValue = ( paragraphs ) => parse( paragraphs, query( 'p', html() ) );

registerBlock( 'core/text', {
title: wp.i18n.__( 'Text' ),
Expand All @@ -14,8 +17,7 @@ registerBlock( 'core/text', {
category: 'common',

attributes: {
content: html( 'div' ),
align: prop( 'div', 'style.textAlign' )
content: query( 'p', html() ),
},

controls: [
Expand Down Expand Up @@ -50,13 +52,15 @@ registerBlock( 'core/text', {

return (
<Editable
value={ content }
onChange={ ( value ) => setAttributes( { content: value } ) }
value={ fromValueToParagraphs( content ) }
onChange={ ( paragraphs ) => setAttributes( {
content: fromParagraphsToValue( paragraphs )
} ) }
style={ align ? { textAlign: align } : null }
onSplit={ ( before, after ) => {
setAttributes( { content: before } );
setAttributes( { content: fromParagraphsToValue( before ) } );
insertBlockAfter( wp.blocks.createBlock( 'core/text', {
content: after
content: fromParagraphsToValue( after )
} ) );
} }
/>
Expand All @@ -67,10 +71,16 @@ registerBlock( 'core/text', {
const { align, content } = attributes;

return (
<div
style={ align ? { textAlign: align } : null }
dangerouslySetInnerHTML={ { __html: content } }
/>
<div>
{ content && content.map( ( paragraph, i ) => (
<p
key={ i }
style={ align ? { textAlign: align } : null }
dangerouslySetInnerHTML={ {
__html: paragraph
} } />
) ) }
</div>
);
}
} );
8 changes: 4 additions & 4 deletions languages/gutenberg.pot
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ msgid "List"
msgstr ""

#: blocks/library/list/index.js:25
#: blocks/library/text/index.js:24
#: blocks/library/text/index.js:26
msgid "Align left"
msgstr ""

#: blocks/library/list/index.js:33
#: blocks/library/text/index.js:32
#: blocks/library/text/index.js:34
msgid "Align center"
msgstr ""

#: blocks/library/list/index.js:41
#: blocks/library/text/index.js:40
#: blocks/library/text/index.js:42
msgid "Align right"
msgstr ""

Expand All @@ -50,7 +50,7 @@ msgstr ""
msgid "Quote"
msgstr ""

#: blocks/library/text/index.js:10
#: blocks/library/text/index.js:13
msgid "Text"
msgstr ""

Expand Down

0 comments on commit de3265a

Please sign in to comment.