Skip to content

Fix highlight function in the editor #1057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
}