editor_page.dart 11 KB

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