2023-01-08 12:10:53 +08:00
|
|
|
import 'package:appflowy_backend/dispatch/dispatch.dart';
|
2023-04-13 18:53:51 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-document2/entities.pb.dart';
|
2023-09-12 20:49:03 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
|
|
|
|
import 'package:dartz/dartz.dart';
|
2021-07-23 16:45:29 +08:00
|
|
|
|
2022-02-28 21:02:46 -05:00
|
|
|
class DocumentService {
|
2023-05-16 14:58:24 +08:00
|
|
|
// unused now.
|
|
|
|
Future<Either<FlowyError, Unit>> createDocument({
|
2022-10-22 21:57:44 +08:00
|
|
|
required ViewPB view,
|
2022-03-06 21:22:42 +08:00
|
|
|
}) async {
|
2023-05-16 14:58:24 +08:00
|
|
|
final canOpen = await openDocument(view: view);
|
|
|
|
if (canOpen.isRight()) {
|
|
|
|
return const Right(unit);
|
|
|
|
}
|
2023-05-23 16:13:12 +08:00
|
|
|
final payload = CreateDocumentPayloadPB()..documentId = view.id;
|
|
|
|
final result = await DocumentEventCreateDocument(payload).send();
|
2023-05-16 14:58:24 +08:00
|
|
|
return result.swap();
|
2021-09-11 21:30:58 +08:00
|
|
|
}
|
|
|
|
|
2023-05-23 16:13:12 +08:00
|
|
|
Future<Either<FlowyError, DocumentDataPB>> openDocument({
|
2023-04-13 18:53:51 +08:00
|
|
|
required ViewPB view,
|
|
|
|
}) async {
|
2023-05-23 16:13:12 +08:00
|
|
|
final payload = OpenDocumentPayloadPB()..documentId = view.id;
|
|
|
|
final result = await DocumentEventOpenDocument(payload).send();
|
2023-05-16 14:58:24 +08:00
|
|
|
return result.swap();
|
2023-04-13 18:53:51 +08:00
|
|
|
}
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
Future<Either<FlowyError, Unit>> closeDocument({
|
2023-04-13 18:53:51 +08:00
|
|
|
required ViewPB view,
|
|
|
|
}) async {
|
2023-05-23 16:13:12 +08:00
|
|
|
final payload = CloseDocumentPayloadPB()..documentId = view.id;
|
|
|
|
final result = await DocumentEventCloseDocument(payload).send();
|
2023-05-16 14:58:24 +08:00
|
|
|
return result.swap();
|
2023-04-13 18:53:51 +08:00
|
|
|
}
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
Future<Either<FlowyError, Unit>> applyAction({
|
|
|
|
required String documentId,
|
|
|
|
required Iterable<BlockActionPB> actions,
|
2023-04-13 18:53:51 +08:00
|
|
|
}) async {
|
2023-05-23 16:13:12 +08:00
|
|
|
final payload = ApplyActionPayloadPB(
|
2023-05-16 14:58:24 +08:00
|
|
|
documentId: documentId,
|
2023-04-13 18:53:51 +08:00
|
|
|
actions: actions,
|
|
|
|
);
|
2023-05-23 16:13:12 +08:00
|
|
|
final result = await DocumentEventApplyAction(payload).send();
|
2023-05-16 14:58:24 +08:00
|
|
|
return result.swap();
|
2023-04-13 18:53:51 +08:00
|
|
|
}
|
2023-09-12 20:49:03 +08:00
|
|
|
|
|
|
|
/// Creates a new external text.
|
|
|
|
///
|
|
|
|
/// Normally, it's used to the block that needs sync long text.
|
|
|
|
///
|
|
|
|
/// the delta parameter is the json representation of the delta.
|
|
|
|
Future<Either<FlowyError, Unit>> createExternalText({
|
|
|
|
required String documentId,
|
|
|
|
required String textId,
|
|
|
|
String? delta,
|
|
|
|
}) async {
|
|
|
|
final payload = TextDeltaPayloadPB(
|
|
|
|
documentId: documentId,
|
|
|
|
textId: textId,
|
|
|
|
delta: delta,
|
|
|
|
);
|
|
|
|
final result = await DocumentEventCreateText(payload).send();
|
|
|
|
return result.swap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the external text.
|
|
|
|
///
|
|
|
|
/// this function is compatible with the [createExternalText] function.
|
|
|
|
///
|
|
|
|
/// the delta parameter is the json representation of the delta too.
|
|
|
|
Future<Either<FlowyError, Unit>> updateExternalText({
|
|
|
|
required String documentId,
|
|
|
|
required String textId,
|
|
|
|
String? delta,
|
|
|
|
}) async {
|
|
|
|
final payload = TextDeltaPayloadPB(
|
|
|
|
documentId: documentId,
|
|
|
|
textId: textId,
|
|
|
|
delta: delta,
|
|
|
|
);
|
|
|
|
final result = await DocumentEventApplyTextDeltaEvent(payload).send();
|
|
|
|
return result.swap();
|
|
|
|
}
|
2021-07-23 16:45:29 +08:00
|
|
|
}
|