mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: add comments to HTMLConvertert
This commit is contained in:
parent
0c0039ca95
commit
a26b693807
@ -31,6 +31,12 @@ extension on Color {
|
|||||||
/// Converting the HTML to nodes
|
/// Converting the HTML to nodes
|
||||||
class HTMLToNodesConverter {
|
class HTMLToNodesConverter {
|
||||||
final html.Document _document;
|
final html.Document _document;
|
||||||
|
|
||||||
|
/// This flag is used for parsing HTML pasting from Google Docs
|
||||||
|
/// Google docs wraps the the content inside the `<b></b>` tag. It's strange.
|
||||||
|
///
|
||||||
|
/// If a `<b>` element is parsing in the <p>, we regard it as as text spans.
|
||||||
|
/// Otherwise, it's parsed as a container.
|
||||||
bool _inParagraph = false;
|
bool _inParagraph = false;
|
||||||
|
|
||||||
HTMLToNodesConverter(String htmlString) : _document = parse(htmlString);
|
HTMLToNodesConverter(String htmlString) : _document = parse(htmlString);
|
||||||
@ -51,7 +57,7 @@ class HTMLToNodesConverter {
|
|||||||
child.localName == tagStrong) {
|
child.localName == tagStrong) {
|
||||||
_handleRichTextElement(delta, child);
|
_handleRichTextElement(delta, child);
|
||||||
} else if (child.localName == tagBold) {
|
} else if (child.localName == tagBold) {
|
||||||
// Google docs wraps the the content inside the <b></b> tag.
|
// Google docs wraps the the content inside the `<b></b>` tag.
|
||||||
// It's strange
|
// It's strange
|
||||||
if (!_inParagraph) {
|
if (!_inParagraph) {
|
||||||
result.addAll(_handleBTag(child));
|
result.addAll(_handleBTag(child));
|
||||||
@ -152,6 +158,8 @@ class HTMLToNodesConverter {
|
|||||||
return attrs.isEmpty ? null : attrs;
|
return attrs.isEmpty ? null : attrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Try to parse the `rgba(red, greed, blue, alpha)`
|
||||||
|
/// from the string.
|
||||||
Color? _tryParseCssColorString(String? colorString) {
|
Color? _tryParseCssColorString(String? colorString) {
|
||||||
if (colorString == null) {
|
if (colorString == null) {
|
||||||
return null;
|
return null;
|
||||||
@ -200,6 +208,10 @@ class HTMLToNodesConverter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A container contains a <input type="checkbox" > will
|
||||||
|
/// be regarded as a checkbox block.
|
||||||
|
///
|
||||||
|
/// A container contains a <img /> will be regarded as a image block
|
||||||
Node _handleRichText(html.Element element,
|
Node _handleRichText(html.Element element,
|
||||||
[Map<String, dynamic>? attributes]) {
|
[Map<String, dynamic>? attributes]) {
|
||||||
final image = element.querySelector(tagImage);
|
final image = element.querySelector(tagImage);
|
||||||
@ -288,11 +300,23 @@ class HTMLToNodesConverter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// [NodesToHTMLConverter] is used to convert the nodes to HTML.
|
||||||
|
/// Can be used to copy & paste, exporting the document.
|
||||||
class NodesToHTMLConverter {
|
class NodesToHTMLConverter {
|
||||||
final List<Node> nodes;
|
final List<Node> nodes;
|
||||||
final int? startOffset;
|
final int? startOffset;
|
||||||
final int? endOffset;
|
final int? endOffset;
|
||||||
final List<html.Node> _result = [];
|
final List<html.Node> _result = [];
|
||||||
|
|
||||||
|
/// According to the W3C specs. The bullet list should be wrapped as
|
||||||
|
///
|
||||||
|
/// <ul>
|
||||||
|
/// <li>xxx</li>
|
||||||
|
/// <li>xxx</li>
|
||||||
|
/// <li>xxx</li>
|
||||||
|
/// </ul>
|
||||||
|
///
|
||||||
|
/// This container is used to save the list elements temporarily.
|
||||||
html.Element? _stashListContainer;
|
html.Element? _stashListContainer;
|
||||||
|
|
||||||
NodesToHTMLConverter(
|
NodesToHTMLConverter(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user