Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: table html encoder and decoder added #449

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/src/plugins/html/encoder/parser/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export 'html_node_parser.dart';
export 'image_node_parser.dart';
export 'numbered_list_node_parser.dart';
export 'quote_node_parser.dart';
export 'table_node_parser.dart';
export 'text_node_parser.dart';
export 'todo_list_node_parser.dart';
69 changes: 69 additions & 0 deletions lib/src/plugins/html/encoder/parser/table_node_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:collection/collection.dart';
import 'package:html/dom.dart' as dom;

import '../../../../editor/block_component/table_block_component/util.dart';

class HtmlTableNodeParser extends HTMLNodeParser {
const HtmlTableNodeParser();

@override
String get id => TableBlockKeys.type;

@override
String transformNodeToHTMLString(
Node node, {
required List<HTMLNodeParser> encodeParsers,
}) {
assert(node.type == TableBlockKeys.type);

return toHTMLString(
transformNodeToDomNodes(node, encodeParsers: encodeParsers),
);
}

@override
List<dom.Node> transformNodeToDomNodes(
Node node, {
required List<HTMLNodeParser> encodeParsers,
}) {
final int rowsLen = node.attributes['rowsLen'],
alihassan143 marked this conversation as resolved.
Show resolved Hide resolved
colsLen = node.attributes['colsLen'];
final List<dom.Node> domNodes = [];

for (var i = 0; i < rowsLen; i++) {
final List<dom.Node> nodes = [];
for (var j = 0; j < colsLen; j++) {
final Node cell = getCellNode(node, j, i)!;

for (final childnode in cell.children) {
HTMLNodeParser? parser = encodeParsers.firstWhereOrNull(
(element) => element.id == childnode.type,
);

if (parser != null) {
nodes.add(
wrapChildrenNodesWithTagName(
HTMLTags.tabledata,
childNodes: parser.transformNodeToDomNodes(
childnode,
encodeParsers: encodeParsers,
),
),
);
}
}
}
final rowelement =
wrapChildrenNodesWithTagName(HTMLTags.tableRow, childNodes: nodes);

domNodes.add(rowelement);
}

final element =
wrapChildrenNodesWithTagName(HTMLTags.table, childNodes: domNodes);
return [
element,
];
}
}
1 change: 1 addition & 0 deletions lib/src/plugins/html/html_document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ String documentToHTML(
const HTMLQuoteNodeParser(),
const HTMLHeadingNodeParser(),
const HTMLImageNodeParser(),
const HtmlTableNodeParser()
],
).encode(document);
}
Expand Down
Loading