editor_page.dart 8.3 KB

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