editor_page.dart 8.3 KB

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