Skip to content

Commit 2be4466

Browse files
committed
fix(mobile): fold keyboard pastes that begin with composing text
1 parent 3cb547f commit 2be4466

2 files changed

Lines changed: 52 additions & 13 deletions

File tree

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -691,18 +691,28 @@ internal class SelectionAwareEditText(context: Context) : EditText(context) {
691691
}
692692
}
693693

694-
// Keyboards insert clipboard text, such as Gboard's clipboard strip, with one commitText,
695-
// which replaces any composing text. A single key or a commit that continues the composing
696-
// word is typing, which stays native even at the input limit.
694+
// IME clipboard inserts replace the composing range, just like native commitText.
697695
private fun interceptKeyboardPaste(text: CharSequence?): Boolean {
698696
if (readOnly || textPasteThresholdBytes <= 0 || text == null) return false
697+
val value = text.toString()
699698
val composingStart = BaseInputConnection.getComposingSpanStart(editableText)
700699
val composingEnd = BaseInputConnection.getComposingSpanEnd(editableText)
701700
return when {
702701
Character.codePointCount(text, 0, text.length) < 2 -> false
703-
composingStart < 0 -> interceptPaste(text.toString())
704-
text.startsWith(editableText.subSequence(composingStart, composingEnd)) -> false
705-
else -> interceptPaste(text.toString(), start = composingStart, end = composingEnd)
702+
composingStart < 0 -> interceptPaste(value)
703+
else -> {
704+
val continuesWord = value.startsWith(editableText.subSequence(composingStart, composingEnd))
705+
val exceedsInputLimit =
706+
length().toLong() - (composingEnd - composingStart) + value.length > maxInputChars
707+
val keepTyping = if (continuesWord && exceedsInputLimit && !shouldFoldText(value)) {
708+
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
709+
// commitText has no paste flag; inspect the clipboard only for ambiguous overflow commits.
710+
clipboard?.primaryClip?.plainText() != value
711+
} else {
712+
false
713+
}
714+
!keepTyping && interceptPaste(value, start = composingStart, end = composingEnd)
715+
}
706716
}
707717
}
708718

@@ -781,10 +791,7 @@ internal class SelectionAwareEditText(context: Context) : EditText(context) {
781791
end: Int = maxOf(selectionStart, selectionEnd).coerceIn(start, length())
782792
): Boolean {
783793
val exceedsInputLimit = length().toLong() - (end - start) + text.length > maxInputChars
784-
val shouldFold = foldLargeText && (
785-
text.length >= textPasteThresholdBytes ||
786-
text.toByteArray(Charsets.UTF_8).size >= textPasteThresholdBytes
787-
)
794+
val shouldFold = foldLargeText && shouldFoldText(text)
788795
val shouldIntercept = exceedsInputLimit || shouldFold
789796
if (shouldIntercept) {
790797
pasteTextListener?.invoke(text, start, end)
@@ -793,6 +800,10 @@ internal class SelectionAwareEditText(context: Context) : EditText(context) {
793800
return shouldIntercept
794801
}
795802

803+
private fun shouldFoldText(text: String): Boolean =
804+
text.length >= textPasteThresholdBytes ||
805+
text.toByteArray(Charsets.UTF_8).size >= textPasteThresholdBytes
806+
796807
// Match native paste's newlines, including empty items.
797808
// URI-backed clips stay native: coerceToText can read a provider synchronously.
798809
private fun ClipData.plainText(): String? {

‎apps/mobile/modules/t3-composer-editor/android/src/test/java/expo/modules/t3composereditor/ComposerPasteTest.kt‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,50 @@ class ComposerPasteTest {
116116

117117
@Test
118118
fun keyboardClipboardInsertWhileComposingReplacesTheComposingWord() {
119-
val pasted = "é".repeat(16 * 1024)
119+
val pasted = "wor" + "é".repeat(16 * 1024)
120120
editor.setText("before ")
121121
editor.setSelection(editor.length())
122122
var intercepted: Triple<String, Int, Int>? = null
123123
editor.pasteTextListener = { text, start, end -> intercepted = Triple(text, start, end) }
124124
val connection = editor.onCreateInputConnection(EditorInfo())!!
125125

126+
for (withTextAttribute in listOf(false, true)) {
127+
editor.setText("before ")
128+
editor.setSelection(editor.length())
129+
intercepted = null
130+
assertTrue(connection.setComposingText("wor", 1))
131+
val committed = if (withTextAttribute) {
132+
connection.commitText(pasted, 1, null)
133+
} else {
134+
connection.commitText(pasted, 1)
135+
}
136+
assertTrue(committed)
137+
assertEquals(Triple(pasted, 7, 10), intercepted)
138+
assertEquals("before wor", editor.text.toString())
139+
assertEquals(-1, BaseInputConnection.getComposingSpanStart(editor.text))
140+
}
141+
}
142+
143+
@Test
144+
fun keyboardClipboardPrefixInterceptsInputLimitOverflow() {
145+
val pasted = "word from clipboard"
146+
clipboard.setPrimaryClip(ClipData.newPlainText("test", pasted))
147+
editor.setText("x".repeat(119_997))
148+
editor.setSelection(editor.length())
149+
var intercepted: Triple<String, Int, Int>? = null
150+
editor.pasteTextListener = { text, start, end -> intercepted = Triple(text, start, end) }
151+
val connection = editor.onCreateInputConnection(EditorInfo())!!
152+
126153
assertTrue(connection.setComposingText("wor", 1))
127154
assertTrue(connection.commitText(pasted, 1))
128-
assertEquals(Triple(pasted, 7, 10), intercepted)
129-
assertEquals("before wor", editor.text.toString())
155+
assertEquals(Triple(pasted, 119_997, 120_000), intercepted)
156+
assertEquals(120_000, editor.length())
130157
assertEquals(-1, BaseInputConnection.getComposingSpanStart(editor.text))
131158
}
132159

133160
@Test
134161
fun keyboardCommitsAtTheInputLimitInterceptOnlyClipboardInserts() {
162+
clipboard.setPrimaryClip(ClipData.newPlainText("test", "unrelated clipboard"))
135163
val intercepted = mutableListOf<String>()
136164
editor.pasteTextListener = { text, _, _ -> intercepted.add(text) }
137165
editor.setText("x".repeat(120_000))

0 commit comments

Comments
 (0)