feat: handle text-decoration for text styles

This commit is contained in:
Vincent Chan 2022-08-12 16:25:50 +08:00
parent 0f404e5527
commit 61aaa20113

View File

@ -159,10 +159,8 @@ class HTMLToNodesConverter {
} }
final textDecorationStr = cssMap["text-decoration"]; final textDecorationStr = cssMap["text-decoration"];
if (textDecorationStr == "line-through") { if (textDecorationStr != null) {
attrs[StyleKey.strikethrough] = true; _assignTextDecorations(attrs, textDecorationStr);
} else if (textDecorationStr == "underline") {
attrs[StyleKey.underline] = true;
} }
final backgroundColorStr = cssMap["background-color"]; final backgroundColorStr = cssMap["background-color"];
@ -179,6 +177,17 @@ class HTMLToNodesConverter {
return attrs.isEmpty ? null : attrs; return attrs.isEmpty ? null : attrs;
} }
_assignTextDecorations(Attributes attrs, String decorationStr) {
final decorations = decorationStr.split(" ");
for (final d in decorations) {
if (d == "line-through") {
attrs[StyleKey.strikethrough] = true;
} else if (d == "underline") {
attrs[StyleKey.underline] = true;
}
}
}
/// Try to parse the `rgba(red, greed, blue, alpha)` /// Try to parse the `rgba(red, greed, blue, alpha)`
/// from the string. /// from the string.
Color? _tryParseCssColorString(String? colorString) { Color? _tryParseCssColorString(String? colorString) {
@ -424,6 +433,18 @@ class NodesToHTMLConverter {
checked: textNode.attributes["checkbox"] == true); checked: textNode.attributes["checkbox"] == true);
} }
String _textDecorationsFromAttributes(Attributes attributes) {
var textDecoration = <String>[];
if (attributes[StyleKey.strikethrough] == true) {
textDecoration.add("line-through");
}
if (attributes[StyleKey.underline] == true) {
textDecoration.add("underline");
}
return textDecoration.join(" ");
}
String _attributesToCssStyle(Map<String, dynamic> attributes) { String _attributesToCssStyle(Map<String, dynamic> attributes) {
final cssMap = <String, String>{}; final cssMap = <String, String>{};
if (attributes[StyleKey.backgroundColor] != null) { if (attributes[StyleKey.backgroundColor] != null) {
@ -441,12 +462,12 @@ class NodesToHTMLConverter {
if (attributes[StyleKey.bold] == true) { if (attributes[StyleKey.bold] == true) {
cssMap["font-weight"] = "bold"; cssMap["font-weight"] = "bold";
} }
if (attributes[StyleKey.strikethrough] == true) {
cssMap["text-decoration"] = "line-through"; final textDecoration = _textDecorationsFromAttributes(attributes);
} if (textDecoration.isNotEmpty) {
if (attributes[StyleKey.underline] == true) { cssMap["text-decoration"] = textDecoration;
cssMap["text-decoration"] = "underline";
} }
if (attributes[StyleKey.italic] == true) { if (attributes[StyleKey.italic] == true) {
cssMap["font-style"] = "italic"; cssMap["font-style"] = "italic";
} }