mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
refactor: color extensions
This commit is contained in:
parent
a3ffbe2f00
commit
e826006fa7
@ -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)';
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user