mirror of
https://github.com/divkit/divkit.git
synced 2026-05-07 20:02:32 +00:00
Support set_cursor_position action
commit_hash:ceac23ba85c9d1528e011df0b0b85e2ba302c054
This commit is contained in:
@@ -1178,6 +1178,7 @@
|
||||
"client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedHandlerProxy.kt":"divkit/public/client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedHandlerProxy.kt",
|
||||
"client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedHideTooltipHandler.kt":"divkit/public/client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedHideTooltipHandler.kt",
|
||||
"client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedScrollHandler.kt":"divkit/public/client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedScrollHandler.kt",
|
||||
"client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedSetCursorPositionHandler.kt":"divkit/public/client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedSetCursorPositionHandler.kt",
|
||||
"client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedSetStateHandler.kt":"divkit/public/client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedSetStateHandler.kt",
|
||||
"client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedSetStoredValueHandler.kt":"divkit/public/client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedSetStoredValueHandler.kt",
|
||||
"client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedSetVariableHandler.kt":"divkit/public/client/android/div/src/main/java/com/yandex/div/core/actions/DivActionTypedSetVariableHandler.kt",
|
||||
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.yandex.div.core.actions
|
||||
|
||||
import com.yandex.div.core.util.toIntSafely
|
||||
import com.yandex.div.core.view2.Div2View
|
||||
import com.yandex.div.core.view2.ViewLocator
|
||||
import com.yandex.div.core.view2.divs.widgets.DivInputView
|
||||
import com.yandex.div.json.expressions.ExpressionResolver
|
||||
import com.yandex.div2.DivActionSetCursorPosition
|
||||
import com.yandex.div2.DivActionTyped
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
internal class DivActionTypedSetCursorPositionHandler @Inject constructor() : DivActionTypedHandler {
|
||||
|
||||
override fun handleAction(
|
||||
scopeId: String?,
|
||||
action: DivActionTyped,
|
||||
view: Div2View,
|
||||
resolver: ExpressionResolver
|
||||
): Boolean {
|
||||
return when (action) {
|
||||
is DivActionTyped.SetCursorPosition -> {
|
||||
handleSetCursorPosition(action.value, view, resolver)
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleSetCursorPosition(
|
||||
action: DivActionSetCursorPosition,
|
||||
view: Div2View,
|
||||
resolver: ExpressionResolver
|
||||
) {
|
||||
val id = action.id.evaluate(resolver)
|
||||
val targets = ViewLocator.findViewsWithTag(view, id)
|
||||
.filterIsInstance<DivInputView>()
|
||||
|
||||
val target = when {
|
||||
targets.isEmpty() -> return view.logError("No input view with id '$id'")
|
||||
targets.size > 1 -> return view.logError("Found multiple input views with id '$id'")
|
||||
else -> targets.first()
|
||||
}
|
||||
|
||||
val textLength = target.length()
|
||||
val start = action.position.start.evaluate(resolver).let {
|
||||
it.normalizePosition(textLength)
|
||||
?: return view.logError("Wrong start value $it")
|
||||
}
|
||||
|
||||
val end = action.position.end?.evaluate(resolver)?.let {
|
||||
it.normalizePosition(textLength)
|
||||
?: return view.logError("Wrong end value $it")
|
||||
} ?: start
|
||||
|
||||
target.setCursorPosition(start, end)
|
||||
}
|
||||
|
||||
private fun Long.normalizePosition(textLength: Int): Int? {
|
||||
return when (this) {
|
||||
in 0 .. textLength -> toIntSafely()
|
||||
-1L -> textLength
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private fun DivInputView.setCursorPosition(start: Int, end: Int) {
|
||||
if (!isFocused) {
|
||||
requestFocus()
|
||||
openKeyboard()
|
||||
}
|
||||
setSelection(start, end)
|
||||
}
|
||||
|
||||
private fun Div2View.logError(message: String) = logError(message.toActionError())
|
||||
|
||||
private fun String.toActionError() =
|
||||
RuntimeException("Failed to handle set_cursor_action", IllegalArgumentException(this))
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import com.yandex.div.core.actions.DivActionTypedFocusElementHandler
|
||||
import com.yandex.div.core.actions.DivActionTypedHandler
|
||||
import com.yandex.div.core.actions.DivActionTypedHideTooltipHandler
|
||||
import com.yandex.div.core.actions.DivActionTypedScrollHandler
|
||||
import com.yandex.div.core.actions.DivActionTypedSetCursorPositionHandler
|
||||
import com.yandex.div.core.actions.DivActionTypedSetStateHandler
|
||||
import com.yandex.div.core.actions.DivActionTypedSetStoredValueHandler
|
||||
import com.yandex.div.core.actions.DivActionTypedSetVariableHandler
|
||||
@@ -120,4 +121,10 @@ internal interface DivActionTypedModule {
|
||||
fun provideUpdateStructureHandler(
|
||||
impl: DivActionTypedUpdateStructureHandler
|
||||
): DivActionTypedHandler
|
||||
|
||||
@Binds
|
||||
@IntoSet
|
||||
fun provideSetCursorPositionActionHandler(
|
||||
impl: DivActionTypedSetCursorPositionHandler
|
||||
): DivActionTypedHandler
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
}
|
||||
},
|
||||
"platforms": [
|
||||
"android",
|
||||
"web"
|
||||
],
|
||||
"required": [
|
||||
|
||||
@@ -4102,6 +4102,7 @@
|
||||
{
|
||||
"title": "Input with set_cursor_position actions",
|
||||
"platforms": [
|
||||
"android",
|
||||
"web"
|
||||
],
|
||||
"tags": [
|
||||
|
||||
Reference in New Issue
Block a user