diff --git a/documentation/importing.md b/documentation/importing.md index 7c8acca72..6b09610fa 100644 --- a/documentation/importing.md +++ b/documentation/importing.md @@ -1,16 +1,27 @@ # Importing data -For now, we have supported three ways to import data to initialize AppFlowy Editor. +Currently, we have supported three methods for importing data to initialize AppFlowy Editor. 1. From AppFlowy Document JSON ```dart -const document = r'''{"document":{"type":"editor","children":[{"type":"text","attributes":{"subtype":"heading","heading":"h1"},"delta":[{"insert":"Hello AppFlowy!"}]}]}}'''; -final json = jsonDecode(document); +const document = r'''{ + "document": { + "type": "page", + "children": [ + { + "type": "heading", + "data": { + "delta": [{ "insert": "Hello AppFlowy!" }], + "level": 1 + } + } + ] + } +}'''; +final json = Map.from(jsonDecode(document)); final editorState = EditorState( - document: Document.fromJson( - Map.from(json), - ), + document: Document.fromJson(json), ); ``` @@ -19,18 +30,19 @@ final editorState = EditorState( ```dart const markdown = r'''# Hello AppFlowy!'''; final editorState = EditorState( - document: markdownToDocument(markdown), + document: markdownToDocument(markdown), ); ``` 3. From Quill Delta ```dart -const delta = r'''[{"insert":"Hello AppFlowy!"},{"attributes":{"header":1},"insert":"\n"}]'''; -final json = jsonDecode(delta); -final editorState = EditorState( - document: DeltaDocumentConvert().convertFromJSON(json), -); +const json = r'''[{"insert":"Hello AppFlowy!"},{"attributes":{"header":1},"insert":"\n"}]'''; +final delta = Delta.fromJson(jsonDecode(json)); +final document = quillDeltaEncoder.convert(delta); +final editorState = EditorState(document: document); ``` -For more details, please refer to the function `_importFile` through this [link](https://github.com/AppFlowy-IO/appflowy-editor/blob/main/example/lib/home_page.dart). \ No newline at end of file +> Notes: Some styles, such as font-size, font-family and text-align, are not supported yet. + +For more details, please refer to the function `_importFile` through this [link](https://github.com/AppFlowy-IO/appflowy-editor/blob/main/example/lib/home_page.dart#L298). \ No newline at end of file diff --git a/example/lib/home_page.dart b/example/lib/home_page.dart index 794a1ef83..aa54ed3f7 100644 --- a/example/lib/home_page.dart +++ b/example/lib/home_page.dart @@ -11,7 +11,7 @@ import 'package:flutter/services.dart'; import 'package:universal_html/html.dart' as html; enum ExportFileType { - json, + documentJson, markdown, html, delta, @@ -20,7 +20,7 @@ enum ExportFileType { extension on ExportFileType { String get extension { switch (this) { - case ExportFileType.json: + case ExportFileType.documentJson: case ExportFileType.delta: return 'json'; case ExportFileType.markdown: @@ -123,45 +123,19 @@ class _HomePageState extends State { _loadEditor(context, jsonString); }), - // Text Robot -// _buildSeparator(context, 'Text Robot'), -// _buildListTile(context, 'Type Text Automatically', () async { -// final jsonString = Future.value( -// jsonEncode( -// EditorState.blank(withInitialText: true).document.toJson(), -// ).toString(), -// ); -// await _loadEditor(context, jsonString); - -// Future.delayed(const Duration(seconds: 2), () { -// final textRobot = TextRobot( -// editorState: _editorState, -// ); -// textRobot.insertText( -// ''' -// Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC -// "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" - -// 1914 translation by H. Rackham -// "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" -// ''', -// ); -// }); -// }), - // Encoder Demo - _buildSeparator(context, 'Encoder Demo'), + _buildSeparator(context, 'Export To X Demo'), _buildListTile(context, 'Export To JSON', () { - _exportFile(_editorState, ExportFileType.json); + _exportFile(_editorState, ExportFileType.documentJson); }), _buildListTile(context, 'Export to Markdown', () { _exportFile(_editorState, ExportFileType.markdown); }), // Decoder Demo - _buildSeparator(context, 'Decoder Demo'), - _buildListTile(context, 'Import From JSON', () { - _importFile(ExportFileType.json); + _buildSeparator(context, 'Import From X Demo'), + _buildListTile(context, 'Import From Document JSON', () { + _importFile(ExportFileType.documentJson); }), _buildListTile(context, 'Import From Markdown', () { _importFile(ExportFileType.markdown); @@ -260,7 +234,7 @@ class _HomePageState extends State { var result = ''; switch (fileType) { - case ExportFileType.json: + case ExportFileType.documentJson: result = jsonEncode(editorState.document.toJson()); break; case ExportFileType.markdown: @@ -318,14 +292,16 @@ class _HomePageState extends State { var jsonString = ''; switch (fileType) { - case ExportFileType.json: + case ExportFileType.documentJson: jsonString = plainText; break; case ExportFileType.markdown: jsonString = jsonEncode(markdownToDocument(plainText).toJson()); break; case ExportFileType.delta: - jsonString = 'unsupported'; + final delta = Delta.fromJson(jsonDecode(plainText)); + final document = quillDeltaEncoder.convert(delta); + jsonString = jsonEncode(document.toJson()); break; case ExportFileType.html: throw UnimplementedError(); diff --git a/lib/appflowy_editor.dart b/lib/appflowy_editor.dart index 4c8b8fde7..d5d171459 100644 --- a/lib/appflowy_editor.dart +++ b/lib/appflowy_editor.dart @@ -1,71 +1,68 @@ /// AppFlowyEditor library library appflowy_editor; -export 'src/infra/log.dart'; -export 'src/render/style/editor_style.dart'; +export 'src/core/document/attributes.dart'; +export 'src/core/document/deprecated/document.dart'; +export 'src/core/document/deprecated/node.dart'; +export 'src/core/document/document.dart'; export 'src/core/document/node.dart'; +export 'src/core/document/node_iterator.dart'; export 'src/core/document/path.dart'; -export 'src/core/location/position.dart'; -export 'src/core/location/selection.dart'; -export 'src/core/document/document.dart'; export 'src/core/document/text_delta.dart'; -export 'src/core/document/attributes.dart'; export 'src/core/legacy/built_in_attribute_keys.dart'; -export 'src/editor_state.dart'; +export 'src/core/location/position.dart'; +export 'src/core/location/selection.dart'; export 'src/core/transform/operation.dart'; export 'src/core/transform/transaction.dart'; -export 'src/render/selection/selectable.dart'; -export 'src/render/selection_menu/selection_menu_service.dart'; -export 'src/service/editor_service.dart'; -export 'src/service/render_plugin_service.dart'; -export 'src/service/service.dart'; -export 'src/service/selection_service.dart'; -export 'src/service/scroll_service.dart'; -export 'src/service/toolbar_service.dart'; -export 'src/service/keyboard_service.dart'; -export 'src/service/input_service.dart'; -export 'src/service/shortcut_event/key_mapping.dart'; -export 'src/service/shortcut_event/keybinding.dart'; -export 'src/service/shortcut_event/shortcut_event.dart'; -export 'src/service/shortcut_event/shortcut_event_handler.dart'; -export 'src/render/rich_text/default_selectable.dart'; -export 'src/render/rich_text/flowy_rich_text.dart'; -export 'src/render/rich_text/flowy_rich_text_keys.dart'; -export 'src/render/selection_menu/selection_menu_widget.dart'; -export 'src/render/selection_menu/selection_menu_item_widget.dart'; -export 'src/render/selection_menu/selection_menu_icon.dart'; +export 'src/editor/block_component/block_component.dart'; +export 'src/editor/command/transform.dart'; +export 'src/editor/editor_component/editor_component.dart'; +export 'src/editor/toolbar/toolbar.dart'; +export 'src/editor/util/util.dart'; +export 'src/editor_state.dart'; +export 'src/extensions/extensions.dart'; +export 'src/extensions/node_extensions.dart'; +export 'src/infra/flowy_svg.dart'; +export 'src/infra/html_converter.dart'; +export 'src/infra/log.dart'; +export 'src/infra/mobile/mobile.dart'; export 'src/l10n/l10n.dart'; -export 'src/render/style/plugin_styles.dart'; +export 'src/plugins/html/html_document.dart'; +export 'src/plugins/html/html_document_decoder.dart'; +export 'src/plugins/html/html_document_encoder.dart'; +export 'src/plugins/markdown/decoder/delta_markdown_decoder.dart'; +export 'src/plugins/markdown/document_markdown.dart'; export 'src/plugins/markdown/encoder/delta_markdown_encoder.dart'; export 'src/plugins/markdown/encoder/document_markdown_encoder.dart'; +export 'src/plugins/markdown/encoder/parser/image_node_parser.dart'; export 'src/plugins/markdown/encoder/parser/node_parser.dart'; export 'src/plugins/markdown/encoder/parser/text_node_parser.dart'; -export 'src/plugins/markdown/encoder/parser/image_node_parser.dart'; -export 'src/plugins/markdown/decoder/delta_markdown_decoder.dart'; -export 'src/plugins/markdown/document_markdown.dart'; -export 'src/plugins/quill_delta/delta_document_encoder.dart'; -export 'src/render/toolbar/toolbar_item.dart'; +export 'src/plugins/quill_delta/quill_delta_encoder.dart'; export 'src/render/action_menu/action_menu.dart'; export 'src/render/action_menu/action_menu_item.dart'; -export 'src/core/document/node_iterator.dart'; -export 'src/infra/flowy_svg.dart'; -export 'src/extensions/extensions.dart'; +export 'src/render/rich_text/default_selectable.dart'; +export 'src/render/rich_text/flowy_rich_text.dart'; +export 'src/render/rich_text/flowy_rich_text_keys.dart'; +export 'src/render/selection/selectable.dart'; +export 'src/render/selection_menu/selection_menu_icon.dart'; +export 'src/render/selection_menu/selection_menu_item_widget.dart'; +export 'src/render/selection_menu/selection_menu_service.dart'; +export 'src/render/selection_menu/selection_menu_widget.dart'; +export 'src/render/style/editor_style.dart'; +export 'src/render/style/plugin_styles.dart'; +export 'src/render/toolbar/toolbar_item.dart'; export 'src/service/default_text_operations/format_rich_text_style.dart'; -export 'src/infra/html_converter.dart'; +export 'src/service/editor_service.dart'; +export 'src/service/input_service.dart'; export 'src/service/internal_key_event_handlers/copy_paste_handler.dart'; - -export 'src/editor/block_component/block_component.dart'; -export 'src/editor/editor_component/editor_component.dart'; -export 'src/editor/command/transform.dart'; -export 'src/editor/util/util.dart'; -export 'src/editor/toolbar/toolbar.dart'; -export 'src/extensions/node_extensions.dart'; +export 'src/service/keyboard_service.dart'; +export 'src/service/render_plugin_service.dart'; +export 'src/service/scroll_service.dart'; +export 'src/service/selection_service.dart'; +export 'src/service/service.dart'; +export 'src/service/shortcut_event/key_mapping.dart'; +export 'src/service/shortcut_event/keybinding.dart'; +export 'src/service/shortcut_event/shortcut_event.dart'; +export 'src/service/shortcut_event/shortcut_event_handler.dart'; export 'src/service/standard_block_components.dart'; - -export 'src/core/document/deprecated/node.dart'; -export 'src/core/document/deprecated/document.dart'; - -export 'src/plugins/html/html_document_decoder.dart'; -export 'src/plugins/html/html_document_encoder.dart'; -export 'src/plugins/html/html_document.dart'; -export 'src/infra/mobile/mobile.dart'; +export 'src/service/toolbar_service.dart'; diff --git a/lib/src/plugins/quill_delta/delta_document_encoder.dart b/lib/src/plugins/quill_delta/delta_document_encoder.dart deleted file mode 100644 index 750c5cd19..000000000 --- a/lib/src/plugins/quill_delta/delta_document_encoder.dart +++ /dev/null @@ -1,233 +0,0 @@ -import 'package:appflowy_editor/src/core/document/attributes.dart'; -import 'package:appflowy_editor/src/core/document/document.dart'; -import 'package:appflowy_editor/src/core/document/node.dart'; -import 'package:appflowy_editor/src/core/document/text_delta.dart'; -import 'package:appflowy_editor/src/core/legacy/built_in_attribute_keys.dart'; -import 'package:flutter/material.dart'; - -@Deprecated('This class is outdated and will be removed in the next release.') -class DeltaDocumentConvert { - DeltaDocumentConvert(); - - var _number = 1; - final Map> _bulletedList = {}; - - Document convertFromJSON(List json) { - final delta = Delta.fromJson(json); - return convertFromDelta(delta); - } - - Document convertFromDelta(Delta delta) { - final iter = delta.iterator; - - final document = Document.blank(); - TextNode textNode = TextNode(delta: Delta()); - int path = 0; - - while (iter.moveNext()) { - final op = iter.current; - if (op is TextInsert) { - if (op.text != '\n') { - // Attributes associated with a newline character describes formatting for that line. - final texts = op.text.split('\n'); - if (texts.length > 1) { - textNode.delta.insert(texts[0]); - document.insert([path++], [textNode]); - textNode = TextNode(delta: Delta()..insert(texts[1])); - } else { - _applyStyle(textNode, op.text, op.attributes); - } - } else { - if (!_containNumberListStyle(op.attributes)) { - _number = 1; - } - _applyListStyle(textNode, op.attributes); - _applyHeaderStyle(textNode, op.attributes); - _applyIndent(textNode, op.attributes); - _applyBlockquote(textNode, op.attributes); - // _applyCodeBlock(textNode, op.attributes); - - if (_containIndentBulletedListStyle(op.attributes)) { - final level = _indentLevel(op.attributes); - final path = [ - ..._bulletedList[level - 1]!.last.path, - _bulletedList[level]!.length - 1, - ]; - document.insert(path, [textNode]); - } else { - document.insert([path++], [textNode]); - } - textNode = TextNode(delta: Delta()); - } - } else { - assert(false, 'op must be TextInsert'); - } - } - - return document; - } - - void _applyStyle(TextNode textNode, String text, Map? attributes) { - Attributes attrs = {}; - - if (_containsStyle(attributes, 'strike')) { - attrs[BuiltInAttributeKey.strikethrough] = true; - } - if (_containsStyle(attributes, 'underline')) { - attrs[BuiltInAttributeKey.underline] = true; - } - if (_containsStyle(attributes, 'bold')) { - attrs[BuiltInAttributeKey.bold] = true; - } - if (_containsStyle(attributes, 'italic')) { - attrs[BuiltInAttributeKey.italic] = true; - } - final link = attributes?['link'] as String?; - if (link != null) { - attrs[BuiltInAttributeKey.href] = link; - } - final color = attributes?['color'] as String?; - final colorHex = _convertColorToHexString(color); - if (colorHex != null) { - attrs[BuiltInAttributeKey.textColor] = colorHex; - } - final backgroundColor = attributes?['background'] as String?; - final backgroundHex = _convertColorToHexString(backgroundColor); - if (backgroundHex != null) { - attrs[BuiltInAttributeKey.highlightColor] = backgroundHex; - } - - textNode.delta.insert(text, attributes: attrs); - } - - bool _containsStyle(Map? attributes, String key) { - final value = attributes?[key] as bool?; - return value == true; - } - - String? _convertColorToHexString(String? color) { - if (color == null) { - return null; - } - if (color.startsWith('#')) { - return '0xFF${color.substring(1)}'; - } else if (color.startsWith("rgba")) { - List rgbaList = color.substring(5, color.length - 1).split(','); - return Color.fromRGBO( - int.parse(rgbaList[0]), - int.parse(rgbaList[1]), - int.parse(rgbaList[2]), - double.parse(rgbaList[3]), - ).toHex(); - } - return null; - } - - // convert bullet-list, number-list, check-list to appflowy style list. - void _applyListStyle(TextNode textNode, Map? attributes) { - final indent = attributes?['indent'] as int?; - final list = attributes?['list'] as String?; - if (list != null) { - switch (list) { - case 'bullet': - textNode.updateAttributes({ - BuiltInAttributeKey.subtype: BuiltInAttributeKey.bulletedList, - }); - if (indent != null) { - _bulletedList[indent] ??= []; - _bulletedList[indent]?.add(textNode); - } else { - _bulletedList.clear(); - _bulletedList[0] ??= []; - _bulletedList[0]?.add(textNode); - } - break; - case 'ordered': - textNode.updateAttributes({ - BuiltInAttributeKey.subtype: BuiltInAttributeKey.numberList, - BuiltInAttributeKey.number: _number++, - }); - break; - case 'checked': - textNode.updateAttributes({ - BuiltInAttributeKey.subtype: BuiltInAttributeKey.checkbox, - BuiltInAttributeKey.checkbox: true, - }); - break; - case 'unchecked': - textNode.updateAttributes({ - BuiltInAttributeKey.subtype: BuiltInAttributeKey.checkbox, - BuiltInAttributeKey.checkbox: false, - }); - break; - } - } - } - - bool _containNumberListStyle(Map? attributes) { - final list = attributes?['list'] as String?; - return list == 'ordered'; - } - - bool _containIndentBulletedListStyle(Map? attributes) { - final list = attributes?['list'] as String?; - final indent = attributes?['indent'] as int?; - return list == 'bullet' && indent != null; - } - - int _indentLevel(Map? attributes) { - final indent = attributes?['indent'] as int?; - return indent ?? 1; - } - - // convert header to appflowy style heading - void _applyHeaderStyle(TextNode textNode, Map? attributes) { - final header = attributes?['header'] as int?; - if (header != null) { - textNode.updateAttributes({ - BuiltInAttributeKey.subtype: BuiltInAttributeKey.heading, - BuiltInAttributeKey.heading: 'h$header', - }); - } - } - - // convert indent to tab - void _applyIndent(TextNode textNode, Map? attributes) { - final indent = attributes?['indent'] as int?; - final list = attributes?['list'] as String?; - if (indent != null && list == null) { - textNode.delta = textNode.delta.compose( - Delta() - ..retain(0) - ..insert(' ' * indent), - ); - } - } - - /* - // convert code_block to appflowy style code - void _applyCodeBlock(TextNode textNode, Map? attributes) { - final codeBlock = attributes?['code_block'] as bool?; - if (codeBlock != null) { - textNode.updateAttributes({ - BuiltInAttributeKey.subtype: 'code_block', - }); - } - } - */ - - void _applyBlockquote(TextNode textNode, Map? attributes) { - final blockquote = attributes?['blockquote'] as bool?; - if (blockquote != null) { - textNode.updateAttributes({ - BuiltInAttributeKey.subtype: BuiltInAttributeKey.quote, - }); - } - } -} - -extension on Color { - String toHex() { - return '0x${value.toRadixString(16)}'; - } -} diff --git a/lib/src/plugins/quill_delta/quill_delta_encoder.dart b/lib/src/plugins/quill_delta/quill_delta_encoder.dart new file mode 100644 index 000000000..2b25383ae --- /dev/null +++ b/lib/src/plugins/quill_delta/quill_delta_encoder.dart @@ -0,0 +1,221 @@ +import 'dart:convert'; + +import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:flutter/material.dart'; + +final QuillDeltaEncoder quillDeltaEncoder = QuillDeltaEncoder(); + +const _newLineSymbol = '\n'; +const _header = 'header'; +const _list = 'list'; +const _orderedList = 'ordered'; +const _bulletedList = 'bullet'; +const _uncheckedList = 'unchecked'; +const _checkedList = 'checked'; +const _blockquote = 'blockquote'; +const _indent = 'indent'; + +class QuillDeltaEncoder extends Converter { + final Map> nestedLists = {}; + + @override + Document convert(Delta input) { + final iterator = input.iterator; + final document = Document.blank(withInitialText: false); + + Node node = paragraphNode(); + int index = 0; + + while (iterator.moveNext()) { + final op = iterator.current; + final attributes = op.attributes; + if (op is TextInsert) { + if (op.text == _newLineSymbol) { + if (attributes != null) { + node = _applyListStyleIfNeeded(node, attributes); + node = _applyHeadingStyleIfNeeded(node, attributes); + node = _applyBlockquoteIfNeeded(node, attributes); + _applyIndentIfNeeded(node, attributes); + } + if (_isIndentBulletedList(attributes)) { + final level = _indentLevel(attributes); + final path = [ + ...nestedLists[level - 1]!.last.path, + nestedLists[level]!.length - 1, + ]; + document.insert(path, [node]); + } else { + document.insert([index++], [node]); + } + node = paragraphNode(); + } else { + final texts = op.text.split('\n'); + if (texts.length > 1) { + node.delta?.insert(texts[0]); + document.insert([index++], [node]); + node = paragraphNode(delta: Delta()..insert(texts[1])); + } else { + _applyStyle(node, op.text, attributes); + } + } + } else { + throw UnsupportedError('only support text insert operation'); + } + } + + return document; + } + + void _applyStyle(Node node, String text, Map? attributes) { + final Attributes attrs = {}; + if (_containsStyle(attributes, 'strike')) { + attrs[FlowyRichTextKeys.strikethrough] = true; + } + if (_containsStyle(attributes, 'underline')) { + attrs[FlowyRichTextKeys.underline] = true; + } + if (_containsStyle(attributes, 'bold')) { + attrs[FlowyRichTextKeys.bold] = true; + } + if (_containsStyle(attributes, 'italic')) { + attrs[FlowyRichTextKeys.italic] = true; + } + final link = attributes?['link'] as String?; + if (link != null) { + attrs[FlowyRichTextKeys.href] = link; + } + final color = attributes?['color'] as String?; + final colorHex = _convertColorToHexString(color); + if (colorHex != null) { + attrs[FlowyRichTextKeys.textColor] = colorHex; + } + final backgroundColor = attributes?['background'] as String?; + final backgroundHex = _convertColorToHexString(backgroundColor); + if (backgroundHex != null) { + attrs[FlowyRichTextKeys.highlightColor] = backgroundHex; + } + node.updateAttributes({ + 'delta': (node.delta?..insert(text, attributes: attrs))?.toJson(), + }); + } + + void _applyIndentIfNeeded(Node node, Map attributes) { + final indent = attributes[_indent] as int?; + final list = attributes[_list] as String?; + if (indent != null && list == null && node.delta != null) { + node.updateAttributes({ + 'delta': node.delta + ?.compose( + Delta() + ..retain(0) + ..insert(' ' * indent), + ) + .toJson(), + }); + } + } + + Node _applyBlockquoteIfNeeded(Node node, Map attributes) { + final blockquote = attributes[_blockquote] as bool?; + if (blockquote == true) { + return quoteNode( + delta: node.delta, + ); + } + return node; + } + + Node _applyHeadingStyleIfNeeded(Node node, Map attributes) { + final header = attributes[_header] as int?; + if (header == null) { + return node; + } + return headingNode( + delta: node.delta, + level: header, + ); + } + + // If the attributes contains the list style, then apply the list style to the node. + Node _applyListStyleIfNeeded(Node node, Map attributes) { + final list = attributes[_list] as String?; + switch (list) { + case _bulletedList: + final bulletedList = bulletedListNode( + delta: node.delta, + ); + final indent = attributes[_indent] as int?; + if (indent != null) { + nestedLists[indent] ??= []; + nestedLists[indent]?.add(bulletedList); + } else { + nestedLists.clear(); + nestedLists[0] ??= []; + nestedLists[0]?.add(bulletedList); + } + return bulletedList; + case _orderedList: + final numberedList = numberedListNode( + delta: node.delta, + ); + final indent = attributes[_indent] as int?; + if (indent != null) { + nestedLists[indent] ??= []; + nestedLists[indent]?.add(numberedList); + } else { + nestedLists.clear(); + nestedLists[0] ??= []; + nestedLists[0]?.add(numberedList); + } + return numberedList; + case _checkedList: + final checkedList = todoListNode( + delta: node.delta, + checked: true, + ); + return checkedList; + case _uncheckedList: + final uncheckedList = todoListNode( + delta: node.delta, + checked: false, + ); + return uncheckedList; + default: + return node; + } + } + + int _indentLevel(Map? attributes) { + final indent = attributes?['indent'] as int?; + return indent ?? 1; + } + + bool _isIndentBulletedList(Map? attributes) { + final list = attributes?[_list] as String?; + final indent = attributes?[_indent] as int?; + return [_bulletedList, _orderedList].contains(list) && indent != null; + } + + bool _containsStyle(Map? attributes, String key) { + final value = attributes?[key] as bool?; + return value == true; + } + + String? _convertColorToHexString(String? color) { + if (color == null) { + return null; + } + if (color.startsWith('#')) { + return '0xFF${color.substring(1)}'; + } else if (color.startsWith("rgba")) { + List rgbaList = color.substring(5, color.length - 1).split(','); + return Color.fromRGBO( + int.parse(rgbaList[0]), + int.parse(rgbaList[1]), + int.parse(rgbaList[2]), + double.parse(rgbaList[3]), + ).toHex(); + } + return null; + } +} diff --git a/test/plugins/quill_delta/delta_document_encoder_test.dart b/test/plugins/quill_delta/quill_delta_encoder_test.dart similarity index 53% rename from test/plugins/quill_delta/delta_document_encoder_test.dart rename to test/plugins/quill_delta/quill_delta_encoder_test.dart index eb4975abd..145f3ef33 100644 --- a/test/plugins/quill_delta/delta_document_encoder_test.dart +++ b/test/plugins/quill_delta/quill_delta_encoder_test.dart @@ -1,18 +1,20 @@ +import 'dart:convert'; + +import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter_test/flutter_test.dart'; void main() async { group('delta_document_encoder.dart', () { test('', () { - // TODO: lucas.xu - // final json = jsonDecode(quillDeltaSample.replaceAll('\\\\\n', '\\n')); - // final document = DeltaDocumentConvert().convertFromJSON(json); - // expect(jsonEncode(document.toJson()), documentSample); + final json = jsonDecode(quillDeltaSample.replaceAll('\\\\\n', '\\n')); + final document = quillDeltaEncoder.convert(Delta.fromJson(json)); + expect(jsonEncode(document.toJson()), documentSample); }); }); } const documentSample = - '''{"document":{"type":"document","children":[{"type":"text","data":{"subtype":"heading","heading":"h1"},"delta":[{"insert":"Flutter Quill"}]},{"type":"text","delta":[]},{"type":"text","data":{"subtype":"heading","heading":"h2"},"delta":[{"insert":"Rich text editor for Flutter"}]},{"type":"text","data":{"subtype":"heading","heading":"h3"},"delta":[{"insert":"Quill component for Flutter"}]},{"type":"text","delta":[{"insert":"This "},{"insert":"library","data":{"italic":true}},{"insert":" supports "},{"insert":"mobile","data":{"bold":true,"backgroundColor":"0xFFebd6ff"}},{"insert":" platform "},{"insert":"only","data":{"underline":true,"bold":true,"color":"0xFFe60000"}},{"insert":" and ","data":{"color":"0xd7000000"}},{"insert":"web","data":{"strikethrough":true}},{"insert":" is not supported."}]},{"type":"text","delta":[{"insert":"You are welcome to use "},{"insert":"Bullet Journal","data":{"href":"https://bulletjournal.us/home/index.html"}},{"insert":":"}]},{"type":"text","data":{"subtype":"number-list","number":1},"delta":[{"insert":"Track personal and group journals (ToDo, Note, Ledger) from multiple views with timely reminders"}]},{"type":"text","data":{"subtype":"number-list","number":2},"delta":[{"insert":"Share your tasks and notes with teammates, and see changes as they happen in real-time, across all devices"}]},{"type":"text","data":{"subtype":"number-list","number":3},"delta":[{"insert":"Check out what you and your teammates are working on each day"}]},{"type":"text","delta":[]},{"type":"text","data":{"subtype":"bulleted-list"},"delta":[{"insert":"Splitting bills with friends can never be easier."}]},{"type":"text","data":{"subtype":"bulleted-list"},"delta":[{"insert":"Start creating a group and invite your friends to join."}]},{"type":"text","data":{"subtype":"bulleted-list"},"delta":[{"insert":"Create a BuJo of Ledger type to see expense or balance summary."}]},{"type":"text","delta":[]},{"type":"text","data":{"subtype":"quote"},"delta":[{"insert":"Attach one or multiple labels to tasks, notes or transactions. Later you can track them just using the label(s)."}]},{"type":"text","delta":[]},{"type":"text","delta":[{"insert":"var BuJo = 'Bullet' + 'Journal'"}]},{"type":"text","delta":[]},{"type":"text","delta":[{"insert":" Start tracking in your browser"}]},{"type":"text","delta":[{"insert":" Stop the timer on your phone"}]},{"type":"text","delta":[{"insert":" All your time entries are synced"}]},{"type":"text","delta":[{"insert":" between the phone apps"}]},{"type":"text","delta":[{"insert":" and the website."}]},{"type":"text","delta":[]},{"type":"text","delta":[]},{"type":"text","delta":[{"insert":"Center Align"}]},{"type":"text","delta":[{"insert":"Right Align"}]},{"type":"text","delta":[{"insert":"Justify Align"}]},{"type":"text","data":{"subtype":"number-list","number":1},"delta":[{"insert":"Have trouble finding things? "}]},{"type":"text","data":{"subtype":"number-list","number":2},"delta":[{"insert":"Just type in the search bar"}]},{"type":"text","data":{"subtype":"number-list","number":3},"delta":[{"insert":"and easily find contents"}]},{"type":"text","data":{"subtype":"number-list","number":4},"delta":[{"insert":"across projects or folders."}]},{"type":"text","data":{"subtype":"number-list","number":5},"delta":[{"insert":"It matches text in your note or task."}]},{"type":"text","data":{"subtype":"number-list","number":6},"delta":[{"insert":"Enable reminders so that you will get notified by"}]},{"type":"text","data":{"subtype":"number-list","number":7},"delta":[{"insert":"email"}]},{"type":"text","data":{"subtype":"number-list","number":8},"delta":[{"insert":"message on your phone"}]},{"type":"text","data":{"subtype":"number-list","number":9},"delta":[{"insert":"popup on the web site"}]},{"type":"text","children":[{"type":"text","children":[{"type":"text","data":{"subtype":"bulleted-list"},"delta":[{"insert":"tasks"}]},{"type":"text","data":{"subtype":"bulleted-list"},"delta":[{"insert":"notes"}]},{"type":"text","children":[{"type":"text","data":{"subtype":"bulleted-list"},"delta":[{"insert":"under BuJo "}]}],"data":{"subtype":"bulleted-list"},"delta":[{"insert":"transactions"}]}],"data":{"subtype":"bulleted-list"},"delta":[{"insert":"Organize your"}]}],"data":{"subtype":"bulleted-list"},"delta":[{"insert":"Create a BuJo serving as project or folder"}]},{"type":"text","children":[{"type":"text","data":{"subtype":"bulleted-list"},"delta":[{"insert":"or hierarchical view"}]}],"data":{"subtype":"bulleted-list"},"delta":[{"insert":"See them in Calendar"}]},{"type":"text","data":{"subtype":"checkbox","checkbox":true},"delta":[{"insert":"this is a check list"}]},{"type":"text","data":{"subtype":"checkbox","checkbox":false},"delta":[{"insert":"this is a uncheck list"}]},{"type":"text","delta":[{"insert":"Font Sans Serif Serif Monospace Size Small Large Hugefont size 15 font size 35 font size 20 diff-match-patch"}]},{"type":"text","delta":[{"insert":""}]}]}}'''; + '''{"document":{"type":"page","children":[{"type":"heading","data":{"level":1,"delta":[{"insert":"Flutter Quill"}]}},{"type":"paragraph","data":{"delta":[]}},{"type":"heading","data":{"level":2,"delta":[{"insert":"Rich text editor for Flutter"}]}},{"type":"heading","data":{"level":3,"delta":[{"insert":"Quill component for Flutter"}]}},{"type":"paragraph","data":{"delta":[{"insert":"This "},{"insert":"library","attributes":{"italic":true}},{"insert":" supports "},{"insert":"mobile","attributes":{"bold":true,"bg_color":"0xFFebd6ff"}},{"insert":" platform "},{"insert":"only","attributes":{"underline":true,"bold":true,"font_color":"0xFFe60000"}},{"insert":" and ","attributes":{"font_color":"d7000000"}},{"insert":"web","attributes":{"strikethrough":true}}]}},{"type":"paragraph","data":{"delta":[{"insert":"You are welcome to use "},{"insert":"Bullet Journal","attributes":{"href":"https://bulletjournal.us/home/index.html"}}]}},{"type":"numbered_list","data":{"delta":[{"insert":"Track personal and group journals (ToDo, Note, Ledger) from multiple views with timely reminders"}],"number":null}},{"type":"numbered_list","data":{"delta":[{"insert":"Share your tasks and notes with teammates, and see changes as they happen in real-time, across all devices"}],"number":null}},{"type":"numbered_list","data":{"delta":[{"insert":"Check out what you and your teammates are working on each day"}],"number":null}},{"type":"paragraph","data":{"delta":[]}},{"type":"bulleted_list","data":{"delta":[{"insert":"Splitting bills with friends can never be easier."}]}},{"type":"bulleted_list","data":{"delta":[{"insert":"Start creating a group and invite your friends to join."}]}},{"type":"bulleted_list","data":{"delta":[{"insert":"Create a BuJo of Ledger type to see expense or balance summary."}]}},{"type":"paragraph","data":{"delta":[]}},{"type":"quote","data":{"delta":[{"insert":"Attach one or multiple labels to tasks, notes or transactions. Later you can track them just using the label(s)."}]}},{"type":"paragraph","data":{"delta":[]}},{"type":"paragraph","data":{"delta":[{"insert":"var BuJo = 'Bullet' + 'Journal'"}]}},{"type":"paragraph","data":{"delta":[]}},{"type":"paragraph","data":{"delta":[{"insert":" Start tracking in your browser"}]}},{"type":"paragraph","data":{"delta":[{"insert":" Stop the timer on your phone"}]}},{"type":"paragraph","data":{"delta":[{"insert":" All your time entries are synced"}]}},{"type":"paragraph","data":{"delta":[{"insert":" between the phone apps"}]}},{"type":"paragraph","data":{"delta":[{"insert":" and the website."}]}},{"type":"paragraph","data":{"delta":[]}},{"type":"paragraph","data":{"delta":[]}},{"type":"paragraph","data":{"delta":[{"insert":"Center Align"}]}},{"type":"paragraph","data":{"delta":[{"insert":"Right Align"}]}},{"type":"paragraph","data":{"delta":[{"insert":"Justify Align"}]}},{"type":"numbered_list","children":[{"type":"numbered_list","children":[{"type":"numbered_list","data":{"delta":[{"insert":"and easily find contents"}],"number":null}},{"type":"numbered_list","data":{"delta":[{"insert":"across projects or folders."}],"number":null}}],"data":{"delta":[{"insert":"Just type in the search bar"}],"number":null}},{"type":"numbered_list","data":{"delta":[{"insert":"It matches text in your note or task."}],"number":null}}],"data":{"delta":[{"insert":"Have trouble finding things? "}],"number":null}},{"type":"numbered_list","children":[{"type":"numbered_list","data":{"delta":[{"insert":"email"}],"number":null}},{"type":"numbered_list","data":{"delta":[{"insert":"message on your phone"}],"number":null}},{"type":"numbered_list","data":{"delta":[{"insert":"popup on the web site"}],"number":null}}],"data":{"delta":[{"insert":"Enable reminders so that you will get notified by"}],"number":null}},{"type":"bulleted_list","children":[{"type":"bulleted_list","children":[{"type":"bulleted_list","data":{"delta":[{"insert":"tasks"}]}},{"type":"bulleted_list","data":{"delta":[{"insert":"notes"}]}},{"type":"bulleted_list","children":[{"type":"bulleted_list","data":{"delta":[{"insert":"under BuJo "}]}}],"data":{"delta":[{"insert":"transactions"}]}}],"data":{"delta":[{"insert":"Organize your"}]}}],"data":{"delta":[{"insert":"Create a BuJo serving as project or folder"}]}},{"type":"bulleted_list","children":[{"type":"bulleted_list","data":{"delta":[{"insert":"or hierarchical view"}]}}],"data":{"delta":[{"insert":"See them in Calendar"}]}},{"type":"todo_list","data":{"checked":true,"delta":[{"insert":"this is a check list"}]}},{"type":"todo_list","data":{"checked":false,"delta":[{"insert":"this is a uncheck list"}]}},{"type":"paragraph","data":{"delta":[{"insert":"Font Sans Serif Serif Monospace Size Small Large Hugefont size 15 font size 35 font size 20 diff-match-patch"}]}}]}}'''; const quillDeltaSample = r''' [ @@ -20,7 +22,7 @@ const quillDeltaSample = r''' "insert": "Flutter Quill" }, { - "data": { + "attributes": { "header": 1 }, "insert": "\n" @@ -39,7 +41,7 @@ const quillDeltaSample = r''' "insert": "\nRich text editor for Flutter" }, { - "data": { + "attributes": { "header": 2 }, "insert": "\n" @@ -48,7 +50,7 @@ const quillDeltaSample = r''' "insert": "Quill component for Flutter" }, { - "data": { + "attributes": { "header": 3 }, "insert": "\n" @@ -57,7 +59,7 @@ const quillDeltaSample = r''' "insert": "This " }, { - "data": { + "attributes": { "italic": true, "background": "transparent" }, @@ -67,7 +69,7 @@ const quillDeltaSample = r''' "insert": " supports " }, { - "data": { + "attributes": { "bold": true, "background": "#ebd6ff" }, @@ -77,7 +79,7 @@ const quillDeltaSample = r''' "insert": " platform " }, { - "data": { + "attributes": { "underline": true, "bold": true, "color": "#e60000" @@ -85,13 +87,13 @@ const quillDeltaSample = r''' "insert": "only" }, { - "data": { + "attributes": { "color": "rgba(0, 0, 0, 0.847)" }, "insert": " and " }, { - "data": { + "attributes": { "strike": true, "color": "black" }, @@ -101,7 +103,7 @@ const quillDeltaSample = r''' "insert": " is not supported.\nYou are welcome to use " }, { - "data": { + "attributes": { "link": "https://bulletjournal.us/home/index.html" }, "insert": "Bullet Journal" @@ -110,7 +112,7 @@ const quillDeltaSample = r''' "insert": ":\nTrack personal and group journals (ToDo, Note, Ledger) from multiple views with timely reminders" }, { - "data": { + "attributes": { "list": "ordered" }, "insert": "\n" @@ -119,7 +121,7 @@ const quillDeltaSample = r''' "insert": "Share your tasks and notes with teammates, and see changes as they happen in real-time, across all devices" }, { - "data": { + "attributes": { "list": "ordered" }, "insert": "\n" @@ -128,7 +130,7 @@ const quillDeltaSample = r''' "insert": "Check out what you and your teammates are working on each day" }, { - "data": { + "attributes": { "list": "ordered" }, "insert": "\n" @@ -137,7 +139,7 @@ const quillDeltaSample = r''' "insert": "\nSplitting bills with friends can never be easier." }, { - "data": { + "attributes": { "list": "bullet" }, "insert": "\n" @@ -146,7 +148,7 @@ const quillDeltaSample = r''' "insert": "Start creating a group and invite your friends to join." }, { - "data": { + "attributes": { "list": "bullet" }, "insert": "\n" @@ -155,7 +157,7 @@ const quillDeltaSample = r''' "insert": "Create a BuJo of Ledger type to see expense or balance summary." }, { - "data": { + "attributes": { "list": "bullet" }, "insert": "\n" @@ -164,7 +166,7 @@ const quillDeltaSample = r''' "insert": "\nAttach one or multiple labels to tasks, notes or transactions. Later you can track them just using the label(s)." }, { - "data": { + "attributes": { "blockquote": true }, "insert": "\n" @@ -173,7 +175,7 @@ const quillDeltaSample = r''' "insert": "\nvar BuJo = 'Bullet' + 'Journal'" }, { - "data": { + "attributes": { "code_block": true }, "insert": "\n" @@ -182,7 +184,7 @@ const quillDeltaSample = r''' "insert": "\nStart tracking in your browser" }, { - "data": { + "attributes": { "indent": 1 }, "insert": "\n" @@ -191,7 +193,7 @@ const quillDeltaSample = r''' "insert": "Stop the timer on your phone" }, { - "data": { + "attributes": { "indent": 1 }, "insert": "\n" @@ -200,7 +202,7 @@ const quillDeltaSample = r''' "insert": "All your time entries are synced" }, { - "data": { + "attributes": { "indent": 2 }, "insert": "\n" @@ -209,7 +211,7 @@ const quillDeltaSample = r''' "insert": "between the phone apps" }, { - "data": { + "attributes": { "indent": 2 }, "insert": "\n" @@ -218,7 +220,7 @@ const quillDeltaSample = r''' "insert": "and the website." }, { - "data": { + "attributes": { "indent": 3 }, "insert": "\n" @@ -230,7 +232,7 @@ const quillDeltaSample = r''' "insert": "\nCenter Align" }, { - "data": { + "attributes": { "align": "center" }, "insert": "\n" @@ -239,7 +241,7 @@ const quillDeltaSample = r''' "insert": "Right Align" }, { - "data": { + "attributes": { "align": "right" }, "insert": "\n" @@ -248,7 +250,7 @@ const quillDeltaSample = r''' "insert": "Justify Align" }, { - "data": { + "attributes": { "align": "justify" }, "insert": "\n" @@ -257,7 +259,7 @@ const quillDeltaSample = r''' "insert": "Have trouble finding things? " }, { - "data": { + "attributes": { "list": "ordered" }, "insert": "\n" @@ -266,7 +268,7 @@ const quillDeltaSample = r''' "insert": "Just type in the search bar" }, { - "data": { + "attributes": { "indent": 1, "list": "ordered" }, @@ -276,7 +278,7 @@ const quillDeltaSample = r''' "insert": "and easily find contents" }, { - "data": { + "attributes": { "indent": 2, "list": "ordered" }, @@ -286,7 +288,7 @@ const quillDeltaSample = r''' "insert": "across projects or folders." }, { - "data": { + "attributes": { "indent": 2, "list": "ordered" }, @@ -296,7 +298,7 @@ const quillDeltaSample = r''' "insert": "It matches text in your note or task." }, { - "data": { + "attributes": { "indent": 1, "list": "ordered" }, @@ -306,7 +308,7 @@ const quillDeltaSample = r''' "insert": "Enable reminders so that you will get notified by" }, { - "data": { + "attributes": { "list": "ordered" }, "insert": "\n" @@ -315,7 +317,7 @@ const quillDeltaSample = r''' "insert": "email" }, { - "data": { + "attributes": { "indent": 1, "list": "ordered" }, @@ -325,7 +327,7 @@ const quillDeltaSample = r''' "insert": "message on your phone" }, { - "data": { + "attributes": { "indent": 1, "list": "ordered" }, @@ -335,7 +337,7 @@ const quillDeltaSample = r''' "insert": "popup on the web site" }, { - "data": { + "attributes": { "indent": 1, "list": "ordered" }, @@ -345,7 +347,7 @@ const quillDeltaSample = r''' "insert": "Create a BuJo serving as project or folder" }, { - "data": { + "attributes": { "list": "bullet" }, "insert": "\n" @@ -354,7 +356,7 @@ const quillDeltaSample = r''' "insert": "Organize your" }, { - "data": { + "attributes": { "indent": 1, "list": "bullet" }, @@ -364,7 +366,7 @@ const quillDeltaSample = r''' "insert": "tasks" }, { - "data": { + "attributes": { "indent": 2, "list": "bullet" }, @@ -374,7 +376,7 @@ const quillDeltaSample = r''' "insert": "notes" }, { - "data": { + "attributes": { "indent": 2, "list": "bullet" }, @@ -384,7 +386,7 @@ const quillDeltaSample = r''' "insert": "transactions" }, { - "data": { + "attributes": { "indent": 2, "list": "bullet" }, @@ -394,7 +396,7 @@ const quillDeltaSample = r''' "insert": "under BuJo " }, { - "data": { + "attributes": { "indent": 3, "list": "bullet" }, @@ -404,7 +406,7 @@ const quillDeltaSample = r''' "insert": "See them in Calendar" }, { - "data": { + "attributes": { "list": "bullet" }, "insert": "\n" @@ -413,7 +415,7 @@ const quillDeltaSample = r''' "insert": "or hierarchical view" }, { - "data": { + "attributes": { "indent": 1, "list": "bullet" }, @@ -423,7 +425,7 @@ const quillDeltaSample = r''' "insert": "this is a check list" }, { - "data": { + "attributes": { "list": "checked" }, "insert": "\n" @@ -432,7 +434,7 @@ const quillDeltaSample = r''' "insert": "this is a uncheck list" }, { - "data": { + "attributes": { "list": "unchecked" }, "insert": "\n" @@ -441,7 +443,7 @@ const quillDeltaSample = r''' "insert": "Font " }, { - "data": { + "attributes": { "font": "sans-serif" }, "insert": "Sans Serif" @@ -450,7 +452,7 @@ const quillDeltaSample = r''' "insert": " " }, { - "data": { + "attributes": { "font": "serif" }, "insert": "Serif" @@ -459,7 +461,7 @@ const quillDeltaSample = r''' "insert": " " }, { - "data": { + "attributes": { "font": "monospace" }, "insert": "Monospace" @@ -468,7 +470,7 @@ const quillDeltaSample = r''' "insert": " Size " }, { - "data": { + "attributes": { "size": "small" }, "insert": "Small" @@ -477,7 +479,7 @@ const quillDeltaSample = r''' "insert": " " }, { - "data": { + "attributes": { "size": "large" }, "insert": "Large" @@ -486,13 +488,13 @@ const quillDeltaSample = r''' "insert": " " }, { - "data": { + "attributes": { "size": "huge" }, "insert": "Huge" }, { - "data": { + "attributes": { "size": "15.0" }, "insert": "font size 15" @@ -501,7 +503,7 @@ const quillDeltaSample = r''' "insert": " " }, { - "data": { + "attributes": { "size": "35" }, "insert": "font size 35" @@ -510,25 +512,25 @@ const quillDeltaSample = r''' "insert": " " }, { - "data": { + "attributes": { "size": "20" }, "insert": "font size 20" }, { - "data": { + "attributes": { "token": "built_in" }, "insert": " diff" }, { - "data": { + "attributes": { "token": "operator" }, "insert": "-match" }, { - "data": { + "attributes": { "token": "literal" }, "insert": "-patch" @@ -537,7 +539,7 @@ const quillDeltaSample = r''' "insert": { "image": "https://user-images.githubusercontent.com/122956/72955931-ccc07900-3d52-11ea-89b1-d468a6e2aa2b.png" }, - "data": { + "attributes": { "width": "230", "style": "display: block; margin: auto;" }