From 61aaa20113a16914593a4c3001098f8015764e86 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Fri, 12 Aug 2022 16:25:50 +0800 Subject: [PATCH] feat: handle text-decoration for text styles --- .../lib/src/infra/html_converter.dart | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/infra/html_converter.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/infra/html_converter.dart index 96c001a318..cc7fd949ae 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/src/infra/html_converter.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/src/infra/html_converter.dart @@ -159,10 +159,8 @@ class HTMLToNodesConverter { } final textDecorationStr = cssMap["text-decoration"]; - if (textDecorationStr == "line-through") { - attrs[StyleKey.strikethrough] = true; - } else if (textDecorationStr == "underline") { - attrs[StyleKey.underline] = true; + if (textDecorationStr != null) { + _assignTextDecorations(attrs, textDecorationStr); } final backgroundColorStr = cssMap["background-color"]; @@ -179,6 +177,17 @@ class HTMLToNodesConverter { 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)` /// from the string. Color? _tryParseCssColorString(String? colorString) { @@ -424,6 +433,18 @@ class NodesToHTMLConverter { checked: textNode.attributes["checkbox"] == true); } + String _textDecorationsFromAttributes(Attributes attributes) { + var textDecoration = []; + if (attributes[StyleKey.strikethrough] == true) { + textDecoration.add("line-through"); + } + if (attributes[StyleKey.underline] == true) { + textDecoration.add("underline"); + } + + return textDecoration.join(" "); + } + String _attributesToCssStyle(Map attributes) { final cssMap = {}; if (attributes[StyleKey.backgroundColor] != null) { @@ -441,12 +462,12 @@ class NodesToHTMLConverter { if (attributes[StyleKey.bold] == true) { cssMap["font-weight"] = "bold"; } - if (attributes[StyleKey.strikethrough] == true) { - cssMap["text-decoration"] = "line-through"; - } - if (attributes[StyleKey.underline] == true) { - cssMap["text-decoration"] = "underline"; + + final textDecoration = _textDecorationsFromAttributes(attributes); + if (textDecoration.isNotEmpty) { + cssMap["text-decoration"] = textDecoration; } + if (attributes[StyleKey.italic] == true) { cssMap["font-style"] = "italic"; }