diff --git a/frontend/app_flowy/packages/flowy_editor/lib/infra/html_converter.dart b/frontend/app_flowy/packages/flowy_editor/lib/infra/html_converter.dart index e39d082607..608020e327 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/infra/html_converter.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/infra/html_converter.dart @@ -2,6 +2,7 @@ import 'dart:collection'; import 'package:flowy_editor/document/node.dart'; import 'package:flowy_editor/document/text_delta.dart'; +import 'package:flutter/foundation.dart'; import 'package:html/parser.dart' show parse; import 'package:html/dom.dart' as html; @@ -12,10 +13,24 @@ class HTMLConverter { List toNodes() { final result = []; + final delta = Delta(); - final bodyChildren = _document.body?.children ?? []; - for (final child in bodyChildren) { - _handleElement(result, child); + for (final child in _document.body?.nodes.toList() ?? []) { + if (child is html.Element) { + if (child.localName == "span") { + delta.insert(child.text); + } else if (child.localName == "strong") { + delta.insert(child.text, {"bold": true}); + } else { + _handleElement(result, child); + } + } else { + delta.insert(child.text ?? ""); + } + } + + if (delta.operations.isNotEmpty) { + result.add(TextNode(type: "text", delta: delta)); } return result; @@ -44,34 +59,43 @@ class HTMLConverter { } _handleParagraph(List nodes, html.Element element) { - for (final child in element.children) { - if (child.localName == "a") { - _handleAnchorLink(nodes, child); + final image = element.querySelector("img"); + if (image != null) { + _handleImage(nodes, image); + return; + } + + var delta = Delta(); + + for (final child in element.nodes.toList()) { + if (child is html.Element) { + if (child.localName == "a") { + final hyperLink = child.attributes["href"]; + Map? attributes; + if (hyperLink != null) { + attributes = {"href": hyperLink}; + } + delta.insert(child.text, attributes); + } else { + delta.insert(child.text); + } + } else { + delta.insert(child.text ?? ""); } } - final delta = Delta(); - delta.insert(element.text); if (delta.operations.isNotEmpty) { nodes.add(TextNode(type: "text", delta: delta)); } } - _handleAnchorLink(List nodes, html.Element element) { - for (final child in element.children) { - if (child.localName == "img") { - _handleImage(nodes, child); - return; - } - } - } - _handleImage(List nodes, html.Element element) { final src = element.attributes["src"]; final attributes = {}; if (src != null) { attributes["image_src"] = src; } + debugPrint("insert image: $src"); nodes.add( Node(type: "image", attributes: attributes, children: LinkedList())); }