Skip to content

Commit

Permalink
Refactor onSplit and insertBlockAfter in RichText component, Para…
Browse files Browse the repository at this point in the history
… and Heading blocks for mobile, tryin gto stay closer to the web version. (WordPress#10690)
  • Loading branch information
daniloercoli authored and antpb committed Oct 26, 2018
1 parent 2902d90 commit 6159d79
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 25 deletions.
31 changes: 12 additions & 19 deletions packages/block-library/src/heading/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,11 @@ import './editor.scss';
const minHeight = 50;

class HeadingEdit extends Component {
constructor() {
super( ...arguments );
this.splitBlock = this.splitBlock.bind( this );
}

// eslint-disable-next-line no-unused-vars
splitBlock( htmlText, start, end ) {
const {
insertBlocksAfter,
} = this.props;

if ( insertBlocksAfter ) {
const blocks = [];
blocks.push( createBlock( 'core/paragraph', { content: 'Test' } ) );
insertBlocksAfter( blocks );
}
}

render() {
const {
attributes,
setAttributes,
insertBlocksAfter,
} = this.props;

const {
Expand Down Expand Up @@ -73,7 +56,17 @@ class HeadingEdit extends Component {
content: newParaBlock.attributes.content,
} );
} }
onSplit={ this.splitBlock }
onSplit={
insertBlocksAfter ?
( before, after, ...blocks ) => {
setAttributes( { content: before } );
insertBlocksAfter( [
...blocks,
createBlock( 'core/paragraph', { content: after } ),
] );
} :
undefined
}
onContentSizeChange={ ( event ) => {
setAttributes( { aztecHeight: event.aztecHeight } );
} }
Expand Down
42 changes: 37 additions & 5 deletions packages/block-library/src/paragraph/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,55 @@ import { RichText } from '@wordpress/editor';

const minHeight = 50;

const name = 'core/paragraph';

class ParagraphEdit extends Component {
constructor() {
super( ...arguments );
this.splitBlock = this.splitBlock.bind( this );
}

// eslint-disable-next-line no-unused-vars
splitBlock( htmlText, start, end ) {
/**
* 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,
} = this.props;

if ( insertBlocksAfter ) {
const blocks = [];
blocks.push( createBlock( 'core/paragraph', { content: 'Test' } ) );
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 ) {
// TODO : If before content is omitted, treat as intent to delete block.
// 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() {
Expand Down
48 changes: 47 additions & 1 deletion packages/editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
import { Component, RawHTML } from '@wordpress/element';
import { withInstanceId, compose } from '@wordpress/compose';
import { Toolbar } from '@wordpress/components';
import {
create,
split,
toHTMLString,
} from '@wordpress/rich-text';

/**
* Internal dependencies
Expand Down Expand Up @@ -56,12 +61,53 @@ export class RichText extends Component {
this.splitContent( htmlText, start, end );
}

/*
* Splits the content at the location of the selection.
*
* Replaces the content of the editor inside this element with the contents
* before the selection. Sends the elements after the selection to the `onSplit`
* handler.
*
*/
splitContent( htmlText, start, end ) {
const { onSplit } = this.props;

if ( ! onSplit ) {
return;
}
onSplit( htmlText, start, end );

const record = create( {
html: htmlText,
range: null,
multilineTag: false,
removeNode: null,
unwrapNode: null,
removeAttribute: null,
filterString: null,
} );

// TODO : Fix the index position in AztecNative for Android
let [ before, after ] = split( { start: start - 6, end: end - 6, ...record } );

// TODO : Handle here the cases when the split happens at the trailing or leading edge...
// See the web version for reference.

if ( before ) {
before = this.valueToFormat( before );
}

if ( after ) {
after = this.valueToFormat( after );
}

onSplit( before, after );
}

valueToFormat( { formats, text } ) {
const value = toHTMLString( { formats, text }, this.multilineTag );
// remove the outer p tags
const returningContentWithoutParaTag = value.replace( /<p>|<\/p>/gi, '' );
return returningContentWithoutParaTag;
}

onActiveFormatsChange( formats ) {
Expand Down

0 comments on commit 6159d79

Please sign in to comment.