Skip to content

Commit 205b5f2

Browse files
committed
feat: implement save document to json
1 parent 064ed16 commit 205b5f2

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

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

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import 'dart:convert';
2+
import 'dart:io';
23

34
import 'package:appflowy_editor/appflowy_editor.dart';
45
import 'package:example/pages/simple_editor.dart';
6+
import 'package:file_picker/file_picker.dart';
7+
import 'package:flutter/foundation.dart';
58
import 'package:flutter/material.dart';
69
import 'package:flutter/services.dart';
10+
import 'package:universal_html/html.dart' as html;
711

812
class HomePage extends StatefulWidget {
913
const HomePage({Key? key}) : super(key: key);
@@ -56,18 +60,20 @@ class _HomePageState extends State<HomePage> {
5660
_buildSeparator(context, 'AppFlowy Editor Demo'),
5761
_buildListTile(context, 'With Example.json', () {
5862
final jsonString = rootBundle.loadString('assets/example.json');
59-
_loadJsonEditor(context, jsonString);
63+
_loadEditor(context, jsonString);
6064
}),
6165
_buildListTile(context, 'With Empty Document', () {
6266
final jsonString = Future<String>.value(
63-
json.encode(EditorState.empty().document.toJson()).toString(),
67+
jsonEncode(EditorState.empty().document.toJson()).toString(),
6468
);
65-
_loadJsonEditor(context, jsonString);
69+
_loadEditor(context, jsonString);
6670
}),
6771

6872
// Encoder Demo
6973
_buildSeparator(context, 'Encoder Demo'),
70-
_buildListTile(context, 'Export To JSON', () {}),
74+
_buildListTile(context, 'Export To JSON', () {
75+
_exportJson(_editorState);
76+
}),
7177
_buildListTile(context, 'Export to Markdown', () {}),
7278

7379
// Decoder Demo
@@ -133,7 +139,7 @@ class _HomePageState extends State<HomePage> {
133139
);
134140
}
135141

136-
void _loadJsonEditor(BuildContext context, Future<String> jsonString) {
142+
void _loadEditor(BuildContext context, Future<String> jsonString) {
137143
setState(
138144
() {
139145
_widgetBuilder = (context) => SimpleEditor(
@@ -145,4 +151,59 @@ class _HomePageState extends State<HomePage> {
145151
},
146152
);
147153
}
154+
155+
void _exportJson(EditorState editorState) async {
156+
final document = editorState.document.toJson();
157+
final json = jsonEncode(document);
158+
159+
if (!kIsWeb) {
160+
final path = await FilePicker.platform.saveFile(
161+
dialogTitle: 'Export to JSON',
162+
fileName: 'document.json',
163+
);
164+
if (path != null) {
165+
await File(path).writeAsString(json);
166+
if (mounted) {
167+
ScaffoldMessenger.of(context).showSnackBar(
168+
SnackBar(
169+
content: Text('The document.json is saved to the $path'),
170+
),
171+
);
172+
}
173+
}
174+
} else {
175+
final blob = html.Blob([json], 'text/plain', 'native');
176+
html.AnchorElement(
177+
href: html.Url.createObjectUrlFromBlob(blob).toString(),
178+
)
179+
..setAttribute('download', 'document.json')
180+
..click();
181+
}
182+
}
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+
// }
148209
}

0 commit comments

Comments
 (0)