Ver código fonte

refactor: color extensions

Vincent Chan 2 anos atrás
pai
commit
e826006fa7

+ 36 - 0
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)';
+  }
+}

+ 4 - 39
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,