Skip to content

Commit

Permalink
Fix Chat Input Crashes on Android (keybase#20058)
Browse files Browse the repository at this point in the history
* Fix Chat Input Crashes on Android

The input was crashing when you sent messages that had modified the
native selection props. It crashed when you cleared the text because it
would set the text to empty, and reused a cached selection that was out
of bounds of an empty string.

For example, say you type "hello @" and autocomplete to "hello @foo". We
do some text selection magic to insert the @foo and put your cursor in
the correct spot. The Selection start and end indicies are both 11. When
you submit to message, we clear the text. When we clear the text, RN
keeps reusing those selection indices (of 11) and Android complains the
selection is outside of the bounds of the text ("" has a length of 0,
but you want to move the cursor to position 11, outside the bounds of
0).

The workaround here is to set the selection at the same time as the
text.

The RN Issue is: facebook/react-native#25265

* Apply changes to iOS as well

* Update comment

* Set lastNativeSelection
  • Loading branch information
MarcoPolo authored Oct 2, 2019
1 parent 6c2405a commit 26ad695
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions shared/common-adapters/plain-input.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ class PlainInput extends Component<InternalProps> {
text: this._lastNativeText || '',
}
const newTextInfo = fn(currentTextInfo)
const newCheckedSelection = this._sanityCheckSelection(newTextInfo.selection, newTextInfo.text)
checkTextInfo(newTextInfo)
this.setNativeProps({text: newTextInfo.text})
// This is to workaround this Issue for Android: https://github.com/imnapo/react-native-cn-richtext-editor/issues/81
// By setting the text and selection at the same time
this.setNativeProps({
selection: newCheckedSelection,
text: newTextInfo.text,
})
this._lastNativeText = newTextInfo.text
this._setSelection(newTextInfo.selection)
this._lastNativeSelection = newCheckedSelection
if (reflectChange) {
this._onChangeText(newTextInfo.text)
}
Expand All @@ -87,14 +93,17 @@ class PlainInput extends Component<InternalProps> {
this._setSelection(s)
}

// Validate that this selection makes sense with current value
_sanityCheckSelection = (selection: Selection, nativeText: string): Selection => {
let {start, end} = selection
end = Math.max(0, Math.min(end || 0, nativeText.length))
start = Math.min(start || 0, end)
return {end, start}
}

_setSelection = (selection: Selection) => {
this._setTimeout(() => {
// Validate that this selection makes sense with current value
let {start, end} = selection
const text = this._lastNativeText || '' // TODO write a good internal getValue fcn for this
end = Math.max(0, Math.min(end || 0, text.length))
start = Math.min(start || 0, end)
const newSelection = {end, start}
const newSelection = this._sanityCheckSelection(selection, this._lastNativeText || '')
this.setNativeProps({selection: newSelection})
this._lastNativeSelection = selection
}, 0)
Expand Down

0 comments on commit 26ad695

Please sign in to comment.