editor_page.dart 11 KB

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