editor_page.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import 'package:appflowy/plugins/document/application/doc_bloc.dart';
  2. import 'package:appflowy/plugins/document/presentation/editor_plugins/actions/option_action.dart';
  3. import 'package:appflowy/plugins/document/presentation/editor_plugins/actions/block_action_list.dart';
  4. import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
  5. import 'package:appflowy/plugins/document/presentation/editor_style.dart';
  6. import 'package:appflowy_editor/appflowy_editor.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. /// Wrapper for the appflowy editor.
  10. class AppFlowyEditorPage extends StatefulWidget {
  11. const AppFlowyEditorPage({
  12. super.key,
  13. required this.editorState,
  14. this.header,
  15. });
  16. final EditorState editorState;
  17. final Widget? header;
  18. @override
  19. State<AppFlowyEditorPage> createState() => _AppFlowyEditorPageState();
  20. }
  21. class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
  22. final scrollController = ScrollController();
  23. final slashMenuItems = [
  24. boardMenuItem,
  25. gridMenuItem,
  26. calloutItem,
  27. dividerMenuItem,
  28. mathEquationItem,
  29. codeBlockItem,
  30. emojiMenuItem,
  31. autoGeneratorMenuItem,
  32. ];
  33. final List<CommandShortcutEvent> commandShortcutEvents = [
  34. ...codeBlockCommands,
  35. ...standardCommandShortcutEvents,
  36. ];
  37. final List<ToolbarItem> toolbarItems = [
  38. smartEditItem,
  39. paragraphItem,
  40. ...headingItems,
  41. ...markdownFormatItems,
  42. quoteItem,
  43. bulletedListItem,
  44. numberedListItem,
  45. linkItem,
  46. textColorItem,
  47. highlightColorItem,
  48. ];
  49. late final Map<String, BlockComponentBuilder> blockComponentBuilders =
  50. _customAppFlowyBlockComponentBuilders();
  51. late final List<CharacterShortcutEvent> characterShortcutEvents = [
  52. // divider
  53. convertMinusesToDivider,
  54. // code block
  55. ...codeBlockCharacterEvents,
  56. // toggle list
  57. // formatGreaterToToggleList,
  58. // customize the slash menu command
  59. customSlashCommand(
  60. slashMenuItems,
  61. style: styleCustomizer.selectionMenuStyleBuilder(),
  62. ),
  63. ...standardCharacterShortcutEvents
  64. ..removeWhere(
  65. (element) => element == slashCommand,
  66. ), // remove the default slash command.
  67. ];
  68. late final showSlashMenu = customSlashCommand(
  69. slashMenuItems,
  70. shouldInsertSlash: false,
  71. ).handler;
  72. late final styleCustomizer = EditorStyleCustomizer(context: context);
  73. DocumentBloc get documentBloc => context.read<DocumentBloc>();
  74. @override
  75. Widget build(BuildContext context) {
  76. final (bool autoFocus, Selection? selection) =
  77. _computeAutoFocusParameters();
  78. final editor = AppFlowyEditor.custom(
  79. editorState: widget.editorState,
  80. editable: true,
  81. scrollController: scrollController,
  82. // setup the auto focus parameters
  83. autoFocus: autoFocus,
  84. focusedSelection: selection,
  85. // setup the theme
  86. editorStyle: styleCustomizer.style(),
  87. // customize the block builder
  88. blockComponentBuilders: blockComponentBuilders,
  89. // customize the shortcuts
  90. characterShortcutEvents: characterShortcutEvents,
  91. commandShortcutEvents: commandShortcutEvents,
  92. header: widget.header,
  93. );
  94. return Center(
  95. child: ConstrainedBox(
  96. constraints: const BoxConstraints(
  97. maxWidth: double.infinity,
  98. ),
  99. child: FloatingToolbar(
  100. items: toolbarItems,
  101. editorState: widget.editorState,
  102. scrollController: scrollController,
  103. child: editor,
  104. ),
  105. ),
  106. );
  107. }
  108. Map<String, BlockComponentBuilder> _customAppFlowyBlockComponentBuilders() {
  109. final standardActions = [
  110. OptionAction.delete,
  111. OptionAction.duplicate,
  112. OptionAction.divider,
  113. OptionAction.moveUp,
  114. OptionAction.moveDown,
  115. ];
  116. final configuration = BlockComponentConfiguration(
  117. padding: (_) => const EdgeInsets.symmetric(vertical: 4.0),
  118. );
  119. final customBlockComponentBuilderMap = {
  120. 'document': DocumentComponentBuilder(),
  121. ParagraphBlockKeys.type: TextBlockComponentBuilder(
  122. configuration: configuration,
  123. ),
  124. TodoListBlockKeys.type: TodoListBlockComponentBuilder(
  125. configuration: configuration.copyWith(
  126. placeholderText: (_) => 'To-do',
  127. ),
  128. ),
  129. BulletedListBlockKeys.type: BulletedListBlockComponentBuilder(
  130. configuration: configuration.copyWith(
  131. placeholderText: (_) => 'List',
  132. ),
  133. ),
  134. NumberedListBlockKeys.type: NumberedListBlockComponentBuilder(
  135. configuration: configuration.copyWith(
  136. placeholderText: (_) => 'List',
  137. ),
  138. ),
  139. QuoteBlockKeys.type: QuoteBlockComponentBuilder(
  140. configuration: configuration.copyWith(
  141. placeholderText: (_) => 'Quote',
  142. ),
  143. ),
  144. HeadingBlockKeys.type: HeadingBlockComponentBuilder(
  145. configuration: configuration.copyWith(
  146. padding: (_) => const EdgeInsets.only(top: 12.0, bottom: 4.0),
  147. placeholderText: (node) =>
  148. 'Heading ${node.attributes[HeadingBlockKeys.level]}',
  149. ),
  150. textStyleBuilder: (level) => styleCustomizer.headingStyleBuilder(level),
  151. ),
  152. ImageBlockKeys.type: ImageBlockComponentBuilder(),
  153. BoardBlockKeys.type: BoardBlockComponentBuilder(
  154. configuration: configuration,
  155. ),
  156. GridBlockKeys.type: GridBlockComponentBuilder(
  157. configuration: configuration,
  158. ),
  159. CalloutBlockKeys.type: CalloutBlockComponentBuilder(
  160. configuration: configuration,
  161. ),
  162. DividerBlockKeys.type: DividerBlockComponentBuilder(),
  163. MathEquationBlockKeys.type: MathEquationBlockComponentBuilder(
  164. configuration: configuration.copyWith(
  165. padding: (_) => const EdgeInsets.symmetric(vertical: 20),
  166. ),
  167. ),
  168. CodeBlockKeys.type: CodeBlockComponentBuilder(
  169. configuration: configuration.copyWith(
  170. textStyle: (_) => styleCustomizer.codeBlockStyleBuilder(),
  171. placeholderTextStyle: (_) => styleCustomizer.codeBlockStyleBuilder(),
  172. ),
  173. padding: const EdgeInsets.only(
  174. left: 30,
  175. right: 30,
  176. bottom: 36,
  177. ),
  178. ),
  179. AutoCompletionBlockKeys.type: AutoCompletionBlockComponentBuilder(),
  180. SmartEditBlockKeys.type: SmartEditBlockComponentBuilder(),
  181. ToggleListBlockKeys.type: ToggleListBlockComponentBuilder(),
  182. };
  183. final builders = {
  184. ...standardBlockComponentBuilderMap,
  185. ...customBlockComponentBuilderMap,
  186. };
  187. // customize the action builder. actually, we can customize them in their own builder. Put them here just for convenience.
  188. for (final entry in builders.entries) {
  189. if (entry.key == 'document') {
  190. continue;
  191. }
  192. final builder = entry.value;
  193. // customize the action builder.
  194. final supportColorBuilderTypes = [
  195. ParagraphBlockKeys.type,
  196. HeadingBlockKeys.type,
  197. BulletedListBlockKeys.type,
  198. NumberedListBlockKeys.type,
  199. QuoteBlockKeys.type,
  200. TodoListBlockKeys.type,
  201. CalloutBlockKeys.type
  202. ];
  203. final colorAction = [
  204. OptionAction.divider,
  205. OptionAction.color,
  206. ];
  207. final List<OptionAction> actions = [
  208. ...standardActions,
  209. if (supportColorBuilderTypes.contains(entry.key)) ...colorAction,
  210. ];
  211. builder.showActions = (_) => true;
  212. builder.actionBuilder = (context, state) => BlockActionList(
  213. blockComponentContext: context,
  214. blockComponentState: state,
  215. editorState: widget.editorState,
  216. actions: actions,
  217. showSlashMenu: () => showSlashMenu(
  218. widget.editorState,
  219. ),
  220. );
  221. }
  222. return builders;
  223. }
  224. (bool, Selection?) _computeAutoFocusParameters() {
  225. if (widget.editorState.document.isEmpty) {
  226. return (true, Selection.collapse([0], 0));
  227. }
  228. final nodes = widget.editorState.document.root.children
  229. .where((element) => element.delta != null);
  230. final isAllEmpty =
  231. nodes.isNotEmpty && nodes.every((element) => element.delta!.isEmpty);
  232. if (isAllEmpty) {
  233. return (true, Selection.collapse(nodes.first.path, 0));
  234. }
  235. return const (false, null);
  236. }
  237. }