-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathReactNativeRichTextEditorViewManager.kt
More file actions
285 lines (231 loc) · 10.1 KB
/
Copy pathReactNativeRichTextEditorViewManager.kt
File metadata and controls
285 lines (231 loc) · 10.1 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.swmansion.reactnativerichtexteditor
import android.content.Context
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.uimanager.ReactStylesDiffMap
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewDefaults
import com.facebook.react.uimanager.ViewManagerDelegate
import com.facebook.react.uimanager.ViewProps
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.ReactNativeRichTextEditorViewManagerInterface
import com.facebook.react.viewmanagers.ReactNativeRichTextEditorViewManagerDelegate
import com.facebook.yoga.YogaMeasureMode
import com.facebook.yoga.YogaMeasureOutput
import com.swmansion.reactnativerichtexteditor.events.OnInputBlurEvent
import com.swmansion.reactnativerichtexteditor.events.OnChangeHtmlEvent
import com.swmansion.reactnativerichtexteditor.events.OnChangeSelectionEvent
import com.swmansion.reactnativerichtexteditor.events.OnChangeStateEvent
import com.swmansion.reactnativerichtexteditor.events.OnChangeTextEvent
import com.swmansion.reactnativerichtexteditor.events.OnInputFocusEvent
import com.swmansion.reactnativerichtexteditor.events.OnLinkDetectedEvent
import com.swmansion.reactnativerichtexteditor.events.OnMentionDetectedEvent
import com.swmansion.reactnativerichtexteditor.events.OnMentionEvent
import com.swmansion.reactnativerichtexteditor.spans.EditorSpans
import com.swmansion.reactnativerichtexteditor.styles.RichTextStyle
import com.swmansion.reactnativerichtexteditor.utils.jsonStringToStringMap
@ReactModule(name = ReactNativeRichTextEditorViewManager.NAME)
class ReactNativeRichTextEditorViewManager : SimpleViewManager<ReactNativeRichTextEditorView>(),
ReactNativeRichTextEditorViewManagerInterface<ReactNativeRichTextEditorView> {
private val mDelegate: ViewManagerDelegate<ReactNativeRichTextEditorView>
private var view: ReactNativeRichTextEditorView? = null
init {
mDelegate = ReactNativeRichTextEditorViewManagerDelegate(this)
}
override fun getDelegate(): ViewManagerDelegate<ReactNativeRichTextEditorView>? {
return mDelegate
}
override fun getName(): String {
return NAME
}
public override fun createViewInstance(context: ThemedReactContext): ReactNativeRichTextEditorView {
val view = ReactNativeRichTextEditorView(context)
this.view = view
return view
}
override fun updateState(
view: ReactNativeRichTextEditorView,
props: ReactStylesDiffMap?,
stateWrapper: StateWrapper?
): Any? {
view.stateWrapper = stateWrapper
return super.updateState(view, props, stateWrapper)
}
override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any> {
val map = mutableMapOf<String, Any>()
map.put(OnInputFocusEvent.EVENT_NAME, mapOf("registrationName" to OnInputFocusEvent.EVENT_NAME))
map.put(OnInputBlurEvent.EVENT_NAME, mapOf("registrationName" to OnInputBlurEvent.EVENT_NAME))
map.put(OnChangeTextEvent.EVENT_NAME, mapOf("registrationName" to OnChangeTextEvent.EVENT_NAME))
map.put(OnChangeHtmlEvent.EVENT_NAME, mapOf("registrationName" to OnChangeHtmlEvent.EVENT_NAME))
map.put(OnChangeStateEvent.EVENT_NAME, mapOf("registrationName" to OnChangeStateEvent.EVENT_NAME))
map.put(OnLinkDetectedEvent.EVENT_NAME, mapOf("registrationName" to OnLinkDetectedEvent.EVENT_NAME))
map.put(OnMentionDetectedEvent.EVENT_NAME, mapOf("registrationName" to OnMentionDetectedEvent.EVENT_NAME))
map.put(OnMentionEvent.EVENT_NAME, mapOf("registrationName" to OnMentionEvent.EVENT_NAME))
map.put(OnChangeSelectionEvent.EVENT_NAME, mapOf("registrationName" to OnChangeSelectionEvent.EVENT_NAME))
return map
}
@ReactProp(name = "defaultValue")
override fun setDefaultValue(view: ReactNativeRichTextEditorView?, value: String?) {
view?.setValue(value)
}
@ReactProp(name = "placeholder")
override fun setPlaceholder(view: ReactNativeRichTextEditorView?, value: String?) {
view?.setPlaceholder(value)
}
@ReactProp(name = "placeholderTextColor", customType = "Color")
override fun setPlaceholderTextColor(view: ReactNativeRichTextEditorView?, color: Int?) {
view?.setPlaceholderTextColor(color)
}
@ReactProp(name = "cursorColor", customType = "Color")
override fun setCursorColor(view: ReactNativeRichTextEditorView?, color: Int?) {
view?.setCursorColor(color)
}
@ReactProp(name = "selectionColor", customType = "Color")
override fun setSelectionColor(view: ReactNativeRichTextEditorView?, color: Int?) {
view?.setSelectionColor(color)
}
@ReactProp(name = "autoFocus", defaultBoolean = false)
override fun setAutoFocus(view: ReactNativeRichTextEditorView?, autoFocus: Boolean) {
view?.setAutoFocus(autoFocus)
}
@ReactProp(name = "editable", defaultBoolean = true)
override fun setEditable(view: ReactNativeRichTextEditorView?, editable: Boolean) {
view?.isEnabled = editable
}
@ReactProp(name = "mentionIndicators")
override fun setMentionIndicators(view: ReactNativeRichTextEditorView?, indicators: ReadableArray?) {
if (indicators == null) return
val indicatorsList = mutableListOf<String>()
for (i in 0 until indicators.size()) {
val stringValue = indicators.getString(i) ?: continue
indicatorsList.add(stringValue)
}
val indicatorsArray = indicatorsList.toTypedArray()
view?.parametrizedStyles?.mentionIndicators = indicatorsArray
}
@ReactProp(name = "richTextStyle")
override fun setRichTextStyle(view: ReactNativeRichTextEditorView?, style: ReadableMap?) {
view?.richTextStyle = RichTextStyle(view, style)
}
@ReactProp(name = ViewProps.COLOR, customType = "Color")
override fun setColor(view: ReactNativeRichTextEditorView?, color: Int?) {
view?.setColor(color)
}
@ReactProp(name = "fontSize", defaultFloat = ViewDefaults.FONT_SIZE_SP)
override fun setFontSize(view: ReactNativeRichTextEditorView?, size: Float) {
view?.setFontSize(size)
}
@ReactProp(name = "fontFamily")
override fun setFontFamily(view: ReactNativeRichTextEditorView?, family: String?) {
view?.setFontFamily(family)
}
@ReactProp(name = "fontWeight")
override fun setFontWeight(view: ReactNativeRichTextEditorView?, weight: String?) {
view?.setFontWeight(weight)
}
@ReactProp(name = "fontStyle")
override fun setFontStyle(view: ReactNativeRichTextEditorView?, style: String?) {
view?.setFontStyle(style)
}
override fun onAfterUpdateTransaction(view: ReactNativeRichTextEditorView) {
super.onAfterUpdateTransaction(view)
view.updateTypeface()
}
override fun setPadding(
view: ReactNativeRichTextEditorView?,
left: Int,
top: Int,
right: Int,
bottom: Int
) {
super.setPadding(view, left, top, right, bottom)
view?.setPadding(left, top, right, bottom)
}
override fun setIsOnChangeHtmlSet(view: ReactNativeRichTextEditorView?, value: Boolean) {
// this prop isn't used on Android as of now, but the setter must be present
}
override fun setAutoCapitalize(view: ReactNativeRichTextEditorView?, flag: String?) {
view?.setAutoCapitalize(flag)
}
override fun focus(view: ReactNativeRichTextEditorView?) {
view?.requestFocusProgrammatically()
}
override fun blur(view: ReactNativeRichTextEditorView?) {
view?.clearFocus()
}
override fun setValue(view: ReactNativeRichTextEditorView?, text: String) {
view?.setValue(text)
}
override fun toggleBold(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.BOLD)
}
override fun toggleItalic(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.ITALIC)
}
override fun toggleUnderline(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.UNDERLINE)
}
override fun toggleStrikeThrough(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.STRIKETHROUGH)
}
override fun toggleInlineCode(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.INLINE_CODE)
}
override fun toggleH1(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.H1)
}
override fun toggleH2(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.H2)
}
override fun toggleH3(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.H3)
}
override fun toggleCodeBlock(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.CODE_BLOCK)
}
override fun toggleBlockQuote(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.BLOCK_QUOTE)
}
override fun toggleOrderedList(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.ORDERED_LIST)
}
override fun toggleUnorderedList(view: ReactNativeRichTextEditorView?) {
view?.verifyAndToggleStyle(EditorSpans.UNORDERED_LIST)
}
override fun addLink(view: ReactNativeRichTextEditorView?, start: Int, end: Int, text: String, url: String) {
view?.addLink(start, end, text, url)
}
override fun addImage(view: ReactNativeRichTextEditorView?, src: String) {
view?.addImage(src)
}
override fun startMention(view: ReactNativeRichTextEditorView?, indicator: String) {
view?.startMention(indicator)
}
override fun addMention(view: ReactNativeRichTextEditorView?, indicator: String, text: String, payload: String) {
val attributes = jsonStringToStringMap(payload)
view?.addMention(text, indicator, attributes)
}
override fun measure(
context: Context,
localData: ReadableMap?,
props: ReadableMap?,
state: ReadableMap?,
width: Float,
widthMode: YogaMeasureMode?,
height: Float,
heightMode: YogaMeasureMode?,
attachmentsPositions: FloatArray?
): Long {
val size = this.view?.layoutManager?.getMeasuredSize(width)
if (size != null) {
return YogaMeasureOutput.make(size.first, size.second)
}
return YogaMeasureOutput.make(0, 0)
}
companion object {
const val NAME = "ReactNativeRichTextEditorView"
}
}