Skip to content

Commit

Permalink
Integrate Mobile 1.28.1 release into master (#22607)
Browse files Browse the repository at this point in the history
* RNMobile - Starter Page Templates - Portfolio: adds missing ids for galleries (#22321)

* Fix margin for the post-title (#22331)

* Fix margin for the post-title

Add 4px margin from the edges of the screen and adjust the alignment
of text.

* Only apply margins on the left and right edges.

* Correct buttons appender (#22362)

* Remove no longer necessary selection checks (#22500)

These are now handled on the native side

Co-authored-by: Gerardo Pacheco <[email protected]>
Co-authored-by: Sérgio Estêvão <[email protected]>
Co-authored-by: Luke Walczak <[email protected]>
  • Loading branch information
4 people authored May 26, 2020
1 parent 34d5c6b commit 3c9deb4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 100 deletions.
87 changes: 21 additions & 66 deletions packages/rich-text/src/component/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export class RichText extends Component {
this
);
this.valueToFormat = this.valueToFormat.bind( this );
this.willTrimSpaces = this.willTrimSpaces.bind( this );
this.getHtmlToRender = this.getHtmlToRender.bind( this );
this.insertString = this.insertString.bind( this );
this.state = {
Expand Down Expand Up @@ -647,29 +646,6 @@ export class RichText extends Component {
}
}

willTrimSpaces( html ) {
const { tagName } = this.props;

// aztec won't trim spaces in a case of <pre> block, so we are excluding it
if ( tagName === 'pre' ) {
return false;
}

// regex for detecting spaces around block element html tags
const blockHtmlElements =
'(div|br|blockquote|ul|ol|li|p|pre|h1|h2|h3|h4|h5|h6|iframe|hr)';
const leadingOrTrailingSpaces = new RegExp(
`(\\s+)<\/?${ blockHtmlElements }>|<\/?${ blockHtmlElements }>(\\s+)`,
'g'
);
const matches = html.match( leadingOrTrailingSpaces );
if ( matches && matches.length > 0 ) {
return true;
}

return false;
}

getHtmlToRender( record, tagName ) {
// Save back to HTML from React tree
let value = this.valueToFormat( record );
Expand Down Expand Up @@ -739,51 +715,30 @@ export class RichText extends Component {

// On AztecAndroid, setting the caret to an out-of-bounds position will crash the editor so, let's check for some cases.
if ( ! this.isIOS ) {
// Aztec performs some html text cleanup while parsing it so, its internal representation gets out-of-sync with the
// representation of the format-lib on the RN side. We need to avoid trying to set the caret position because it may
// be outside the text bounds and crash Aztec, at least on Android.
if ( this.willTrimSpaces( html ) ) {
// the html will get trimmed by the cleaning up functions in Aztec and caret position will get out-of-sync.
// So, skip forcing it, let Aztec just do its best and just log the fact.
console.warn(
'RichText value will be trimmed for spaces! Avoiding setting the caret position manually.'
);
selection = null;
} else if (
this.props.selectionStart > record.text.length ||
this.props.selectionEnd > record.text.length
) {
// The following regular expression is used in Aztec here:
// https://github.com/wordpress-mobile/AztecEditor-Android/blob/b1fad439d56fa6d4aa0b78526fef355c59d00dd3/aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt#L656
const brBeforeParaMatches = html.match( /(<br>)+<\/p>$/g );
if ( brBeforeParaMatches ) {
console.warn(
'Oops, selection will land outside the text, skipping setting it...'
'Oops, BR tag(s) at the end of content. Aztec will remove them, adapting the selection...'
);
selection = null;
} else {
// The following regular expression is used in Aztec here:
// https://github.com/wordpress-mobile/AztecEditor-Android/blob/b1fad439d56fa6d4aa0b78526fef355c59d00dd3/aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt#L656
const brBeforeParaMatches = html.match( /(<br>)+<\/p>$/g );
if ( brBeforeParaMatches ) {
console.warn(
'Oops, BR tag(s) at the end of content. Aztec will remove them, adapting the selection...'
);
const count = (
brBeforeParaMatches[ 0 ].match( /br/g ) || []
).length;
if ( count > 0 ) {
let newSelectionStart =
this.props.selectionStart - count;
if ( newSelectionStart < 0 ) {
newSelectionStart = 0;
}
let newSelectionEnd =
this.props.selectionEnd - count;
if ( newSelectionEnd < 0 ) {
newSelectionEnd = 0;
}
selection = {
start: newSelectionStart,
end: newSelectionEnd,
};
const count = (
brBeforeParaMatches[ 0 ].match( /br/g ) || []
).length;
if ( count > 0 ) {
let newSelectionStart =
this.props.selectionStart - count;
if ( newSelectionStart < 0 ) {
newSelectionStart = 0;
}
let newSelectionEnd = this.props.selectionEnd - count;
if ( newSelectionEnd < 0 ) {
newSelectionEnd = 0;
}
selection = {
start: newSelectionStart,
end: newSelectionEnd,
};
}
}
}
Expand Down
34 changes: 0 additions & 34 deletions packages/rich-text/src/component/test/index.native.js

This file was deleted.

0 comments on commit 3c9deb4

Please sign in to comment.