document_page.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // the 44 is the width of the left action list
  95. padding: const EdgeInsets.only(left: 40, right: 40 + 44),
  96. ),
  97. header: _buildCoverAndIcon(context),
  98. );
  99. return Column(
  100. children: [
  101. if (state.isDeleted) _buildBanner(context),
  102. Expanded(
  103. child: appflowyEditorPage,
  104. ),
  105. ],
  106. );
  107. }
  108. Widget _buildBanner(BuildContext context) {
  109. return DocumentBanner(
  110. onRestore: () => documentBloc.add(const DocumentEvent.restorePage()),
  111. onDelete: () => documentBloc.add(const DocumentEvent.deletePermanently()),
  112. );
  113. }
  114. Widget _buildCoverAndIcon(BuildContext context) {
  115. if (editorState == null) {
  116. return const Placeholder();
  117. }
  118. final page = editorState!.document.root;
  119. return DocumentHeaderNodeWidget(
  120. node: page,
  121. editorState: editorState!,
  122. );
  123. }
  124. Future<void> _exportPage(DocumentDataPB data) async {
  125. final picker = getIt<FilePickerService>();
  126. final dir = await picker.getDirectoryPath();
  127. if (dir == null) {
  128. return;
  129. }
  130. final path = p.join(dir, '${documentBloc.view.name}.json');
  131. const encoder = JsonEncoder.withIndent(' ');
  132. final json = encoder.convert(data.toProto3Json());
  133. await File(path).writeAsString(json.base64.base64);
  134. _showMessage('Export success to $path');
  135. }
  136. void _showMessage(String message) {
  137. if (!mounted) {
  138. return;
  139. }
  140. ScaffoldMessenger.of(context).showSnackBar(
  141. SnackBar(
  142. content: FlowyText(message),
  143. ),
  144. );
  145. }
  146. }