document_page.dart 4.8 KB

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