editor_page.dart 13 KB

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