@@ -37,6 +37,7 @@ import org.wordpress.aztec.watchers.TextChangedEvent
37
37
class InlineFormatter (editor : AztecText , val codeStyle : CodeStyle , private val highlightStyle : HighlightStyle ) : AztecFormatter(editor) {
38
38
39
39
var backgroundSpanColor: Int? = null
40
+ var markStyleColor: String? = null
40
41
41
42
data class CodeStyle (val codeBackground : Int , val codeBackgroundAlpha : Float , val codeColor : Int )
42
43
data class HighlightStyle (@ColorRes val color : Int )
@@ -107,12 +108,8 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
107
108
applyInlineStyle(item, textChangedEvent.inputStart, textChangedEvent.inputEnd)
108
109
}
109
110
AztecTextFormat .FORMAT_MARK -> {
110
- // For cases of an empty mark tag, either at the beginning of the text or in between
111
- if (textChangedEvent.inputStart == 0 && textChangedEvent.inputEnd == 1 ) {
112
- applyMarkInlineStyle(textChangedEvent.inputStart, textChangedEvent.inputEnd)
113
- } else {
114
- applyInlineStyle(item, textChangedEvent.inputStart, textChangedEvent.inputEnd)
115
- }
111
+ applyInlineStyle(item, textChangedEvent.inputStart, textChangedEvent.inputEnd)
112
+ applyAfterMarkInlineStyle(textChangedEvent.inputStart, textChangedEvent.inputEnd)
116
113
}
117
114
else -> {
118
115
// do nothing
@@ -128,6 +125,16 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
128
125
val newStart = if (start > end) end else start
129
126
// if there is END_OF_BUFFER_MARKER at the end of or range, extend the range to include it
130
127
128
+ // Clear Mark formatting styles
129
+ if (! editor.selectedStyles.contains(AztecTextFormat .FORMAT_MARK ) && start >= 1 && end > 1 ) {
130
+ val previousMarkSpan = editableText.getSpans(start - 1 , start, MarkSpan ::class .java)
131
+ val markSpan = editableText.getSpans(start, end, MarkSpan ::class .java)
132
+ if (markSpan.isNotEmpty() || previousMarkSpan.isNotEmpty()) {
133
+ removeInlineCssStyle(start, end)
134
+ return
135
+ }
136
+ }
137
+
131
138
// remove lingering empty spans when removing characters
132
139
if (start > end) {
133
140
editableText.getSpans(newStart, end, IAztecInlineSpan ::class .java)
@@ -250,10 +257,40 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
250
257
}
251
258
}
252
259
253
- private fun applyMarkInlineStyle (start : Int = selectionStart, end : Int = selectionEnd) {
254
- val previousSpans = editableText.getSpans(start, end, MarkSpan ::class .java)
255
- previousSpans.forEach {
256
- it.applyInlineStyleAttributes(editableText, start, end)
260
+ private fun applyAfterMarkInlineStyle (start : Int = selectionStart, end : Int = selectionEnd) {
261
+ // If there's no new mark style color to update, it skips applying the style updates.
262
+ if (markStyleColor == null ) {
263
+ return
264
+ }
265
+
266
+ val spans = editableText.getSpans(start, end, MarkSpan ::class .java)
267
+ spans.forEach { span ->
268
+ if (span != null ) {
269
+ val color = span.getTextColor()
270
+ val currentSpanStart = editableText.getSpanStart(span)
271
+ val currentSpanEnd = editableText.getSpanEnd(span)
272
+
273
+ if (end < currentSpanEnd) {
274
+ markStyleColor = null
275
+ return
276
+ }
277
+
278
+ if (! color.equals(markStyleColor, ignoreCase = true )) {
279
+ editableText.removeSpan(span)
280
+ editableText.setSpan(
281
+ MarkSpan (AztecAttributes (), color),
282
+ currentSpanStart,
283
+ start,
284
+ Spanned .SPAN_EXCLUSIVE_EXCLUSIVE
285
+ )
286
+ editableText.setSpan(
287
+ MarkSpan (AztecAttributes (), markStyleColor),
288
+ start,
289
+ end,
290
+ Spanned .SPAN_EXCLUSIVE_EXCLUSIVE
291
+ )
292
+ }
293
+ }
257
294
}
258
295
}
259
296
@@ -444,7 +481,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
444
481
AztecTextFormat .FORMAT_HIGHLIGHT -> {
445
482
HighlightSpan .create(context = editor.context, defaultStyle = highlightStyle)
446
483
}
447
- AztecTextFormat .FORMAT_MARK -> MarkSpan ()
484
+ AztecTextFormat .FORMAT_MARK -> MarkSpan (AztecAttributes (), markStyleColor )
448
485
else -> AztecStyleSpan (Typeface .NORMAL )
449
486
}
450
487
}
0 commit comments