Skip to content

Commit 3796763

Browse files
authored
Merge pull request #1057 from wordpress-mobile/fix/highlight-action
Fix highlight function in the editor
2 parents 448622c + a7a4518 commit 3796763

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

aztec/src/main/java/org/wordpress/aztec/Html.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ private void handleStartTag(String tag, Attributes attributes, int nestingLevel)
363363
start(spannableStringBuilder, AztecTextFormat.FORMAT_CODE, attributes);
364364
} else if (tag.equalsIgnoreCase("mark")) {
365365
start(spannableStringBuilder, AztecTextFormat.FORMAT_MARK, attributes);
366+
} else if (tag.equalsIgnoreCase("highlight")) {
367+
start(spannableStringBuilder, AztecTextFormat.FORMAT_HIGHLIGHT, attributes);
366368
} else if (!UnknownHtmlSpan.Companion.getKNOWN_TAGS().contains(tag.toLowerCase())) {
367369
// Initialize a new "Unknown" node
368370
if (contentHandlerLevel == 0) {
@@ -458,6 +460,8 @@ private void handleEndTag(String tag, int nestingLevel) {
458460
end(spannableStringBuilder, AztecTextFormat.FORMAT_CODE);
459461
} else if (tag.equalsIgnoreCase("mark")) {
460462
end(spannableStringBuilder, AztecTextFormat.FORMAT_MARK);
463+
} else if (tag.equalsIgnoreCase("highlight")) {
464+
end(spannableStringBuilder, AztecTextFormat.FORMAT_HIGHLIGHT);
461465
}
462466
}
463467

@@ -616,6 +620,9 @@ private static void end(SpannableStringBuilder text, AztecTextFormat textFormat)
616620
case FORMAT_MARK:
617621
span = (MarkSpan) getLast(text, MarkSpan.class);
618622
break;
623+
case FORMAT_HIGHLIGHT:
624+
span = (HighlightSpan) getLast(text, HighlightSpan.class);
625+
break;
619626
default:
620627
throw new IllegalArgumentException("Style not supported");
621628
}

aztec/src/main/kotlin/org/wordpress/aztec/AztecTagHandler.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,5 @@ class AztecTagHandler(val context: Context, val plugins: List<IAztecPlugin> = Ar
295295
private val VIDEO = "video"
296296
private val AUDIO = "audio"
297297
private val LINE = "hr"
298-
private val MARK = "mark"
299298
}
300299
}

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
18571857
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_CODE, start, end)
18581858
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_BACKGROUND, start, end)
18591859
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_MARK, start, end)
1860+
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_HIGHLIGHT, start, end)
18601861
}
18611862

18621863
fun removeBlockStylesFromRange(start: Int, end: Int, ignoreLineBounds: Boolean = false) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
442442
AztecTextFormat.FORMAT_CODE -> AztecCodeSpan(codeStyle)
443443
AztecTextFormat.FORMAT_BACKGROUND -> AztecBackgroundColorSpan(backgroundSpanColor ?: R.color.background)
444444
AztecTextFormat.FORMAT_HIGHLIGHT -> {
445-
HighlightSpan(highlightStyle = highlightStyle, context = editor.context)
445+
HighlightSpan.create(context = editor.context, defaultStyle = highlightStyle)
446446
}
447447
AztecTextFormat.FORMAT_MARK -> MarkSpan()
448448
else -> AztecStyleSpan(Typeface.NORMAL)

aztec/src/main/kotlin/org/wordpress/aztec/spans/HighlightSpan.kt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,40 @@ import androidx.core.content.ContextCompat
66
import org.wordpress.aztec.AztecAttributes
77
import org.wordpress.aztec.R
88
import org.wordpress.aztec.formatting.InlineFormatter
9+
import org.wordpress.aztec.source.CssStyleFormatter
10+
import org.wordpress.aztec.util.ColorConverter
911

1012
class HighlightSpan(
1113
override var attributes: AztecAttributes = AztecAttributes(),
12-
highlightStyle: InlineFormatter.HighlightStyle = InlineFormatter.HighlightStyle(R.color.grey_lighten_10),
13-
context: Context
14-
) : BackgroundColorSpan(ContextCompat.getColor(context, highlightStyle.color)), IAztecInlineSpan {
14+
val colorHex: Int
15+
) : BackgroundColorSpan(colorHex), IAztecInlineSpan {
16+
override var TAG = HIGHLIGHT_TAG
1517

16-
override var TAG = "highlight"
1718
companion object {
19+
const val HIGHLIGHT_TAG = "highlight"
20+
1821
@JvmStatic
19-
fun create(attributes: AztecAttributes, context: Context) = HighlightSpan(attributes = attributes, context = context)
22+
@JvmOverloads
23+
fun create(attributes: AztecAttributes = AztecAttributes(),
24+
context: Context,
25+
defaultStyle: InlineFormatter.HighlightStyle? = null
26+
) = HighlightSpan(attributes = attributes,
27+
colorHex = buildColor(context, attributes, defaultStyle))
28+
29+
private fun buildColor(context: Context, attrs: AztecAttributes, defaultStyle: InlineFormatter.HighlightStyle?): Int {
30+
return if (CssStyleFormatter.containsStyleAttribute(
31+
attrs,
32+
CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE
33+
)
34+
) {
35+
val att = CssStyleFormatter.getStyleAttribute(attrs,
36+
CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE)
37+
return ColorConverter.getColorInt(att)
38+
} else if (defaultStyle != null) {
39+
ContextCompat.getColor(context, defaultStyle.color)
40+
} else {
41+
ContextCompat.getColor(context, R.color.grey_lighten_10)
42+
}
43+
}
2044
}
2145
}

0 commit comments

Comments
 (0)