document_page.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 locatization, 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 Column(
  69. children: [
  70. if (state.isDeleted) _renderBanner(context),
  71. // AppFlowy Editor
  72. _renderAppFlowyEditor(context.read<DocumentBloc>().editorState),
  73. ],
  74. );
  75. }
  76. Widget _renderBanner(BuildContext context) {
  77. return DocumentBanner(
  78. onRestore: () =>
  79. context.read<DocumentBloc>().add(const DocumentEvent.restorePage()),
  80. onDelete: () => context
  81. .read<DocumentBloc>()
  82. .add(const DocumentEvent.deletePermanently()),
  83. );
  84. }
  85. Widget _renderAppFlowyEditor(EditorState editorState) {
  86. final editor = AppFlowyEditor(
  87. editorState: editorState,
  88. editorStyle: customEditorStyle(context),
  89. customBuilders: {
  90. 'horizontal_rule': HorizontalRuleWidgetBuilder(),
  91. },
  92. shortcutEvents: [
  93. insertHorizontalRule,
  94. ],
  95. );
  96. return Expanded(
  97. child: SizedBox.expand(
  98. child: editor,
  99. ),
  100. );
  101. }
  102. }