Skip to content

Commit 55be554

Browse files
authored
Merge pull request #1144 from LucasXu0/web_support
Support web platform for AppFlowy Editor
2 parents 69423b0 + 6bda1fd commit 55be554

32 files changed

+973
-452
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "appflowy-editor"
4+
}
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"hosting": {
3+
"public": "build/web",
4+
"ignore": [
5+
"firebase.json",
6+
"**/.*",
7+
"**/node_modules/**"
8+
],
9+
"rewrites": [
10+
{
11+
"source": "**",
12+
"destination": "/index.html"
13+
}
14+
],
15+
"headers": [ {
16+
"source": "**/*.@(png|jpg|jpeg|gif)",
17+
"headers": [ {
18+
"key": "Access-Control-Allow-Origin",
19+
"value": "*"
20+
} ]
21+
} ]
22+
}
23+
}

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

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import 'dart:convert';
22
import 'dart:io';
33

4-
import 'package:example/plugin/underscore_to_italic.dart';
4+
import 'package:flutter/foundation.dart';
55
import 'package:flutter/material.dart';
66
import 'package:flutter/services.dart';
7+
8+
import 'package:example/plugin/underscore_to_italic.dart';
9+
import 'package:file_picker/file_picker.dart';
710
import 'package:flutter_localizations/flutter_localizations.dart';
811
import 'package:google_fonts/google_fonts.dart';
9-
1012
import 'package:path_provider/path_provider.dart';
13+
import 'package:universal_html/html.dart' as html;
1114

1215
import '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) {

frontend/app_flowy/packages/appflowy_editor/example/lib/plugin/image_node_widget.dart

Lines changed: 0 additions & 165 deletions
This file was deleted.

0 commit comments

Comments
 (0)