editor_page.dart 9.0 KB

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