diff --git a/apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt b/apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt index 3010b5240997c..163bde2f9702e 100644 --- a/apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt +++ b/apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt @@ -95,10 +95,14 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView( ), ) emitContentSizeIfNeeded() + editor.keepSelectionVisible() } }, ) - editor.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> emitContentSizeIfNeeded() } + editor.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> + emitContentSizeIfNeeded() + editor.keepSelectionVisible() + } addView( editor, LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT), @@ -214,6 +218,10 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView( fun setScrollEnabled(scrollEnabled: Boolean) { editor.isVerticalScrollBarEnabled = scrollEnabled + editor.scrollEnabled = scrollEnabled + if (scrollEnabled) { + editor.keepSelectionVisible() + } } fun setAutoFocus(autoFocus: Boolean) { @@ -256,6 +264,7 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView( // state, so a no-op assignment must be skipped. if (editor.selectionStart == safeStart && editor.selectionEnd == safeEnd) return editor.setSelection(safeStart, safeEnd) + editor.keepSelectionVisible() } private fun updateInputFlags() { @@ -457,10 +466,47 @@ private fun parseTokens(value: String): List = try { private class SelectionAwareEditText(context: Context) : EditText(context) { var selectionListener: ((Int, Int) -> Unit)? = null var pasteImagesListener: ((List) -> Unit)? = null + var scrollEnabled = true + private val keepSelectionVisibleRunnable = Runnable { + if (scrollEnabled && isFocused) { + val textLayout = layout ?: return@Runnable + val offset = selectionEnd.coerceIn(0, text?.length ?: 0) + val line = textLayout.getLineForOffset(offset) + val viewportHeight = height - compoundPaddingTop - compoundPaddingBottom + if (viewportHeight <= 0) return@Runnable + + val lineTop = textLayout.getLineTop(line) + val lineBottom = textLayout.getLineBottom(line) + val targetScrollY = when { + lineBottom > scrollY + viewportHeight -> lineBottom - viewportHeight + lineTop < scrollY -> lineTop + else -> scrollY + }.coerceIn(0, (textLayout.height - viewportHeight).coerceAtLeast(0)) + + if (targetScrollY != scrollY) { + scrollTo(scrollX, targetScrollY) + } + } + } + + override fun scrollTo(x: Int, y: Int) { + if (scrollEnabled) { + super.scrollTo(x, y) + } + } + + fun keepSelectionVisible() { + if (!scrollEnabled) return + removeCallbacks(keepSelectionVisibleRunnable) + post(keepSelectionVisibleRunnable) + } override fun onSelectionChanged(selStart: Int, selEnd: Int) { super.onSelectionChanged(selStart, selEnd) selectionListener?.invoke(selStart, selEnd) + if (isAttachedToWindow) { + keepSelectionVisible() + } } override fun onTextContextMenuItem(id: Int): Boolean {