-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnrichedMarkdownTextManager.kt
More file actions
211 lines (181 loc) · 6.57 KB
/
EnrichedMarkdownTextManager.kt
File metadata and controls
211 lines (181 loc) · 6.57 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
package com.swmansion.enriched.markdown
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.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewManagerDelegate
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.EnrichedMarkdownTextManagerDelegate
import com.facebook.react.viewmanagers.EnrichedMarkdownTextManagerInterface
import com.facebook.yoga.YogaMeasureMode
import com.swmansion.enriched.markdown.spoiler.SpoilerOverlay
import com.swmansion.enriched.markdown.utils.common.emitContextMenuItemPress
import com.swmansion.enriched.markdown.utils.common.emitLinkLongPress
import com.swmansion.enriched.markdown.utils.common.emitLinkPress
import com.swmansion.enriched.markdown.utils.common.emitTaskListItemPress
import com.swmansion.enriched.markdown.utils.common.markdownEventTypeConstants
import com.swmansion.enriched.markdown.utils.common.parseContextMenuItems
import com.swmansion.enriched.markdown.utils.common.parseMd4cFlags
import com.swmansion.enriched.markdown.utils.text.interaction.TaskListTapUtils
import com.swmansion.enriched.markdown.utils.text.interaction.TaskListToggleUtils
@ReactModule(name = EnrichedMarkdownTextManager.NAME)
class EnrichedMarkdownTextManager :
SimpleViewManager<EnrichedMarkdownText>(),
EnrichedMarkdownTextManagerInterface<EnrichedMarkdownText> {
private val mDelegate: ViewManagerDelegate<EnrichedMarkdownText> = EnrichedMarkdownTextManagerDelegate(this)
override fun getDelegate(): ViewManagerDelegate<EnrichedMarkdownText>? = mDelegate
override fun getName(): String = NAME
override fun createViewInstance(reactContext: ThemedReactContext): EnrichedMarkdownText {
val view = EnrichedMarkdownText(reactContext)
view.onContextMenuItemPressCallback = { itemText, selectedText, selectionStart, selectionEnd ->
emitContextMenuItemPress(view, itemText, selectedText, selectionStart, selectionEnd)
}
return view
}
override fun onDropViewInstance(view: EnrichedMarkdownText) {
super.onDropViewInstance(view)
MeasurementStore.clearFontScalingSettings(view.id)
view.layoutManager.releaseMeasurementStore()
view.clearActiveImageSpans()
}
override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any> = markdownEventTypeConstants()
@ReactProp(name = "markdown")
override fun setMarkdown(
view: EnrichedMarkdownText?,
markdown: String?,
) {
view?.setOnLinkPressCallback { url ->
emitLinkPress(view, url)
}
view?.setOnLinkLongPressCallback { url ->
emitLinkLongPress(view, url)
}
view?.setOnTaskListItemPressCallback { taskIndex, checked, itemText ->
val newChecked = !checked
val styleConfig = view.markdownStyle
val optimizedSuccess =
styleConfig != null && TaskListTapUtils.updateTaskListItemCheckedState(view, taskIndex, newChecked, styleConfig)
if (optimizedSuccess) {
emitTaskListItemPress(view, taskIndex, newChecked, itemText)
return@setOnTaskListItemPressCallback
}
val currentMarkdown = view.currentMarkdown
val updatedMarkdown = TaskListToggleUtils.toggleAtIndex(currentMarkdown, taskIndex, newChecked)
view.setMarkdownContent(updatedMarkdown)
emitTaskListItemPress(view, taskIndex, newChecked, itemText)
}
view?.setMarkdownContent(markdown ?: "No markdown content")
}
@ReactProp(name = "markdownStyle")
override fun setMarkdownStyle(
view: EnrichedMarkdownText?,
style: ReadableMap?,
) {
view?.setMarkdownStyle(style)
}
@ReactProp(name = "selectable", defaultBoolean = true)
override fun setSelectable(
view: EnrichedMarkdownText?,
selectable: Boolean,
) {
view?.setIsSelectable(selectable)
}
override fun setSelectionColor(
view: EnrichedMarkdownText?,
value: Int?,
) {
view?.setSelectionColor(value)
}
override fun setSelectionHandleColor(
view: EnrichedMarkdownText?,
value: Int?,
) {
view?.setSelectionHandleColor(value)
}
@ReactProp(name = "md4cFlags")
override fun setMd4cFlags(
view: EnrichedMarkdownText?,
flags: ReadableMap?,
) {
view?.setMd4cFlags(parseMd4cFlags(flags))
}
@ReactProp(name = "allowFontScaling", defaultBoolean = true)
override fun setAllowFontScaling(
view: EnrichedMarkdownText?,
allowFontScaling: Boolean,
) {
view?.setAllowFontScaling(allowFontScaling)
}
@ReactProp(name = "maxFontSizeMultiplier", defaultFloat = 0f)
override fun setMaxFontSizeMultiplier(
view: EnrichedMarkdownText?,
maxFontSizeMultiplier: Float,
) {
view?.setMaxFontSizeMultiplier(maxFontSizeMultiplier)
}
@ReactProp(name = "allowTrailingMargin", defaultBoolean = false)
override fun setAllowTrailingMargin(
view: EnrichedMarkdownText?,
allowTrailingMargin: Boolean,
) {
view?.setAllowTrailingMargin(allowTrailingMargin)
}
@ReactProp(name = "enableLinkPreview", defaultBoolean = true)
override fun setEnableLinkPreview(
view: EnrichedMarkdownText?,
enableLinkPreview: Boolean,
) {
// No-op on Android — only used on iOS
}
@ReactProp(name = "streamingAnimation", defaultBoolean = false)
override fun setStreamingAnimation(
view: EnrichedMarkdownText?,
streamingAnimation: Boolean,
) {
view?.setStreamingAnimation(streamingAnimation)
}
@ReactProp(name = "spoilerOverlay")
override fun setSpoilerOverlay(
view: EnrichedMarkdownText?,
mode: String?,
) {
view?.spoilerOverlay = SpoilerOverlay.fromString(mode)
}
@ReactProp(name = "contextMenuItems")
override fun setContextMenuItems(
view: EnrichedMarkdownText?,
value: ReadableArray?,
) {
if (view == null) return
view.setContextMenuItems(parseContextMenuItems(value))
}
override fun setPadding(
view: EnrichedMarkdownText,
left: Int,
top: Int,
right: Int,
bottom: Int,
) {
super.setPadding(view, left, top, right, bottom)
view.setPadding(left, top, right, bottom)
}
override fun measure(
context: Context,
localData: ReadableMap?,
props: ReadableMap?,
state: ReadableMap?,
width: Float,
widthMode: YogaMeasureMode?,
height: Float,
heightMode: YogaMeasureMode?,
attachmentsPositions: FloatArray?,
): Long {
val id = localData?.getInt("viewTag")
return MeasurementStore.getMeasureById(context, id, width, height, heightMode, props)
}
companion object {
const val NAME = "EnrichedMarkdownText"
}
}