diff --git a/blocks/components/editable/index.js b/blocks/components/editable/index.js index ff37f8692f98a5..0e2e15a85cb321 100644 --- a/blocks/components/editable/index.js +++ b/blocks/components/editable/index.js @@ -2,6 +2,7 @@ * External dependencies */ import classnames from 'classnames'; +import { last } from 'lodash'; /** * Internal dependencies @@ -14,6 +15,7 @@ export default class Editable extends wp.element.Component { this.onInit = this.onInit.bind( this ); this.onSetup = this.onSetup.bind( this ); this.onChange = this.onChange.bind( this ); + this.onNewBlock = this.onNewBlock.bind( this ); this.bindNode = this.bindNode.bind( this ); } @@ -42,6 +44,7 @@ export default class Editable extends wp.element.Component { this.editor = editor; editor.on( 'init', this.onInit ); editor.on( 'focusout', this.onChange ); + editor.on( 'NewBlock', this.onNewBlock ); } onInit() { @@ -58,6 +61,35 @@ export default class Editable extends wp.element.Component { this.props.onChange( value ); } + onNewBlock() { + if ( this.props.tagName || ! this.props.onSplit ) { + return; + } + + // Getting the content before and after the cursor + 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, '' ); + const beforeNodes = childNodes.slice( 0, splitIndex ); + const lastNodeBeforeCursor = last( beforeNodes ); + // Avoid splitting on single enter + if ( + ! lastNodeBeforeCursor || + beforeNodes.length < 2 || + !! lastNodeBeforeCursor.textContent + ) { + return; + } + const before = getHtml( beforeNodes.slice( 0, beforeNodes.length - 1 ) ); + const after = getHtml( childNodes.slice( splitIndex ) ); + + // Splitting into two blocks + this.editor.setContent( this.props.value || '' ); + const hasAfter = !! childNodes.slice( splitIndex ) + .reduce( ( memo, node ) => memo + node.textContent, '' ); + this.props.onSplit( before, hasAfter ? after : '' ); + } + bindNode( ref ) { this.node = ref; } diff --git a/blocks/components/editable/style.scss b/blocks/components/editable/style.scss index 1a143b68d8ea17..468315ca41743b 100644 --- a/blocks/components/editable/style.scss +++ b/blocks/components/editable/style.scss @@ -1,3 +1,13 @@ -.blocks-editable:focus { - outline: none; +.blocks-editable { + > p:first-child { + margin-top: 0; + } + + > p:last-child { + margin-bottom: 0; + } + + &:focus { + outline: none; + } } diff --git a/blocks/library/text/index.js b/blocks/library/text/index.js index 3d3033d2181933..540c8c01d232a9 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( 'p' ), - align: prop( 'p', 'style.textAlign' ) + content: query( 'p', html() ), }, controls: [ @@ -45,15 +47,22 @@ registerBlock( 'core/text', { } ], - edit( { attributes, setAttributes } ) { + edit( { attributes, setAttributes, insertBlockAfter } ) { const { content, align } = attributes; return (I imagine prior to the launch of the iPod, or the iPhone, there were teams saying the same thing: the copy + paste guys are so close to being ready and we know Walt Mossberg is going to ding us for this so let\'s just not ship to the manufacturers in China for just a few more weeks… The Apple teams were probably embarrassed. But if you\'re not embarrassed when you ship your first version you waited too long.
', + 'I imagine prior to the launch of the iPod, or the iPhone, there were teams saying the same thing: the copy + paste guys are so close to being ready and we know Walt Mossberg is going to ding us for this so let\'s just not ship to the manufacturers in China for just a few more weeks… The Apple teams were probably embarrassed. But if you\'re not embarrassed when you ship your first version you waited too long.
A beautiful thing about Apple is how quickly they obsolete their own products. I imagine this also makes the discipline of getting things out there easier. Like I mentioned before, the longer it’s been since the last release the more pressure there is, but if you know that if your bit of code doesn’t make this version but there’s the +0.1 coming out in 6 weeks, then it’s not that bad. It’s like flights from San Francisco to LA, if you miss one you know there’s another one an hour later so it’s not a big deal. Amazon has done a fantastic job of this with the Kindle as well, with a new model every year.
', + 'A beautiful thing about Apple is how quickly they obsolete their own products. I imagine this also makes the discipline of getting things out there easier. Like I mentioned before, the longer it’s been since the last release the more pressure there is, but if you know that if your bit of code doesn’t make this version but there’s the +0.1 coming out in 6 weeks, then it’s not that bad. It’s like flights from San Francisco to LA, if you miss one you know there’s another one an hour later so it’s not a big deal. Amazon has done a fantastic job of this with the Kindle as well, with a new model every year.
By shipping early and often you have the unique competitive advantage of hearing from real people what they think of your work, which in best case helps you anticipate market direction, and in worst case gives you a few people rooting for you that you can email when your team pivots to a new idea. Nothing can recreate the crucible of real usage.
', + 'By shipping early and often you have the unique competitive advantage of hearing from real people what they think of your work, which in best case helps you anticipate market direction, and in worst case gives you a few people rooting for you that you can email when your team pivots to a new idea. Nothing can recreate the crucible of real usage.