Skip to content

Commit 9a908ab

Browse files
committed
feat: implement save document to markdown
1 parent 205b5f2 commit 9a908ab

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

frontend/app_flowy/packages/appflowy_editor/example/assets/example.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"delta": [
1212
{ "insert": "👋 " },
13-
{ "insert": "Welcome to ", "attributes": { "bold": true } },
13+
{ "insert": "Welcome to", "attributes": { "bold": true } },
14+
{ "insert": " " },
1415
{
1516
"insert": "AppFlowy Editor",
1617
"attributes": {
@@ -25,7 +26,8 @@
2526
{
2627
"type": "text",
2728
"delta": [
28-
{ "insert": "AppFlowy Editor is a " },
29+
{ "insert": "AppFlowy Editor is a" },
30+
{ "insert": " " },
2931
{ "insert": "highly customizable", "attributes": { "bold": true } },
3032
{ "insert": " " },
3133
{ "insert": "rich-text editor", "attributes": { "italic": true } },

frontend/app_flowy/packages/appflowy_editor/example/lib/home_page.dart

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ import 'package:flutter/material.dart';
99
import 'package:flutter/services.dart';
1010
import 'package:universal_html/html.dart' as html;
1111

12+
enum ExportFileType {
13+
json,
14+
markdown,
15+
html,
16+
}
17+
18+
extension on ExportFileType {
19+
String get extension {
20+
switch (this) {
21+
case ExportFileType.json:
22+
return 'json';
23+
case ExportFileType.markdown:
24+
return 'md';
25+
case ExportFileType.html:
26+
return 'html';
27+
}
28+
}
29+
}
30+
1231
class HomePage extends StatefulWidget {
1332
const HomePage({Key? key}) : super(key: key);
1433

@@ -72,9 +91,11 @@ class _HomePageState extends State<HomePage> {
7291
// Encoder Demo
7392
_buildSeparator(context, 'Encoder Demo'),
7493
_buildListTile(context, 'Export To JSON', () {
75-
_exportJson(_editorState);
94+
_exportFile(_editorState, ExportFileType.json);
95+
}),
96+
_buildListTile(context, 'Export to Markdown', () {
97+
_exportFile(_editorState, ExportFileType.markdown);
7698
}),
77-
_buildListTile(context, 'Export to Markdown', () {}),
7899

79100
// Decoder Demo
80101
_buildSeparator(context, 'Decoder Demo'),
@@ -152,58 +173,44 @@ class _HomePageState extends State<HomePage> {
152173
);
153174
}
154175

155-
void _exportJson(EditorState editorState) async {
156-
final document = editorState.document.toJson();
157-
final json = jsonEncode(document);
176+
void _exportFile(
177+
EditorState editorState,
178+
ExportFileType fileType,
179+
) async {
180+
var result = '';
181+
182+
switch (fileType) {
183+
case ExportFileType.json:
184+
result = jsonEncode(editorState.document.toJson());
185+
break;
186+
case ExportFileType.markdown:
187+
result = documentToMarkdown(editorState.document);
188+
break;
189+
case ExportFileType.html:
190+
throw UnimplementedError();
191+
}
158192

159193
if (!kIsWeb) {
160194
final path = await FilePicker.platform.saveFile(
161-
dialogTitle: 'Export to JSON',
162-
fileName: 'document.json',
195+
fileName: 'document.${fileType.extension}',
163196
);
164197
if (path != null) {
165-
await File(path).writeAsString(json);
198+
await File(path).writeAsString(result);
166199
if (mounted) {
167200
ScaffoldMessenger.of(context).showSnackBar(
168201
SnackBar(
169-
content: Text('The document.json is saved to the $path'),
202+
content: Text('This document is saved to the $path'),
170203
),
171204
);
172205
}
173206
}
174207
} else {
175-
final blob = html.Blob([json], 'text/plain', 'native');
208+
final blob = html.Blob([result], 'text/plain', 'native');
176209
html.AnchorElement(
177210
href: html.Url.createObjectUrlFromBlob(blob).toString(),
178211
)
179-
..setAttribute('download', 'document.json')
212+
..setAttribute('download', 'document.${fileType.extension}')
180213
..click();
181214
}
182215
}
183-
184-
// void _exportDocument(EditorState editorState) async {
185-
// final document = editorState.document.toJson();
186-
// final json = jsonEncode(document);
187-
// if (kIsWeb) {
188-
// final blob = html.Blob([json], 'text/plain', 'native');
189-
// html.AnchorElement(
190-
// href: html.Url.createObjectUrlFromBlob(blob).toString(),
191-
// )
192-
// ..setAttribute('download', 'editor.json')
193-
// ..click();
194-
// } else {
195-
// final directory = await getTemporaryDirectory();
196-
// final path = directory.path;
197-
// final file = File('$path/editor.json');
198-
// await file.writeAsString(json);
199-
200-
// if (mounted) {
201-
// ScaffoldMessenger.of(context).showSnackBar(
202-
// SnackBar(
203-
// content: Text('The document is saved to the ${file.path}'),
204-
// ),
205-
// );
206-
// }
207-
// }
208-
// }
209216
}

0 commit comments

Comments
 (0)