-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathHTMLElementEmojiSearchWidgetFactory.kt
More file actions
106 lines (91 loc) · 3.21 KB
/
Copy pathHTMLElementEmojiSearchWidgetFactory.kt
File metadata and controls
106 lines (91 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (C) 2022 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.redwood.emojisearch.browser
import app.cash.redwood.LayoutModifier
import com.example.redwood.emojisearch.widget.EmojiSearchWidgetFactory
import com.example.redwood.emojisearch.widget.Image
import com.example.redwood.emojisearch.widget.Text
import com.example.redwood.emojisearch.widget.TextInput
import example.values.TextFieldState
import org.w3c.dom.Document
import org.w3c.dom.HTMLElement
import org.w3c.dom.HTMLImageElement
import org.w3c.dom.HTMLInputElement
import org.w3c.dom.HTMLSpanElement
class HTMLElementEmojiSearchWidgetFactory(private val document: Document) : EmojiSearchWidgetFactory<HTMLElement> {
override fun TextInput(): TextInput<HTMLElement> = HtmlTextInput(document.createElement("input") as HTMLInputElement)
override fun Text(): Text<HTMLElement> = HtmlText(document.createElement("span") as HTMLSpanElement)
override fun Image(): Image<HTMLElement> = HtmlImage(document.createElement("img") as HTMLImageElement)
}
private class HtmlTextInput(
override val value: HTMLInputElement,
) : TextInput<HTMLElement> {
private var onChange: ((TextFieldState) -> Unit)? = null
private var state = TextFieldState()
private var updating = false
init {
value.oninput = { stateChanged() }
value.addEventListener("selectionchange", { stateChanged() })
}
private fun stateChanged() {
if (updating) return
val newState = state.userEdit(
text = value.value,
selectionStart = value.selectionStart ?: 0,
selectionEnd = value.selectionEnd ?: 0,
)
if (!state.contentEquals(newState)) {
state = newState
onChange?.invoke(newState)
}
}
override fun onChange(onChange: ((TextFieldState) -> Unit)?) {
this.onChange = onChange
}
override fun state(state: TextFieldState) {
if (state.userEditCount < this.state.userEditCount) return
if (!updating) {
updating = true
try {
value.value = state.text
value.selectionStart = state.selectionStart
value.selectionEnd = state.selectionEnd
} finally {
updating = false
}
}
}
override fun hint(hint: String) {
value.placeholder = hint
}
override var layoutModifiers: LayoutModifier = LayoutModifier
}
private class HtmlText(
override val value: HTMLSpanElement,
) : Text<HTMLElement> {
override var layoutModifiers: LayoutModifier = LayoutModifier
override fun text(text: String) {
value.textContent = text
}
}
private class HtmlImage(
override val value: HTMLImageElement,
) : Image<HTMLElement> {
override var layoutModifiers: LayoutModifier = LayoutModifier
override fun url(url: String) {
value.src = url
}
}