From de3265ac1f27ee9bd9ab39fbd741d04a8ea4eeb4 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 18 Apr 2017 09:39:43 +0100 Subject: [PATCH] - No div wrapper for text block - Do not split when hitting Enter on the first row - Fix the nullable value error --- blocks/components/editable/index.js | 7 ++++-- blocks/library/text/index.js | 34 +++++++++++++++++++---------- languages/gutenberg.pot | 8 +++---- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/blocks/components/editable/index.js b/blocks/components/editable/index.js index 139d7a481ccbc..60acb76bc92dc 100644 --- a/blocks/components/editable/index.js +++ b/blocks/components/editable/index.js @@ -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, '' ); @@ -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; @@ -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 : '' ); diff --git a/blocks/library/text/index.js b/blocks/library/text/index.js index 7250c2e047ea8..f793cddca9e76 100644 --- a/blocks/library/text/index.js +++ b/blocks/library/text/index.js @@ -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 ) => `

${ paragraph }

` ).join( '' ) : ''; +const fromParagraphsToValue = ( paragraphs ) => parse( paragraphs, query( 'p', html() ) ); registerBlock( 'core/text', { title: wp.i18n.__( 'Text' ), @@ -14,8 +17,7 @@ registerBlock( 'core/text', { category: 'common', attributes: { - content: html( 'div' ), - align: prop( 'div', 'style.textAlign' ) + content: query( 'p', html() ), }, controls: [ @@ -50,13 +52,15 @@ registerBlock( 'core/text', { return ( 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 ) } ) ); } } /> @@ -67,10 +71,16 @@ registerBlock( 'core/text', { const { align, content } = attributes; return ( -
+
+ { content && content.map( ( paragraph, i ) => ( +

+ ) ) } +

); } } ); diff --git a/languages/gutenberg.pot b/languages/gutenberg.pot index 0139235b67a69..940666763cb11 100644 --- a/languages/gutenberg.pot +++ b/languages/gutenberg.pot @@ -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 "" @@ -50,7 +50,7 @@ msgstr "" msgid "Quote" msgstr "" -#: blocks/library/text/index.js:10 +#: blocks/library/text/index.js:13 msgid "Text" msgstr ""