@@ -37,6 +37,7 @@ import org.wordpress.aztec.watchers.TextChangedEvent
3737class InlineFormatter (editor : AztecText , val codeStyle : CodeStyle , private val highlightStyle : HighlightStyle ) : AztecFormatter(editor) {
3838
3939 var backgroundSpanColor: Int? = null
40+ var markStyleColor: String? = null
4041
4142 data class CodeStyle (val codeBackground : Int , val codeBackgroundAlpha : Float , val codeColor : Int )
4243 data class HighlightStyle (@ColorRes val color : Int )
@@ -107,12 +108,8 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
107108 applyInlineStyle(item, textChangedEvent.inputStart, textChangedEvent.inputEnd)
108109 }
109110 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)
116113 }
117114 else -> {
118115 // do nothing
@@ -128,6 +125,16 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
128125 val newStart = if (start > end) end else start
129126 // if there is END_OF_BUFFER_MARKER at the end of or range, extend the range to include it
130127
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+
131138 // remove lingering empty spans when removing characters
132139 if (start > end) {
133140 editableText.getSpans(newStart, end, IAztecInlineSpan ::class .java)
@@ -250,10 +257,40 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
250257 }
251258 }
252259
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+ }
257294 }
258295 }
259296
@@ -444,7 +481,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
444481 AztecTextFormat .FORMAT_HIGHLIGHT -> {
445482 HighlightSpan .create(context = editor.context, defaultStyle = highlightStyle)
446483 }
447- AztecTextFormat .FORMAT_MARK -> MarkSpan ()
484+ AztecTextFormat .FORMAT_MARK -> MarkSpan (AztecAttributes (), markStyleColor )
448485 else -> AztecStyleSpan (Typeface .NORMAL )
449486 }
450487 }
0 commit comments