Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

refactor: color extensions

Vincent Chane826006fa704
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/extensions/color_extension.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/extensions/color_extension.dart
new file mode 100644
index 0000000000..7228127104
--- /dev/null
+++ b/frontend/app_flowy/packages/flowy_editor/lib/src/extensions/color_extension.dart
@@ -0,0 +1,36 @@
+import 'package:flutter/painting.dart';
+
+extension ColorExtension on Color {
+ /// Try to parse the `rgba(red, greed, blue, alpha)`
+ /// from the string.
+ static Color? tryFromRgbaString(String colorString) {
+ final reg = RegExp(r'rgba\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)');
+ final match = reg.firstMatch(colorString);
+ if (match == null) {
+ return null;
+ }
+
+ if (match.groupCount < 4) {
+ return null;
+ }
+ final redStr = match.group(1);
+ final greenStr = match.group(2);
+ final blueStr = match.group(3);
+ final alphaStr = match.group(4);
+
+ final red = redStr != null ? int.tryParse(redStr) : null;
+ final green = greenStr != null ? int.tryParse(greenStr) : null;
+ final blue = blueStr != null ? int.tryParse(blueStr) : null;
+ final alpha = alphaStr != null ? int.tryParse(alphaStr) : null;
+
+ if (red == null || green == null || blue == null || alpha == null) {
+ return null;
+ }
+
+ return Color.fromARGB(alpha, red, green, blue);
+ }
+
+ String toRgbaString() {
+ return 'rgba($red, $green, $blue, $alpha)';
+ }
+}
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 59efabe364..0aa8d4fd13 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
@@ -5,6 +5,7 @@ import 'package:flowy_editor/src/document/attributes.dart';
import 'package:flowy_editor/src/document/node.dart';
import 'package:flowy_editor/src/document/text_delta.dart';
import 'package:flowy_editor/src/render/rich_text/rich_text_style.dart';
+import 'package:flowy_editor/src/extensions/color_extension.dart';
import 'package:flutter/material.dart';
import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart' as html;
@@ -39,12 +40,6 @@ class HTMLTag {
}
}
-extension on Color {
- String toRgbaString() {
- return 'rgba($red, $green, $blue, $alpha)';
- }
-}
-
/// Converting the HTML to nodes
class HTMLToNodesConverter {
final html.Document _document;
@@ -192,7 +187,9 @@ class HTMLToNodesConverter {
}
final backgroundColorStr = cssMap["background-color"];
- final backgroundColor = _tryParseCssColorString(backgroundColorStr);
+ final backgroundColor = backgroundColorStr == null
+ ? null
+ : ColorExtension.tryFromRgbaString(backgroundColorStr);
if (backgroundColor != null) {
attrs[StyleKey.backgroundColor] =
'0x${backgroundColor.value.toRadixString(16)}';
@@ -216,38 +213,6 @@ class HTMLToNodesConverter {
}
}
- /// Try to parse the `rgba(red, greed, blue, alpha)`
- /// from the string.
- Color? _tryParseCssColorString(String? colorString) {
- if (colorString == null) {
- return null;
- }
- final reg = RegExp(r'rgba\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)');
- final match = reg.firstMatch(colorString);
- if (match == null) {
- return null;
- }
-
- if (match.groupCount < 4) {
- return null;
- }
- final redStr = match.group(1);
- final greenStr = match.group(2);
- final blueStr = match.group(3);
- final alphaStr = match.group(4);
-
- final red = redStr != null ? int.tryParse(redStr) : null;
- final green = greenStr != null ? int.tryParse(greenStr) : null;
- final blue = blueStr != null ? int.tryParse(blueStr) : null;
- final alpha = alphaStr != null ? int.tryParse(alphaStr) : null;
-
- if (red == null || green == null || blue == null || alpha == null) {
- return null;
- }
-
- return Color.fromARGB(alpha, red, green, blue);
- }
-
_handleRichTextElement(Delta delta, html.Element element) {
if (element.localName == HTMLTag.span) {
delta.insert(element.text,