11import 'dart:convert' ;
22import 'dart:io' ;
33
4- import 'package:example/plugin/underscore_to_italic .dart' ;
4+ import 'package:flutter/foundation .dart' ;
55import 'package:flutter/material.dart' ;
66import 'package:flutter/services.dart' ;
7+
8+ import 'package:example/plugin/underscore_to_italic.dart' ;
9+ import 'package:file_picker/file_picker.dart' ;
710import 'package:flutter_localizations/flutter_localizations.dart' ;
811import 'package:google_fonts/google_fonts.dart' ;
9-
1012import 'package:path_provider/path_provider.dart' ;
13+ import 'package:universal_html/html.dart' as html;
1114
1215import 'package:appflowy_editor/appflowy_editor.dart' ;
1316
@@ -112,6 +115,7 @@ class _MyHomePageState extends State<MyHomePage> {
112115 child: AppFlowyEditor (
113116 editorState: _editorState! ,
114117 editorStyle: _editorStyle,
118+ editable: true ,
115119 shortcutEvents: [
116120 underscoreToItalic,
117121 ],
@@ -148,7 +152,7 @@ class _MyHomePageState extends State<MyHomePage> {
148152 ),
149153 ActionButton (
150154 icon: const Icon (Icons .import_export),
151- onPressed: () => _importDocument (),
155+ onPressed: () async => await _importDocument (),
152156 ),
153157 ActionButton (
154158 icon: const Icon (Icons .color_lens),
@@ -167,28 +171,53 @@ class _MyHomePageState extends State<MyHomePage> {
167171 void _exportDocument (EditorState editorState) async {
168172 final document = editorState.document.toJson ();
169173 final json = jsonEncode (document);
170- final directory = await getTemporaryDirectory ();
171- final path = directory.path;
172- final file = File ('$path /editor.json' );
173- await file.writeAsString (json);
174-
175- if (mounted) {
176- ScaffoldMessenger .of (context).showSnackBar (
177- SnackBar (
178- content: Text ('The document is saved to the ${file .path }' ),
179- ),
180- );
174+ if (kIsWeb) {
175+ final blob = html.Blob ([json], 'text/plain' , 'native' );
176+ html.AnchorElement (
177+ href: html.Url .createObjectUrlFromBlob (blob).toString (),
178+ )
179+ ..setAttribute ('download' , 'editor.json' )
180+ ..click ();
181+ } else {
182+ final directory = await getTemporaryDirectory ();
183+ final path = directory.path;
184+ final file = File ('$path /editor.json' );
185+ await file.writeAsString (json);
186+
187+ if (mounted) {
188+ ScaffoldMessenger .of (context).showSnackBar (
189+ SnackBar (
190+ content: Text ('The document is saved to the ${file .path }' ),
191+ ),
192+ );
193+ }
181194 }
182195 }
183196
184- void _importDocument () async {
185- final directory = await getTemporaryDirectory ();
186- final path = directory.path;
187- final file = File ('$path /editor.json' );
188- setState (() {
189- _editorState = null ;
190- _jsonString = file.readAsString ();
191- });
197+ Future <void > _importDocument () async {
198+ if (kIsWeb) {
199+ final result = await FilePicker .platform.pickFiles (
200+ allowMultiple: false ,
201+ allowedExtensions: ['json' ],
202+ type: FileType .custom,
203+ );
204+ final bytes = result? .files.first.bytes;
205+ if (bytes != null ) {
206+ final jsonString = const Utf8Decoder ().convert (bytes);
207+ setState (() {
208+ _editorState = null ;
209+ _jsonString = Future .value (jsonString);
210+ });
211+ }
212+ } else {
213+ final directory = await getTemporaryDirectory ();
214+ final path = '${directory .path }/editor.json' ;
215+ final file = File (path);
216+ setState (() {
217+ _editorState = null ;
218+ _jsonString = file.readAsString ();
219+ });
220+ }
192221 }
193222
194223 void _switchToPage (int pageIndex) {
0 commit comments