Skip to content

Commit

Permalink
Remove the root tags returned by Aztec by using the correct tag refer…
Browse files Browse the repository at this point in the history
…ence (WordPress#10797)
  • Loading branch information
daniloercoli authored and antpb committed Oct 26, 2018
1 parent 222712e commit 0c1d9a0
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ export class RichText extends Component {

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

onActiveFormatsChange( formats ) {
Expand All @@ -133,6 +132,18 @@ export class RichText extends Component {
selectedNodeId: this.state.selectedNodeId + 1,
} );
}

/*
* Cleans up any root tags produced by aztec.
* TODO: This should be removed on a later version when aztec doesn't return the top tag of the text being edited
*/

removeRootTagsProduceByAztec( html ) {
const openingTagRegexp = RegExp( '^<' + this.props.tagName + '>', 'gim' );
const closingTagRegexp = RegExp( '</' + this.props.tagName + '>$', 'gim' );
return html.replace( openingTagRegexp, '' ).replace( closingTagRegexp, '' );
}

/**
* Handles any case where the content of the AztecRN instance has changed.
*/
Expand All @@ -143,11 +154,7 @@ export class RichText extends Component {
clearTimeout( this.currentTimer );
}
this.lastEventCount = event.nativeEvent.eventCount;
// The following method just cleans up any root tags produced by aztec and replaces them with a br tag
// This should be removed on a later version when aztec doesn't return the top tag of the text being edited
const openingTagRegexp = RegExp( '^<' + this.props.tagName + '>', 'gim' );
const closingTagRegexp = RegExp( '</' + this.props.tagName + '>$', 'gim' );
const contentWithoutRootTag = event.nativeEvent.text.replace( openingTagRegexp, '' ).replace( closingTagRegexp, '' );
const contentWithoutRootTag = this.removeRootTagsProduceByAztec( event.nativeEvent.text );
this.lastContent = contentWithoutRootTag;
// Set a time to call the onChange prop if nothing changes in the next second
this.currentTimer = setTimeout( function() {
Expand Down

0 comments on commit 0c1d9a0

Please sign in to comment.