document_page.dart 4.6 KB

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