diff --git a/blocks/components/editable/index.js b/blocks/components/editable/index.js index 139d7a481ccbc8..60acb76bc92dcc 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 7250c2e047ea8d..f793cddca9e768 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 (