Skip to content

Commit

Permalink
chore: provide an option to disable color parser (#784)
Browse files Browse the repository at this point in the history
* chore: provide an option to disable color parser

* feat: support table cell color
  • Loading branch information
LucasXu0 authored Apr 26, 2024
1 parent 28655b4 commit c8f1c12
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ class TableCellBlockKeys {
static const String colBackgroundColor = 'colBackgroundColor';
}

typedef TableBlockCellComponentColorBuilder = Color? Function(
BuildContext context,
Node node,
);

class TableCellBlockComponentBuilder extends BlockComponentBuilder {
TableCellBlockComponentBuilder({
super.configuration,
this.menuBuilder,
this.colorBuilder,
});

final TableBlockComponentMenuBuilder? menuBuilder;
final TableBlockCellComponentColorBuilder? colorBuilder;

@override
BlockComponentWidget build(BlockComponentContext blockComponentContext) {
Expand All @@ -38,6 +45,7 @@ class TableCellBlockComponentBuilder extends BlockComponentBuilder {
node: node,
configuration: configuration,
menuBuilder: menuBuilder,
colorBuilder: colorBuilder,
showActions: showActions(node),
actionBuilder: (context, state) => actionBuilder(
blockComponentContext,
Expand All @@ -58,12 +66,14 @@ class TableCelBlockWidget extends BlockComponentStatefulWidget {
super.key,
required super.node,
this.menuBuilder,
this.colorBuilder,
super.showActions,
super.actionBuilder,
super.configuration = const BlockComponentConfiguration(),
});

final TableBlockComponentMenuBuilder? menuBuilder;
final TableBlockCellComponentColorBuilder? colorBuilder;

@override
State<TableCelBlockWidget> createState() => _TableCeBlockWidgetState();
Expand All @@ -87,6 +97,7 @@ class _TableCeBlockWidgetState extends State<TableCelBlockWidget> {
),
color: context.select(
(Node n) =>
widget.colorBuilder?.call(context, n) ??
(n.attributes[TableCellBlockKeys.colBackgroundColor]
as String?)
?.tryToColor() ??
Expand Down
11 changes: 7 additions & 4 deletions lib/src/plugins/html/html_document_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import 'package:html/parser.dart' show parse;
class DocumentHTMLDecoder extends Converter<String, Document> {
DocumentHTMLDecoder();

// Set to true to enable parsing color from HTML
static bool enableColorParse = true;

@override
Document convert(String input) {
final document = parse(input);
Expand Down Expand Up @@ -54,7 +57,7 @@ class DocumentHTMLDecoder extends Converter<String, Document> {
}
delta.insert(domNode.text);
} else {
assert(false, 'Unknown node type: $domNode');
Log.editor.debug('Unknown node type: $domNode');
}
}
if (delta.isNotEmpty) {
Expand Down Expand Up @@ -418,7 +421,7 @@ class DocumentHTMLDecoder extends Converter<String, Document> {

// background color
final backgroundColor = css['background-color'];
if (backgroundColor != null) {
if (enableColorParse && backgroundColor != null) {
final highlightColor = backgroundColor.tryToColor()?.toHex();
if (highlightColor != null) {
attributes[AppFlowyRichTextKeys.backgroundColor] = highlightColor;
Expand All @@ -427,7 +430,7 @@ class DocumentHTMLDecoder extends Converter<String, Document> {

// background
final background = css['background'];
if (background != null) {
if (enableColorParse && background != null) {
final highlightColor = background.tryToColor()?.toHex();
if (highlightColor != null) {
attributes[AppFlowyRichTextKeys.backgroundColor] = highlightColor;
Expand All @@ -436,7 +439,7 @@ class DocumentHTMLDecoder extends Converter<String, Document> {

// color
final color = css['color'];
if (color != null) {
if (enableColorParse && color != null) {
final textColor = color.tryToColor()?.toHex();
if (textColor != null) {
attributes[AppFlowyRichTextKeys.textColor] = textColor;
Expand Down

0 comments on commit c8f1c12

Please sign in to comment.