-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor the html_node_parser.dart
- Loading branch information
Showing
17 changed files
with
221 additions
and
219 deletions.
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
46 changes: 22 additions & 24 deletions
46
lib/src/plugins/html/encoder/parser/bulleted_list_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 |
---|---|---|
@@ -1,43 +1,41 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:html/dom.dart' as dom; | ||
|
||
class HtmlBulletedListNodeParser extends HtmlNodeParser { | ||
class HtmlBulletedListNodeParser extends HTMLNodeParser { | ||
const HtmlBulletedListNodeParser(); | ||
|
||
@override | ||
String get id => BulletedListBlockKeys.type; | ||
|
||
@override | ||
String transform(Node node, {required List<HtmlNodeParser> encodeParsers}) { | ||
String transformNodeToHTMLString( | ||
Node node, { | ||
required List<HTMLNodeParser> encodeParsers, | ||
}) { | ||
assert(node.type == BulletedListBlockKeys.type); | ||
|
||
return toHTMLString(htmlNodes(node, encodeParsers: encodeParsers)); | ||
return toHTMLString( | ||
transformNodeToDomNodes(node, encodeParsers: encodeParsers), | ||
); | ||
} | ||
|
||
@override | ||
List<dom.Node> htmlNodes( | ||
List<dom.Node> transformNodeToDomNodes( | ||
Node node, { | ||
required List<HtmlNodeParser> encodeParsers, | ||
required List<HTMLNodeParser> encodeParsers, | ||
}) { | ||
final List<dom.Node> result = []; | ||
final delta = node.delta; | ||
if (delta == null) { | ||
throw Exception('Delta is null'); | ||
} | ||
final convertedNodes = DeltaHtmlEncoder().convert(delta); | ||
const tagName = HTMLTags.list; | ||
if (node.children.isNotEmpty) { | ||
convertedNodes.addAll( | ||
childrenNodes(node.children.toList(), encodeParsers: encodeParsers), | ||
); | ||
} | ||
final element = insertText(tagName, childNodes: convertedNodes); | ||
|
||
final stashListContainer = dom.Element.tag( | ||
HTMLTags.unorderedList, | ||
final delta = node.delta ?? Delta(); | ||
final domNodes = deltaHTMLEncoder.convert(delta); | ||
domNodes.addAll( | ||
childrenNodes( | ||
node.children, | ||
encodeParsers: encodeParsers, | ||
), | ||
); | ||
stashListContainer.append(element); | ||
result.add(stashListContainer); | ||
return result; | ||
|
||
final element = insertText(HTMLTags.list, childNodes: domNodes); | ||
return [ | ||
dom.Element.tag(HTMLTags.unorderedList)..append(element), | ||
]; | ||
} | ||
} |
38 changes: 17 additions & 21 deletions
38
lib/src/plugins/html/encoder/parser/heading_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 |
---|---|---|
@@ -1,38 +1,34 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:html/dom.dart' as dom; | ||
|
||
class HtmlHeadingNodeParser extends HtmlNodeParser { | ||
class HtmlHeadingNodeParser extends HTMLNodeParser { | ||
const HtmlHeadingNodeParser(); | ||
|
||
@override | ||
String get id => HeadingBlockKeys.type; | ||
|
||
@override | ||
String transform(Node node, {required List<HtmlNodeParser> encodeParsers}) { | ||
return toHTMLString(htmlNodes(node, encodeParsers: encodeParsers)); | ||
String transformNodeToHTMLString( | ||
Node node, { | ||
required List<HTMLNodeParser> encodeParsers, | ||
}) { | ||
return toHTMLString( | ||
transformNodeToDomNodes(node, encodeParsers: encodeParsers), | ||
); | ||
} | ||
|
||
@override | ||
List<dom.Node> htmlNodes( | ||
List<dom.Node> transformNodeToDomNodes( | ||
Node node, { | ||
required List<HtmlNodeParser> encodeParsers, | ||
required List<HTMLNodeParser> encodeParsers, | ||
}) { | ||
final delta = node.delta; | ||
final attribute = node.attributes; | ||
final List<dom.Node> result = []; | ||
if (delta == null) { | ||
assert(false, 'Delta is null'); | ||
} | ||
final convertedNodes = DeltaHtmlEncoder().convert(delta!); | ||
if (node.children.isNotEmpty) { | ||
convertedNodes.addAll( | ||
childrenNodes(node.children.toList(), encodeParsers: encodeParsers), | ||
); | ||
} | ||
String tagName = "h${attribute[HeadingBlockKeys.level]}"; | ||
|
||
final delta = node.delta ?? Delta(); | ||
final convertedNodes = deltaHTMLEncoder.convert(delta); | ||
convertedNodes.addAll( | ||
childrenNodes(node.children, encodeParsers: encodeParsers), | ||
); | ||
final tagName = 'h${node.attributes[HeadingBlockKeys.level]}'; | ||
final element = insertText(tagName, childNodes: convertedNodes); | ||
result.add(element); | ||
return result; | ||
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
File renamed without changes.
48 changes: 27 additions & 21 deletions
48
lib/src/plugins/html/encoder/parser/image_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 |
---|---|---|
@@ -1,42 +1,48 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:html/dom.dart' as dom; | ||
|
||
class HtmlImageNodeParser extends HtmlNodeParser { | ||
class HtmlImageNodeParser extends HTMLNodeParser { | ||
const HtmlImageNodeParser(); | ||
|
||
@override | ||
String get id => ImageBlockKeys.type; | ||
|
||
@override | ||
String transform(Node node, {required List<HtmlNodeParser> encodeParsers}) { | ||
return toHTMLString(htmlNodes(node, encodeParsers: encodeParsers)); | ||
String transformNodeToHTMLString( | ||
Node node, { | ||
required List<HTMLNodeParser> encodeParsers, | ||
}) { | ||
return toHTMLString( | ||
transformNodeToDomNodes(node, encodeParsers: encodeParsers), | ||
); | ||
} | ||
|
||
@override | ||
List<dom.Node> htmlNodes( | ||
List<dom.Node> transformNodeToDomNodes( | ||
Node node, { | ||
required List<HtmlNodeParser> encodeParsers, | ||
required List<HTMLNodeParser> encodeParsers, | ||
}) { | ||
final List<dom.Node> result = []; | ||
|
||
final anchor = dom.Element.tag(HTMLTags.image); | ||
anchor.attributes["src"] = node.attributes[ImageBlockKeys.url]; | ||
if (node.attributes[ImageBlockKeys.height] != null) { | ||
anchor.attributes["height"] = node.attributes[ImageBlockKeys.height]; | ||
} | ||
if (node.attributes[ImageBlockKeys.width] != null) { | ||
anchor.attributes["width"] = node.attributes[ImageBlockKeys.width]; | ||
anchor.attributes['src'] = node.attributes[ImageBlockKeys.url]; | ||
|
||
final height = node.attributes[ImageBlockKeys.height]; | ||
if (height != null) { | ||
anchor.attributes['height'] = height; | ||
} | ||
if (node.attributes[ImageBlockKeys.align] != null) { | ||
anchor.attributes["align"] = node.attributes[ImageBlockKeys.align]; | ||
|
||
final width = node.attributes[ImageBlockKeys.width]; | ||
if (width != null) { | ||
anchor.attributes['width'] = width; | ||
} | ||
result.add(anchor); | ||
|
||
if (node.children.isNotEmpty) { | ||
result.addAll( | ||
childrenNodes(node.children.toList(), encodeParsers: encodeParsers), | ||
); | ||
final align = node.attributes[ImageBlockKeys.align]; | ||
if (align != null) { | ||
anchor.attributes['align'] = align; | ||
} | ||
return result; | ||
|
||
return [ | ||
anchor, | ||
...childrenNodes(node.children.toList(), encodeParsers: encodeParsers), | ||
]; | ||
} | ||
} |
43 changes: 19 additions & 24 deletions
43
lib/src/plugins/html/encoder/parser/numbered_list_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 |
---|---|---|
@@ -1,43 +1,38 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:html/dom.dart' as dom; | ||
|
||
class HtmlNumberedListNodeParser extends HtmlNodeParser { | ||
class HtmlNumberedListNodeParser extends HTMLNodeParser { | ||
const HtmlNumberedListNodeParser(); | ||
|
||
@override | ||
String get id => NumberedListBlockKeys.type; | ||
|
||
@override | ||
String transform(Node node, {required List<HtmlNodeParser> encodeParsers}) { | ||
String transformNodeToHTMLString( | ||
Node node, { | ||
required List<HTMLNodeParser> encodeParsers, | ||
}) { | ||
assert(node.type == NumberedListBlockKeys.type); | ||
|
||
return toHTMLString(htmlNodes(node, encodeParsers: encodeParsers)); | ||
return toHTMLString( | ||
transformNodeToDomNodes(node, encodeParsers: encodeParsers), | ||
); | ||
} | ||
|
||
@override | ||
List<dom.Node> htmlNodes( | ||
List<dom.Node> transformNodeToDomNodes( | ||
Node node, { | ||
required List<HtmlNodeParser> encodeParsers, | ||
required List<HTMLNodeParser> encodeParsers, | ||
}) { | ||
final List<dom.Node> result = []; | ||
final delta = node.delta; | ||
if (delta == null) { | ||
throw Exception('Delta is null'); | ||
} | ||
final convertedNodes = DeltaHtmlEncoder().convert(delta); | ||
const tagName = HTMLTags.list; | ||
if (node.children.isNotEmpty) { | ||
convertedNodes.addAll( | ||
childrenNodes(node.children.toList(), encodeParsers: encodeParsers), | ||
); | ||
} | ||
final element = insertText(tagName, childNodes: convertedNodes); | ||
|
||
final stashListContainer = dom.Element.tag( | ||
HTMLTags.orderedList, | ||
final delta = node.delta ?? Delta(); | ||
final domNodes = deltaHTMLEncoder.convert(delta); | ||
domNodes.addAll( | ||
childrenNodes(node.children, encodeParsers: encodeParsers), | ||
); | ||
stashListContainer.append(element); | ||
result.add(stashListContainer); | ||
return result; | ||
|
||
final element = insertText(HTMLTags.list, childNodes: domNodes); | ||
return [ | ||
dom.Element.tag(HTMLTags.orderedList)..append(element), | ||
]; | ||
} | ||
} |
Oops, something went wrong.