2022-11-09 11:37:26 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
|
|
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
|
2023-06-05 13:16:20 +08:00
|
|
|
..level = LogLevel.all;
|
2022-11-09 11:37:26 +08:00
|
|
|
onEditorStateChange(editorState);
|
2023-04-28 13:33:13 +08:00
|
|
|
final scrollController = ScrollController();
|
2023-04-25 19:58:48 +08:00
|
|
|
if (PlatformExtension.isDesktopOrWeb) {
|
|
|
|
|
return FloatingToolbar(
|
2023-04-28 16:25:51 +08:00
|
|
|
items: [
|
|
|
|
|
paragraphItem,
|
2023-04-29 16:39:32 +08:00
|
|
|
...headingItems,
|
2023-05-02 18:44:52 +08:00
|
|
|
...markdownFormatItems,
|
2023-04-29 11:22:39 +08:00
|
|
|
quoteItem,
|
|
|
|
|
bulletedListItem,
|
|
|
|
|
numberedListItem,
|
|
|
|
|
linkItem,
|
2023-05-10 22:26:52 -05:00
|
|
|
textColorItem,
|
|
|
|
|
highlightColorItem
|
2023-04-28 16:25:51 +08:00
|
|
|
],
|
2023-04-28 13:33:13 +08:00
|
|
|
editorState: editorState,
|
|
|
|
|
scrollController: scrollController,
|
|
|
|
|
child: _buildEditor(
|
|
|
|
|
context,
|
|
|
|
|
editorState,
|
|
|
|
|
scrollController,
|
|
|
|
|
),
|
|
|
|
|
);
|
2023-06-13 01:01:08 -05:00
|
|
|
} else if (PlatformExtension.isMobile) {
|
2023-04-25 19:58:48 +08:00
|
|
|
return Column(
|
|
|
|
|
children: [
|
2023-04-28 13:33:13 +08:00
|
|
|
Expanded(
|
2023-05-20 05:43:38 -05:00
|
|
|
child: _buildMobileEditor(
|
|
|
|
|
context,
|
|
|
|
|
editorState,
|
|
|
|
|
scrollController,
|
|
|
|
|
),
|
2023-04-28 13:33:13 +08:00
|
|
|
),
|
2023-06-13 01:01:08 -05:00
|
|
|
MobileToolbar(
|
|
|
|
|
editorState: editorState,
|
|
|
|
|
toolbarItems: [
|
|
|
|
|
textDecorationMobileToolbarItem,
|
|
|
|
|
headingMobileToolbarItem,
|
|
|
|
|
todoListMobileToolbarItem,
|
|
|
|
|
listMobileToolbarItem,
|
|
|
|
|
linkMobileToolbarItem,
|
|
|
|
|
quoteMobileToolbarItem,
|
|
|
|
|
codeMobileToolbarItem,
|
|
|
|
|
// dividerMobileToolbarItem,
|
|
|
|
|
],
|
|
|
|
|
),
|
2023-04-25 19:58:48 +08:00
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-11-09 11:37:26 +08:00
|
|
|
}
|
2023-06-13 01:01:08 -05:00
|
|
|
return const Center(
|
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
|
);
|
2022-11-09 11:37:26 +08:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-04-20 13:45:46 +08:00
|
|
|
|
2023-05-20 05:43:38 -05:00
|
|
|
Widget _buildMobileEditor(
|
|
|
|
|
BuildContext context,
|
|
|
|
|
EditorState editorState,
|
|
|
|
|
ScrollController? scrollController,
|
|
|
|
|
) {
|
2023-06-01 15:04:34 +08:00
|
|
|
return AppFlowyEditor.standard(
|
2023-05-20 05:43:38 -05:00
|
|
|
editorStyle: const EditorStyle.mobile(),
|
|
|
|
|
editorState: editorState,
|
|
|
|
|
scrollController: scrollController,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 13:33:13 +08:00
|
|
|
Widget _buildEditor(
|
|
|
|
|
BuildContext context,
|
|
|
|
|
EditorState editorState,
|
|
|
|
|
ScrollController? scrollController,
|
|
|
|
|
) {
|
2023-05-03 13:44:54 +08:00
|
|
|
return AppFlowyEditor.standard(
|
2023-04-20 13:45:46 +08:00
|
|
|
editorState: editorState,
|
2023-04-28 13:33:13 +08:00
|
|
|
scrollController: scrollController,
|
2023-04-20 13:45:46 +08:00
|
|
|
);
|
|
|
|
|
}
|
2022-11-09 11:37:26 +08:00
|
|
|
}
|