Skip to content

Commit

Permalink
feat: migrate the delta encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Jul 3, 2023
1 parent 8bfb37f commit a9eb307
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 396 deletions.
38 changes: 25 additions & 13 deletions documentation/importing.md
Original file line number Diff line number Diff line change
@@ -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<String, Object>.from(jsonDecode(document));
final editorState = EditorState(
document: Document.fromJson(
Map<String, Object>.from(json),
),
document: Document.fromJson(json),
);
```

Expand All @@ -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).
> 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).
48 changes: 12 additions & 36 deletions example/lib/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:flutter/services.dart';
import 'package:universal_html/html.dart' as html;

enum ExportFileType {
json,
documentJson,
markdown,
html,
delta,
Expand All @@ -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:
Expand Down Expand Up @@ -123,45 +123,19 @@ class _HomePageState extends State<HomePage> {
_loadEditor(context, jsonString);
}),

// Text Robot
// _buildSeparator(context, 'Text Robot'),
// _buildListTile(context, 'Type Text Automatically', () async {
// final jsonString = Future<String>.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);
Expand Down Expand Up @@ -260,7 +234,7 @@ class _HomePageState extends State<HomePage> {
var result = '';

switch (fileType) {
case ExportFileType.json:
case ExportFileType.documentJson:
result = jsonEncode(editorState.document.toJson());
break;
case ExportFileType.markdown:
Expand Down Expand Up @@ -318,14 +292,16 @@ class _HomePageState extends State<HomePage> {

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();
Expand Down
101 changes: 49 additions & 52 deletions lib/appflowy_editor.dart
Original file line number Diff line number Diff line change
@@ -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';
Loading

0 comments on commit a9eb307

Please sign in to comment.