Skip to content

Commit 070ceb3

Browse files
authored
feat: migrate the delta encoder (AppFlowy-IO#277)
1 parent cba0647 commit 070ceb3

File tree

6 files changed

+371
-396
lines changed

6 files changed

+371
-396
lines changed

documentation/importing.md

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
# Importing data
22

3-
For now, we have supported three ways to import data to initialize AppFlowy Editor.
3+
Currently, we have supported three methods for importing data to initialize AppFlowy Editor.
44

55
1. From AppFlowy Document JSON
66

77
```dart
8-
const document = r'''{"document":{"type":"editor","children":[{"type":"text","attributes":{"subtype":"heading","heading":"h1"},"delta":[{"insert":"Hello AppFlowy!"}]}]}}''';
9-
final json = jsonDecode(document);
8+
const document = r'''{
9+
"document": {
10+
"type": "page",
11+
"children": [
12+
{
13+
"type": "heading",
14+
"data": {
15+
"delta": [{ "insert": "Hello AppFlowy!" }],
16+
"level": 1
17+
}
18+
}
19+
]
20+
}
21+
}''';
22+
final json = Map<String, Object>.from(jsonDecode(document));
1023
final editorState = EditorState(
11-
document: Document.fromJson(
12-
Map<String, Object>.from(json),
13-
),
24+
document: Document.fromJson(json),
1425
);
1526
```
1627

@@ -19,18 +30,19 @@ final editorState = EditorState(
1930
```dart
2031
const markdown = r'''# Hello AppFlowy!''';
2132
final editorState = EditorState(
22-
document: markdownToDocument(markdown),
33+
document: markdownToDocument(markdown),
2334
);
2435
```
2536

2637
3. From Quill Delta
2738

2839
```dart
29-
const delta = r'''[{"insert":"Hello AppFlowy!"},{"attributes":{"header":1},"insert":"\n"}]''';
30-
final json = jsonDecode(delta);
31-
final editorState = EditorState(
32-
document: DeltaDocumentConvert().convertFromJSON(json),
33-
);
40+
const json = r'''[{"insert":"Hello AppFlowy!"},{"attributes":{"header":1},"insert":"\n"}]''';
41+
final delta = Delta.fromJson(jsonDecode(json));
42+
final document = quillDeltaEncoder.convert(delta);
43+
final editorState = EditorState(document: document);
3444
```
3545

36-
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).
46+
> Notes: Some styles, such as font-size, font-family and text-align, are not supported yet.
47+
48+
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).

example/lib/home_page.dart

+12-36
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:flutter/services.dart';
1111
import 'package:universal_html/html.dart' as html;
1212

