-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathInlineStyles.kt
More file actions
262 lines (220 loc) · 7.87 KB
/
Copy pathInlineStyles.kt
File metadata and controls
262 lines (220 loc) · 7.87 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
package com.swmansion.enriched.textinput.styles
import android.text.Editable
import android.text.Spannable
import com.swmansion.enriched.common.EnrichedSpanFlags
import com.swmansion.enriched.textinput.EnrichedTextInputView
import com.swmansion.enriched.textinput.spans.EnrichedSpans
import com.swmansion.enriched.textinput.utils.getSafeSpanBoundaries
class InlineStyles(
private val view: EnrichedTextInputView,
) {
private fun <T> setSpan(
spannable: Spannable,
type: Class<T>,
start: Int,
end: Int,
) {
val previousSpanStart = (start - 1).coerceAtLeast(0)
val previousSpanEnd = previousSpanStart + 1
val nextSpanStart = (end + 1).coerceAtMost(spannable.length)
val nextSpanEnd = (nextSpanStart + 1).coerceAtMost(spannable.length)
val previousSpans = spannable.getSpans(previousSpanStart, previousSpanEnd, type)
val nextSpans = spannable.getSpans(nextSpanStart, nextSpanEnd, type)
var minimum = start
var maximum = end
for (span in previousSpans) {
val spanStart = spannable.getSpanStart(span)
minimum = spanStart.coerceAtMost(minimum)
}
for (span in nextSpans) {
val spanEnd = spannable.getSpanEnd(span)
maximum = spanEnd.coerceAtLeast(maximum)
}
val spans = spannable.getSpans(minimum, maximum, type)
for (span in spans) {
spannable.removeSpan(span)
}
val span = type.getDeclaredConstructor(HtmlStyle::class.java).newInstance(view.htmlStyle)
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(minimum, maximum)
spannable.setSpan(span, safeStart, safeEnd, EnrichedSpanFlags.forSpan(span))
}
private fun <T> setAndMergeSpans(
spannable: Spannable,
type: Class<T>,
start: Int,
end: Int,
) {
val spans = spannable.getSpans(start, end, type)
// No spans setup for current selection, means we just need to assign new span
if (spans.isEmpty()) {
setSpan(spannable, type, start, end)
return
}
var setSpanOnFinish = false
// Some spans are present, we have to remove spans and (optionally) apply new spans
for (span in spans) {
val spanStart = spannable.getSpanStart(span)
val spanEnd = spannable.getSpanEnd(span)
var finalStart: Int? = null
var finalEnd: Int? = null
if (spanStart == -1 || spanEnd == -1) continue
spannable.removeSpan(span)
if (start == spanStart && end == spanEnd) {
setSpanOnFinish = false
} else if (start > spanStart && end < spanEnd) {
setSpan(spannable, type, spanStart, start)
setSpan(spannable, type, end, spanEnd)
} else if (start == spanStart && end < spanEnd) {
finalStart = end
finalEnd = spanEnd
} else if (start > spanStart && end == spanEnd) {
finalStart = spanStart
finalEnd = start
} else if (start > spanStart) {
finalStart = spanStart
finalEnd = end
} else if (start < spanStart && end < spanEnd) {
finalStart = start
finalEnd = spanEnd
} else {
setSpanOnFinish = true
}
if (!setSpanOnFinish && finalStart != null && finalEnd != null) {
setSpan(spannable, type, finalStart, finalEnd)
}
}
if (setSpanOnFinish) {
setSpan(spannable, type, start, end)
}
}
fun afterTextChanged(
s: Editable,
startCursorPosition: Int,
endCursorPosition: Int,
) {
if (endCursorPosition > startCursorPosition) {
for ((style, config) in EnrichedSpans.inlineSpans) {
if (view.spanState?.getStart(style) != null) continue
splitSpanOnInsertion(s, config.clazz, startCursorPosition, endCursorPosition)
}
}
for ((style, config) in EnrichedSpans.inlineSpans) {
val start = view.spanState?.getStart(style) ?: continue
var end = endCursorPosition
val spans = s.getSpans(start, end, config.clazz)
for (span in spans) {
end = s.getSpanEnd(span).coerceAtLeast(end)
s.removeSpan(span)
}
setSpan(s, config.clazz, start, end)
}
val isBackspace = endCursorPosition == startCursorPosition
if (!isBackspace) {
return
}
// Collapse same-type inline spans that ended up adjacent to each other after deletion.
// Without this the HTML output would emit separate tags like <b>...</b><b>...</b>.
for ((_, config) in EnrichedSpans.inlineSpans) {
for (span in s.getSpans(startCursorPosition, startCursorPosition, config.clazz)) {
val spanStart = s.getSpanStart(span)
val spanEnd = s.getSpanEnd(span)
if (spanStart < 0 || spanEnd < 0) continue
setSpan(s, config.clazz, spanStart, spanEnd)
}
}
}
fun applyStyleOnRange(
name: String,
start: Int,
end: Int,
) {
val config = EnrichedSpans.inlineSpans[name] ?: return
val type = config.clazz
val spannable = view.text as Spannable
val spans = spannable.getSpans(start, end, type)
if (spans.any { spannable.getSpanStart(it) <= start && spannable.getSpanEnd(it) >= end }) {
return
}
setAndMergeSpans(spannable, type, start, end)
}
fun restoreStyleOnRange(
name: String,
start: Int,
end: Int,
) {
if (start >= end) return
val config = EnrichedSpans.inlineSpans[name] ?: return
val spannable = view.text as? Spannable ?: return
val spans = spannable.getSpans(start, end, config.clazz)
if (spans.any { spannable.getSpanStart(it) <= start && spannable.getSpanEnd(it) >= end }) {
return
}
setSpan(spannable, config.clazz, start, end)
}
fun toggleStyle(name: String) {
if (view.selection == null) return
val (start, end) = view.selection.getInlineSelection()
val config = EnrichedSpans.inlineSpans[name] ?: return
val type = config.clazz
// We either start or end current span
if (start == end) {
val styleStart = view.spanState?.getStart(name)
if (styleStart != null) {
view.spanState.setStart(name, null)
} else {
view.spanState?.setStart(name, start)
}
return
}
val spannable = view.text as Spannable
setAndMergeSpans(spannable, type, start, end)
view.selection.validateStyles()
}
private fun <T> splitSpanOnInsertion(
spannable: Spannable,
type: Class<T>,
insertStart: Int,
insertEnd: Int,
) {
val spans = spannable.getSpans(insertStart, insertEnd, type)
for (span in spans) {
val spanStart = spannable.getSpanStart(span)
val spanEnd = spannable.getSpanEnd(span)
if (spanStart < 0 || spanEnd < 0) continue
spannable.removeSpan(span)
if (spanStart < insertStart) {
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(spanStart, insertStart)
val left = type.getDeclaredConstructor(HtmlStyle::class.java).newInstance(view.htmlStyle)
spannable.setSpan(left, safeStart, safeEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
if (spanEnd > insertEnd) {
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(insertEnd, spanEnd)
val right = type.getDeclaredConstructor(HtmlStyle::class.java).newInstance(view.htmlStyle)
spannable.setSpan(right, safeStart, safeEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
}
fun removeStyle(
name: String,
start: Int,
end: Int,
): Boolean {
val config = EnrichedSpans.inlineSpans[name] ?: return false
val spannable = view.text as Spannable
val spans = spannable.getSpans(start, end, config.clazz)
if (spans.isEmpty()) return false
for (span in spans) {
val spanStart = spannable.getSpanStart(span)
val spanEnd = spannable.getSpanEnd(span)
spannable.removeSpan(span)
if (spanStart < start) {
setSpan(spannable, config.clazz, spanStart, start - 1)
}
if (spanEnd > end) {
setSpan(spannable, config.clazz, end, spanEnd)
}
}
return true
}
fun getStyleRange(): Pair<Int, Int> = view.selection?.getInlineSelection() ?: Pair(0, 0)
}