-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathHtmlStyle.kt
More file actions
359 lines (283 loc) · 12.2 KB
/
Copy pathHtmlStyle.kt
File metadata and controls
359 lines (283 loc) · 12.2 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package com.swmansion.enriched.textinput.styles
import android.graphics.Color
import com.facebook.react.bridge.ColorPropConverter
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.views.text.ReactTypefaceUtils.parseFontWeight
import com.swmansion.enriched.common.EnrichedConstants
import com.swmansion.enriched.common.EnrichedStyle
import com.swmansion.enriched.common.MentionStyle
import com.swmansion.enriched.common.pixelFromSpOrDp
import com.swmansion.enriched.textinput.EnrichedTextInputView
import kotlin.Float
import kotlin.Int
import kotlin.String
import kotlin.math.ceil
class HtmlStyle : EnrichedStyle {
private var style: ReadableMap? = null
private var view: EnrichedTextInputView? = null
// Default values are ignored as they are specified on the JS side.
// They are specified only because they are required by the constructor.
// JS passes them as a prop - so they are initialized after the constructor is called.
override var h1FontSize: Int = 72
override var h1Bold: Boolean = false
override var h2FontSize: Int = 64
override var h2Bold: Boolean = false
override var h3FontSize: Int = 56
override var h3Bold: Boolean = false
override var h4FontSize: Int = 48
override var h4Bold: Boolean = false
override var h5FontSize: Int = 40
override var h5Bold: Boolean = false
override var h6FontSize: Int = 32
override var h6Bold: Boolean = false
override var blockquoteColor: Int? = null
override var blockquoteBorderColor: Int = Color.BLACK
override var blockquoteStripeWidth: Int = 2
override var blockquoteGapWidth: Int = 16
override var olGapWidth: Int = 16
override var olMarginLeft: Int = 24
override var olMarkerFontWeight: Int? = null
override var olMarkerColor: Int? = null
override var ulGapWidth: Int = 16
override var ulMarginLeft: Int = 24
override var ulBulletSize: Int = 8
override var ulBulletColor: Int = Color.BLACK
override var ulCheckboxBoxSize: Int = 50
override var ulCheckboxGapWidth: Int = 16
override var ulCheckboxMarginLeft: Int = 24
override var ulCheckboxBoxColor: Int = Color.BLACK
override var aColor: Int = Color.BLACK
override var aUnderline: Boolean = true
override var codeBlockColor: Int = Color.BLACK
override var codeBlockBackgroundColor: Int = Color.BLACK
override var codeBlockRadius: Float = 4f
override var inlineCodeColor: Int = Color.BLACK
override var inlineCodeBackgroundColor: Int = Color.BLACK
override var mentionsStyle: MutableMap<String, MentionStyle> = mutableMapOf()
constructor(view: EnrichedTextInputView?, style: ReadableMap?) {
this.view = view
this.style = style
invalidateStyles()
}
fun invalidateStyles() {
val style = this.style ?: return
val h1Style = style.getMap("h1")
h1FontSize = parseFloat(h1Style, "fontSize").toInt()
h1Bold = h1Style?.getBoolean("bold") == true
val h2Style = style.getMap("h2")
h2FontSize = parseFloat(h2Style, "fontSize").toInt()
h2Bold = h2Style?.getBoolean("bold") == true
val h3Style = style.getMap("h3")
h3FontSize = parseFloat(h3Style, "fontSize").toInt()
h3Bold = h3Style?.getBoolean("bold") == true
val h4Style = style.getMap("h4")
h4FontSize = parseFloat(h4Style, "fontSize").toInt()
h4Bold = h4Style?.getBoolean("bold") == true
val h5Style = style.getMap("h5")
h5FontSize = parseFloat(h5Style, "fontSize").toInt()
h5Bold = h5Style?.getBoolean("bold") == true
val h6Style = style.getMap("h6")
h6FontSize = parseFloat(h6Style, "fontSize").toInt()
h6Bold = h6Style?.getBoolean("bold") == true
val blockquoteStyle = style.getMap("blockquote")
blockquoteColor = parseOptionalColor(blockquoteStyle, "color")
blockquoteBorderColor = parseColor(blockquoteStyle, "borderColor")
blockquoteGapWidth = parseFloat(blockquoteStyle, "gapWidth").toInt()
blockquoteStripeWidth = parseFloat(blockquoteStyle, "borderWidth").toInt()
val olStyle = style.getMap("ol")
olMarginLeft = parseFloat(olStyle, "marginLeft").toInt()
olGapWidth = parseFloat(olStyle, "gapWidth").toInt()
olMarkerColor = parseOptionalColor(olStyle, "markerColor")
olMarkerFontWeight = parseOptionalFontWeight(olStyle, "markerFontWeight")
val ulStyle = style.getMap("ul")
ulBulletColor = parseColor(ulStyle, "bulletColor")
ulGapWidth = parseFloat(ulStyle, "gapWidth").toInt()
ulMarginLeft = parseFloat(ulStyle, "marginLeft").toInt()
ulBulletSize = parseFloat(ulStyle, "bulletSize").toInt()
val ulCheckboxStyle = style.getMap("ulCheckbox")
ulCheckboxBoxSize = parseFloat(ulCheckboxStyle, "boxSize").toInt()
ulCheckboxGapWidth = parseFloat(ulCheckboxStyle, "gapWidth").toInt()
ulCheckboxMarginLeft = parseFloat(ulCheckboxStyle, "marginLeft").toInt()
ulCheckboxBoxColor = parseColor(ulCheckboxStyle, "boxColor")
val aStyle = style.getMap("a")
aColor = parseColor(aStyle, "color")
aUnderline = parseIsUnderline(aStyle)
val codeBlockStyle = style.getMap("codeblock")
codeBlockRadius = parseFloat(codeBlockStyle, "borderRadius")
codeBlockColor = parseColor(codeBlockStyle, "color")
codeBlockBackgroundColor = parseColorWithOpacity(codeBlockStyle, "backgroundColor", 80)
val inlineCodeStyle = style.getMap("code")
inlineCodeColor = parseColor(inlineCodeStyle, "color")
inlineCodeBackgroundColor = parseColorWithOpacity(inlineCodeStyle, "backgroundColor", 80)
val mentionStyle = style.getMap("mention")
mentionsStyle = parseMentionsStyle(mentionStyle)
}
private fun parseFloat(
map: ReadableMap?,
key: String,
): Float {
val safeMap = ensureValueIsSet(map, key)
val value = safeMap.getDouble(key)
return ceil(pixelFromSpOrDp(value, view?.allowFontScaling ?: EnrichedConstants.ALLOW_FONT_SCALING_DEFAULT))
}
private fun parseColorWithOpacity(
map: ReadableMap?,
key: String,
opacity: Int,
): Int {
val color = parseColor(map, key)
return withOpacity(color, opacity)
}
private fun parseOptionalColor(
map: ReadableMap?,
key: String,
): Int? {
if (map == null) return null
if (!map.hasKey(key)) return null
if (map.isNull(key)) return null
return parseColor(map, key)
}
private fun parseColor(
map: ReadableMap?,
key: String,
): Int {
val safeMap = ensureValueIsSet(map, key)
val color = safeMap.getDouble(key)
val parsedColor = ColorPropConverter.getColor(color, view?.context as ReactContext)
if (parsedColor == null) {
throw Error("Specified color value is not supported: $color")
}
return parsedColor
}
private fun withOpacity(
color: Int,
alpha: Int,
): Int {
if (Color.alpha(color) != 255) return color
val a = alpha.coerceIn(0, 255)
return (color and 0x00FFFFFF) or (a shl 24)
}
private fun parseIsUnderline(map: ReadableMap?): Boolean {
val underline = map?.getString("textDecorationLine")
val isEnabled = underline == "underline"
val isDisabled = underline == "none"
if (isEnabled) return true
if (isDisabled) return false
throw Error("Specified textDecorationLine value is not supported: $underline. Supported values are 'underline' and 'none'.")
}
private fun ensureValueIsSet(
map: ReadableMap?,
key: String,
): ReadableMap {
if (map == null) throw Error("Style map cannot be null")
if (!map.hasKey(key)) throw Error("Style map must contain key: $key")
if (map.isNull(key)) throw Error("Style map cannot contain null value for key: $key")
return map
}
private fun parseMentionsStyle(mentionsStyle: ReadableMap?): MutableMap<String, MentionStyle> {
if (mentionsStyle == null) throw Error("Mentions style cannot be null")
val parsedMentionsStyle: MutableMap<String, MentionStyle> = mutableMapOf()
val iterator = mentionsStyle.keySetIterator()
while (iterator.hasNextKey()) {
val key = iterator.nextKey()
val value = mentionsStyle.getMap(key)
if (value == null) throw Error("Mention style for key '$key' cannot be null")
val color = parseColor(value, "color")
val backgroundColor = parseColorWithOpacity(value, "backgroundColor", 80)
val isUnderline = parseIsUnderline(value)
val parsedStyle = MentionStyle(color, backgroundColor, isUnderline)
parsedMentionsStyle.put(key, parsedStyle)
}
return parsedMentionsStyle
}
private fun parseOptionalFontWeight(
map: ReadableMap?,
key: String,
): Int? {
if (map == null) return null
if (!map.hasKey(key)) return null
if (map.isNull(key)) return null
val fontWeight = map.getString(key) ?: return null
return parseFontWeight(fontWeight)
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is HtmlStyle) return false
return h1FontSize == other.h1FontSize &&
h1Bold == other.h1Bold &&
h2FontSize == other.h2FontSize &&
h2Bold == other.h2Bold &&
h3FontSize == other.h3FontSize &&
h3Bold == other.h3Bold &&
h4FontSize == other.h4FontSize &&
h4Bold == other.h4Bold &&
h5FontSize == other.h5FontSize &&
h5Bold == other.h5Bold &&
h6FontSize == other.h6FontSize &&
h6Bold == other.h6Bold &&
blockquoteColor == other.blockquoteColor &&
blockquoteBorderColor == other.blockquoteBorderColor &&
blockquoteStripeWidth == other.blockquoteStripeWidth &&
blockquoteGapWidth == other.blockquoteGapWidth &&
olGapWidth == other.olGapWidth &&
olMarginLeft == other.olMarginLeft &&
olMarkerFontWeight == other.olMarkerFontWeight &&
olMarkerColor == other.olMarkerColor &&
ulGapWidth == other.ulGapWidth &&
ulMarginLeft == other.ulMarginLeft &&
ulBulletSize == other.ulBulletSize &&
ulBulletColor == other.ulBulletColor &&
ulCheckboxBoxSize == other.ulCheckboxBoxSize &&
ulCheckboxGapWidth == other.ulCheckboxGapWidth &&
ulCheckboxMarginLeft == other.ulCheckboxMarginLeft &&
ulCheckboxBoxColor == other.ulCheckboxBoxColor &&
aColor == other.aColor &&
aUnderline == other.aUnderline &&
codeBlockColor == other.codeBlockColor &&
codeBlockBackgroundColor == other.codeBlockBackgroundColor &&
codeBlockRadius == other.codeBlockRadius &&
inlineCodeColor == other.inlineCodeColor &&
inlineCodeBackgroundColor == other.inlineCodeBackgroundColor &&
mentionsStyle == other.mentionsStyle
}
override fun hashCode(): Int {
var result = h1FontSize.hashCode()
result = 31 * result + h1Bold.hashCode()
result = 31 * result + h2FontSize.hashCode()
result = 31 * result + h2Bold.hashCode()
result = 31 * result + h3FontSize.hashCode()
result = 31 * result + h3Bold.hashCode()
result = 31 * result + h4FontSize.hashCode()
result = 31 * result + h4Bold.hashCode()
result = 31 * result + h5FontSize.hashCode()
result = 31 * result + h5Bold.hashCode()
result = 31 * result + h6FontSize.hashCode()
result = 31 * result + h6Bold.hashCode()
result = 31 * result + (blockquoteColor ?: 0)
result = 31 * result + blockquoteBorderColor.hashCode()
result = 31 * result + blockquoteStripeWidth.hashCode()
result = 31 * result + blockquoteGapWidth.hashCode()
result = 31 * result + olGapWidth.hashCode()
result = 31 * result + olMarginLeft.hashCode()
result = 31 * result + (olMarkerFontWeight?.hashCode() ?: 0)
result = 31 * result + (olMarkerColor ?: 0)
result = 31 * result + ulGapWidth.hashCode()
result = 31 * result + ulMarginLeft.hashCode()
result = 31 * result + ulBulletSize.hashCode()
result = 31 * result + ulBulletColor.hashCode()
result = 31 * result + ulCheckboxBoxSize.hashCode()
result = 31 * result + ulCheckboxGapWidth.hashCode()
result = 31 * result + ulCheckboxMarginLeft.hashCode()
result = 31 * result + ulCheckboxBoxColor.hashCode()
result = 31 * result + aColor.hashCode()
result = 31 * result + aUnderline.hashCode()
result = 31 * result + codeBlockColor.hashCode()
result = 31 * result + codeBlockBackgroundColor.hashCode()
result = 31 * result + codeBlockRadius.hashCode()
result = 31 * result + inlineCodeColor.hashCode()
result = 31 * result + inlineCodeBackgroundColor.hashCode()
result = 31 * result + mentionsStyle.hashCode()
return result
}
}