145 lines
4.1 KiB
Dart
Raw Normal View History

2022-11-15 10:46:26 +08:00
import 'package:app_flowy/plugins/document/editor_styles.dart';
2021-07-24 18:55:13 +08:00
import 'package:app_flowy/startup/startup.dart';
2022-11-15 10:46:26 +08:00
import 'package:app_flowy/plugins/document/presentation/banner.dart';
2022-10-22 21:57:44 +08:00
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';
2021-10-19 13:56:11 +08:00
import 'package:flowy_infra_ui/widget/error_page.dart';
2022-07-04 15:00:54 +08:00
import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
2021-07-24 14:05:49 +08:00
import 'package:flutter/material.dart';
2021-07-24 18:55:13 +08:00
import 'package:flutter_bloc/flutter_bloc.dart';
2022-10-22 21:57:44 +08:00
import 'package:intl/intl.dart';
import 'application/doc_bloc.dart';
2021-10-22 23:49:56 +08:00
2022-02-23 22:17:47 +08:00
class DocumentPage extends StatefulWidget {
final VoidCallback onDeleted;
2022-07-19 14:11:29 +08:00
final ViewPB view;
2021-07-24 14:05:49 +08:00
DocumentPage({
required this.view,
required this.onDeleted,
Key? key,
}) : super(key: ValueKey(view.id));
2021-07-24 14:05:49 +08:00
@override
2022-02-23 22:17:47 +08:00
State<DocumentPage> createState() => _DocumentPageState();
}
2022-02-23 22:17:47 +08:00
class _DocumentPageState extends State<DocumentPage> {
late DocumentBloc documentBloc;
final FocusNode _focusNode = FocusNode();
2021-10-19 13:56:11 +08:00
@override
void 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';
documentBloc = getIt<DocumentBloc>(param1: super.widget.view)
..add(const DocumentEvent.initial());
2021-10-19 13:56:11 +08:00
super.initState();
}
2021-07-24 18:55:13 +08:00
@override
Widget build(BuildContext context) {
2021-10-19 13:56:11 +08:00
return MultiBlocProvider(
providers: [
2022-02-23 22:17:47 +08:00
BlocProvider<DocumentBloc>.value(value: documentBloc),
2021-10-19 13:56:11 +08:00
],
child:
BlocBuilder<DocumentBloc, DocumentState>(builder: (context, state) {
2022-02-23 22:17:47 +08:00
return state.loadingState.map(
2022-10-22 21:57:44 +08:00
loading: (_) => SizedBox.expand(
child: Container(color: Colors.transparent),
),
2021-10-19 13:56:11 +08:00
finish: (result) => result.successOrFail.fold(
(_) {
if (state.forceClose) {
widget.onDeleted();
return const SizedBox();
} else {
2022-02-23 22:17:47 +08:00
return _renderDocument(context, state);
}
},
2021-10-19 13:56:11 +08:00
(err) => FlowyErrorPage(err.toString()),
),
);
}),
2021-07-24 14:05:49 +08:00
);
}
@override
Future<void> dispose() async {
2022-02-23 22:17:47 +08:00
documentBloc.close();
2022-03-29 22:58:38 +08:00
_focusNode.dispose();
super.dispose();
2021-10-19 13:56:11 +08:00
}
2022-02-23 22:17:47 +08:00
Widget _renderDocument(BuildContext context, DocumentState state) {
return Column(
children: [
if (state.isDeleted) _renderBanner(context),
// AppFlowy Editor
_renderAppFlowyEditor(
context.read<DocumentBloc>().editorState,
),
],
);
}
Widget _renderBanner(BuildContext context) {
2022-02-23 22:17:47 +08:00
return DocumentBanner(
onRestore: () =>
context.read<DocumentBloc>().add(const DocumentEvent.restorePage()),
onDelete: () => context
.read<DocumentBloc>()
.add(const DocumentEvent.deletePermanently()),
);
}
2022-10-22 21:57:44 +08:00
Widget _renderAppFlowyEditor(EditorState editorState) {
final theme = Theme.of(context);
final editorMaxWidth = MediaQuery.of(context).size.width * 0.6;
2022-10-22 21:57:44 +08:00
final editor = AppFlowyEditor(
editorState: editorState,
2022-10-30 10:33:43 +08:00
autoFocus: editorState.document.isEmpty,
2022-10-22 21:57:44 +08:00
customBuilders: {
// Divider
kDividerType: DividerWidgetBuilder(),
// Math Equation
kMathEquationType: MathEquationNodeWidgetBuidler(),
// Code Block
kCodeBlockType: CodeBlockNodeWidgetBuilder(),
2022-10-22 21:57:44 +08:00
},
shortcutEvents: [
// Divider
insertDividerEvent,
// Code Block
enterInCodeBlock,
ignoreKeysInCodeBlock,
pasteInCodeBlock,
],
selectionMenuItems: [
// Divider
dividerMenuItem,
// Math Equation
mathEquationMenuItem,
// Code Block
codeBlockMenuItem,
2022-10-22 21:57:44 +08:00
],
themeData: theme.copyWith(extensions: [
...theme.extensions.values,
customEditorTheme(context),
...customPluginTheme(context),
]),
2021-07-24 14:05:49 +08:00
);
2021-07-26 16:35:51 +08:00
return Expanded(
child: Center(
child: Container(
constraints: BoxConstraints(
maxWidth: editorMaxWidth,
),
child: editor,
),
),
2021-07-24 14:05:49 +08:00
);
}
}