editor_page.dart 9.6 KB

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