Skip to content

Commit 6e9814f

Browse files
author
Gerardo Pacheco
authored
Merge pull request #1073 from wordpress-mobile/fix/mark-style-issues
Improve Mark formatting
2 parents e204002 + 2f73564 commit 6e9814f

File tree

4 files changed

+109
-13
lines changed

4 files changed

+109
-13
lines changed

Diff for: aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
13441344
AztecTextFormat.FORMAT_CODE -> inlineFormatter.toggle(textFormat)
13451345
AztecTextFormat.FORMAT_BOLD,
13461346
AztecTextFormat.FORMAT_STRONG -> inlineFormatter.toggleAny(ToolbarAction.BOLD.textFormats)
1347+
AztecTextFormat.FORMAT_MARK -> inlineFormatter.toggle(textFormat)
13471348
AztecTextFormat.FORMAT_UNORDERED_LIST -> blockFormatter.toggleUnorderedList()
13481349
AztecTextFormat.FORMAT_TASK_LIST -> blockFormatter.toggleTaskList()
13491350
AztecTextFormat.FORMAT_ORDERED_LIST -> blockFormatter.toggleOrderedList()

Diff for: aztec/src/main/kotlin/org/wordpress/aztec/formatting/InlineFormatter.kt

+48-11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import org.wordpress.aztec.watchers.TextChangedEvent
3737
class 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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.wordpress.aztec.plugins
2+
3+
import android.text.SpannableStringBuilder
4+
import org.wordpress.aztec.plugins.visual2html.ISpanPreprocessor
5+
import org.wordpress.aztec.source.CssStyleFormatter
6+
import org.wordpress.aztec.spans.MarkSpan
7+
8+
class MarkPlugin : ISpanPreprocessor {
9+
10+
override fun beforeSpansProcessed(spannable: SpannableStringBuilder) {
11+
spannable.getSpans(0, spannable.length, MarkSpan::class.java).forEach {
12+
if (!CssStyleFormatter.containsStyleAttribute(it.attributes, CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE)) {
13+
CssStyleFormatter.addStyleAttribute(it.attributes, CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE, "rgba(0, 0, 0, 0)")
14+
}
15+
16+
if (!CssStyleFormatter.containsStyleAttribute(it.attributes, CssStyleFormatter.CSS_COLOR_ATTRIBUTE)) {
17+
CssStyleFormatter.addStyleAttribute(it.attributes, CssStyleFormatter.CSS_COLOR_ATTRIBUTE, it.getTextColor())
18+
}
19+
20+
if (!it.attributes.hasAttribute("class")) {
21+
it.attributes.setValue("class", "has-inline-color")
22+
}
23+
}
24+
}
25+
}
+35-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
package org.wordpress.aztec.spans
22

3+
import android.graphics.Color
34
import android.text.TextPaint
45
import android.text.style.CharacterStyle
56
import org.wordpress.aztec.AztecAttributes
7+
import org.wordpress.aztec.source.CssStyleFormatter
68

7-
class MarkSpan(override var attributes: AztecAttributes = AztecAttributes()) : CharacterStyle(), IAztecInlineSpan {
9+
class MarkSpan : CharacterStyle, IAztecInlineSpan {
810
override var TAG = "mark"
911

10-
override fun updateDrawState(tp: TextPaint?) {
12+
override var attributes: AztecAttributes = AztecAttributes()
13+
private val textColorValue: Int?
14+
15+
constructor(attributes: AztecAttributes = AztecAttributes()) : super() {
16+
this.attributes = attributes
17+
18+
val color = CssStyleFormatter.getStyleAttribute(attributes,
19+
CssStyleFormatter.CSS_COLOR_ATTRIBUTE)
20+
textColorValue = if (color.isNotEmpty()) {
21+
Color.parseColor(color)
22+
} else {
23+
null
24+
}
25+
}
26+
27+
constructor(attributes: AztecAttributes = AztecAttributes(), colorString: String?) : super() {
28+
this.attributes = attributes
29+
30+
textColorValue = if (colorString != null) {
31+
Color.parseColor(colorString)
32+
} else {
33+
null
34+
}
35+
}
36+
37+
override fun updateDrawState(tp: TextPaint) {
38+
textColorValue?.let { tp.color = it }
39+
}
40+
41+
fun getTextColor(): String {
42+
val currentColor = textColorValue ?: 0
43+
return String.format("#%06X", 0xFFFFFF and currentColor)
1144
}
1245
}

0 commit comments

Comments
 (0)