From 034b6a2b6e075039dedc311a7b92b9f0edc2dbbe Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Tue, 26 Feb 2019 11:12:11 -0300 Subject: [PATCH 1/4] Fixes pasting in heading blocks. --- .../block-library/src/heading/edit.native.js | 58 +++++++++++++++---- .../src/components/rich-text/index.native.js | 9 ++- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/packages/block-library/src/heading/edit.native.js b/packages/block-library/src/heading/edit.native.js index dab8589b9c923..b946c19b0ec65 100644 --- a/packages/block-library/src/heading/edit.native.js +++ b/packages/block-library/src/heading/edit.native.js @@ -25,11 +25,57 @@ class HeadingEdit extends Component { constructor( props ) { super( props ); + this.splitBlock = this.splitBlock.bind( this ); + this.state = { aztecHeight: 0, }; } + + /** + * Split handler for RichText value, namely when content is pasted or the + * user presses the Enter key. + * + * @param {?Array} before Optional before value, to be used as content + * in place of what exists currently for the + * block. If undefined, the block is deleted. + * @param {?Array} after Optional after value, to be appended in a new + * paragraph block to the set of blocks passed + * as spread. + * @param {...WPBlock} blocks Optional blocks inserted between the before + * and after value blocks. + */ + splitBlock( before, after, ...blocks ) { + const { + attributes, + insertBlocksAfter, + setAttributes, + onReplace, + } = this.props; + + if ( after !== null ) { + // Append "After" content as a new paragraph block to the end of + // any other blocks being inserted after the current paragraph. + const newBlock = createBlock( name, { content: after } ); + blocks.push( newBlock ); + } + + if ( blocks.length && insertBlocksAfter ) { + insertBlocksAfter( blocks ); + } + + const { content } = attributes; + if ( before === null ) { + onReplace( [] ); + } else if ( content !== before ) { + // Only update content if it has in-fact changed. In case that user + // has created a new paragraph at end of an existing one, the value + // of before will be strictly equal to the current content. + setAttributes( { content: before } ); + } + } + render() { const { attributes, @@ -65,17 +111,7 @@ class HeadingEdit extends Component { } } onChange={ ( value ) => setAttributes( { content: value } ) } onMerge={ mergeBlocks } - onSplit={ - insertBlocksAfter ? - ( before, after, ...blocks ) => { - setAttributes( { content: before } ); - insertBlocksAfter( [ - ...blocks, - createBlock( 'core/paragraph', { content: after } ), - ] ); - } : - undefined - } + onSplit={ this.splitBlock } onContentSizeChange={ ( event ) => { this.setState( { aztecHeight: event.aztecHeight } ); } } diff --git a/packages/editor/src/components/rich-text/index.native.js b/packages/editor/src/components/rich-text/index.native.js index 1e3de4f168086..c090e1820be30 100644 --- a/packages/editor/src/components/rich-text/index.native.js +++ b/packages/editor/src/components/rich-text/index.native.js @@ -96,9 +96,16 @@ export class RichText extends Component { */ getRecord() { const { formatPlaceholder, start, end } = this.state; + + var value = this.props.value === undefined ? null : this.props.value; + // Since we get the text selection from Aztec we need to be in sync with the HTML `value` // Removing leading white spaces using `trim()` should make sure this is the case. - const { formats, text } = this.formatToValue( this.props.value === undefined ? undefined : this.props.value.trimLeft() ); + if (typeof value === 'string' || value instanceof String) { + value = value.trimLeft(); + } + + const { formats, text } = this.formatToValue( value ); return { formats, formatPlaceholder, text, start, end }; } From f976f912f3dea08f544ac1f91b0f4f7a95de94be Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Mon, 4 Mar 2019 18:33:34 -0800 Subject: [PATCH 2/4] Fixed an issue with a missing constant. --- packages/block-library/src/heading/edit.native.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/block-library/src/heading/edit.native.js b/packages/block-library/src/heading/edit.native.js index b946c19b0ec65..7627288b6f52c 100644 --- a/packages/block-library/src/heading/edit.native.js +++ b/packages/block-library/src/heading/edit.native.js @@ -16,6 +16,8 @@ import { Component } from '@wordpress/element'; import { RichText, BlockControls } from '@wordpress/editor'; import { createBlock } from '@wordpress/blocks'; +const name = 'core/heading'; + /** * Internal dependencies */ From 6c18a540c4a61d72642c9b9f0ee84b1db4813e52 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Mon, 4 Mar 2019 18:38:05 -0800 Subject: [PATCH 3/4] Removes some code that was added back as part of a merge conflict. --- packages/block-library/src/heading/edit.native.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/block-library/src/heading/edit.native.js b/packages/block-library/src/heading/edit.native.js index 41e957a542ddd..1668b7fbfec1e 100644 --- a/packages/block-library/src/heading/edit.native.js +++ b/packages/block-library/src/heading/edit.native.js @@ -24,17 +24,6 @@ const name = 'core/heading'; import styles from './style.scss'; class HeadingEdit extends Component { - constructor( props ) { - super( props ); - - this.splitBlock = this.splitBlock.bind( this ); - - this.state = { - aztecHeight: 0, - }; - } - - /** * Split handler for RichText value, namely when content is pasted or the * user presses the Enter key. From 4d35a9cf929599d9b60f048d758aaec3b61c1261 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Mon, 4 Mar 2019 18:42:56 -0800 Subject: [PATCH 4/4] Re-adds some code removed by mistake. --- packages/block-library/src/heading/edit.native.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/block-library/src/heading/edit.native.js b/packages/block-library/src/heading/edit.native.js index 1668b7fbfec1e..d21613cabe098 100644 --- a/packages/block-library/src/heading/edit.native.js +++ b/packages/block-library/src/heading/edit.native.js @@ -24,6 +24,13 @@ const name = 'core/heading'; import styles from './style.scss'; class HeadingEdit extends Component { + constructor( props ) { + super( props ); + + this.splitBlock = this.splitBlock.bind( this ); + } + + /** * Split handler for RichText value, namely when content is pasted or the * user presses the Enter key.