-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathParametrizedStyles.kt
More file actions
191 lines (149 loc) · 6.3 KB
/
Copy pathParametrizedStyles.kt
File metadata and controls
191 lines (149 loc) · 6.3 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
package com.swmansion.reactnativerichtexteditor.styles
import android.net.Uri
import android.text.Editable
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.Spanned
import com.swmansion.reactnativerichtexteditor.ReactNativeRichTextEditorView
import com.swmansion.reactnativerichtexteditor.spans.EditorImageSpan
import com.swmansion.reactnativerichtexteditor.spans.EditorLinkSpan
import com.swmansion.reactnativerichtexteditor.spans.EditorMentionSpan
import com.swmansion.reactnativerichtexteditor.spans.EditorSpans
import com.swmansion.reactnativerichtexteditor.utils.getSafeSpanBoundaries
import java.io.File
class ParametrizedStyles(private val editorView: ReactNativeRichTextEditorView) {
private var mentionStart: Int? = null
var mentionIndicators: Array<String> = emptyArray<String>()
fun <T>removeSpansForRange(spannable: Spannable, start: Int, end: Int, clazz: Class<T>) {
val ssb = spannable as SpannableStringBuilder
ssb.replace(start, end, ssb.substring(start, end).replace("\u200B", ""))
val spans = ssb.getSpans(start, end, clazz)
for (span in spans) {
ssb.removeSpan(span)
}
}
fun setLinkSpan(start: Int, end: Int, text: String, url: String) {
val spannable = editorView.text as SpannableStringBuilder
val spans = spannable.getSpans(start, end, EditorLinkSpan::class.java)
for (span in spans) {
spannable.removeSpan(span)
}
if (start == end) {
spannable.insert(start, text)
} else {
spannable.replace(start, end, text)
}
val spanEnd = start + text.length
val span = EditorLinkSpan(url, editorView.richTextStyle)
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(start, spanEnd)
spannable.setSpan(span, safeStart, safeEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
editorView.selection?.validateStyles()
}
fun afterTextChanged(s: Editable, endCursorPosition: Int) {
val result = getWordAtIndex(s, endCursorPosition) ?: return
afterTextChangedLinks(result)
afterTextChangedMentions(result)
}
private fun getWordAtIndex(s: Editable, index: Int): Triple<String, Int, Int>? {
if (index < 0 ) return null
var start = index
var end = index
while (start > 0 && !Character.isWhitespace(s[start - 1])) {
start--
}
while (end < s.length && !Character.isWhitespace(s[end])) {
end++
}
val result = s.subSequence(start, end).toString()
return Triple(result, start, end)
}
private fun afterTextChangedLinks(result: Triple<String, Int, Int>) {
val spannable = editorView.text as Spannable
val (word, start, end) = result
// TODO: Consider using more reliable regex, this one matches almost anything
val urlPattern = android.util.Patterns.WEB_URL.matcher(word)
val spans = spannable.getSpans(start, end, EditorLinkSpan::class.java)
for (span in spans) {
spannable.removeSpan(span)
}
if (urlPattern.matches()) {
val span = EditorLinkSpan(word, editorView.richTextStyle)
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(start, end)
spannable.setSpan(span, safeStart, safeEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
private fun afterTextChangedMentions(result: Triple<String, Int, Int>) {
val mentionHandler = editorView.mentionHandler ?: return
val spannable = editorView.text as Spannable
val (word, start, end) = result
val indicatorsPattern = mentionIndicators.joinToString("|") { Regex.escape(it) }
val mentionIndicatorRegex = Regex("^($indicatorsPattern)")
val mentionRegex= Regex("^($indicatorsPattern)\\w*")
val spans = spannable.getSpans(start, end, EditorMentionSpan::class.java)
for (span in spans) {
spannable.removeSpan(span)
}
if (mentionRegex.matches(word)) {
val indicator = mentionIndicatorRegex.find(word)?.value ?: ""
val text = word.replaceFirst(indicator, "")
// Means we are starting mention
if (text.isEmpty()) {
mentionStart = start
}
mentionHandler.onMention(indicator, word.replaceFirst(indicator, ""))
} else {
mentionHandler.endMention()
}
}
fun setImageSpan(src: String) {
if (editorView.selection == null) return
val spannable = editorView.text as SpannableStringBuilder
var (start, end) = editorView.selection.getInlineSelection()
val spans = spannable.getSpans(start, end, EditorImageSpan::class.java)
for (s in spans) {
spannable.removeSpan(s)
}
if (start == end) {
spannable.insert(start, "\uFFFC")
end++
}
val uri = Uri.fromFile(File(src))
val span = EditorImageSpan(editorView.context, uri, editorView.richTextStyle)
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(start, end)
spannable.setSpan(span, safeStart, safeEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
fun startMention(indicator: String) {
val selection = editorView.selection ?: return
val spannable = editorView.text as SpannableStringBuilder
var (start, end) = selection.getInlineSelection()
if (start == end) {
spannable.insert(start, indicator)
} else {
spannable.replace(start, end, indicator)
}
}
fun setMentionSpan(indicator: String, text: String, attributes: Map<String, String>) {
val selection = editorView.selection ?: return
val spannable = editorView.text as SpannableStringBuilder
var (selectionStart, selectionEnd) = selection.getInlineSelection()
val spans = spannable.getSpans(selectionStart, selectionEnd, EditorMentionSpan::class.java)
for (span in spans) {
spannable.removeSpan(span)
}
var start = mentionStart ?: return
spannable.replace(start, selectionEnd, text)
val span = EditorMentionSpan(text, indicator, attributes, editorView.richTextStyle)
val spanEnd = start + text.length
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(start, spanEnd)
spannable.setSpan(span, safeStart, safeEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
editorView.selection.validateStyles()
}
fun getStyleRange(): Pair<Int, Int> {
return editorView.selection?.getInlineSelection() ?: Pair(0, 0)
}
fun removeStyle(name: String, start: Int, end: Int) {
val config = EditorSpans.parametrizedStyles[name] ?: return
val spannable = editorView.text as Spannable
removeSpansForRange(spannable, start, end, config.clazz)
}
}