| 
									
										
										
										
											2024-01-24 15:15:57 +01:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-20 16:03:39 -10:00
										 |  |  | import 'package:appflowy/generated/locale_keys.g.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | import 'package:appflowy/plugins/document/application/doc_bloc.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/document/presentation/banner.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/document/presentation/editor_page.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart'; | 
					
						
							| 
									
										
										
										
											2023-06-15 16:33:44 +08:00
										 |  |  | import 'package:appflowy/plugins/document/presentation/editor_style.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | import 'package:appflowy/startup/startup.dart'; | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  | import 'package:appflowy/workspace/application/notifications/notification_action.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/workspace/application/notifications/notification_action_bloc.dart'; | 
					
						
							| 
									
										
										
										
											2023-11-02 15:24:17 +08:00
										 |  |  | import 'package:appflowy/workspace/application/view/prelude.dart'; | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  | import 'package:appflowy_backend/log.dart'; | 
					
						
							| 
									
										
										
										
											2023-12-31 07:29:40 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  | import 'package:appflowy_editor/appflowy_editor.dart' hide Log; | 
					
						
							| 
									
										
										
										
											2023-06-20 16:03:39 -10:00
										 |  |  | import 'package:easy_localization/easy_localization.dart'; | 
					
						
							| 
									
										
										
										
											2021-10-19 13:56:11 +08:00
										 |  |  | import 'package:flowy_infra_ui/widget/error_page.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | import 'package:flutter_bloc/flutter_bloc.dart'; | 
					
						
							| 
									
										
										
										
											2021-10-22 23:49:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 15:24:32 +08:00
										 |  |  | enum EditorNotificationType { | 
					
						
							|  |  |  |   undo, | 
					
						
							|  |  |  |   redo, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EditorNotification extends Notification { | 
					
						
							|  |  |  |   const EditorNotification({ | 
					
						
							|  |  |  |     required this.type, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   EditorNotification.undo() : type = EditorNotificationType.undo; | 
					
						
							|  |  |  |   EditorNotification.redo() : type = EditorNotificationType.redo; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final EditorNotificationType type; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-23 22:17:47 +08:00
										 |  |  | class DocumentPage extends StatefulWidget { | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   const DocumentPage({ | 
					
						
							|  |  |  |     super.key, | 
					
						
							|  |  |  |     required this.view, | 
					
						
							| 
									
										
										
										
											2024-01-24 15:15:57 +01:00
										 |  |  |     required this.onDeleted, | 
					
						
							|  |  |  |     this.initialSelection, | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 14:11:29 +08:00
										 |  |  |   final ViewPB view; | 
					
						
							| 
									
										
										
										
											2024-01-24 15:15:57 +01:00
										 |  |  |   final VoidCallback onDeleted; | 
					
						
							|  |  |  |   final Selection? initialSelection; | 
					
						
							| 
									
										
										
										
											2021-07-24 14:05:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-08 16:58:58 +08:00
										 |  |  |   @override | 
					
						
							| 
									
										
										
										
											2022-02-23 22:17:47 +08:00
										 |  |  |   State<DocumentPage> createState() => _DocumentPageState(); | 
					
						
							| 
									
										
										
										
											2021-10-08 16:58:58 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-23 22:17:47 +08:00
										 |  |  | class _DocumentPageState extends State<DocumentPage> { | 
					
						
							| 
									
										
										
										
											2021-10-19 13:56:11 +08:00
										 |  |  |   @override | 
					
						
							|  |  |  |   void initState() { | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     super.initState(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-26 10:38:57 +08:00
										 |  |  |     // The appflowy editor use Intl as localization, set the default language as fallback.
 | 
					
						
							| 
									
										
										
										
											2022-10-22 21:57:44 +08:00
										 |  |  |     Intl.defaultLocale = 'en_US'; | 
					
						
							| 
									
										
										
										
											2021-10-19 13:56:11 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-24 18:55:13 +08:00
										 |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  |     return MultiBlocProvider( | 
					
						
							|  |  |  |       providers: [ | 
					
						
							|  |  |  |         BlocProvider.value(value: getIt<NotificationActionBloc>()), | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |         BlocProvider( | 
					
						
							|  |  |  |           create: (_) => DocumentBloc(view: widget.view) | 
					
						
							|  |  |  |             ..add(const DocumentEvent.initial()), | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  |       ], | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |       child: BlocBuilder<DocumentBloc, DocumentState>( | 
					
						
							|  |  |  |         builder: (context, state) { | 
					
						
							|  |  |  |           if (state.isLoading) { | 
					
						
							|  |  |  |             return const Center(child: CircularProgressIndicator.adaptive()); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           final editorState = state.editorState; | 
					
						
							|  |  |  |           final error = state.error; | 
					
						
							|  |  |  |           if (error != null || editorState == null) { | 
					
						
							|  |  |  |             Log.error(error); | 
					
						
							|  |  |  |             return FlowyErrorPage.message( | 
					
						
							|  |  |  |               error.toString(), | 
					
						
							|  |  |  |               howToFix: LocaleKeys.errorDialog_howToFixFallback.tr(), | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (state.forceClose) { | 
					
						
							|  |  |  |             widget.onDeleted(); | 
					
						
							|  |  |  |             return const SizedBox.shrink(); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return BlocListener<NotificationActionBloc, NotificationActionState>( | 
					
						
							|  |  |  |             listener: _onNotificationAction, | 
					
						
							| 
									
										
										
										
											2024-01-24 15:15:57 +01:00
										 |  |  |             listenWhen: (_, curr) => curr.action != null, | 
					
						
							|  |  |  |             child: _buildEditorPage(context, state), | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |           ); | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |       ), | 
					
						
							| 
									
										
										
										
											2021-07-24 14:05:49 +08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   Widget _buildEditorPage(BuildContext context, DocumentState state) { | 
					
						
							|  |  |  |     final appflowyEditorPage = AppFlowyEditorPage( | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |       editorState: state.editorState!, | 
					
						
							| 
									
										
										
										
											2023-06-15 16:33:44 +08:00
										 |  |  |       styleCustomizer: EditorStyleCustomizer( | 
					
						
							|  |  |  |         context: context, | 
					
						
							| 
									
										
										
										
											2023-08-30 17:21:32 +08:00
										 |  |  |         // the 44 is the width of the left action list
 | 
					
						
							| 
									
										
										
										
											2023-11-02 15:24:17 +08:00
										 |  |  |         padding: EditorStyleCustomizer.documentPadding, | 
					
						
							| 
									
										
										
										
											2023-06-15 16:33:44 +08:00
										 |  |  |       ), | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |       header: _buildCoverAndIcon(context, state.editorState!), | 
					
						
							| 
									
										
										
										
											2024-01-24 15:15:57 +01:00
										 |  |  |       initialSelection: widget.initialSelection, | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-11-07 15:24:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 15:31:39 +08:00
										 |  |  |     return Column( | 
					
						
							|  |  |  |       children: [ | 
					
						
							| 
									
										
										
										
											2023-12-21 08:12:40 +08:00
										 |  |  |         // Only show the indicator in integration test mode
 | 
					
						
							|  |  |  |         // if (FlowyRunner.currentMode.isIntegrationTest)
 | 
					
						
							|  |  |  |         //   const DocumentSyncIndicator(),
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |         if (state.isDeleted) _buildBanner(context), | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  |         Expanded(child: appflowyEditorPage), | 
					
						
							| 
									
										
										
										
											2022-11-14 15:31:39 +08:00
										 |  |  |       ], | 
					
						
							| 
									
										
										
										
											2021-10-31 19:48:20 +08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   Widget _buildBanner(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2022-02-23 22:17:47 +08:00
										 |  |  |     return DocumentBanner( | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |       onRestore: () => context.read<DocumentBloc>().add( | 
					
						
							|  |  |  |             const DocumentEvent.restorePage(), | 
					
						
							|  |  |  |           ), | 
					
						
							|  |  |  |       onDelete: () => context.read<DocumentBloc>().add( | 
					
						
							|  |  |  |             const DocumentEvent.deletePermanently(), | 
					
						
							|  |  |  |           ), | 
					
						
							| 
									
										
										
										
											2021-10-31 19:48:20 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2021-10-08 16:58:58 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-02-16 10:17:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |   Widget _buildCoverAndIcon(BuildContext context, EditorState editorState) { | 
					
						
							|  |  |  |     final page = editorState.document.root; | 
					
						
							| 
									
										
										
										
											2023-06-27 15:17:51 +08:00
										 |  |  |     return DocumentHeaderNodeWidget( | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |       node: page, | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |       editorState: editorState, | 
					
						
							| 
									
										
										
										
											2023-11-02 15:24:17 +08:00
										 |  |  |       view: widget.view, | 
					
						
							|  |  |  |       onIconChanged: (icon) async { | 
					
						
							|  |  |  |         await ViewBackendService.updateViewIcon( | 
					
						
							|  |  |  |           viewId: widget.view.id, | 
					
						
							|  |  |  |           viewIcon: icon, | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-07-24 14:05:49 +08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-02-21 13:25:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |   // Future<void> _exportPage(DocumentDataPB data) async {
 | 
					
						
							|  |  |  |   //   final picker = getIt<FilePickerService>();
 | 
					
						
							|  |  |  |   //   final dir = await picker.getDirectoryPath();
 | 
					
						
							|  |  |  |   //   if (dir == null) {
 | 
					
						
							|  |  |  |   //     return;
 | 
					
						
							|  |  |  |   //   }
 | 
					
						
							|  |  |  |   //   final path = p.join(dir, '${documentBloc.view.name}.json');
 | 
					
						
							|  |  |  |   //   const encoder = JsonEncoder.withIndent('  ');
 | 
					
						
							|  |  |  |   //   final json = encoder.convert(data.toProto3Json());
 | 
					
						
							|  |  |  |   //   await File(path).writeAsString(json.base64.base64);
 | 
					
						
							|  |  |  |   //   if (mounted) {
 | 
					
						
							|  |  |  |   //     showSnackBarMessage(context, 'Export success to $path');
 | 
					
						
							|  |  |  |   //   }
 | 
					
						
							|  |  |  |   // }
 | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 10:26:45 +08:00
										 |  |  |   void _onNotificationAction( | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  |     BuildContext context, | 
					
						
							|  |  |  |     NotificationActionState state, | 
					
						
							| 
									
										
										
										
											2024-01-29 10:26:45 +08:00
										 |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  |     if (state.action != null && state.action!.type == ActionType.jumpToBlock) { | 
					
						
							| 
									
										
										
										
											2024-01-24 15:15:57 +01:00
										 |  |  |       final path = state.action?.arguments?[ActionArgumentKeys.nodePath]; | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |       final editorState = context.read<DocumentBloc>().state.editorState; | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  |       if (editorState != null && widget.view.id == state.action?.objectId) { | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |         editorState.updateSelectionWithReason( | 
					
						
							| 
									
										
										
										
											2024-01-24 15:15:57 +01:00
										 |  |  |           Selection.collapsed(Position(path: [path])), | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-07-24 14:05:49 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-12-21 08:12:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DocumentSyncIndicator extends StatelessWidget { | 
					
						
							|  |  |  |   const DocumentSyncIndicator({super.key}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							|  |  |  |     return BlocBuilder<DocumentBloc, DocumentState>( | 
					
						
							|  |  |  |       builder: (context, state) { | 
					
						
							|  |  |  |         if (state.isSyncing) { | 
					
						
							|  |  |  |           return const SizedBox(height: 1, child: LinearProgressIndicator()); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           return const SizedBox(height: 1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |