Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native mobile: don't set caret when rich-text text will get trimmed #15021

Merged
merged 6 commits into from
Apr 17, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/block-editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class RichText extends Component {
// This prevents a bug in Aztec which triggers onSelectionChange twice on format change
this.onSelectionChange = this.onSelectionChange.bind( this );
this.valueToFormat = this.valueToFormat.bind( this );
this.willTrimSpaces = this.willTrimSpaces.bind( this );
this.state = {
start: 0,
end: 0,
Expand Down Expand Up @@ -624,6 +625,17 @@ export class RichText extends Component {
}
}

willTrimSpaces( html ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice opportunity to write some unit test against this method, so that we are always sure it's working like expected, WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Will it be OK with you to do it in a separate PR, only to make sure we get this one merged today if possible? (to have at least a couple of days of testing until the code freeze)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, of course, I will finish testing very soon so that you can merge this one. @hypest

// regex for detecting spaces around html tags
const trailingSpaces = /(\s+)<.+?>|<.+?>(\s+)/g;
const matches = html.match(trailingSpaces);
if (matches && matches.length > 0) {
return true;
}

return false;
}

render() {
const {
tagName,
Expand Down Expand Up @@ -651,7 +663,17 @@ export class RichText extends Component {
let selection = null;
if ( this.needsSelectionUpdate ) {
this.needsSelectionUpdate = false;
selection = { start: this.state.start, end: this.state.end };

// 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.isIOS && 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." + html);
} else {
selection = { start: this.state.start, end: this.state.end };
}
}

return (
Expand Down