Skip to content

Commit bdc719c

Browse files
author
Gerardo
committed
Improve setting and resetting Mark formatting styles
1 parent ffdddf3 commit bdc719c

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

aztec/src/main/kotlin/org/wordpress/aztec/formatting/InlineFormatter.kt

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
125125
val newStart = if (start > end) end else start
126126
// if there is END_OF_BUFFER_MARKER at the end of or range, extend the range to include it
127127

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() && markStyleColor == null)) {
133+
removeInlineCssStyle(start, end)
134+
return
135+
}
136+
}
137+
128138
// remove lingering empty spans when removing characters
129139
if (start > end) {
130140
editableText.getSpans(newStart, end, IAztecInlineSpan::class.java)
@@ -133,14 +143,6 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
133143
return
134144
}
135145

136-
// Remove leading Mark formatting styles if the format is not active
137-
if (!editor.selectedStyles.contains(AztecTextFormat.FORMAT_MARK) && newStart >=1 && end > 1) {
138-
val markSpan = editableText.getSpans(newStart - 1, newStart, MarkSpan::class.java);
139-
if (markSpan.isNotEmpty()) {
140-
removeInlineCssStyle(newStart, end)
141-
}
142-
}
143-
144146
editableText.getSpans(newStart, end, IAztecInlineSpan::class.java).forEach {
145147
if (!editor.selectedStyles.contains(spanToTextFormat(it)) || ignoreSelectedStyles || (newStart == 0 && end == 0) ||
146148
(newStart > end && editableText.length > end && editableText[end] == '\n')) {
@@ -266,21 +268,28 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
266268
if (span != null) {
267269
val color = span.getTextColor()
268270
val currentSpanStart = editableText.getSpanStart(span)
271+
val currentSpanEnd = editableText.getSpanEnd(span)
269272

270-
editableText.removeSpan(span)
271-
editableText.setSpan(
272-
MarkSpan(AztecAttributes(), color),
273-
currentSpanStart,
274-
start,
275-
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
276-
)
277-
editableText.setSpan(
278-
MarkSpan(AztecAttributes(), markStyleColor),
279-
start,
280-
end,
281-
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
282-
)
283-
markStyleColor = null
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+
}
284293
}
285294
}
286295
}

0 commit comments

Comments
 (0)