editor_page.dart 13 KB

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