| 
									
										
										
										
											2024-05-02 02:10:56 +02:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-20 16:03:39 -10:00
										 |  |  | import 'package:appflowy/generated/locale_keys.g.dart'; | 
					
						
							| 
									
										
										
										
											2024-04-30 16:55:15 +08:00
										 |  |  | import 'package:appflowy/mobile/application/page_style/document_page_style_bloc.dart'; | 
					
						
							| 
									
										
										
										
											2024-04-08 18:01:20 +08:00
										 |  |  | import 'package:appflowy/plugins/document/application/document_bloc.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | import 'package:appflowy/plugins/document/presentation/banner.dart'; | 
					
						
							| 
									
										
										
										
											2024-03-18 13:06:12 +07:00
										 |  |  | import 'package:appflowy/plugins/document/presentation/editor_notification.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | import 'package:appflowy/plugins/document/presentation/editor_page.dart'; | 
					
						
							| 
									
										
										
										
											2024-04-30 16:55:15 +08:00
										 |  |  | import 'package:appflowy/plugins/document/presentation/editor_plugins/cover/document_immersive_cover.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | 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'; | 
					
						
							| 
									
										
										
										
											2024-04-12 10:21:41 +02:00
										 |  |  | import 'package:appflowy/workspace/application/action_navigation/action_navigation_bloc.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/workspace/application/action_navigation/navigation_action.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-18 13:16:29 +08:00
										 |  |  | class _DocumentPageState extends State<DocumentPage> | 
					
						
							|  |  |  |     with WidgetsBindingObserver { | 
					
						
							| 
									
										
										
										
											2024-03-18 13:06:12 +07:00
										 |  |  |   EditorState? editorState; | 
					
						
							| 
									
										
										
										
											2024-04-29 13:44:42 +08:00
										 |  |  |   late final documentBloc = DocumentBloc(documentId: widget.view.id) | 
					
						
							| 
									
										
										
										
											2024-04-18 13:16:29 +08:00
										 |  |  |     ..add(const DocumentEvent.initial()); | 
					
						
							| 
									
										
										
										
											2024-03-18 13:06:12 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 13:56:11 +08:00
										 |  |  |   @override | 
					
						
							|  |  |  |   void initState() { | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     super.initState(); | 
					
						
							| 
									
										
										
										
											2024-04-18 13:16:29 +08:00
										 |  |  |     WidgetsBinding.instance.addObserver(this); | 
					
						
							| 
									
										
										
										
											2024-03-18 13:06:12 +07:00
										 |  |  |     EditorNotification.addListener(_onEditorNotification); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   void dispose() { | 
					
						
							|  |  |  |     EditorNotification.removeListener(_onEditorNotification); | 
					
						
							| 
									
										
										
										
											2024-04-18 13:16:29 +08:00
										 |  |  |     WidgetsBinding.instance.removeObserver(this); | 
					
						
							|  |  |  |     documentBloc.close(); | 
					
						
							| 
									
										
										
										
											2024-03-18 13:06:12 +07:00
										 |  |  |     super.dispose(); | 
					
						
							| 
									
										
										
										
											2021-10-19 13:56:11 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-18 13:16:29 +08:00
										 |  |  |   @override | 
					
						
							|  |  |  |   void didChangeAppLifecycleState(AppLifecycleState state) { | 
					
						
							|  |  |  |     if (state == AppLifecycleState.paused || | 
					
						
							|  |  |  |         state == AppLifecycleState.detached) { | 
					
						
							|  |  |  |       documentBloc.add(const DocumentEvent.clearAwarenessStates()); | 
					
						
							|  |  |  |     } else if (state == AppLifecycleState.resumed) { | 
					
						
							|  |  |  |       documentBloc.add(const DocumentEvent.syncAwarenessStates()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-24 18:55:13 +08:00
										 |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2023-10-26 03:41:03 +02:00
										 |  |  |     return MultiBlocProvider( | 
					
						
							|  |  |  |       providers: [ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:21:41 +02:00
										 |  |  |         BlocProvider.value(value: getIt<ActionNavigationBloc>()), | 
					
						
							| 
									
										
										
										
											2024-04-18 13:16:29 +08:00
										 |  |  |         BlocProvider.value(value: documentBloc), | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2024-03-18 13:06:12 +07:00
										 |  |  |           this.editorState = editorState; | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |           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(); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-12 10:21:41 +02:00
										 |  |  |           return BlocListener<ActionNavigationBloc, ActionNavigationState>( | 
					
						
							| 
									
										
										
										
											2024-03-22 14:15:38 +07:00
										 |  |  |             listenWhen: (_, curr) => curr.action != null, | 
					
						
							| 
									
										
										
										
											2024-04-12 10:21:41 +02:00
										 |  |  |             listener: _onNotificationAction, | 
					
						
							| 
									
										
										
										
											2024-01-24 15:15:57 +01:00
										 |  |  |             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) { | 
					
						
							| 
									
										
										
										
											2024-04-30 16:55:15 +08:00
										 |  |  |     final Widget child; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PlatformExtension.isMobile) { | 
					
						
							|  |  |  |       child = BlocBuilder<DocumentPageStyleBloc, DocumentPageStyleState>( | 
					
						
							|  |  |  |         builder: (context, styleState) { | 
					
						
							|  |  |  |           return AppFlowyEditorPage( | 
					
						
							|  |  |  |             editorState: state.editorState!, | 
					
						
							|  |  |  |             styleCustomizer: EditorStyleCustomizer( | 
					
						
							|  |  |  |               context: context, | 
					
						
							|  |  |  |               // the 44 is the width of the left action list
 | 
					
						
							|  |  |  |               padding: EditorStyleCustomizer.documentPadding, | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             header: _buildCoverAndIcon(context, state), | 
					
						
							|  |  |  |             initialSelection: widget.initialSelection, | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       child = AppFlowyEditorPage( | 
					
						
							|  |  |  |         editorState: state.editorState!, | 
					
						
							|  |  |  |         styleCustomizer: EditorStyleCustomizer( | 
					
						
							|  |  |  |           context: context, | 
					
						
							|  |  |  |           // the 44 is the width of the left action list
 | 
					
						
							|  |  |  |           padding: EditorStyleCustomizer.documentPadding, | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         header: _buildCoverAndIcon(context, state), | 
					
						
							|  |  |  |         initialSelection: widget.initialSelection, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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), | 
					
						
							| 
									
										
										
										
											2024-04-30 16:55:15 +08:00
										 |  |  |         Expanded(child: child), | 
					
						
							| 
									
										
										
										
											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(), | 
					
						
							|  |  |  |           ), | 
					
						
							| 
									
										
										
										
											2024-05-02 02:10:56 +02:00
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 16:55:15 +08:00
										 |  |  |   Widget _buildCoverAndIcon(BuildContext context, DocumentState state) { | 
					
						
							|  |  |  |     final editorState = state.editorState; | 
					
						
							|  |  |  |     final userProfilePB = state.userProfilePB; | 
					
						
							|  |  |  |     if (editorState == null || userProfilePB == null) { | 
					
						
							|  |  |  |       return const SizedBox.shrink(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PlatformExtension.isMobile) { | 
					
						
							|  |  |  |       return DocumentImmersiveCover( | 
					
						
							|  |  |  |         view: widget.view, | 
					
						
							|  |  |  |         userProfilePB: userProfilePB, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:26:23 +07:00
										 |  |  |     final page = editorState.document.root; | 
					
						
							| 
									
										
										
										
											2024-04-30 16:55:15 +08:00
										 |  |  |     return DocumentCoverWidget( | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2024-05-02 02:10:56 +02:00
										 |  |  |       onIconChanged: (icon) async => ViewBackendService.updateViewIcon( | 
					
						
							|  |  |  |         viewId: widget.view.id, | 
					
						
							|  |  |  |         viewIcon: icon, | 
					
						
							|  |  |  |       ), | 
					
						
							| 
									
										
										
										
											2021-07-24 14:05:49 +08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-02-21 13:25:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 13:06:12 +07:00
										 |  |  |   void _onEditorNotification(EditorNotificationType type) { | 
					
						
							|  |  |  |     final editorState = this.editorState; | 
					
						
							|  |  |  |     if (editorState == null) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (type == EditorNotificationType.undo) { | 
					
						
							|  |  |  |       undoCommand.execute(editorState); | 
					
						
							|  |  |  |     } else if (type == EditorNotificationType.redo) { | 
					
						
							|  |  |  |       redoCommand.execute(editorState); | 
					
						
							| 
									
										
										
										
											2024-05-02 02:10:56 +02:00
										 |  |  |     } else if (type == EditorNotificationType.exitEditing && | 
					
						
							|  |  |  |         editorState.selection != null) { | 
					
						
							|  |  |  |       editorState.selection = null; | 
					
						
							| 
									
										
										
										
											2024-03-18 13:06:12 +07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2024-04-12 10:21:41 +02:00
										 |  |  |     ActionNavigationState 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
										 |  |  | } |