2022-11-09 11:37:26 +08:00
|
|
|
import 'dart:convert';
|
2023-04-20 13:45:46 +08:00
|
|
|
import 'dart:io';
|
2022-11-09 11:37:26 +08:00
|
|
|
|
|
|
|
|
import 'package:appflowy_editor/appflowy_editor.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
|
|
class SimpleEditor extends StatelessWidget {
|
|
|
|
|
const SimpleEditor({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.jsonString,
|
2022-11-09 15:36:30 +08:00
|
|
|
required this.themeData,
|
2022-11-09 11:37:26 +08:00
|
|
|
required this.onEditorStateChange,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final Future<String> jsonString;
|
2022-11-09 15:36:30 +08:00
|
|
|
final ThemeData themeData;
|
2022-11-09 11:37:26 +08:00
|
|
|
final void Function(EditorState editorState) onEditorStateChange;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return FutureBuilder<String>(
|
|
|
|
|
future: jsonString,
|
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
|
if (snapshot.hasData &&
|
|
|
|
|
snapshot.connectionState == ConnectionState.done) {
|
|
|
|
|
final editorState = EditorState(
|
|
|
|
|
document: Document.fromJson(
|
|
|
|
|
Map<String, Object>.from(
|
|
|
|
|
json.decode(snapshot.data!),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
2022-12-20 09:37:18 +08:00
|
|
|
editorState.logConfiguration
|
|
|
|
|
..handler = debugPrint
|
|
|
|
|
..level = LogLevel.all;
|
2022-11-09 11:37:26 +08:00
|
|
|
onEditorStateChange(editorState);
|
2023-04-20 13:45:46 +08:00
|
|
|
return Column(
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(child: _buildEditor(context, editorState)),
|
|
|
|
|
if (Platform.isIOS || Platform.isAndroid)
|
|
|
|
|
_buildMobileToolbar(context, editorState),
|
|
|
|
|
],
|
2022-11-09 11:37:26 +08:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return const Center(
|
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-04-20 13:45:46 +08:00
|
|
|
|
|
|
|
|
Widget _buildEditor(BuildContext context, EditorState editorState) {
|
|
|
|
|
return AppFlowyEditor(
|
|
|
|
|
editorState: editorState,
|
|
|
|
|
themeData: themeData,
|
|
|
|
|
autoFocus: editorState.document.isEmpty,
|
2023-04-25 16:36:03 +08:00
|
|
|
// customBuilders: {
|
|
|
|
|
// 'paragraph': TextBlockComponentBuilder(),
|
|
|
|
|
// 'todo_list': TodoListBlockComponentBuilder(),
|
|
|
|
|
// 'bulleted_list': BulletedListBlockComponentBuilder(),
|
|
|
|
|
// 'numbered_list': NumberedListBlockComponentBuilder(),
|
|
|
|
|
// 'quote': QuoteBlockComponentBuilder(),
|
|
|
|
|
// },
|
|
|
|
|
blockComponentBuilders: {
|
2023-04-20 13:45:46 +08:00
|
|
|
'paragraph': TextBlockComponentBuilder(),
|
|
|
|
|
'todo_list': TodoListBlockComponentBuilder(),
|
|
|
|
|
'bulleted_list': BulletedListBlockComponentBuilder(),
|
|
|
|
|
'numbered_list': NumberedListBlockComponentBuilder(),
|
|
|
|
|
'quote': QuoteBlockComponentBuilder(),
|
|
|
|
|
},
|
2023-04-23 21:26:05 +08:00
|
|
|
characterShortcutEvents: [
|
2023-04-24 20:05:02 +08:00
|
|
|
// '\n'
|
2023-04-23 21:26:05 +08:00
|
|
|
insertNewLine,
|
|
|
|
|
|
|
|
|
|
// bulleted list
|
|
|
|
|
formatAsteriskToBulletedList,
|
|
|
|
|
formatMinusToBulletedList,
|
2023-04-23 22:50:14 +08:00
|
|
|
|
|
|
|
|
// slash
|
|
|
|
|
slashCommand,
|
2023-04-23 21:26:05 +08:00
|
|
|
],
|
2023-04-24 20:05:02 +08:00
|
|
|
commandShortcutEvents: [
|
|
|
|
|
// backspace
|
|
|
|
|
backspaceCommand,
|
|
|
|
|
],
|
2023-04-20 13:45:46 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildMobileToolbar(BuildContext context, EditorState editorState) {
|
|
|
|
|
return MobileToolbar(editorState: editorState);
|
|
|
|
|
}
|
2022-11-09 11:37:26 +08:00
|
|
|
}
|