document_page.dart 4.3 KB

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