editor_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/plugins/document/presentation/editor_plugins/inline_page/inline_page_reference.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 inlinePageReferenceService = InlinePageReferenceService();
  34. final List<CommandShortcutEvent> commandShortcutEvents = [
  35. ...codeBlockCommands,
  36. ...standardCommandShortcutEvents,
  37. ];
  38. final List<ToolbarItem> toolbarItems = [
  39. smartEditItem,
  40. paragraphItem,
  41. ...headingItems,
  42. ...markdownFormatItems,
  43. quoteItem,
  44. bulletedListItem,
  45. numberedListItem,
  46. linkItem,
  47. textColorItem,
  48. highlightColorItem,
  49. ];
  50. late final slashMenuItems = [
  51. inlineGridMenuItem(documentBloc),
  52. referencedGridMenuItem,
  53. inlineBoardMenuItem(documentBloc),
  54. referencedBoardMenuItem,
  55. inlineCalendarMenuItem(documentBloc),
  56. referencedCalendarMenuItem,
  57. calloutItem,
  58. mathEquationItem,
  59. codeBlockItem,
  60. emojiMenuItem,
  61. autoGeneratorMenuItem,
  62. outlineItem,
  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. OutlineBlockKeys.type: OutlineBlockComponentBuilder(),
  234. };
  235. final builders = {
  236. ...standardBlockComponentBuilderMap,
  237. ...customBlockComponentBuilderMap,
  238. };
  239. // customize the action builder. actually, we can customize them in their own builder. Put them here just for convenience.
  240. for (final entry in builders.entries) {
  241. if (entry.key == PageBlockKeys.type) {
  242. continue;
  243. }
  244. final builder = entry.value;
  245. // customize the action builder.
  246. final supportColorBuilderTypes = [
  247. ParagraphBlockKeys.type,
  248. HeadingBlockKeys.type,
  249. BulletedListBlockKeys.type,
  250. NumberedListBlockKeys.type,
  251. QuoteBlockKeys.type,
  252. TodoListBlockKeys.type,
  253. CalloutBlockKeys.type,
  254. OutlineBlockKeys.type,
  255. ];
  256. final supportAlignBuilderType = [
  257. ImageBlockKeys.type,
  258. ];
  259. final colorAction = [
  260. OptionAction.divider,
  261. OptionAction.color,
  262. ];
  263. final alignAction = [
  264. OptionAction.divider,
  265. OptionAction.align,
  266. ];
  267. final List<OptionAction> actions = [
  268. ...standardActions,
  269. if (supportColorBuilderTypes.contains(entry.key)) ...colorAction,
  270. if (supportAlignBuilderType.contains(entry.key)) ...alignAction,
  271. ];
  272. builder.showActions = (_) => true;
  273. builder.actionBuilder = (context, state) {
  274. final padding = context.node.type == HeadingBlockKeys.type
  275. ? const EdgeInsets.only(top: 8.0)
  276. : const EdgeInsets.all(0);
  277. return Padding(
  278. padding: padding,
  279. child: BlockActionList(
  280. blockComponentContext: context,
  281. blockComponentState: state,
  282. editorState: widget.editorState,
  283. actions: actions,
  284. showSlashMenu: () => showSlashMenu(
  285. widget.editorState,
  286. ),
  287. ),
  288. );
  289. };
  290. }
  291. return builders;
  292. }
  293. (bool, Selection?) _computeAutoFocusParameters() {
  294. if (widget.editorState.document.isEmpty) {
  295. return (true, Selection.collapse([0], 0));
  296. }
  297. final nodes = widget.editorState.document.root.children
  298. .where((element) => element.delta != null);
  299. final isAllEmpty =
  300. nodes.isNotEmpty && nodes.every((element) => element.delta!.isEmpty);
  301. if (isAllEmpty) {
  302. return (true, Selection.collapse(nodes.first.path, 0));
  303. }
  304. return const (false, null);
  305. }
  306. }