mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: init flowy document 2 (#2248)
* feat: init flowy document 2 * feat: convert inner document to document PB * feat: integrate colla document into Flutter * feat: integrate colla document into tauri * fix: cargo clippy
This commit is contained in:
@ -3,6 +3,7 @@ import 'package:appflowy/plugins/document/presentation/plugins/cover/cover_node_
|
||||
import 'package:appflowy/plugins/trash/application/trash_service.dart';
|
||||
import 'package:appflowy/user/application/user_service.dart';
|
||||
import 'package:appflowy/workspace/application/view/view_listener.dart';
|
||||
import 'package:appflowy/workspace/application/doc/doc_listener.dart';
|
||||
import 'package:appflowy/plugins/document/application/doc_service.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pbserver.dart';
|
||||
@ -17,12 +18,13 @@ import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'dart:async';
|
||||
import 'package:appflowy/util/either_extension.dart';
|
||||
|
||||
import 'package:appflowy_backend/protobuf/flowy-document2/entities.pb.dart';
|
||||
part 'doc_bloc.freezed.dart';
|
||||
|
||||
class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
final ViewPB view;
|
||||
final DocumentService _documentService;
|
||||
final DocumentListener _docListener;
|
||||
|
||||
final ViewListener _listener;
|
||||
final TrashService _trashService;
|
||||
@ -32,12 +34,14 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
DocumentBloc({
|
||||
required this.view,
|
||||
}) : _documentService = DocumentService(),
|
||||
_docListener = DocumentListener(id: view.id),
|
||||
_listener = ViewListener(view: view),
|
||||
_trashService = TrashService(),
|
||||
super(DocumentState.initial()) {
|
||||
on<DocumentEvent>((event, emit) async {
|
||||
await event.map(
|
||||
initial: (Initial value) async {
|
||||
_listenOnDocChange();
|
||||
await _initial(value, emit);
|
||||
_listenOnViewChange();
|
||||
},
|
||||
@ -73,6 +77,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
}
|
||||
|
||||
await _documentService.closeDocument(docId: view.id);
|
||||
await _documentService.closeDocumentV2(view: view);
|
||||
return super.close();
|
||||
}
|
||||
|
||||
@ -88,6 +93,39 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
);
|
||||
}
|
||||
final result = await _documentService.openDocument(view: view);
|
||||
// test code
|
||||
final document = await _documentService.openDocumentV2(view: view);
|
||||
BlockPB? root;
|
||||
document.fold((l) {
|
||||
print('---------<open document v2>-----------');
|
||||
print('page id = ${l.pageId}');
|
||||
l.blocks.blocks.forEach((key, value) {
|
||||
print('-----<block begin>-----');
|
||||
print('block = $value');
|
||||
if (value.ty == 'page') {
|
||||
root = value;
|
||||
}
|
||||
print('-----<block end>-----');
|
||||
});
|
||||
print('---------<open document v2>-----------');
|
||||
}, (r) {});
|
||||
if (root != null) {
|
||||
await _documentService.applyAction(
|
||||
view: view,
|
||||
actions: [
|
||||
BlockActionPB(
|
||||
action: BlockActionTypePB.Insert,
|
||||
payload: BlockActionPayloadPB(
|
||||
block: BlockPB()
|
||||
..id = 'id_0'
|
||||
..ty = 'text'
|
||||
..parentId = root!.id,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
return result.fold(
|
||||
(documentData) async {
|
||||
await _initEditorState(documentData).whenComplete(() {
|
||||
@ -126,6 +164,14 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
);
|
||||
}
|
||||
|
||||
void _listenOnDocChange() {
|
||||
_docListener.start(
|
||||
didReceiveUpdate: () {
|
||||
print('---------<receive document update>-----------');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _initEditorState(DocumentDataPB documentData) async {
|
||||
final document = Document.fromJson(jsonDecode(documentData.content));
|
||||
final editorState = EditorState(document: document);
|
||||
|
@ -4,6 +4,7 @@ import 'package:appflowy_backend/dispatch/dispatch.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-document2/entities.pb.dart';
|
||||
|
||||
class DocumentService {
|
||||
Future<Either<DocumentDataPB, FlowyError>> openDocument({
|
||||
@ -39,4 +40,32 @@ class DocumentService {
|
||||
final payload = ViewIdPB(value: docId);
|
||||
return FolderEventCloseView(payload).send();
|
||||
}
|
||||
|
||||
Future<Either<DocumentDataPB2, FlowyError>> openDocumentV2({
|
||||
required ViewPB view,
|
||||
}) async {
|
||||
await FolderEventSetLatestView(ViewIdPB(value: view.id)).send();
|
||||
|
||||
final payload = OpenDocumentPayloadPBV2()..documentId = view.id;
|
||||
|
||||
return DocumentEvent2OpenDocument(payload).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> closeDocumentV2({
|
||||
required ViewPB view,
|
||||
}) async {
|
||||
final payload = CloseDocumentPayloadPBV2()..documentId = view.id;
|
||||
return DocumentEvent2CloseDocument(payload).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> applyAction({
|
||||
required ViewPB view,
|
||||
required List<BlockActionPB> actions,
|
||||
}) async {
|
||||
final payload = ApplyActionPayloadPBV2(
|
||||
documentId: view.id,
|
||||
actions: actions,
|
||||
);
|
||||
return DocumentEvent2ApplyAction(payload).send();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user