11package com.swmansion.enriched.utils
22
3- import android.text.Spannable
4- import android.text.SpannableString
5- import android.text.SpannableStringBuilder
63import android.util.Log
7- import com.swmansion.enriched.spans.interfaces.EnrichedBlockSpan
8- import com.swmansion.enriched.spans.interfaces.EnrichedParagraphSpan
94import org.json.JSONObject
105
116fun jsonStringToStringMap (json : String ): Map <String , String > {
@@ -24,90 +19,3 @@ fun jsonStringToStringMap(json: String): Map<String, String> {
2419
2520 return result
2621}
27-
28- fun Spannable.getSafeSpanBoundaries (
29- start : Int ,
30- end : Int ,
31- ): Pair <Int , Int > {
32- val safeStart = start.coerceAtMost(end).coerceAtLeast(0 )
33- val safeEnd = end.coerceAtLeast(start).coerceAtMost(this .length)
34-
35- return Pair (safeStart, safeEnd)
36- }
37-
38- fun Spannable.getParagraphBounds (
39- start : Int ,
40- end : Int ,
41- ): Pair <Int , Int > {
42- var startPosition = start.coerceAtLeast(0 ).coerceAtMost(this .length)
43- var endPosition = end.coerceAtLeast(0 ).coerceAtMost(this .length)
44-
45- // Find the start of the paragraph
46- while (startPosition > 0 && this [startPosition - 1 ] != ' \n ' ) {
47- startPosition--
48- }
49-
50- // Find the end of the paragraph
51- while (endPosition < this .length && this [endPosition] != ' \n ' ) {
52- endPosition++
53- }
54-
55- if (startPosition >= endPosition) {
56- // If the start position is equal or greater than the end position, return the same position
57- startPosition = endPosition
58- }
59-
60- return Pair (startPosition, endPosition)
61- }
62-
63- fun Spannable.getParagraphBounds (index : Int ): Pair <Int , Int > = this .getParagraphBounds(index, index)
64-
65- fun Spannable.mergeSpannables (
66- start : Int ,
67- end : Int ,
68- string : String ,
69- ): Spannable = this .mergeSpannables(start, end, SpannableString (string))
70-
71- fun Spannable.mergeSpannables (
72- start : Int ,
73- end : Int ,
74- spannable : Spannable ,
75- ): Spannable {
76- var finalStart = start
77- var finalEnd = end
78-
79- val builder = SpannableStringBuilder (this )
80- val startBlockSpans = spannable.getSpans(0 , 0 , EnrichedBlockSpan ::class .java)
81- val startParagraphSpans = spannable.getSpans(0 , 0 , EnrichedParagraphSpan ::class .java)
82- val endBlockSpans = spannable.getSpans(this .length, this .length, EnrichedBlockSpan ::class .java)
83- val endParagraphSpans = spannable.getSpans(this .length, this .length, EnrichedParagraphSpan ::class .java)
84- val (paragraphStart, paragraphEnd) = this .getParagraphBounds(start, end)
85- val isNewLineStart = startBlockSpans.isNotEmpty() || startParagraphSpans.isNotEmpty()
86- val isNewLineEnd = endBlockSpans.isNotEmpty() || endParagraphSpans.isNotEmpty()
87-
88- if (isNewLineStart && start != paragraphStart) {
89- builder.insert(start, " \n " )
90- finalStart = start + 1
91- finalEnd = end + 1
92- }
93-
94- if (isNewLineEnd && end != paragraphEnd) {
95- builder.insert(finalEnd, " \n " )
96- }
97-
98- builder.replace(finalStart, finalEnd, spannable)
99-
100- return builder
101- }
102-
103- // Removes zero-width spaces from the given range in the SpannableStringBuilder without affecting spans
104- fun SpannableStringBuilder.removeZWS (
105- start : Int ,
106- end : Int ,
107- ) {
108- for (i in (end - 1 ) downTo start) {
109- if (this [i] == EnrichedConstants .ZWS ) {
110- delete(i, i + 1 )
111- }
112- }
113- }
0 commit comments