Skip to content

Commit

Permalink
Merge pull request #1057 from wordpress-mobile/fix/highlight-action
Browse files Browse the repository at this point in the history
Fix highlight function in the editor
  • Loading branch information
planarvoid authored Aug 11, 2023
2 parents 448622c + a7a4518 commit 3796763
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
7 changes: 7 additions & 0 deletions aztec/src/main/java/org/wordpress/aztec/Html.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ private void handleStartTag(String tag, Attributes attributes, int nestingLevel)
start(spannableStringBuilder, AztecTextFormat.FORMAT_CODE, attributes);
} else if (tag.equalsIgnoreCase("mark")) {
start(spannableStringBuilder, AztecTextFormat.FORMAT_MARK, attributes);
} else if (tag.equalsIgnoreCase("highlight")) {
start(spannableStringBuilder, AztecTextFormat.FORMAT_HIGHLIGHT, attributes);
} else if (!UnknownHtmlSpan.Companion.getKNOWN_TAGS().contains(tag.toLowerCase())) {
// Initialize a new "Unknown" node
if (contentHandlerLevel == 0) {
Expand Down Expand Up @@ -458,6 +460,8 @@ private void handleEndTag(String tag, int nestingLevel) {
end(spannableStringBuilder, AztecTextFormat.FORMAT_CODE);
} else if (tag.equalsIgnoreCase("mark")) {
end(spannableStringBuilder, AztecTextFormat.FORMAT_MARK);
} else if (tag.equalsIgnoreCase("highlight")) {
end(spannableStringBuilder, AztecTextFormat.FORMAT_HIGHLIGHT);
}
}

Expand Down Expand Up @@ -616,6 +620,9 @@ private static void end(SpannableStringBuilder text, AztecTextFormat textFormat)
case FORMAT_MARK:
span = (MarkSpan) getLast(text, MarkSpan.class);
break;
case FORMAT_HIGHLIGHT:
span = (HighlightSpan) getLast(text, HighlightSpan.class);
break;
default:
throw new IllegalArgumentException("Style not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,5 @@ class AztecTagHandler(val context: Context, val plugins: List<IAztecPlugin> = Ar
private val VIDEO = "video"
private val AUDIO = "audio"
private val LINE = "hr"
private val MARK = "mark"
}
}
1 change: 1 addition & 0 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_CODE, start, end)
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_BACKGROUND, start, end)
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_MARK, start, end)
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_HIGHLIGHT, start, end)
}

fun removeBlockStylesFromRange(start: Int, end: Int, ignoreLineBounds: Boolean = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h
AztecTextFormat.FORMAT_CODE -> AztecCodeSpan(codeStyle)
AztecTextFormat.FORMAT_BACKGROUND -> AztecBackgroundColorSpan(backgroundSpanColor ?: R.color.background)
AztecTextFormat.FORMAT_HIGHLIGHT -> {
HighlightSpan(highlightStyle = highlightStyle, context = editor.context)
HighlightSpan.create(context = editor.context, defaultStyle = highlightStyle)
}
AztecTextFormat.FORMAT_MARK -> MarkSpan()
else -> AztecStyleSpan(Typeface.NORMAL)
Expand Down
34 changes: 29 additions & 5 deletions aztec/src/main/kotlin/org/wordpress/aztec/spans/HighlightSpan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,40 @@ import androidx.core.content.ContextCompat
import org.wordpress.aztec.AztecAttributes
import org.wordpress.aztec.R
import org.wordpress.aztec.formatting.InlineFormatter
import org.wordpress.aztec.source.CssStyleFormatter
import org.wordpress.aztec.util.ColorConverter

class HighlightSpan(
override var attributes: AztecAttributes = AztecAttributes(),
highlightStyle: InlineFormatter.HighlightStyle = InlineFormatter.HighlightStyle(R.color.grey_lighten_10),
context: Context
) : BackgroundColorSpan(ContextCompat.getColor(context, highlightStyle.color)), IAztecInlineSpan {
val colorHex: Int
) : BackgroundColorSpan(colorHex), IAztecInlineSpan {
override var TAG = HIGHLIGHT_TAG

override var TAG = "highlight"
companion object {
const val HIGHLIGHT_TAG = "highlight"

@JvmStatic
fun create(attributes: AztecAttributes, context: Context) = HighlightSpan(attributes = attributes, context = context)
@JvmOverloads
fun create(attributes: AztecAttributes = AztecAttributes(),
context: Context,
defaultStyle: InlineFormatter.HighlightStyle? = null
) = HighlightSpan(attributes = attributes,
colorHex = buildColor(context, attributes, defaultStyle))

private fun buildColor(context: Context, attrs: AztecAttributes, defaultStyle: InlineFormatter.HighlightStyle?): Int {
return if (CssStyleFormatter.containsStyleAttribute(
attrs,
CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE
)
) {
val att = CssStyleFormatter.getStyleAttribute(attrs,
CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE)
return ColorConverter.getColorInt(att)
} else if (defaultStyle != null) {
ContextCompat.getColor(context, defaultStyle.color)
} else {
ContextCompat.getColor(context, R.color.grey_lighten_10)
}
}
}
}

0 comments on commit 3796763

Please sign in to comment.