1313
enum ExportFileType {
14-
json,
14+
documentJson,
1515
markdown,
1616
html,
1717
delta,
@@ -20,7 +20,7 @@ enum ExportFileType {
2020
extension on ExportFileType {
2121
String get extension {
2222
switch (this) {
23-
case ExportFileType.json:
23+
case ExportFileType.documentJson:
2424
case ExportFileType.delta:
2525
return 'json';
2626
case ExportFileType.markdown:
@@ -123,45 +123,19 @@ class _HomePageState extends State<HomePage> {
123123
_loadEditor(context, jsonString);
124124
}),
125125

126-
// Text Robot
127-
// _buildSeparator(context, 'Text Robot'),
128-
// _buildListTile(context, 'Type Text Automatically', () async {
129-
// final jsonString = Future<String>.value(
130-
// jsonEncode(
131-
// EditorState.blank(withInitialText: true).document.toJson(),
132-
// ).toString(),
133-
// );
134-
// await _loadEditor(context, jsonString);
135-
136-
// Future.delayed(const Duration(seconds: 2), () {
137-
// final textRobot = TextRobot(
138-
// editorState: _editorState,
139-
// );
140-
// textRobot.insertText(
141-
// '''
142-
// Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
143-
// "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?"
144-
145-
// 1914 translation by H. Rackham
146-
// "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?"
147-
// ''',
148-
// );
149-
// });
150-
// }),
151-
152126
// Encoder Demo
153-
_buildSeparator(context, 'Encoder Demo'),
127+
_buildSeparator(context, 'Export To X Demo'),
154128
_buildListTile(context, 'Export To JSON', () {
155-
_exportFile(_editorState, ExportFileType.json);
129+
_exportFile(_editorState, ExportFileType.documentJson);
156130
}),
157131
_buildListTile(context, 'Export to Markdown', () {
158132
_exportFile(_editorState, ExportFileType.markdown);
159133
}),
160134

161135
// Decoder Demo
162-
_buildSeparator(context, 'Decoder Demo'),
163-
_buildListTile(context, 'Import From JSON', () {
164-
_importFile(ExportFileType.json);
136+
_buildSeparator(context, 'Import From X Demo'),
137+
_buildListTile(context, 'Import From Document JSON', () {
138+
_importFile(ExportFileType.documentJson);
165139
}),
166140
_buildListTile(context, 'Import From Markdown', () {
167141
_importFile(ExportFileType.markdown);
@@ -260,7 +234,7 @@ class _HomePageState extends State<HomePage> {
260234
var result = '';
261235

262236
switch (fileType) {
263-
case ExportFileType.json:
237+
case ExportFileType.documentJson:
264238
result = jsonEncode(editorState.document.toJson());
265239
break;
266240
case ExportFileType.markdown:
@@ -318,14 +292,16 @@ class _HomePageState extends State<HomePage> {
318292

319293
var jsonString = '';
320294
switch (fileType) {
321-
case ExportFileType.json:
295+
case ExportFileType.documentJson:
322296
jsonString = plainText;
323297
break;
324298
case ExportFileType.markdown:
325299
jsonString = jsonEncode(markdownToDocument(plainText).toJson());
326300
break;
327301
case ExportFileType.delta:
328-
jsonString = 'unsupported';
302+
final delta = Delta.fromJson(jsonDecode(plainText));
303+
final document = quillDeltaEncoder.convert(delta);
304+
jsonString = jsonEncode(document.toJson());
329305
break;
330306
case ExportFileType.html:
331307
throw UnimplementedError();

lib/appflowy_editor.dart

+49-52
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,68 @@
11
/// AppFlowyEditor library
22
library appflowy_editor;
33

4-
export 'src/infra/log.dart';
5-
export 'src/render/style/editor_style.dart';
4+
export 'src/core/document/attributes.dart';
5+
export 'src/core/document/deprecated/document.dart';
6+
export 'src/core/document/deprecated/node.dart';
7+
export 'src/core/document/document.dart';
68
export 'src/core/document/node.dart';
9+
export 'src/core/document/node_iterator.dart';
710
export 'src/core/document/path.dart';
8-
export 'src/core/location/position.dart';
9-
export 'src/core/location/selection.dart';
10-
export 'src/core/document/document.dart';
1111
export 'src/core/document/text_delta.dart';
12-
export 'src/core/document/attributes.dart';
1312
export 'src/core/legacy/built_in_attribute_keys.dart';
14-
export 'src/editor_state.dart';
13+
export 'src/core/location/position.dart';
14+
export 'src/core/location/selection.dart';
1515
export 'src/core/transform/operation.dart';
1616
export 'src/core/transform/transaction.dart';
17-
export 'src/render/selection/selectable.dart';
18-
export 'src/render/selection_menu/selection_menu_service.dart';
19-
export 'src/service/editor_service.dart';
20-
export 'src/service/render_plugin_service.dart';
21-
export 'src/service/service.dart';
22-
export 'src/service/selection_service.dart';
23-
export 'src/service/scroll_service.dart';
24-
export 'src/service/toolbar_service.dart';
25-
export 'src/service/keyboard_service.dart';
26-
export 'src/service/input_service.dart';
27-
export 'src/service/shortcut_event/key_mapping.dart';
28-
export 'src/service/shortcut_event/keybinding.dart';
29-
export 'src/service/shortcut_event/shortcut_event.dart';
30-
export 'src/service/shortcut_event/shortcut_event_handler.dart';
31-
export 'src/render/rich_text/default_selectable.dart';
32-
export 'src/render/rich_text/flowy_rich_text.dart';
33-
export 'src/render/rich_text/flowy_rich_text_keys.dart';
34-
export 'src/render/selection_menu/selection_menu_widget.dart';
35-
export 'src/render/selection_menu/selection_menu_item_widget.dart';
36-
export 'src/render/selection_menu/selection_menu_icon.dart';
17+
export 'src/editor/block_component/block_component.dart';
18+
export 'src/editor/command/transform.dart';
19+
export 'src/editor/editor_component/editor_component.dart';
20+
export 'src/editor/toolbar/toolbar.dart';
21+
export 'src/editor/util/util.dart';
22+
export 'src/editor_state.dart';
23+
export 'src/extensions/extensions.dart';
24+
export 'src/extensions/node_extensions.dart';
25+
export 'src/infra/flowy_svg.dart';
26+
export 'src/infra/html_converter.dart';
27+
export 'src/infra/log.dart';
28+
export 'src/infra/mobile/mobile.dart';
3729
export 'src/l10n/l10n.dart';
38-
export 'src/render/style/plugin_styles.dart';
30+
export 'src/plugins/html/html_document.dart';
31+
export 'src/plugins/html/html_document_decoder.dart';
32+
export 'src/plugins/html/html_document_encoder.dart';
33+
export 'src/plugins/markdown/decoder/delta_markdown_decoder.dart';
34+
export 'src/plugins/markdown/document_markdown.dart';
3935
export 'src/plugins/markdown/encoder/delta_markdown_encoder.dart';
4036
export 'src/plugins/markdown/encoder/document_markdown_encoder.dart';
37+
export 'src/plugins/markdown/encoder/parser/image_node_parser.dart';
4138
export 'src/plugins/markdown/encoder/parser/node_parser.dart';
4239
export 'src/plugins/markdown/encoder/parser/text_node_parser.dart';
43-
export 'src/plugins/markdown/encoder/parser/image_node_parser.dart';
44-
export 'src/plugins/markdown/decoder/delta_markdown_decoder.dart';
45-
export 'src/plugins/markdown/document_markdown.dart';
46-
export 'src/plugins/quill_delta/delta_document_encoder.dart';
47-
export 'src/render/toolbar/toolbar_item.dart';
40+
export 'src/plugins/quill_delta/quill_delta_encoder.dart';
4841
export 'src/render/action_menu/action_menu.dart';
4942
export 'src/render/action_menu/action_menu_item.dart';
50-
export 'src/core/document/node_iterator.dart';
51-
export 'src/infra/flowy_svg.dart';
52-
export 'src/extensions/extensions.dart';
43+
export 'src/render/rich_text/default_selectable.dart';
44+
export 'src/render/rich_text/flowy_rich_text.dart';
45+
export 'src/render/rich_text/flowy_rich_text_keys.dart';
46+
export 'src/render/selection/selectable.dart';
47+
export 'src/render/selection_menu/selection_menu_icon.dart';
48+
export 'src/render/selection_menu/selection_menu_item_widget.dart';
49+
export 'src/render/selection_menu/selection_menu_service.dart';
50+
export 'src/render/selection_menu/selection_menu_widget.dart';
51+
export 'src/render/style/editor_style.dart';
52+
export 'src/render/style/plugin_styles.dart';
53+
export 'src/render/toolbar/toolbar_item.dart';
5354
export 'src/service/default_text_operations/format_rich_text_style.dart';
54-
export 'src/infra/html_converter.dart';
55+
export 'src/service/editor_service.dart';
56+
export 'src/service/input_service.dart';
5557
export 'src/service/internal_key_event_handlers/copy_paste_handler.dart';
56-
57-
export 'src/editor/block_component/block_component.dart';
58-
export 'src/editor/editor_component/editor_component.dart';
59-
export 'src/editor/command/transform.dart';
60-
export 'src/editor/util/util.dart';
61-
export 'src/editor/toolbar/toolbar.dart';
62-
export 'src/extensions/node_extensions.dart';
58+
export 'src/service/keyboard_service.dart';
59+
export 'src/service/render_plugin_service.dart';
60+
export 'src/service/scroll_service.dart';
61+
export 'src/service/selection_service.dart';
62+
export 'src/service/service.dart';
63+
export 'src/service/shortcut_event/key_mapping.dart';
64+
export 'src/service/shortcut_event/keybinding.dart';
65+
export 'src/service/shortcut_event/shortcut_event.dart';
66+
export 'src/service/shortcut_event/shortcut_event_handler.dart';
6367
export 'src/service/standard_block_components.dart';
64-
65-
export 'src/core/document/deprecated/node.dart';
66-
export 'src/core/document/deprecated/document.dart';
67-
68-
export 'src/plugins/html/html_document_decoder.dart';
69-
export 'src/plugins/html/html_document_encoder.dart';
70-
export 'src/plugins/html/html_document.dart';
71-
export 'src/infra/mobile/mobile.dart';
68+
export 'src/service/toolbar_service.dart';

0 commit comments

Comments
 (0)