diff --git a/packages/react-native-aztec/android/src/main/kotlin/org/wordpress/mobile/ReactNativeAztec/EnterPressedWatcher.kt b/packages/react-native-aztec/android/src/main/kotlin/org/wordpress/mobile/ReactNativeAztec/EnterPressedWatcher.kt index cc68aac2e17fd..097ec027fdee6 100644 --- a/packages/react-native-aztec/android/src/main/kotlin/org/wordpress/mobile/ReactNativeAztec/EnterPressedWatcher.kt +++ b/packages/react-native-aztec/android/src/main/kotlin/org/wordpress/mobile/ReactNativeAztec/EnterPressedWatcher.kt @@ -24,6 +24,9 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter) private var selEnd: Int = 0 var done = false + private var gboardReplacement: CharSequence? = null + + override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) { val aztecText = aztecTextRef.get() if (aztecText?.getAztecKeyListener() != null && !aztecText.isTextChangedListenerDisabled()) { @@ -32,6 +35,13 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter) this.start = start this.selStart = aztecText.selectionStart this.selEnd = aztecText.selectionEnd + + if (selStart == selEnd && 0 < count && count < after) { + // possible gboard replacement detected + gboardReplacement = text.subSequence(start, start + count) + } else { + gboardReplacement = null + } } } @@ -40,14 +50,25 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter) val aztecKeyListener = aztecText?.getAztecKeyListener() if (aztecText != null && !aztecText.isTextChangedListenerDisabled() && aztecKeyListener != null) { val newTextCopy = SpannableStringBuilder(text) + + var gboardOffset = this.start + // If gboard replacement is happening, we offset the start position by the length + // of the gboard replacement + if (gboardReplacement != null) { + val gboardRestored = newTextCopy.subSequence(start, start + before) + if (gboardRestored.toString() == gboardReplacement.toString()) { + gboardOffset += gboardRestored.length + } + } + // if new text length is longer than original text by 1 if (textBefore?.length == newTextCopy.length - 1) { // now check that the inserted character is actually a NEWLINE - if (newTextCopy[this.start] == Constants.NEWLINE) { + if (newTextCopy[gboardOffset] == Constants.NEWLINE) { done = false aztecText.editableText.setSpan(EnterPressedUnderway(), 0, 0, Spanned.SPAN_USER) aztecKeyListener.onEnterKey( - newTextCopy.replace(this.start, this.start + 1, ""), + newTextCopy.replace(gboardOffset, gboardOffset + 1, ""), true, selStart, selEnd @@ -61,9 +82,12 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter) aztecTextRef.get()?.editableText?.getSpans(0, 0, EnterPressedUnderway::class.java)?.forEach { if (!done) { done = true - if (enterDeleter.shouldDeleteEnter()) - text.replace(start, start + 1, "") + if (enterDeleter.shouldDeleteEnter()) { + var gboardOffset = start + gboardReplacement?.let { replacement -> gboardOffset += replacement.length } + text.replace(gboardOffset, gboardOffset + 1, "") } + } aztecTextRef.get()?.editableText?.removeSpan(it) } }