2023-08-17 14:50:48 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:appflowy/plugins/document/application/doc_service.dart';
|
2023-05-16 14:58:24 +08:00
|
|
|
import 'package:appflowy/plugins/document/application/document_data_pb_extension.dart';
|
|
|
|
import 'package:appflowy/plugins/document/application/editor_transaction_adapter.dart';
|
2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/plugins/trash/application/trash_service.dart';
|
2024-01-20 23:16:18 +08:00
|
|
|
import 'package:appflowy/startup/startup.dart';
|
|
|
|
import 'package:appflowy/user/application/auth/auth_service.dart';
|
2023-04-13 18:53:51 +08:00
|
|
|
import 'package:appflowy/workspace/application/doc/doc_listener.dart';
|
2023-12-21 08:12:40 +08:00
|
|
|
import 'package:appflowy/workspace/application/doc/sync_state_listener.dart';
|
2023-08-17 14:50:48 +08:00
|
|
|
import 'package:appflowy/workspace/application/view/view_listener.dart';
|
2023-12-31 07:29:40 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-document/protobuf.dart';
|
2023-01-08 12:10:53 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
2023-12-31 07:29:40 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
|
2023-08-17 14:50:48 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pbserver.dart';
|
|
|
|
import 'package:appflowy_editor/appflowy_editor.dart'
|
|
|
|
show
|
|
|
|
EditorState,
|
|
|
|
LogLevel,
|
|
|
|
TransactionTime,
|
|
|
|
Selection,
|
|
|
|
Position,
|
|
|
|
paragraphNode;
|
2024-02-24 20:54:10 +07:00
|
|
|
import 'package:appflowy_result/appflowy_result.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
2021-09-11 21:30:58 +08:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2021-07-24 18:55:13 +08:00
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2023-08-17 14:50:48 +08:00
|
|
|
|
2021-07-24 18:55:13 +08:00
|
|
|
part 'doc_bloc.freezed.dart';
|
|
|
|
|
2022-02-23 22:17:47 +08:00
|
|
|
class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
|
|
|
DocumentBloc({
|
2021-10-31 19:48:20 +08:00
|
|
|
required this.view,
|
2023-05-16 14:58:24 +08:00
|
|
|
}) : _documentListener = DocumentListener(id: view.id),
|
2023-12-21 08:12:40 +08:00
|
|
|
_syncStateListener = DocumentSyncStateListener(id: view.id),
|
2023-06-14 22:16:33 +08:00
|
|
|
_viewListener = ViewListener(viewId: view.id),
|
2022-10-26 10:38:57 +08:00
|
|
|
super(DocumentState.initial()) {
|
2023-05-16 14:58:24 +08:00
|
|
|
on<DocumentEvent>(_onDocumentEvent);
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
2021-09-11 21:30:58 +08:00
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
final ViewPB view;
|
2021-11-03 22:04:45 +08:00
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
final DocumentListener _documentListener;
|
2023-12-21 08:12:40 +08:00
|
|
|
final DocumentSyncStateListener _syncStateListener;
|
2023-05-16 14:58:24 +08:00
|
|
|
final ViewListener _viewListener;
|
2021-11-03 23:03:42 +08:00
|
|
|
|
2023-11-09 00:30:50 +01:00
|
|
|
final DocumentService _documentService = DocumentService();
|
|
|
|
final TrashService _trashService = TrashService();
|
2023-05-16 14:58:24 +08:00
|
|
|
|
2023-11-09 00:30:50 +01:00
|
|
|
late final TransactionAdapter _transactionAdapter = TransactionAdapter(
|
|
|
|
documentId: view.id,
|
|
|
|
documentService: _documentService,
|
|
|
|
);
|
2023-05-16 14:58:24 +08:00
|
|
|
|
|
|
|
StreamSubscription? _subscription;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> close() async {
|
2023-12-10 20:26:23 +07:00
|
|
|
await _documentListener.stop();
|
2023-12-21 08:12:40 +08:00
|
|
|
await _syncStateListener.stop();
|
2023-05-16 14:58:24 +08:00
|
|
|
await _viewListener.stop();
|
|
|
|
await _subscription?.cancel();
|
|
|
|
await _documentService.closeDocument(view: view);
|
2023-12-28 12:37:10 +08:00
|
|
|
state.editorState?.service.keyboardService?.closeKeyboard();
|
2023-12-10 20:26:23 +07:00
|
|
|
state.editorState?.dispose();
|
2021-10-19 13:56:11 +08:00
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
Future<void> _onDocumentEvent(
|
|
|
|
DocumentEvent event,
|
|
|
|
Emitter<DocumentState> emit,
|
|
|
|
) async {
|
2023-12-21 08:12:40 +08:00
|
|
|
await event.when(
|
|
|
|
initial: () async {
|
2023-12-10 20:26:23 +07:00
|
|
|
final editorState = await _fetchDocumentState();
|
|
|
|
_onViewChanged();
|
|
|
|
_onDocumentChanged();
|
2024-01-20 23:16:18 +08:00
|
|
|
await editorState.fold(
|
2024-02-24 20:54:10 +07:00
|
|
|
(s) async {
|
2024-01-20 23:16:18 +08:00
|
|
|
final result = await getIt<AuthService>().getUser();
|
2024-02-24 20:54:10 +07:00
|
|
|
final userProfilePB = result.fold(
|
|
|
|
(s) => s,
|
|
|
|
(e) => null,
|
|
|
|
);
|
2024-01-20 23:16:18 +08:00
|
|
|
emit(
|
|
|
|
state.copyWith(
|
|
|
|
error: null,
|
2024-02-24 20:54:10 +07:00
|
|
|
editorState: s,
|
2024-01-20 23:16:18 +08:00
|
|
|
isLoading: false,
|
|
|
|
userProfilePB: userProfilePB,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2024-02-24 20:54:10 +07:00
|
|
|
(f) async => emit(
|
|
|
|
state.copyWith(
|
|
|
|
error: f,
|
|
|
|
editorState: null,
|
|
|
|
isLoading: false,
|
|
|
|
),
|
|
|
|
),
|
2023-12-10 20:26:23 +07:00
|
|
|
);
|
2022-10-22 21:57:44 +08:00
|
|
|
},
|
2023-12-21 08:12:40 +08:00
|
|
|
moveToTrash: () async {
|
2023-05-16 14:58:24 +08:00
|
|
|
emit(state.copyWith(isDeleted: true));
|
|
|
|
},
|
2023-12-21 08:12:40 +08:00
|
|
|
restore: () async {
|
2023-05-16 14:58:24 +08:00
|
|
|
emit(state.copyWith(isDeleted: false));
|
|
|
|
},
|
2023-12-21 08:12:40 +08:00
|
|
|
deletePermanently: () async {
|
2023-05-16 14:58:24 +08:00
|
|
|
final result = await _trashService.deleteViews([view.id]);
|
2023-06-05 13:10:14 +08:00
|
|
|
final forceClose = result.fold((l) => true, (r) => false);
|
|
|
|
emit(state.copyWith(forceClose: forceClose));
|
2023-05-16 14:58:24 +08:00
|
|
|
},
|
2023-12-21 08:12:40 +08:00
|
|
|
restorePage: () async {
|
2023-05-16 14:58:24 +08:00
|
|
|
final result = await _trashService.putback(view.id);
|
2023-06-05 13:10:14 +08:00
|
|
|
final isDeleted = result.fold((l) => false, (r) => true);
|
|
|
|
emit(state.copyWith(isDeleted: isDeleted));
|
2022-10-22 21:57:44 +08:00
|
|
|
},
|
2023-12-21 08:12:40 +08:00
|
|
|
syncStateChanged: (isSyncing) {
|
|
|
|
emit(state.copyWith(isSyncing: isSyncing));
|
|
|
|
},
|
2022-10-22 21:57:44 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
/// subscribe to the view(document page) change
|
|
|
|
void _onViewChanged() {
|
|
|
|
_viewListener.start(
|
2023-06-05 13:10:14 +08:00
|
|
|
onViewMoveToTrash: (r) {
|
2024-02-24 20:54:10 +07:00
|
|
|
r.map((r) => add(const DocumentEvent.moveToTrash()));
|
2023-06-05 13:10:14 +08:00
|
|
|
},
|
|
|
|
onViewDeleted: (r) {
|
2024-02-24 20:54:10 +07:00
|
|
|
r.map((r) => add(const DocumentEvent.moveToTrash()));
|
2023-06-05 13:10:14 +08:00
|
|
|
},
|
2024-02-24 20:54:10 +07:00
|
|
|
onViewRestored: (r) => r.map((r) => add(const DocumentEvent.restore())),
|
2023-05-16 14:58:24 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// subscribe to the document content change
|
|
|
|
void _onDocumentChanged() {
|
|
|
|
_documentListener.start(
|
2023-09-12 20:49:03 +08:00
|
|
|
didReceiveUpdate: syncDocumentDataPB,
|
2022-05-05 21:15:01 +08:00
|
|
|
);
|
2023-12-21 08:12:40 +08:00
|
|
|
|
|
|
|
_syncStateListener.start(
|
|
|
|
didReceiveSyncState: (syncState) {
|
|
|
|
if (!isClosed) {
|
|
|
|
add(DocumentEvent.syncStateChanged(syncState.isSyncing));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-10-20 22:19:01 +08:00
|
|
|
}
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
/// Fetch document
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<EditorState?, FlowyError>> _fetchDocumentState() async {
|
2023-12-05 10:04:23 -08:00
|
|
|
final result = await _documentService.openDocument(viewId: view.id);
|
2023-12-10 20:26:23 +07:00
|
|
|
return result.fold(
|
2024-02-24 20:54:10 +07:00
|
|
|
(s) async => FlowyResult.success(await _initAppFlowyEditorState(s)),
|
|
|
|
(e) => FlowyResult.failure(e),
|
2023-04-13 18:53:51 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:26:23 +07:00
|
|
|
Future<EditorState?> _initAppFlowyEditorState(DocumentDataPB data) async {
|
2023-05-16 14:58:24 +08:00
|
|
|
final document = data.toDocument();
|
|
|
|
if (document == null) {
|
|
|
|
assert(false, 'document is null');
|
2023-12-10 20:26:23 +07:00
|
|
|
return null;
|
2023-05-16 14:58:24 +08:00
|
|
|
}
|
|
|
|
|
2023-05-23 19:55:43 +08:00
|
|
|
final editorState = EditorState(document: document);
|
2023-03-07 09:33:59 +08:00
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
// subscribe to the document change from the editor
|
2023-06-21 19:53:29 +08:00
|
|
|
_subscription = editorState.transactionStream.listen((event) async {
|
|
|
|
final time = event.$1;
|
|
|
|
if (time != TransactionTime.before) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await _transactionAdapter.apply(event.$2, editorState);
|
2023-07-02 11:46:45 +08:00
|
|
|
|
|
|
|
// check if the document is empty.
|
2024-01-29 10:26:45 +08:00
|
|
|
await applyRules();
|
2023-11-09 00:30:50 +01:00
|
|
|
|
|
|
|
if (!isClosed) {
|
|
|
|
// ignore: invalid_use_of_visible_for_testing_member
|
|
|
|
emit(state.copyWith(isDocumentEmpty: editorState.document.isEmpty));
|
|
|
|
}
|
2022-10-22 21:57:44 +08:00
|
|
|
});
|
2023-05-16 14:58:24 +08:00
|
|
|
|
|
|
|
// output the log from the editor when debug mode
|
2023-03-07 09:33:59 +08:00
|
|
|
if (kDebugMode) {
|
2023-05-16 14:58:24 +08:00
|
|
|
editorState.logConfiguration
|
|
|
|
..level = LogLevel.all
|
|
|
|
..handler = (log) {
|
2023-05-23 23:55:21 +08:00
|
|
|
// Log.debug(log);
|
2023-05-16 14:58:24 +08:00
|
|
|
};
|
2023-03-07 09:33:59 +08:00
|
|
|
}
|
2023-12-10 20:26:23 +07:00
|
|
|
|
|
|
|
return editorState;
|
2021-10-20 22:19:01 +08:00
|
|
|
}
|
2023-07-02 11:46:45 +08:00
|
|
|
|
|
|
|
Future<void> applyRules() async {
|
2024-01-29 10:26:45 +08:00
|
|
|
await Future.wait([
|
|
|
|
ensureAtLeastOneParagraphExists(),
|
|
|
|
ensureLastNodeIsEditable(),
|
|
|
|
]);
|
2023-07-02 11:46:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> ensureLastNodeIsEditable() async {
|
2023-12-10 20:26:23 +07:00
|
|
|
final editorState = state.editorState;
|
2023-07-02 11:46:45 +08:00
|
|
|
if (editorState == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final document = editorState.document;
|
|
|
|
final lastNode = document.root.children.lastOrNull;
|
|
|
|
if (lastNode == null || lastNode.delta == null) {
|
|
|
|
final transaction = editorState.transaction;
|
|
|
|
transaction.insertNode([document.root.children.length], paragraphNode());
|
2023-09-01 10:15:21 +03:30
|
|
|
transaction.afterSelection = transaction.beforeSelection;
|
2023-07-02 11:46:45 +08:00
|
|
|
await editorState.apply(transaction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> ensureAtLeastOneParagraphExists() async {
|
2023-12-10 20:26:23 +07:00
|
|
|
final editorState = state.editorState;
|
2023-07-02 11:46:45 +08:00
|
|
|
if (editorState == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final document = editorState.document;
|
|
|
|
if (document.root.children.isEmpty) {
|
|
|
|
final transaction = editorState.transaction;
|
|
|
|
transaction.insertNode([0], paragraphNode());
|
2023-08-17 14:50:48 +08:00
|
|
|
transaction.afterSelection = Selection.collapsed(
|
2024-01-25 16:37:36 +01:00
|
|
|
Position(path: [0]),
|
2023-08-17 14:50:48 +08:00
|
|
|
);
|
2023-07-02 11:46:45 +08:00
|
|
|
await editorState.apply(transaction);
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 20:49:03 +08:00
|
|
|
|
|
|
|
void syncDocumentDataPB(DocEventPB docEvent) {
|
|
|
|
// prettyPrintJson(docEvent.toProto3Json());
|
|
|
|
// todo: integrate the document change to the editor
|
|
|
|
// for (final event in docEvent.events) {
|
|
|
|
// for (final blockEvent in event.event) {
|
|
|
|
// switch (blockEvent.command) {
|
|
|
|
// case DeltaTypePB.Inserted:
|
|
|
|
// break;
|
|
|
|
// case DeltaTypePB.Updated:
|
|
|
|
// break;
|
|
|
|
// case DeltaTypePB.Removed:
|
|
|
|
// break;
|
|
|
|
// default:
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2022-02-23 22:17:47 +08:00
|
|
|
class DocumentEvent with _$DocumentEvent {
|
|
|
|
const factory DocumentEvent.initial() = Initial;
|
2023-06-05 13:10:14 +08:00
|
|
|
const factory DocumentEvent.moveToTrash() = MoveToTrash;
|
2022-02-23 22:17:47 +08:00
|
|
|
const factory DocumentEvent.restore() = Restore;
|
|
|
|
const factory DocumentEvent.restorePage() = RestorePage;
|
|
|
|
const factory DocumentEvent.deletePermanently() = DeletePermanently;
|
2023-12-21 08:12:40 +08:00
|
|
|
const factory DocumentEvent.syncStateChanged(bool isSyncing) =
|
|
|
|
syncStateChanged;
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2022-02-23 22:17:47 +08:00
|
|
|
class DocumentState with _$DocumentState {
|
|
|
|
const factory DocumentState({
|
2021-10-31 17:24:55 +08:00
|
|
|
required bool isDeleted,
|
2021-10-31 20:27:37 +08:00
|
|
|
required bool forceClose,
|
2023-12-10 20:26:23 +07:00
|
|
|
required bool isLoading,
|
2023-12-21 08:12:40 +08:00
|
|
|
required bool isSyncing,
|
2023-11-09 00:30:50 +01:00
|
|
|
bool? isDocumentEmpty,
|
2023-02-16 10:17:08 +08:00
|
|
|
UserProfilePB? userProfilePB,
|
2023-12-10 20:26:23 +07:00
|
|
|
EditorState? editorState,
|
|
|
|
FlowyError? error,
|
2022-02-23 22:17:47 +08:00
|
|
|
}) = _DocumentState;
|
2021-10-19 13:56:11 +08:00
|
|
|
|
2022-02-23 22:17:47 +08:00
|
|
|
factory DocumentState.initial() => const DocumentState(
|
2021-10-31 17:24:55 +08:00
|
|
|
isDeleted: false,
|
2021-10-31 20:27:37 +08:00
|
|
|
forceClose: false,
|
2023-12-10 20:26:23 +07:00
|
|
|
isLoading: true,
|
2023-12-21 08:12:40 +08:00
|
|
|
isSyncing: false,
|
2021-10-31 17:24:55 +08:00
|
|
|
);
|
2021-10-19 13:56:11 +08:00
|
|
|
}
|