editor_page.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. slashMenuItems = _customSlashMenuItems();
  95. effectiveScrollController = widget.scrollController ?? ScrollController();
  96. }
  97. @override
  98. void dispose() {
  99. if (widget.scrollController == null) {
  100. effectiveScrollController.dispose();
  101. }
  102. super.dispose();
  103. }
  104. @override
  105. Widget build(BuildContext context) {
  106. final (bool autoFocus, Selection? selection) =
  107. _computeAutoFocusParameters();
  108. final editor = AppFlowyEditor(
  109. editorState: widget.editorState,
  110. editable: true,
  111. shrinkWrap: widget.shrinkWrap,
  112. scrollController: effectiveScrollController,
  113. // setup the auto focus parameters
  114. autoFocus: widget.autoFocus ?? autoFocus,
  115. focusedSelection: selection,
  116. // setup the theme
  117. editorStyle: styleCustomizer.style(),
  118. // customize the block builder
  119. blockComponentBuilders: blockComponentBuilders,
  120. // customize the shortcuts
  121. characterShortcutEvents: characterShortcutEvents,
  122. commandShortcutEvents: commandShortcutEvents,
  123. header: widget.header,
  124. footer: const VSpace(200),
  125. );
  126. return Center(
  127. child: ConstrainedBox(
  128. constraints: const BoxConstraints(
  129. maxWidth: double.infinity,
  130. maxHeight: double.infinity,
  131. ),
  132. child: FloatingToolbar(
  133. style: styleCustomizer.floatingToolbarStyleBuilder(),
  134. items: toolbarItems,
  135. editorState: widget.editorState,
  136. scrollController: effectiveScrollController,
  137. child: editor,
  138. ),
  139. ),
  140. );
  141. }
  142. Map<String, BlockComponentBuilder> _customAppFlowyBlockComponentBuilders() {
  143. final standardActions = [
  144. OptionAction.delete,
  145. OptionAction.duplicate,
  146. // OptionAction.divider,
  147. // OptionAction.moveUp,
  148. // OptionAction.moveDown,
  149. ];
  150. final configuration = BlockComponentConfiguration(
  151. padding: (_) => const EdgeInsets.symmetric(vertical: 5.0),
  152. );
  153. final customBlockComponentBuilderMap = {
  154. PageBlockKeys.type: PageBlockComponentBuilder(),
  155. ParagraphBlockKeys.type: TextBlockComponentBuilder(
  156. configuration: configuration,
  157. ),
  158. TodoListBlockKeys.type: TodoListBlockComponentBuilder(
  159. configuration: configuration.copyWith(
  160. placeholderText: (_) => 'To-do',
  161. ),
  162. ),
  163. BulletedListBlockKeys.type: BulletedListBlockComponentBuilder(
  164. configuration: configuration.copyWith(
  165. placeholderText: (_) => 'List',
  166. ),
  167. ),
  168. NumberedListBlockKeys.type: NumberedListBlockComponentBuilder(
  169. configuration: configuration.copyWith(
  170. placeholderText: (_) => 'List',
  171. ),
  172. ),
  173. QuoteBlockKeys.type: QuoteBlockComponentBuilder(
  174. configuration: configuration.copyWith(
  175. placeholderText: (_) => 'Quote',
  176. ),
  177. ),
  178. HeadingBlockKeys.type: HeadingBlockComponentBuilder(
  179. configuration: configuration.copyWith(
  180. padding: (_) => const EdgeInsets.only(top: 12.0, bottom: 4.0),
  181. placeholderText: (node) =>
  182. 'Heading ${node.attributes[HeadingBlockKeys.level]}',
  183. ),
  184. textStyleBuilder: (level) => styleCustomizer.headingStyleBuilder(level),
  185. ),
  186. ImageBlockKeys.type: ImageBlockComponentBuilder(
  187. configuration: configuration,
  188. showMenu: true,
  189. menuBuilder: (node, state) => Positioned(
  190. top: 0,
  191. right: 10,
  192. child: ImageMenu(
  193. node: node,
  194. state: state,
  195. ),
  196. ),
  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. configuration: configuration,
  234. ),
  235. OutlineBlockKeys.type: OutlineBlockComponentBuilder(
  236. configuration: configuration.copyWith(
  237. placeholderTextStyle: (_) =>
  238. styleCustomizer.outlineBlockPlaceholderStyleBuilder(),
  239. ),
  240. ),
  241. };
  242. final builders = {
  243. ...standardBlockComponentBuilderMap,
  244. ...customBlockComponentBuilderMap,
  245. };
  246. // customize the action builder. actually, we can customize them in their own builder. Put them here just for convenience.
  247. for (final entry in builders.entries) {
  248. if (entry.key == PageBlockKeys.type) {
  249. continue;
  250. }
  251. final builder = entry.value;
  252. // customize the action builder.
  253. final supportColorBuilderTypes = [
  254. ParagraphBlockKeys.type,
  255. HeadingBlockKeys.type,
  256. BulletedListBlockKeys.type,
  257. NumberedListBlockKeys.type,
  258. QuoteBlockKeys.type,
  259. TodoListBlockKeys.type,
  260. CalloutBlockKeys.type,
  261. OutlineBlockKeys.type,
  262. ToggleListBlockKeys.type,
  263. ];
  264. final supportAlignBuilderType = [
  265. ImageBlockKeys.type,
  266. ];
  267. final colorAction = [
  268. OptionAction.divider,
  269. OptionAction.color,
  270. ];
  271. final alignAction = [
  272. OptionAction.divider,
  273. OptionAction.align,
  274. ];
  275. final List<OptionAction> actions = [
  276. ...standardActions,
  277. if (supportColorBuilderTypes.contains(entry.key)) ...colorAction,
  278. if (supportAlignBuilderType.contains(entry.key)) ...alignAction,
  279. ];
  280. builder.showActions = (_) => true;
  281. builder.actionBuilder = (context, state) {
  282. final top = builder.configuration.padding(context.node).top;
  283. final padding = context.node.type == HeadingBlockKeys.type
  284. ? EdgeInsets.only(top: top + 8.0)
  285. : EdgeInsets.only(top: top + 2.0);
  286. return Padding(
  287. padding: padding,
  288. child: BlockActionList(
  289. blockComponentContext: context,
  290. blockComponentState: state,
  291. editorState: widget.editorState,
  292. actions: actions,
  293. showSlashMenu: () => showSlashMenu(widget.editorState),
  294. ),
  295. );
  296. };
  297. }
  298. return builders;
  299. }
  300. List<SelectionMenuItem> _customSlashMenuItems() {
  301. final items = [...standardSelectionMenuItems];
  302. final imageItem = items.firstWhereOrNull(
  303. (element) => element.name == AppFlowyEditorLocalizations.current.image,
  304. );
  305. if (imageItem != null) {
  306. final imageItemIndex = items.indexOf(imageItem);
  307. if (imageItemIndex != -1) {
  308. items[imageItemIndex] = customImageMenuItem;
  309. }
  310. }
  311. return [
  312. ...items,
  313. inlineGridMenuItem(documentBloc),
  314. referencedGridMenuItem,
  315. inlineBoardMenuItem(documentBloc),
  316. referencedBoardMenuItem,
  317. inlineCalendarMenuItem(documentBloc),
  318. referencedCalendarMenuItem,
  319. calloutItem,
  320. outlineItem,
  321. mathEquationItem,
  322. codeBlockItem,
  323. emojiMenuItem,
  324. autoGeneratorMenuItem,
  325. ];
  326. }
  327. (bool, Selection?) _computeAutoFocusParameters() {
  328. if (widget.editorState.document.isEmpty) {
  329. return (true, Selection.collapse([0], 0));
  330. }
  331. final nodes = widget.editorState.document.root.children
  332. .where((element) => element.delta != null);
  333. final isAllEmpty =
  334. nodes.isNotEmpty && nodes.every((element) => element.delta!.isEmpty);
  335. if (isAllEmpty) {
  336. return (true, Selection.collapse(nodes.first.path, 0));
  337. }
  338. return const (false, null);
  339. }
  340. }