document_page.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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: () => const SizedBox.shrink(),
  59. finish: (result) => result.fold(
  60. (error) {
  61. Log.error(error);
  62. return FlowyErrorPage.message(
  63. error.toString(),
  64. howToFix: LocaleKeys.errorDialog_howToFixFallback.tr(),
  65. );
  66. },
  67. (data) {
  68. if (state.forceClose) {
  69. widget.onDeleted();
  70. return const SizedBox.shrink();
  71. } else if (documentBloc.editorState == null) {
  72. return Center(
  73. child: ExportPageWidget(
  74. onTap: () async => await _exportPage(data),
  75. ),
  76. );
  77. } else {
  78. editorState = documentBloc.editorState!;
  79. return _buildEditorPage(context, state);
  80. }
  81. },
  82. ),
  83. );
  84. },
  85. ),
  86. );
  87. }
  88. Widget _buildEditorPage(BuildContext context, DocumentState state) {
  89. final appflowyEditorPage = AppFlowyEditorPage(
  90. editorState: editorState!,
  91. styleCustomizer: EditorStyleCustomizer(
  92. context: context,
  93. padding: const EdgeInsets.symmetric(horizontal: 50),
  94. ),
  95. header: _buildCoverAndIcon(context),
  96. );
  97. return Column(
  98. children: [
  99. if (state.isDeleted) _buildBanner(context),
  100. Expanded(
  101. child: appflowyEditorPage,
  102. ),
  103. ],
  104. );
  105. }
  106. Widget _buildBanner(BuildContext context) {
  107. return DocumentBanner(
  108. onRestore: () => documentBloc.add(const DocumentEvent.restorePage()),
  109. onDelete: () => documentBloc.add(const DocumentEvent.deletePermanently()),
  110. );
  111. }
  112. Widget _buildCoverAndIcon(BuildContext context) {
  113. if (editorState == null) {
  114. return const Placeholder();
  115. }
  116. final page = editorState!.document.root;
  117. return DocumentHeaderNodeWidget(
  118. node: page,
  119. editorState: editorState!,
  120. );
  121. }
  122. Future<void> _exportPage(DocumentDataPB data) async {
  123. final picker = getIt<FilePickerService>();
  124. final dir = await picker.getDirectoryPath();
  125. if (dir == null) {
  126. return;
  127. }
  128. final path = p.join(dir, '${documentBloc.view.name}.json');
  129. const encoder = JsonEncoder.withIndent(' ');
  130. final json = encoder.convert(data.toProto3Json());
  131. await File(path).writeAsString(json.base64.base64);
  132. _showMessage('Export success to $path');
  133. }
  134. void _showMessage(String message) {
  135. if (!mounted) {
  136. return;
  137. }
  138. ScaffoldMessenger.of(context).showSnackBar(
  139. SnackBar(
  140. content: FlowyText(message),
  141. ),
  142. );
  143. }
  144. }