From 01a388c1c45a58b98b5048c0d259e5c5d7ae832c Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Fri, 24 Feb 2023 09:16:51 +0800 Subject: [PATCH] chore: abstract appflowy editor build function to widget (#1878) * chore: abstract appflowy editor build function to widget * feat: refactor the editor state to optional --- .../document/application/doc_bloc.dart | 4 +- .../lib/plugins/document/document_page.dart | 51 +++++++++++++------ 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/frontend/app_flowy/lib/plugins/document/application/doc_bloc.dart b/frontend/app_flowy/lib/plugins/document/application/doc_bloc.dart index abc4078476..f66aa94008 100644 --- a/frontend/app_flowy/lib/plugins/document/application/doc_bloc.dart +++ b/frontend/app_flowy/lib/plugins/document/application/doc_bloc.dart @@ -24,7 +24,7 @@ class DocumentBloc extends Bloc { final ViewListener _listener; final TrashService _trashService; - late EditorState editorState; + EditorState? editorState; StreamSubscription? _subscription; DocumentBloc({ @@ -127,7 +127,7 @@ class DocumentBloc extends Bloc { } void _listenOnDocumentChange() { - _subscription = editorState.transactionStream.listen((transaction) { + _subscription = editorState?.transactionStream.listen((transaction) { final json = jsonEncode(TransactionAdaptor(transaction).toJson()); _documentService .applyEdit(docId: view.id, operations: json) diff --git a/frontend/app_flowy/lib/plugins/document/document_page.dart b/frontend/app_flowy/lib/plugins/document/document_page.dart index 0f53268e35..e89896df54 100644 --- a/frontend/app_flowy/lib/plugins/document/document_page.dart +++ b/frontend/app_flowy/lib/plugins/document/document_page.dart @@ -44,12 +44,9 @@ class _DocumentPageState extends State { } @override - Future dispose() async { - // https://github.com/flutter/flutter/issues/64935#issuecomment-686852369 + void dispose() { + documentBloc.close(); super.dispose(); - - await _clearTemporaryNodes(); - await documentBloc.close(); } @override @@ -69,6 +66,8 @@ class _DocumentPageState extends State { if (state.forceClose) { widget.onDeleted(); return const SizedBox(); + } else if (documentBloc.editorState == null) { + return const SizedBox(); } else { return _renderDocument(context, state); } @@ -85,10 +84,7 @@ class _DocumentPageState extends State { children: [ if (state.isDeleted) _renderBanner(context), // AppFlowy Editor - _renderAppFlowyEditor( - context, - context.read().editorState, - ), + const _AppFlowyEditorPage(), ], ); } @@ -102,12 +98,31 @@ class _DocumentPageState extends State { .add(const DocumentEvent.deletePermanently()), ); } +} - Widget _renderAppFlowyEditor(BuildContext context, EditorState editorState) { - // enable open ai features if needed. - final userProfilePB = context.read().state.userProfilePB; - final openAIKey = userProfilePB?.openaiKey; +class _AppFlowyEditorPage extends StatefulWidget { + const _AppFlowyEditorPage({ + Key? key, + }) : super(key: key); + @override + State<_AppFlowyEditorPage> createState() => _AppFlowyEditorPageState(); +} + +class _AppFlowyEditorPageState extends State<_AppFlowyEditorPage> { + late DocumentBloc documentBloc; + late EditorState editorState; + String? get openAIKey => documentBloc.state.userProfilePB?.openaiKey; + + @override + void initState() { + super.initState(); + documentBloc = context.read(); + editorState = documentBloc.editorState ?? EditorState.empty(); + } + + @override + Widget build(BuildContext context) { final theme = Theme.of(context); final editor = AppFlowyEditor( editorState: editorState, @@ -152,7 +167,8 @@ class _DocumentPageState extends State { // Callout calloutMenuItem, // AI - if (openAIKey != null && openAIKey.isNotEmpty) ...[ + // enable open ai features if needed. + if (openAIKey != null && openAIKey!.isNotEmpty) ...[ autoGeneratorMenuItem, ] ], @@ -174,8 +190,13 @@ class _DocumentPageState extends State { ); } + @override + void dispose() { + _clearTemporaryNodes(); + super.dispose(); + } + Future _clearTemporaryNodes() async { - final editorState = documentBloc.editorState; final document = editorState.document; if (document.root.children.isEmpty) { return;