-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1073 from wordpress-mobile/fix/mark-style-issues
Improve Mark formatting
- Loading branch information
Showing
4 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
aztec/src/main/kotlin/org/wordpress/aztec/plugins/MarkPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.wordpress.aztec.plugins | ||
|
||
import android.text.SpannableStringBuilder | ||
import org.wordpress.aztec.plugins.visual2html.ISpanPreprocessor | ||
import org.wordpress.aztec.source.CssStyleFormatter | ||
import org.wordpress.aztec.spans.MarkSpan | ||
|
||
class MarkPlugin : ISpanPreprocessor { | ||
|
||
override fun beforeSpansProcessed(spannable: SpannableStringBuilder) { | ||
spannable.getSpans(0, spannable.length, MarkSpan::class.java).forEach { | ||
if (!CssStyleFormatter.containsStyleAttribute(it.attributes, CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE)) { | ||
CssStyleFormatter.addStyleAttribute(it.attributes, CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE, "rgba(0, 0, 0, 0)") | ||
} | ||
|
||
if (!CssStyleFormatter.containsStyleAttribute(it.attributes, CssStyleFormatter.CSS_COLOR_ATTRIBUTE)) { | ||
CssStyleFormatter.addStyleAttribute(it.attributes, CssStyleFormatter.CSS_COLOR_ATTRIBUTE, it.getTextColor()) | ||
} | ||
|
||
if (!it.attributes.hasAttribute("class")) { | ||
it.attributes.setValue("class", "has-inline-color") | ||
} | ||
} | ||
} | ||
} |
37 changes: 35 additions & 2 deletions
37
aztec/src/main/kotlin/org/wordpress/aztec/spans/MarkSpan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,45 @@ | ||
package org.wordpress.aztec.spans | ||
|
||
import android.graphics.Color | ||
import android.text.TextPaint | ||
import android.text.style.CharacterStyle | ||
import org.wordpress.aztec.AztecAttributes | ||
import org.wordpress.aztec.source.CssStyleFormatter | ||
|
||
class MarkSpan(override var attributes: AztecAttributes = AztecAttributes()) : CharacterStyle(), IAztecInlineSpan { | ||
class MarkSpan : CharacterStyle, IAztecInlineSpan { | ||
override var TAG = "mark" | ||
|
||
override fun updateDrawState(tp: TextPaint?) { | ||
override var attributes: AztecAttributes = AztecAttributes() | ||
private val textColorValue: Int? | ||
|
||
constructor(attributes: AztecAttributes = AztecAttributes()) : super() { | ||
this.attributes = attributes | ||
|
||
val color = CssStyleFormatter.getStyleAttribute(attributes, | ||
CssStyleFormatter.CSS_COLOR_ATTRIBUTE) | ||
textColorValue = if (color.isNotEmpty()) { | ||
Color.parseColor(color) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
constructor(attributes: AztecAttributes = AztecAttributes(), colorString: String?) : super() { | ||
this.attributes = attributes | ||
|
||
textColorValue = if (colorString != null) { | ||
Color.parseColor(colorString) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
override fun updateDrawState(tp: TextPaint) { | ||
textColorValue?.let { tp.color = it } | ||
} | ||
|
||
fun getTextColor(): String { | ||
val currentColor = textColorValue ?: 0 | ||
return String.format("#%06X", 0xFFFFFF and currentColor) | ||
} | ||
} |