mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: abstract appflowy editor build function to widget (#1878)
* chore: abstract appflowy editor build function to widget * feat: refactor the editor state to optional
This commit is contained in:
parent
f4dea31560
commit
01a388c1c4
@ -24,7 +24,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
|||||||
|
|
||||||
final ViewListener _listener;
|
final ViewListener _listener;
|
||||||
final TrashService _trashService;
|
final TrashService _trashService;
|
||||||
late EditorState editorState;
|
EditorState? editorState;
|
||||||
StreamSubscription? _subscription;
|
StreamSubscription? _subscription;
|
||||||
|
|
||||||
DocumentBloc({
|
DocumentBloc({
|
||||||
@ -127,7 +127,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _listenOnDocumentChange() {
|
void _listenOnDocumentChange() {
|
||||||
_subscription = editorState.transactionStream.listen((transaction) {
|
_subscription = editorState?.transactionStream.listen((transaction) {
|
||||||
final json = jsonEncode(TransactionAdaptor(transaction).toJson());
|
final json = jsonEncode(TransactionAdaptor(transaction).toJson());
|
||||||
_documentService
|
_documentService
|
||||||
.applyEdit(docId: view.id, operations: json)
|
.applyEdit(docId: view.id, operations: json)
|
||||||
|
@ -44,12 +44,9 @@ class _DocumentPageState extends State<DocumentPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> dispose() async {
|
void dispose() {
|
||||||
// https://github.com/flutter/flutter/issues/64935#issuecomment-686852369
|
documentBloc.close();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
|
|
||||||
await _clearTemporaryNodes();
|
|
||||||
await documentBloc.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -69,6 +66,8 @@ class _DocumentPageState extends State<DocumentPage> {
|
|||||||
if (state.forceClose) {
|
if (state.forceClose) {
|
||||||
widget.onDeleted();
|
widget.onDeleted();
|
||||||
return const SizedBox();
|
return const SizedBox();
|
||||||
|
} else if (documentBloc.editorState == null) {
|
||||||
|
return const SizedBox();
|
||||||
} else {
|
} else {
|
||||||
return _renderDocument(context, state);
|
return _renderDocument(context, state);
|
||||||
}
|
}
|
||||||
@ -85,10 +84,7 @@ class _DocumentPageState extends State<DocumentPage> {
|
|||||||
children: [
|
children: [
|
||||||
if (state.isDeleted) _renderBanner(context),
|
if (state.isDeleted) _renderBanner(context),
|
||||||
// AppFlowy Editor
|
// AppFlowy Editor
|
||||||
_renderAppFlowyEditor(
|
const _AppFlowyEditorPage(),
|
||||||
context,
|
|
||||||
context.read<DocumentBloc>().editorState,
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -102,12 +98,31 @@ class _DocumentPageState extends State<DocumentPage> {
|
|||||||
.add(const DocumentEvent.deletePermanently()),
|
.add(const DocumentEvent.deletePermanently()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Widget _renderAppFlowyEditor(BuildContext context, EditorState editorState) {
|
class _AppFlowyEditorPage extends StatefulWidget {
|
||||||
// enable open ai features if needed.
|
const _AppFlowyEditorPage({
|
||||||
final userProfilePB = context.read<DocumentBloc>().state.userProfilePB;
|
Key? key,
|
||||||
final openAIKey = userProfilePB?.openaiKey;
|
}) : 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<DocumentBloc>();
|
||||||
|
editorState = documentBloc.editorState ?? EditorState.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
final editor = AppFlowyEditor(
|
final editor = AppFlowyEditor(
|
||||||
editorState: editorState,
|
editorState: editorState,
|
||||||
@ -152,7 +167,8 @@ class _DocumentPageState extends State<DocumentPage> {
|
|||||||
// Callout
|
// Callout
|
||||||
calloutMenuItem,
|
calloutMenuItem,
|
||||||
// AI
|
// AI
|
||||||
if (openAIKey != null && openAIKey.isNotEmpty) ...[
|
// enable open ai features if needed.
|
||||||
|
if (openAIKey != null && openAIKey!.isNotEmpty) ...[
|
||||||
autoGeneratorMenuItem,
|
autoGeneratorMenuItem,
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
@ -174,8 +190,13 @@ class _DocumentPageState extends State<DocumentPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_clearTemporaryNodes();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _clearTemporaryNodes() async {
|
Future<void> _clearTemporaryNodes() async {
|
||||||
final editorState = documentBloc.editorState;
|
|
||||||
final document = editorState.document;
|
final document = editorState.document;
|
||||||
if (document.root.children.isEmpty) {
|
if (document.root.children.isEmpty) {
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user