editor_page.dart 10 KB

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