editor_page.dart 8.7 KB

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