-
Notifications
You must be signed in to change notification settings - Fork 121
[web] Refactor inner implementation of web input #2838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: jb-main
Are you sure you want to change the base?
Changes from 5 commits
edac355
5130e14
e93d1e6
3e5bd54
061208c
73e379d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,15 +26,16 @@ import kotlin.js.ExperimentalWasmJsInterop | |
| import kotlin.js.JsAny | ||
| import kotlin.js.JsName | ||
| import kotlin.js.definedExternally | ||
| import kotlin.js.js | ||
| import kotlin.js.unsafeCast | ||
| import kotlinx.browser.document | ||
| import kotlinx.browser.window | ||
| import org.w3c.dom.EventInit | ||
| import org.w3c.dom.HTMLElement | ||
| import org.w3c.dom.events.CompositionEvent | ||
| import org.w3c.dom.events.Event | ||
| import org.w3c.dom.events.InputEvent | ||
| import org.w3c.dom.events.KeyboardEvent | ||
| import org.w3c.dom.events.UIEvent | ||
|
|
||
| internal class DomInputStrategy( | ||
| imeOptions: ImeOptions, | ||
|
|
@@ -77,18 +78,19 @@ internal class DomInputStrategy( | |
| } | ||
| } | ||
|
|
||
| private val tabKeyCode = Key.Tab.keyCode.toInt() | ||
| private fun KeyboardEvent.isComposeOnlyEvent(): Boolean { | ||
| return when (keyCode.toLong()) { | ||
| // In text inputs we want tab to be controlled entirely by Compose, same about navigation via errors | ||
| Key.Tab.keyCode, Key.DirectionLeft.keyCode, Key.DirectionUp.keyCode, Key.DirectionRight.keyCode, Key.DirectionDown.keyCode -> true | ||
| else -> false | ||
| } | ||
| } | ||
|
|
||
| private fun initEvents() { | ||
| htmlInput.addEventListener("blur", { evt -> | ||
| // TODO: any actions here? | ||
| }) | ||
|
|
||
| htmlInput.addEventListener("keydown", { evt -> | ||
| nativeInputEventsProcessor.registerEvent(evt as KeyboardEvent) | ||
|
|
||
| if (evt.keyCode == tabKeyCode) { | ||
| // Compose logic will handle the focus movement or insert Tabs if necessary | ||
| if (evt.isComposeOnlyEvent()) { | ||
| evt.preventDefault() | ||
| } | ||
|
|
||
|
|
@@ -113,7 +115,11 @@ internal class DomInputStrategy( | |
| }) | ||
|
|
||
| htmlInput.addEventListener("compositionstart", { evt -> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated this logic - basically, we need to register this event but don't care about any handlers |
||
| nativeInputEventsProcessor.registerEvent(evt as CompositionEvent) | ||
| nativeInputEventsProcessor.updateImeStatus(evt as CompositionEvent) | ||
| }) | ||
|
|
||
| htmlInput.addEventListener("compositionupdate", { evt -> | ||
| nativeInputEventsProcessor.updateImeStatus(evt as CompositionEvent) | ||
| }) | ||
|
|
||
| htmlInput.addEventListener("compositionend", { evt -> | ||
|
|
@@ -159,16 +165,16 @@ private external interface DocumentOrShadowRootLike : JsAny { | |
| } | ||
|
|
||
| @JsName("InputEvent") | ||
| internal external class InputEventExt : JsAny { | ||
| internal external class InputEventExt : UIEvent { | ||
| val data: String? | ||
| val inputType: String | ||
| var textRangeStart: Int | ||
| var textRangeEnd: Int | ||
| } | ||
|
|
||
| internal val InputEvent.textRangeSize: Int | ||
| get() = this.asInputEventExt().let { it.textRangeEnd - it.textRangeStart } | ||
| constructor(type: String, eventInitDict: EventInit = definedExternally) | ||
| } | ||
|
|
||
| internal expect inline fun InputEvent.asInputEventExt(): InputEventExt | ||
| internal inline fun UIEvent.asInputEventExt(): InputEventExt = unsafeCast<InputEventExt>() | ||
|
|
||
| private fun ImeOptions.createDomElement(): HTMLElement { | ||
| val htmlElement = document.createElement( | ||
|
|
@@ -250,13 +256,11 @@ private fun ImeOptions.createDomElement(): HTMLElement { | |
| return htmlElement | ||
| } | ||
|
|
||
| // HTMLTextAreaElement and HTMLInputElement do not intersect in Kotlin definition, so we introduce helper interface | ||
| private external interface HTMLElementWithValue { | ||
| var value: String | ||
| val selectionStart: Int | ||
| val selectionEnd: Int | ||
| val selectionDirection: String? | ||
| fun setSelectionRange(start: Int, end: Int, direction: String = definedExternally) | ||
| } | ||
|
|
||
| internal fun isTypedEvent(evt: KeyboardEvent): Boolean = | ||
| js("!evt.metaKey && !evt.ctrlKey && evt.key.charAt(0) === evt.key") | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So now we have 3 different declarations.
We can't avoid implementing the
expect/actualand we can't avoid extracting thejscalls into a separate function.But do we need
private val KeyboardEvent.isTypedEvent: Boolean?I think 2 declarations would be enough. WDYT?