Skip to content

Commit 853be71

Browse files
committed
feat: implement initalize editor from json and markdown
1 parent 9a908ab commit 853be71

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,12 @@ class _HomePageState extends State<HomePage> {
9999

100100
// Decoder Demo
101101
_buildSeparator(context, 'Decoder Demo'),
102-
_buildListTile(context, 'Import From JSON', () {}),
103-
_buildListTile(context, 'Import From Markdown', () {}),
102+
_buildListTile(context, 'Import From JSON', () {
103+
_importFile(ExportFileType.json);
104+
}),
105+
_buildListTile(context, 'Import From Markdown', () {
106+
_importFile(ExportFileType.markdown);
107+
}),
104108

105109
// Theme Demo
106110
_buildSeparator(context, 'Theme Demo'),
@@ -213,4 +217,32 @@ class _HomePageState extends State<HomePage> {
213217
..click();
214218
}
215219
}
220+
221+
void _importFile(ExportFileType fileType) async {
222+
final result = await FilePicker.platform.pickFiles(
223+
allowMultiple: false,
224+
allowedExtensions: [fileType.extension],
225+
type: FileType.custom,
226+
);
227+
final path = result?.files.single.path;
228+
if (path == null) {
229+
return;
230+
}
231+
final plainText = await File(path).readAsString();
232+
var jsonString = '';
233+
switch (fileType) {
234+
case ExportFileType.json:
235+
jsonString = jsonEncode(plainText);
236+
break;
237+
case ExportFileType.markdown:
238+
jsonString = jsonEncode(markdownToDocument(plainText).toJson());
239+
break;
240+
case ExportFileType.html:
241+
throw UnimplementedError();
242+
}
243+
244+
if (mounted) {
245+
_loadEditor(context, Future<String>.value(jsonString));
246+
}
247+
}
216248
}

0 commit comments

Comments
 (0)