2024-04-30 16:55:15 +08:00
|
|
|
import 'package:appflowy/mobile/application/page_style/document_page_style_bloc.dart';
|
2024-09-18 10:43:23 +08:00
|
|
|
import 'package:appflowy/plugins/document/application/document_appearance_cubit.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-10-09 15:23:42 +02:00
|
|
|
import 'package:appflowy/plugins/document/presentation/editor_drop_handler.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';
|
2024-09-25 22:44:59 +08:00
|
|
|
import 'package:appflowy/plugins/document/presentation/editor_plugins/shared_context/shared_context.dart';
|
2024-10-21 10:34:30 +02:00
|
|
|
import 'package:appflowy/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_service.dart';
|
2023-06-15 16:33:44 +08:00
|
|
|
import 'package:appflowy/plugins/document/presentation/editor_style.dart';
|
2024-08-28 21:25:57 +08:00
|
|
|
import 'package:appflowy/shared/flowy_error_page.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';
|
2024-09-12 14:40:19 +08:00
|
|
|
import 'package:appflowy_editor/appflowy_editor.dart';
|
2024-10-17 13:29:34 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2023-05-16 14:58:24 +08:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-07-31 15:49:35 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2024-09-12 14:40:19 +08:00
|
|
|
import 'package:universal_platform/universal_platform.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,
|
2024-10-17 13:29:34 +08:00
|
|
|
this.initialBlockId,
|
2024-08-19 11:06:34 +08:00
|
|
|
this.fixedTitle,
|
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;
|
2024-10-17 13:29:34 +08:00
|
|
|
final String? initialBlockId;
|
2024-08-19 11:06:34 +08:00
|
|
|
final String? fixedTitle;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
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) {
|
2024-10-09 15:23:42 +02:00
|
|
|
return MultiBlocProvider(
|
|
|
|
providers: [
|
|
|
|
BlocProvider.value(value: getIt<ActionNavigationBloc>()),
|
|
|
|
BlocProvider.value(value: documentBloc),
|
|
|
|
],
|
|
|
|
child: BlocBuilder<DocumentBloc, DocumentState>(
|
|
|
|
buildWhen: shouldRebuildDocument,
|
|
|
|
builder: (context, state) {
|
|
|
|
if (state.isLoading) {
|
|
|
|
return const Center(child: CircularProgressIndicator.adaptive());
|
|
|
|
}
|
|
|
|
|
|
|
|
final editorState = state.editorState;
|
|
|
|
this.editorState = editorState;
|
|
|
|
final error = state.error;
|
|
|
|
if (error != null || editorState == null) {
|
|
|
|
Log.error(error);
|
|
|
|
return Center(child: AppFlowyErrorPage(error: error));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.forceClose) {
|
|
|
|
widget.onDeleted();
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
}
|
|
|
|
|
|
|
|
return BlocListener<ActionNavigationBloc, ActionNavigationState>(
|
|
|
|
listenWhen: (_, curr) => curr.action != null,
|
|
|
|
listener: onNotificationAction,
|
|
|
|
child: buildEditorPage(context, state),
|
|
|
|
);
|
|
|
|
},
|
2023-04-10 15:10:42 +08:00
|
|
|
),
|
2021-07-24 14:05:49 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-03 04:58:08 +02:00
|
|
|
Widget buildEditorPage(
|
2024-07-31 15:49:35 +02:00
|
|
|
BuildContext context,
|
|
|
|
DocumentState state,
|
|
|
|
) {
|
2024-10-17 13:29:34 +08:00
|
|
|
final editorState = state.editorState;
|
|
|
|
if (editorState == null) {
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
}
|
|
|
|
|
2024-09-18 10:43:23 +08:00
|
|
|
final width = context.read<DocumentAppearanceCubit>().state.width;
|
|
|
|
|
2024-04-30 16:55:15 +08:00
|
|
|
final Widget child;
|
2024-09-12 14:40:19 +08:00
|
|
|
if (UniversalPlatform.isMobile) {
|
2024-04-30 16:55:15 +08:00
|
|
|
child = BlocBuilder<DocumentPageStyleBloc, DocumentPageStyleState>(
|
2024-10-03 04:58:08 +02:00
|
|
|
builder: (context, styleState) => AppFlowyEditorPage(
|
2024-10-17 13:29:34 +08:00
|
|
|
editorState: editorState,
|
2024-10-08 14:29:07 +08:00
|
|
|
// if the view's name is empty, focus on the title
|
|
|
|
autoFocus: widget.view.name.isEmpty ? false : null,
|
2024-10-03 04:58:08 +02:00
|
|
|
styleCustomizer: EditorStyleCustomizer(
|
|
|
|
context: context,
|
|
|
|
width: width,
|
|
|
|
padding: EditorStyleCustomizer.documentPadding,
|
|
|
|
),
|
|
|
|
header: buildCoverAndIcon(context, state),
|
|
|
|
initialSelection: widget.initialSelection,
|
|
|
|
),
|
2024-04-30 16:55:15 +08:00
|
|
|
);
|
|
|
|
} else {
|
2024-10-09 15:23:42 +02:00
|
|
|
child = EditorDropHandler(
|
|
|
|
viewId: widget.view.id,
|
2024-10-17 13:29:34 +08:00
|
|
|
editorState: editorState,
|
2024-10-09 15:23:42 +02:00
|
|
|
isLocalMode: context.read<DocumentBloc>().isLocalMode,
|
2024-07-26 00:08:55 +02:00
|
|
|
child: AppFlowyEditorPage(
|
2024-10-17 13:29:34 +08:00
|
|
|
editorState: editorState,
|
2024-09-25 22:44:59 +08:00
|
|
|
// if the view's name is empty, focus on the title
|
|
|
|
autoFocus: widget.view.name.isEmpty ? false : null,
|
2024-07-26 00:08:55 +02:00
|
|
|
styleCustomizer: EditorStyleCustomizer(
|
|
|
|
context: context,
|
2024-09-18 10:43:23 +08:00
|
|
|
width: width,
|
2024-07-26 00:08:55 +02:00
|
|
|
padding: EditorStyleCustomizer.documentPadding,
|
|
|
|
),
|
2024-10-03 04:58:08 +02:00
|
|
|
header: buildCoverAndIcon(context, state),
|
2024-10-17 13:29:34 +08:00
|
|
|
initialSelection: _calculateInitialSelection(editorState),
|
2024-04-30 16:55:15 +08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-11-07 15:24:32 +08:00
|
|
|
|
2024-09-25 22:44:59 +08:00
|
|
|
return Provider(
|
|
|
|
create: (_) => SharedEditorContext(),
|
2024-10-21 10:34:30 +02:00
|
|
|
child: EditorTransactionService(
|
|
|
|
viewId: widget.view.id,
|
|
|
|
editorState: state.editorState!,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
if (state.isDeleted) buildBanner(context),
|
|
|
|
Expanded(child: child),
|
|
|
|
],
|
|
|
|
),
|
2024-09-25 22:44:59 +08:00
|
|
|
),
|
2021-10-31 19:48:20 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-03 04:58:08 +02:00
|
|
|
Widget buildBanner(BuildContext context) {
|
2022-02-23 22:17:47 +08:00
|
|
|
return DocumentBanner(
|
2024-10-10 11:57:42 +02:00
|
|
|
viewName: widget.view.name,
|
2024-10-03 04:58:08 +02: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-10-03 04:58:08 +02:00
|
|
|
Widget buildCoverAndIcon(BuildContext context, DocumentState state) {
|
2024-04-30 16:55:15 +08:00
|
|
|
final editorState = state.editorState;
|
|
|
|
final userProfilePB = state.userProfilePB;
|
|
|
|
if (editorState == null || userProfilePB == null) {
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:40:19 +08:00
|
|
|
if (UniversalPlatform.isMobile) {
|
2024-04-30 16:55:15 +08:00
|
|
|
return DocumentImmersiveCover(
|
2024-08-19 11:06:34 +08:00
|
|
|
fixedTitle: widget.fixedTitle,
|
2024-04-30 16:55:15 +08:00
|
|
|
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-10-03 04:58:08 +02: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
|
|
|
) {
|
2024-10-17 13:29:34 +08:00
|
|
|
final action = state.action;
|
|
|
|
if (action == null ||
|
|
|
|
action.type != ActionType.jumpToBlock ||
|
|
|
|
action.objectId != widget.view.id) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-26 03:41:03 +02:00
|
|
|
|
2024-10-17 13:29:34 +08:00
|
|
|
final editorState = context.read<DocumentBloc>().state.editorState;
|
|
|
|
if (editorState == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Path? path = _getPathFromAction(action, editorState);
|
|
|
|
if (path != null) {
|
|
|
|
debugPrint('jump to block: $path');
|
|
|
|
editorState.updateSelectionWithReason(
|
|
|
|
Selection.collapsed(Position(path: path)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Path? _getPathFromAction(NavigationAction action, EditorState editorState) {
|
|
|
|
Path? path = action.arguments?[ActionArgumentKeys.nodePath];
|
|
|
|
if (path == null || path.isEmpty) {
|
|
|
|
final blockId = action.arguments?[ActionArgumentKeys.blockId];
|
|
|
|
if (blockId != null) {
|
|
|
|
path = _findNodePathByBlockId(editorState, blockId);
|
2023-10-26 03:41:03 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-17 13:29:34 +08:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
Path? _findNodePathByBlockId(EditorState editorState, String blockId) {
|
|
|
|
final document = editorState.document;
|
|
|
|
final startNode = document.root.children.firstOrNull;
|
|
|
|
if (startNode == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final nodeIterator = NodeIterator(document: document, startNode: startNode);
|
|
|
|
while (nodeIterator.moveNext()) {
|
|
|
|
final node = nodeIterator.current;
|
|
|
|
if (node.id == blockId) {
|
|
|
|
return node.path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2023-10-26 03:41:03 +02:00
|
|
|
}
|
2024-08-19 11:06:34 +08:00
|
|
|
|
2024-10-03 04:58:08 +02:00
|
|
|
bool shouldRebuildDocument(DocumentState previous, DocumentState current) {
|
2024-08-19 11:06:34 +08:00
|
|
|
// only rebuild the document page when the below fields are changed
|
|
|
|
// this is to prevent unnecessary rebuilds
|
|
|
|
//
|
|
|
|
// If you confirm the newly added fields should be rebuilt, please update
|
|
|
|
// this function.
|
|
|
|
if (previous.editorState != current.editorState) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previous.forceClose != current.forceClose ||
|
|
|
|
previous.isDeleted != current.isDeleted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previous.userProfilePB != current.userProfilePB) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previous.isLoading != current.isLoading ||
|
|
|
|
previous.error != current.error) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-10-03 04:58:08 +02:00
|
|
|
|
2024-10-17 13:29:34 +08:00
|
|
|
Selection? _calculateInitialSelection(EditorState editorState) {
|
|
|
|
if (widget.initialSelection != null) {
|
|
|
|
return widget.initialSelection;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widget.initialBlockId != null) {
|
|
|
|
final path = _findNodePathByBlockId(editorState, widget.initialBlockId!);
|
|
|
|
if (path != null) {
|
|
|
|
editorState.selectionType = SelectionType.block;
|
|
|
|
return Selection.collapsed(
|
|
|
|
Position(
|
|
|
|
path: path,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2021-07-24 14:05:49 +08:00
|
|
|
}
|