document_page.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:app_flowy/plugins/doc/editor_styles.dart';
  2. import 'package:app_flowy/plugins/doc/presentation/plugins/horizontal_rule_node_widget.dart';
  3. import 'package:app_flowy/startup/startup.dart';
  4. import 'package:app_flowy/plugins/doc/presentation/banner.dart';
  5. import 'package:appflowy_editor/appflowy_editor.dart';
  6. import 'package:flowy_infra_ui/widget/error_page.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter_bloc/flutter_bloc.dart';
  10. import 'package:intl/intl.dart';
  11. import 'application/doc_bloc.dart';
  12. class DocumentPage extends StatefulWidget {
  13. final VoidCallback onDeleted;
  14. final ViewPB view;
  15. DocumentPage({
  16. required this.view,
  17. required this.onDeleted,
  18. Key? key,
  19. }) : super(key: ValueKey(view.id));
  20. @override
  21. State<DocumentPage> createState() => _DocumentPageState();
  22. }
  23. class _DocumentPageState extends State<DocumentPage> {
  24. late DocumentBloc documentBloc;
  25. final FocusNode _focusNode = FocusNode();
  26. @override
  27. void initState() {
  28. // The appflowy editor use Intl as localization, set the default language as fallback.
  29. Intl.defaultLocale = 'en_US';
  30. documentBloc = getIt<DocumentBloc>(param1: super.widget.view)
  31. ..add(const DocumentEvent.initial());
  32. super.initState();
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. return MultiBlocProvider(
  37. providers: [
  38. BlocProvider<DocumentBloc>.value(value: documentBloc),
  39. ],
  40. child:
  41. BlocBuilder<DocumentBloc, DocumentState>(builder: (context, state) {
  42. return state.loadingState.map(
  43. loading: (_) => SizedBox.expand(
  44. child: Container(color: Colors.transparent),
  45. ),
  46. finish: (result) => result.successOrFail.fold(
  47. (_) {
  48. if (state.forceClose) {
  49. widget.onDeleted();
  50. return const SizedBox();
  51. } else {
  52. return _renderDocument(context, state);
  53. }
  54. },
  55. (err) => FlowyErrorPage(err.toString()),
  56. ),
  57. );
  58. }),
  59. );
  60. }
  61. @override
  62. Future<void> dispose() async {
  63. documentBloc.close();
  64. _focusNode.dispose();
  65. super.dispose();
  66. }
  67. Widget _renderDocument(BuildContext context, DocumentState state) {
  68. return Container(
  69. color: Theme.of(context).colorScheme.surface,
  70. child: Column(
  71. children: [
  72. if (state.isDeleted) _renderBanner(context),
  73. // AppFlowy Editor
  74. _renderAppFlowyEditor(
  75. context.read<DocumentBloc>().editorState,
  76. ),
  77. ],
  78. ),
  79. );
  80. }
  81. Widget _renderBanner(BuildContext context) {
  82. return DocumentBanner(
  83. onRestore: () =>
  84. context.read<DocumentBloc>().add(const DocumentEvent.restorePage()),
  85. onDelete: () => context
  86. .read<DocumentBloc>()
  87. .add(const DocumentEvent.deletePermanently()),
  88. );
  89. }
  90. Widget _renderAppFlowyEditor(EditorState editorState) {
  91. final theme = Theme.of(context);
  92. final editor = AppFlowyEditor(
  93. editorState: editorState,
  94. autoFocus: editorState.document.isEmpty,
  95. customBuilders: {
  96. 'horizontal_rule': HorizontalRuleWidgetBuilder(),
  97. },
  98. shortcutEvents: [
  99. insertHorizontalRule,
  100. ],
  101. themeData: theme.copyWith(extensions: [
  102. ...theme.extensions.values,
  103. customEditorTheme(context),
  104. ...customPluginTheme(context),
  105. ]),
  106. );
  107. return Expanded(
  108. child: SizedBox.expand(
  109. child: editor,
  110. ),
  111. );
  112. }
  113. }