editor_page.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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.copyWith(
  210. padding: (_) => const EdgeInsets.symmetric(vertical: 10),
  211. ),
  212. ),
  213. DatabaseBlockKeys.boardType: DatabaseViewBlockComponentBuilder(
  214. configuration: configuration.copyWith(
  215. padding: (_) => const EdgeInsets.symmetric(vertical: 10),
  216. ),
  217. ),
  218. DatabaseBlockKeys.calendarType: DatabaseViewBlockComponentBuilder(
  219. configuration: configuration.copyWith(
  220. padding: (_) => const EdgeInsets.symmetric(vertical: 10),
  221. ),
  222. ),
  223. CalloutBlockKeys.type: CalloutBlockComponentBuilder(
  224. configuration: configuration,
  225. ),
  226. DividerBlockKeys.type: DividerBlockComponentBuilder(
  227. configuration: configuration,
  228. height: 28.0,
  229. ),
  230. MathEquationBlockKeys.type: MathEquationBlockComponentBuilder(
  231. configuration: configuration.copyWith(
  232. padding: (_) => const EdgeInsets.symmetric(vertical: 20),
  233. ),
  234. ),
  235. CodeBlockKeys.type: CodeBlockComponentBuilder(
  236. configuration: configuration.copyWith(
  237. textStyle: (_) => styleCustomizer.codeBlockStyleBuilder(),
  238. placeholderTextStyle: (_) => styleCustomizer.codeBlockStyleBuilder(),
  239. ),
  240. padding: const EdgeInsets.only(
  241. left: 30,
  242. right: 30,
  243. bottom: 36,
  244. ),
  245. ),
  246. AutoCompletionBlockKeys.type: AutoCompletionBlockComponentBuilder(),
  247. SmartEditBlockKeys.type: SmartEditBlockComponentBuilder(),
  248. ToggleListBlockKeys.type: ToggleListBlockComponentBuilder(
  249. configuration: configuration,
  250. ),
  251. OutlineBlockKeys.type: OutlineBlockComponentBuilder(
  252. configuration: configuration.copyWith(
  253. placeholderTextStyle: (_) =>
  254. styleCustomizer.outlineBlockPlaceholderStyleBuilder(),
  255. ),
  256. ),
  257. };
  258. final builders = {
  259. ...standardBlockComponentBuilderMap,
  260. ...customBlockComponentBuilderMap,
  261. };
  262. // customize the action builder. actually, we can customize them in their own builder. Put them here just for convenience.
  263. for (final entry in builders.entries) {
  264. if (entry.key == PageBlockKeys.type) {
  265. continue;
  266. }
  267. final builder = entry.value;
  268. // customize the action builder.
  269. final supportColorBuilderTypes = [
  270. ParagraphBlockKeys.type,
  271. HeadingBlockKeys.type,
  272. BulletedListBlockKeys.type,
  273. NumberedListBlockKeys.type,
  274. QuoteBlockKeys.type,
  275. TodoListBlockKeys.type,
  276. CalloutBlockKeys.type,
  277. OutlineBlockKeys.type,
  278. ToggleListBlockKeys.type,
  279. ];
  280. final supportAlignBuilderType = [
  281. ImageBlockKeys.type,
  282. ];
  283. final colorAction = [
  284. OptionAction.divider,
  285. OptionAction.color,
  286. ];
  287. final alignAction = [
  288. OptionAction.divider,
  289. OptionAction.align,
  290. ];
  291. final List<OptionAction> actions = [
  292. ...standardActions,
  293. if (supportColorBuilderTypes.contains(entry.key)) ...colorAction,
  294. if (supportAlignBuilderType.contains(entry.key)) ...alignAction,
  295. ];
  296. builder.showActions = (_) => true;
  297. builder.actionBuilder = (context, state) {
  298. final top = builder.configuration.padding(context.node).top;
  299. final padding = context.node.type == HeadingBlockKeys.type
  300. ? EdgeInsets.only(top: top + 8.0)
  301. : EdgeInsets.only(top: top + 2.0);
  302. return Padding(
  303. padding: padding,
  304. child: BlockActionList(
  305. blockComponentContext: context,
  306. blockComponentState: state,
  307. editorState: widget.editorState,
  308. actions: actions,
  309. showSlashMenu: () => showSlashMenu(widget.editorState),
  310. ),
  311. );
  312. };
  313. }
  314. return builders;
  315. }
  316. List<SelectionMenuItem> _customSlashMenuItems() {
  317. final items = [...standardSelectionMenuItems];
  318. final imageItem = items.firstWhereOrNull(
  319. (element) => element.name == AppFlowyEditorLocalizations.current.image,
  320. );
  321. if (imageItem != null) {
  322. final imageItemIndex = items.indexOf(imageItem);
  323. if (imageItemIndex != -1) {
  324. items[imageItemIndex] = customImageMenuItem;
  325. }
  326. }
  327. return [
  328. ...items,
  329. inlineGridMenuItem(documentBloc),
  330. referencedGridMenuItem,
  331. inlineBoardMenuItem(documentBloc),
  332. referencedBoardMenuItem,
  333. inlineCalendarMenuItem(documentBloc),
  334. referencedCalendarMenuItem,
  335. calloutItem,
  336. outlineItem,
  337. mathEquationItem,
  338. codeBlockItem,
  339. toggleListBlockItem,
  340. emojiMenuItem,
  341. autoGeneratorMenuItem,
  342. ];
  343. }
  344. (bool, Selection?) _computeAutoFocusParameters() {
  345. if (widget.editorState.document.isEmpty) {
  346. return (true, Selection.collapse([0], 0));
  347. }
  348. final nodes = widget.editorState.document.root.children
  349. .where((element) => element.delta != null);
  350. final isAllEmpty =
  351. nodes.isNotEmpty && nodes.every((element) => element.delta!.isEmpty);
  352. if (isAllEmpty) {
  353. return (true, Selection.collapse(nodes.first.path, 0));
  354. }
  355. return const (false, null);
  356. }
  357. Future<void> _initializeShortcuts() async {
  358. //TODO(Xazin): Refactor lazy initialization
  359. defaultCommandShortcutEvents;
  360. final settingsShortcutService = SettingsShortcutService();
  361. final customizeShortcuts =
  362. await settingsShortcutService.getCustomizeShortcuts();
  363. await settingsShortcutService.updateCommandShortcuts(
  364. standardCommandShortcutEvents,
  365. customizeShortcuts,
  366. );
  367. }
  368. }