-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathUtils.kt
More file actions
86 lines (69 loc) · 2.84 KB
/
Copy pathUtils.kt
File metadata and controls
86 lines (69 loc) · 2.84 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
package com.swmansion.reactnativerichtexteditor.utils
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.util.Log
import com.swmansion.reactnativerichtexteditor.spans.interfaces.EditorBlockSpan
import com.swmansion.reactnativerichtexteditor.spans.interfaces.EditorParagraphSpan
import org.json.JSONObject
fun jsonStringToStringMap(json: String): Map<String, String> {
val result = mutableMapOf<String, String>()
try {
val jsonObject = JSONObject(json)
for (key in jsonObject.keys()) {
val value = jsonObject.opt(key)
if (value is String) {
result[key] = value
}
}
} catch (e: Exception) {
Log.w("ReactNativeRichTextEditorView", "Failed to parse JSON string to Map: $json", e)
}
return result
}
fun Spannable.getSafeSpanBoundaries(start: Int, end: Int): Pair<Int, Int> {
val safeStart = start.coerceAtMost(end).coerceAtLeast(0)
val safeEnd = end.coerceAtLeast(start).coerceAtMost(this.length)
return Pair(safeStart, safeEnd)
}
fun Spannable.getParagraphBounds(start: Int, end: Int): Pair<Int, Int> {
var startPosition = start.coerceAtLeast(0).coerceAtMost(this.length)
var endPosition = end.coerceAtLeast(0).coerceAtMost(this.length)
// Find the start of the paragraph
while (startPosition > 0 && this[startPosition - 1] != '\n') {
startPosition--
}
// Find the end of the paragraph
while (endPosition < this.length && this[endPosition] != '\n') {
endPosition++
}
if (startPosition >= endPosition) {
// If the start position is equal or greater than the end position, return the same position
startPosition = endPosition
}
return Pair(startPosition, endPosition)
}
fun Spannable.getParagraphBounds(index: Int): Pair<Int, Int> {
return this.getParagraphBounds(index, index)
}
fun Spannable.mergeSpannables(start: Int, end: Int, spannable: Spannable): Spannable {
var finalStart = start
var finalEnd = end
val builder = SpannableStringBuilder(this)
val startBlockSpans = spannable.getSpans(0, 0, EditorBlockSpan::class.java)
val startParagraphSpans = spannable.getSpans(0, 0, EditorParagraphSpan::class.java)
val endBlockSpans = spannable.getSpans(this.length, this.length, EditorBlockSpan::class.java)
val endParagraphSpans = spannable.getSpans(this.length, this.length, EditorParagraphSpan::class.java)
val (paragraphStart, paragraphEnd) = this.getParagraphBounds(start, end)
val isNewLineStart = startBlockSpans.isNotEmpty() || startParagraphSpans.isNotEmpty()
val isNewLineEnd = endBlockSpans.isNotEmpty() || endParagraphSpans.isNotEmpty()
if (isNewLineStart && start != paragraphStart) {
builder.insert(start, "\n")
finalStart = start + 1
finalEnd = end + 1
}
if (isNewLineEnd && end != paragraphEnd) {
builder.insert(finalEnd, "\n")
}
builder.replace(finalStart, finalEnd, spannable)
return builder
}