editor_page.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. style: styleCustomizer.selectionMenuStyleBuilder(),
  72. ).handler;
  73. late final styleCustomizer = EditorStyleCustomizer(context: context);
  74. DocumentBloc get documentBloc => context.read<DocumentBloc>();
  75. @override
  76. Widget build(BuildContext context) {
  77. final (bool autoFocus, Selection? selection) =
  78. _computeAutoFocusParameters();
  79. final editor = AppFlowyEditor.custom(
  80. editorState: widget.editorState,
  81. editable: true,
  82. scrollController: scrollController,
  83. // setup the auto focus parameters
  84. autoFocus: autoFocus,
  85. focusedSelection: selection,
  86. // setup the theme
  87. editorStyle: styleCustomizer.style(),
  88. // customize the block builder
  89. blockComponentBuilders: blockComponentBuilders,
  90. // customize the shortcuts
  91. characterShortcutEvents: characterShortcutEvents,
  92. commandShortcutEvents: commandShortcutEvents,
  93. header: widget.header,
  94. );
  95. return Center(
  96. child: ConstrainedBox(
  97. constraints: const BoxConstraints(
  98. maxWidth: double.infinity,
  99. ),
  100. child: FloatingToolbar(
  101. items: toolbarItems,
  102. editorState: widget.editorState,
  103. scrollController: scrollController,
  104. child: editor,
  105. ),
  106. ),
  107. );
  108. }
  109. Map<String, BlockComponentBuilder> _customAppFlowyBlockComponentBuilders() {
  110. final standardActions = [
  111. OptionAction.delete,
  112. OptionAction.duplicate,
  113. OptionAction.divider,
  114. OptionAction.moveUp,
  115. OptionAction.moveDown,
  116. ];
  117. final configuration = BlockComponentConfiguration(
  118. padding: (_) => const EdgeInsets.symmetric(vertical: 4.0),
  119. );
  120. final customBlockComponentBuilderMap = {
  121. 'document': DocumentComponentBuilder(),
  122. ParagraphBlockKeys.type: TextBlockComponentBuilder(
  123. configuration: configuration,
  124. ),
  125. TodoListBlockKeys.type: TodoListBlockComponentBuilder(
  126. configuration: configuration.copyWith(
  127. placeholderText: (_) => 'To-do',
  128. ),
  129. ),
  130. BulletedListBlockKeys.type: BulletedListBlockComponentBuilder(
  131. configuration: configuration.copyWith(
  132. placeholderText: (_) => 'List',
  133. ),
  134. ),
  135. NumberedListBlockKeys.type: NumberedListBlockComponentBuilder(
  136. configuration: configuration.copyWith(
  137. placeholderText: (_) => 'List',
  138. ),
  139. ),
  140. QuoteBlockKeys.type: QuoteBlockComponentBuilder(
  141. configuration: configuration.copyWith(
  142. placeholderText: (_) => 'Quote',
  143. ),
  144. ),
  145. HeadingBlockKeys.type: HeadingBlockComponentBuilder(
  146. configuration: configuration.copyWith(
  147. padding: (_) => const EdgeInsets.only(top: 12.0, bottom: 4.0),
  148. placeholderText: (node) =>
  149. 'Heading ${node.attributes[HeadingBlockKeys.level]}',
  150. ),
  151. textStyleBuilder: (level) => styleCustomizer.headingStyleBuilder(level),
  152. ),
  153. ImageBlockKeys.type: ImageBlockComponentBuilder(),
  154. BoardBlockKeys.type: BoardBlockComponentBuilder(
  155. configuration: configuration,
  156. ),
  157. GridBlockKeys.type: GridBlockComponentBuilder(
  158. configuration: configuration,
  159. ),
  160. CalloutBlockKeys.type: CalloutBlockComponentBuilder(
  161. configuration: configuration,
  162. ),
  163. DividerBlockKeys.type: DividerBlockComponentBuilder(),
  164. MathEquationBlockKeys.type: MathEquationBlockComponentBuilder(
  165. configuration: configuration.copyWith(
  166. padding: (_) => const EdgeInsets.symmetric(vertical: 20),
  167. ),
  168. ),
  169. CodeBlockKeys.type: CodeBlockComponentBuilder(
  170. configuration: configuration.copyWith(
  171. textStyle: (_) => styleCustomizer.codeBlockStyleBuilder(),
  172. placeholderTextStyle: (_) => styleCustomizer.codeBlockStyleBuilder(),
  173. ),
  174. padding: const EdgeInsets.only(
  175. left: 30,
  176. right: 30,
  177. bottom: 36,
  178. ),
  179. ),
  180. AutoCompletionBlockKeys.type: AutoCompletionBlockComponentBuilder(),
  181. SmartEditBlockKeys.type: SmartEditBlockComponentBuilder(),
  182. ToggleListBlockKeys.type: ToggleListBlockComponentBuilder(),
  183. };
  184. final builders = {
  185. ...standardBlockComponentBuilderMap,
  186. ...customBlockComponentBuilderMap,
  187. };
  188. // customize the action builder. actually, we can customize them in their own builder. Put them here just for convenience.
  189. for (final entry in builders.entries) {
  190. if (entry.key == 'document') {
  191. continue;
  192. }
  193. final builder = entry.value;
  194. // customize the action builder.
  195. final supportColorBuilderTypes = [
  196. ParagraphBlockKeys.type,
  197. HeadingBlockKeys.type,
  198. BulletedListBlockKeys.type,
  199. NumberedListBlockKeys.type,
  200. QuoteBlockKeys.type,
  201. TodoListBlockKeys.type,
  202. CalloutBlockKeys.type
  203. ];
  204. final colorAction = [
  205. OptionAction.divider,
  206. OptionAction.color,
  207. ];
  208. final List<OptionAction> actions = [
  209. ...standardActions,
  210. if (supportColorBuilderTypes.contains(entry.key)) ...colorAction,
  211. ];
  212. builder.showActions = (_) => true;
  213. builder.actionBuilder = (context, state) => BlockActionList(
  214. blockComponentContext: context,
  215. blockComponentState: state,
  216. editorState: widget.editorState,
  217. actions: actions,
  218. showSlashMenu: () => showSlashMenu(
  219. widget.editorState,
  220. ),
  221. );
  222. }
  223. return builders;
  224. }
  225. (bool, Selection?) _computeAutoFocusParameters() {
  226. if (widget.editorState.document.isEmpty) {
  227. return (true, Selection.collapse([0], 0));
  228. }
  229. final nodes = widget.editorState.document.root.children
  230. .where((element) => element.delta != null);
  231. final isAllEmpty =
  232. nodes.isNotEmpty && nodes.every((element) => element.delta!.isEmpty);
  233. if (isAllEmpty) {
  234. return (true, Selection.collapse(nodes.first.path, 0));
  235. }
  236. return const (false, null);
  237. }
  238. }