document_page.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:appflowy/plugins/document/application/doc_bloc.dart';
  4. import 'package:appflowy/plugins/document/presentation/banner.dart';
  5. import 'package:appflowy/plugins/document/presentation/editor_page.dart';
  6. import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
  7. import 'package:appflowy/plugins/document/presentation/export_page_widget.dart';
  8. import 'package:appflowy/startup/startup.dart';
  9. import 'package:appflowy/util/base64_string.dart';
  10. import 'package:appflowy/util/file_picker/file_picker_service.dart';
  11. import 'package:appflowy_backend/protobuf/flowy-document2/protobuf.dart'
  12. hide DocumentEvent;
  13. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  14. import 'package:appflowy_editor/appflowy_editor.dart';
  15. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  16. import 'package:flowy_infra_ui/widget/error_page.dart';
  17. import 'package:flutter/material.dart';
  18. import 'package:flutter_bloc/flutter_bloc.dart';
  19. import 'package:intl/intl.dart';
  20. import 'package:path/path.dart' as p;
  21. class DocumentPage extends StatefulWidget {
  22. const DocumentPage({
  23. super.key,
  24. required this.onDeleted,
  25. required this.view,
  26. });
  27. final VoidCallback onDeleted;
  28. final ViewPB view;
  29. @override
  30. State<DocumentPage> createState() => _DocumentPageState();
  31. }
  32. class _DocumentPageState extends State<DocumentPage> {
  33. late final DocumentBloc documentBloc;
  34. EditorState? editorState;
  35. @override
  36. void initState() {
  37. super.initState();
  38. documentBloc = getIt<DocumentBloc>(param1: widget.view)
  39. ..add(const DocumentEvent.initial());
  40. // The appflowy editor use Intl as localization, set the default language as fallback.
  41. Intl.defaultLocale = 'en_US';
  42. }
  43. @override
  44. void dispose() {
  45. documentBloc.close();
  46. super.dispose();
  47. }
  48. @override
  49. Widget build(BuildContext context) {
  50. return BlocProvider.value(
  51. value: documentBloc,
  52. child: BlocBuilder<DocumentBloc, DocumentState>(
  53. builder: (context, state) {
  54. return state.loadingState.when(
  55. loading: () => const SizedBox.shrink(),
  56. finish: (result) => result.fold(
  57. (error) => FlowyErrorPage(error.toString()),
  58. (data) {
  59. if (state.forceClose) {
  60. widget.onDeleted();
  61. return const SizedBox.shrink();
  62. } else if (documentBloc.editorState == null) {
  63. return Center(
  64. child: ExportPageWidget(
  65. onTap: () async => await _exportPage(data),
  66. ),
  67. );
  68. } else {
  69. editorState = documentBloc.editorState!;
  70. return _buildEditorPage(context, state);
  71. }
  72. },
  73. ),
  74. );
  75. },
  76. ),
  77. );
  78. }
  79. Widget _buildEditorPage(BuildContext context, DocumentState state) {
  80. final appflowyEditorPage = AppFlowyEditorPage(
  81. editorState: editorState!,
  82. header: _buildCoverAndIcon(context),
  83. );
  84. return Column(
  85. children: [
  86. if (state.isDeleted) _buildBanner(context),
  87. Expanded(
  88. child: appflowyEditorPage,
  89. ),
  90. ],
  91. );
  92. }
  93. Widget _buildBanner(BuildContext context) {
  94. return DocumentBanner(
  95. onRestore: () => documentBloc.add(const DocumentEvent.restorePage()),
  96. onDelete: () => documentBloc.add(const DocumentEvent.deletePermanently()),
  97. );
  98. }
  99. Widget _buildCoverAndIcon(BuildContext context) {
  100. if (editorState == null) {
  101. return const Placeholder();
  102. }
  103. final page = editorState!.document.root;
  104. return CoverImageNodeWidget(
  105. node: page,
  106. editorState: editorState!,
  107. );
  108. }
  109. Future<void> _exportPage(DocumentDataPB data) async {
  110. final picker = getIt<FilePickerService>();
  111. final dir = await picker.getDirectoryPath();
  112. if (dir == null) {
  113. return;
  114. }
  115. final path = p.join(dir, '${documentBloc.view.name}.json');
  116. const encoder = JsonEncoder.withIndent(' ');
  117. final json = encoder.convert(data.toProto3Json());
  118. await File(path).writeAsString(json.base64.base64);
  119. _showMessage('Export success to $path');
  120. }
  121. void _showMessage(String message) {
  122. if (!mounted) {
  123. return;
  124. }
  125. ScaffoldMessenger.of(context).showSnackBar(
  126. SnackBar(
  127. content: FlowyText(message),
  128. ),
  129. );
  130. }
  131. }