document_page.dart 4.9 KB

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