@chaitrabhairappa/react-native-rich-text-editor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,236 @@
1
+ package com.richtext.editor
2
+
3
+ import com.facebook.react.bridge.ReadableArray
4
+ import com.facebook.react.common.MapBuilder
5
+ import com.facebook.react.uimanager.SimpleViewManager
6
+ import com.facebook.react.uimanager.ThemedReactContext
7
+ import com.facebook.react.uimanager.annotations.ReactProp
8
+
9
+ class RichTextEditorViewManager : SimpleViewManager<RichTextEditorView>() {
10
+
11
+ override fun getName(): String = "RichTextEditorView"
12
+
13
+ override fun createViewInstance(reactContext: ThemedReactContext): RichTextEditorView {
14
+ return RichTextEditorView(reactContext)
15
+ }
16
+
17
+ @ReactProp(name = "placeholder")
18
+ fun setPlaceholder(view: RichTextEditorView, placeholder: String?) {
19
+ try {
20
+ view.setPlaceholderText(placeholder ?: "")
21
+ } catch (e: Exception) {
22
+ e.printStackTrace()
23
+ }
24
+ }
25
+
26
+ @ReactProp(name = "editable")
27
+ fun setEditable(view: RichTextEditorView, editable: Boolean) {
28
+ try {
29
+ view.setEditable(editable)
30
+ } catch (e: Exception) {
31
+ e.printStackTrace()
32
+ }
33
+ }
34
+
35
+ @ReactProp(name = "maxHeight")
36
+ fun setMaxHeight(view: RichTextEditorView, maxHeight: Int) {
37
+ try {
38
+ view.setMaxHeightValue(maxHeight)
39
+ } catch (e: Exception) {
40
+ e.printStackTrace()
41
+ }
42
+ }
43
+
44
+ @ReactProp(name = "showToolbar")
45
+ fun setShowToolbar(view: RichTextEditorView, showToolbar: Boolean) {
46
+ try {
47
+ view.setShowToolbar(showToolbar)
48
+ } catch (e: Exception) {
49
+ e.printStackTrace()
50
+ }
51
+ }
52
+
53
+ @ReactProp(name = "toolbarOptions")
54
+ fun setToolbarOptions(view: RichTextEditorView, toolbarOptions: ReadableArray?) {
55
+ try {
56
+ if (toolbarOptions != null) {
57
+ val options = mutableListOf<String>()
58
+ for (i in 0 until toolbarOptions.size()) {
59
+ toolbarOptions.getString(i)?.let { options.add(it) }
60
+ }
61
+ view.setToolbarOptions(options)
62
+ } else {
63
+ view.setToolbarOptions(null)
64
+ }
65
+ } catch (e: Exception) {
66
+ e.printStackTrace()
67
+ }
68
+ }
69
+
70
+ @ReactProp(name = "variant")
71
+ fun setVariant(view: RichTextEditorView, variant: String?) {
72
+ try {
73
+ view.setVariant(variant ?: "outlined")
74
+ } catch (e: Exception) {
75
+ e.printStackTrace()
76
+ }
77
+ }
78
+
79
+ @ReactProp(name = "initialContent")
80
+ fun setInitialContent(view: RichTextEditorView, initialContent: ReadableArray?) {
81
+ if (initialContent == null || initialContent.size() == 0) return
82
+
83
+ try {
84
+ val blocksList = mutableListOf<Map<String, Any>>()
85
+ for (i in 0 until initialContent.size()) {
86
+ val block = initialContent.getMap(i) ?: continue
87
+ val blockMap = mutableMapOf<String, Any>()
88
+ blockMap["text"] = block.getString("text") ?: ""
89
+ blockMap["type"] = block.getString("type") ?: "paragraph"
90
+
91
+ val stylesList = mutableListOf<Map<String, Any>>()
92
+ if (block.hasKey("styles")) {
93
+ val styles = block.getArray("styles")
94
+ if (styles != null) {
95
+ for (j in 0 until styles.size()) {
96
+ val style = styles.getMap(j) ?: continue
97
+ val styleMap = mutableMapOf<String, Any>()
98
+ styleMap["style"] = style.getString("style") ?: ""
99
+ styleMap["start"] = if (style.hasKey("start")) style.getInt("start") else 0
100
+ styleMap["end"] = if (style.hasKey("end")) style.getInt("end") else 0
101
+ stylesList.add(styleMap)
102
+ }
103
+ }
104
+ }
105
+ blockMap["styles"] = stylesList
106
+ blocksList.add(blockMap)
107
+ }
108
+ // Delay setting content until view is ready
109
+ view.post {
110
+ view.setContent(blocksList)
111
+ }
112
+ } catch (e: Exception) {
113
+ e.printStackTrace()
114
+ }
115
+ }
116
+
117
+ override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any>? {
118
+ return MapBuilder.builder<String, Any>()
119
+ .put("onContentChange", MapBuilder.of("registrationName", "onContentChange"))
120
+ .put("onSelectionChange", MapBuilder.of("registrationName", "onSelectionChange"))
121
+ .put("onEditorFocus", MapBuilder.of("registrationName", "onEditorFocus"))
122
+ .put("onEditorBlur", MapBuilder.of("registrationName", "onEditorBlur"))
123
+ .put("onSizeChange", MapBuilder.of("registrationName", "onSizeChange"))
124
+ .build()
125
+ }
126
+
127
+ override fun getCommandsMap(): Map<String, Int>? {
128
+ return MapBuilder.builder<String, Int>()
129
+ // Content management
130
+ .put("setContent", 1)
131
+ .put("getText", 2)
132
+ .put("getBlocks", 3)
133
+ .put("clear", 4)
134
+ // Focus management
135
+ .put("focus", 5)
136
+ .put("blur", 6)
137
+ // Text styles
138
+ .put("toggleBold", 10)
139
+ .put("toggleItalic", 11)
140
+ .put("toggleUnderline", 12)
141
+ .put("toggleStrikethrough", 13)
142
+ .put("toggleCode", 14)
143
+ .put("toggleHighlight", 15)
144
+ // Block types
145
+ .put("setHeading", 16)
146
+ .put("setBulletList", 17)
147
+ .put("setNumberedList", 18)
148
+ .put("setQuote", 19)
149
+ .put("setChecklist", 20)
150
+ .put("setParagraph", 21)
151
+ // Actions
152
+ .put("insertLink", 7)
153
+ .put("undo", 8)
154
+ .put("redo", 9)
155
+ .put("clearFormatting", 22)
156
+ // Indentation
157
+ .put("indent", 23)
158
+ .put("outdent", 24)
159
+ // Alignment
160
+ .put("setAlignment", 25)
161
+ // Checklist
162
+ .put("toggleChecklistItem", 26)
163
+ .build()
164
+ }
165
+
166
+ override fun receiveCommand(view: RichTextEditorView, commandId: Int, args: ReadableArray?) {
167
+ when (commandId) {
168
+ 1 -> {
169
+ val blocks = args?.getArray(0)
170
+ if (blocks != null) {
171
+ val blocksList = mutableListOf<Map<String, Any>>()
172
+ for (i in 0 until blocks.size()) {
173
+ val block = blocks.getMap(i)
174
+ val blockMap = mutableMapOf<String, Any>()
175
+ blockMap["text"] = block?.getString("text") ?: ""
176
+ blockMap["type"] = block?.getString("type") ?: "paragraph"
177
+
178
+ val styles = block?.getArray("styles")
179
+ if (styles != null) {
180
+ val stylesList = mutableListOf<Map<String, Any>>()
181
+ for (j in 0 until styles.size()) {
182
+ val style = styles.getMap(j)
183
+ val styleMap = mutableMapOf<String, Any>()
184
+ styleMap["style"] = style?.getString("style") ?: ""
185
+ styleMap["start"] = style?.getInt("start") ?: 0
186
+ styleMap["end"] = style?.getInt("end") ?: 0
187
+ stylesList.add(styleMap)
188
+ }
189
+ blockMap["styles"] = stylesList
190
+ }
191
+ blocksList.add(blockMap)
192
+ }
193
+ view.setContent(blocksList)
194
+ }
195
+ }
196
+ 4 -> view.clearContent()
197
+ 5 -> view.focusEditor()
198
+ 6 -> view.blurEditor()
199
+ 7 -> {
200
+ val url = args?.getString(0) ?: ""
201
+ val text = args?.getString(1) ?: ""
202
+ view.insertLink(url, text)
203
+ }
204
+ 8 -> view.undo()
205
+ 9 -> view.redo()
206
+ 10 -> view.toggleBold()
207
+ 11 -> view.toggleItalic()
208
+ 12 -> view.toggleUnderline()
209
+ 13 -> view.toggleStrikethrough()
210
+ 14 -> view.toggleCode()
211
+ 15 -> {
212
+ val color = args?.getString(0)
213
+ view.toggleHighlight(color)
214
+ }
215
+ 16 -> view.setHeading()
216
+ 17 -> view.toggleBulletList()
217
+ 18 -> view.toggleNumberedList()
218
+ 19 -> view.setQuote()
219
+ 20 -> view.setChecklist()
220
+ 21 -> view.setParagraph()
221
+ 22 -> view.clearFormatting()
222
+ 23 -> view.indent()
223
+ 24 -> view.outdent()
224
+ 25 -> {
225
+ val alignment = args?.getString(0) ?: "left"
226
+ val layoutAlignment = when (alignment) {
227
+ "center" -> android.text.Layout.Alignment.ALIGN_CENTER
228
+ "right" -> android.text.Layout.Alignment.ALIGN_OPPOSITE
229
+ else -> android.text.Layout.Alignment.ALIGN_NORMAL
230
+ }
231
+ view.setAlignment(layoutAlignment)
232
+ }
233
+ 26 -> view.toggleChecklistItem()
234
+ }
235
+ }
236
+ }