editor_page.dart 11 KB

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