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

[RNMobile] Try fixing split/merge issue on native mobile by dropping selection update event when late #29683

Merged
merged 12 commits into from
Mar 18, 2021
Merged
29 changes: 27 additions & 2 deletions packages/rich-text/src/component/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ export class RichText extends Component {
this.convertFontSizeFromString = this.convertFontSizeFromString.bind(
this
);
this.manipulateEventCounterToForceNativeToRefresh = this.manipulateEventCounterToForceNativeToRefresh.bind(
this
);
this.state = {
activeFormats: [],
selectedFormat: null,
Expand Down Expand Up @@ -593,6 +596,13 @@ export class RichText extends Component {
}

onSelectionChangeFromAztec( start, end, text, event ) {
if ( ! this.isIOS && event.nativeEvent.eventCount <= this.lastEventCount ) {
console.log(
`Dropping onSelectionChange from Aztec as its event counter is older than latest sent to the native side.`
);
return;
}

// `end` can be less than `start` on iOS
// Let's fix that here so `rich-text/slice` can work properly
const realStart = Math.min( start, end );
Expand Down Expand Up @@ -667,6 +677,21 @@ export class RichText extends Component {
return value;
}

manipulateEventCounterToForceNativeToRefresh() {
if ( this.isIOS ) {
this.lastEventCount = undefined;
return;
}

if ( typeof this.lastEventCount !== 'undefined' ) {
this.lastEventCount += 100; // bump by a hundred, hopefully native hasn't bombarded the JS side in the meantime.
} else {
window.console.warn(
"Tried to bump the RichText native event counter but was 'undefined'. Aborting bump."
);
}
}

shouldComponentUpdate( nextProps ) {
if (
nextProps.tagName !== this.props.tagName ||
Expand Down Expand Up @@ -703,7 +728,7 @@ export class RichText extends Component {
this.needsSelectionUpdate = true;
}

this.lastEventCount = undefined; // force a refresh on the native side
this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
}

if ( ! this.comesFromAztec ) {
Expand Down Expand Up @@ -748,7 +773,7 @@ export class RichText extends Component {
componentDidUpdate( prevProps ) {
if ( this.props.value !== this.value ) {
this.value = this.props.value;
this.lastEventCount = undefined;
this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
}
const { __unstableIsSelected: isSelected } = this.props;

Expand Down