-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: table html encoder and decoder added (#449)
* feat: table html encoder and decoder added * feat: html table encoder test added * feat: fix the test ran issues * fix: removed unused code
- Loading branch information
1 parent
89b93d8
commit 522f17a
Showing
9 changed files
with
453 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
lib/src/plugins/html/encoder/parser/table_node_parser.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[TableBlockKeys.rowsLen], | ||
colsLen = node.attributes[TableBlockKeys.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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.