editor_page.dart 12 KB

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