2023-01-08 12:10:53 +08:00
|
|
|
import 'package:appflowy_backend/dispatch/dispatch.dart';
|
2023-12-31 07:29:40 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart';
|
2023-09-12 20:49:03 +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';
|
2024-03-28 17:46:31 +08:00
|
|
|
import 'package:appflowy_editor/appflowy_editor.dart';
|
2024-02-24 20:54:10 +07:00
|
|
|
import 'package:appflowy_result/appflowy_result.dart';
|
2024-03-28 17:46:31 +08:00
|
|
|
import 'package:fixnum/fixnum.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.
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> createDocument({
|
2022-10-22 21:57:44 +08:00
|
|
|
required ViewPB view,
|
2022-03-06 21:22:42 +08:00
|
|
|
}) async {
|
2024-04-29 13:44:42 +08:00
|
|
|
final canOpen = await openDocument(documentId: view.id);
|
2024-03-22 16:15:18 +07:00
|
|
|
if (canOpen.isSuccess) {
|
2024-02-24 20:54:10 +07:00
|
|
|
return FlowyResult.success(null);
|
2023-05-16 14:58:24 +08:00
|
|
|
}
|
2023-05-23 16:13:12 +08:00
|
|
|
final payload = CreateDocumentPayloadPB()..documentId = view.id;
|
|
|
|
final result = await DocumentEventCreateDocument(payload).send();
|
2024-02-24 20:54:10 +07:00
|
|
|
return result;
|
2021-09-11 21:30:58 +08:00
|
|
|
}
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<DocumentDataPB, FlowyError>> openDocument({
|
2024-04-29 13:44:42 +08:00
|
|
|
required String documentId,
|
2023-04-13 18:53:51 +08:00
|
|
|
}) async {
|
2024-04-29 13:44:42 +08:00
|
|
|
final payload = OpenDocumentPayloadPB()..documentId = documentId;
|
2023-05-23 16:13:12 +08:00
|
|
|
final result = await DocumentEventOpenDocument(payload).send();
|
2024-02-24 20:54:10 +07:00
|
|
|
return result;
|
2023-04-13 18:53:51 +08:00
|
|
|
}
|
|
|
|
|
2024-03-21 12:26:48 +07:00
|
|
|
Future<FlowyResult<DocumentDataPB, FlowyError>> getDocument({
|
2024-04-29 13:44:42 +08:00
|
|
|
required String documentId,
|
2024-03-21 12:26:48 +07:00
|
|
|
}) async {
|
2024-04-29 13:44:42 +08:00
|
|
|
final payload = OpenDocumentPayloadPB()..documentId = documentId;
|
2024-03-21 12:26:48 +07:00
|
|
|
final result = await DocumentEventGetDocumentData(payload).send();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<BlockPB, FlowyError>> getBlockFromDocument({
|
2023-10-26 03:41:03 +02:00
|
|
|
required DocumentDataPB document,
|
|
|
|
required String blockId,
|
|
|
|
}) async {
|
|
|
|
final block = document.blocks[blockId];
|
|
|
|
|
|
|
|
if (block != null) {
|
2024-02-24 20:54:10 +07:00
|
|
|
return FlowyResult.success(block);
|
2023-10-26 03:41:03 +02:00
|
|
|
}
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
return FlowyResult.failure(
|
2023-10-26 03:41:03 +02:00
|
|
|
FlowyError(
|
|
|
|
msg: 'Block($blockId) not found in Document(${document.pageId})',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> closeDocument({
|
2024-04-29 13:44:42 +08:00
|
|
|
required String viewId,
|
2023-04-13 18:53:51 +08:00
|
|
|
}) async {
|
2024-04-29 13:44:42 +08:00
|
|
|
final payload = ViewIdPB()..value = viewId;
|
2024-04-02 13:36:59 +08:00
|
|
|
final result = await FolderEventCloseView(payload).send();
|
2024-02-24 20:54:10 +07:00
|
|
|
return result;
|
2023-04-13 18:53:51 +08:00
|
|
|
}
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> applyAction({
|
2023-05-16 14:58:24 +08:00
|
|
|
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();
|
2024-02-24 20:54:10 +07:00
|
|
|
return result;
|
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.
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> createExternalText({
|
2023-09-12 20:49:03 +08:00
|
|
|
required String documentId,
|
|
|
|
required String textId,
|
|
|
|
String? delta,
|
|
|
|
}) async {
|
|
|
|
final payload = TextDeltaPayloadPB(
|
|
|
|
documentId: documentId,
|
|
|
|
textId: textId,
|
|
|
|
delta: delta,
|
|
|
|
);
|
|
|
|
final result = await DocumentEventCreateText(payload).send();
|
2024-02-24 20:54:10 +07:00
|
|
|
return result;
|
2023-09-12 20:49:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the external text.
|
|
|
|
///
|
|
|
|
/// this function is compatible with the [createExternalText] function.
|
|
|
|
///
|
|
|
|
/// the delta parameter is the json representation of the delta too.
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> updateExternalText({
|
2023-09-12 20:49:03 +08:00
|
|
|
required String documentId,
|
|
|
|
required String textId,
|
|
|
|
String? delta,
|
|
|
|
}) async {
|
|
|
|
final payload = TextDeltaPayloadPB(
|
|
|
|
documentId: documentId,
|
|
|
|
textId: textId,
|
|
|
|
delta: delta,
|
|
|
|
);
|
|
|
|
final result = await DocumentEventApplyTextDeltaEvent(payload).send();
|
2024-02-24 20:54:10 +07:00
|
|
|
return result;
|
2023-09-12 20:49:03 +08:00
|
|
|
}
|
2024-01-20 23:16:18 +08:00
|
|
|
|
|
|
|
/// Upload a file to the cloud storage.
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<UploadedFilePB, FlowyError>> uploadFile({
|
2024-01-20 23:16:18 +08:00
|
|
|
required String localFilePath,
|
2024-02-19 16:24:47 +07:00
|
|
|
bool isAsync = true,
|
2024-01-20 23:16:18 +08:00
|
|
|
}) async {
|
|
|
|
final workspace = await FolderEventReadCurrentWorkspace().send();
|
|
|
|
return workspace.fold((l) async {
|
|
|
|
final payload = UploadFileParamsPB(
|
|
|
|
workspaceId: l.id,
|
|
|
|
localFilePath: localFilePath,
|
2024-02-19 16:24:47 +07:00
|
|
|
isAsync: isAsync,
|
2024-01-20 23:16:18 +08:00
|
|
|
);
|
|
|
|
final result = await DocumentEventUploadFile(payload).send();
|
2024-02-24 20:54:10 +07:00
|
|
|
return result;
|
2024-01-20 23:16:18 +08:00
|
|
|
}, (r) async {
|
2024-02-24 20:54:10 +07:00
|
|
|
return FlowyResult.failure(FlowyError(msg: 'Workspace not found'));
|
2024-01-20 23:16:18 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Download a file from the cloud storage.
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> downloadFile({
|
2024-01-20 23:16:18 +08:00
|
|
|
required String url,
|
|
|
|
}) async {
|
|
|
|
final workspace = await FolderEventReadCurrentWorkspace().send();
|
|
|
|
return workspace.fold((l) async {
|
|
|
|
final payload = UploadedFilePB(
|
|
|
|
url: url,
|
|
|
|
);
|
|
|
|
final result = await DocumentEventDownloadFile(payload).send();
|
2024-02-24 20:54:10 +07:00
|
|
|
return result;
|
2024-01-20 23:16:18 +08:00
|
|
|
}, (r) async {
|
2024-02-24 20:54:10 +07:00
|
|
|
return FlowyResult.failure(FlowyError(msg: 'Workspace not found'));
|
2024-01-20 23:16:18 +08:00
|
|
|
});
|
|
|
|
}
|
2024-03-28 17:46:31 +08:00
|
|
|
|
|
|
|
/// Sync the awareness states
|
|
|
|
/// For example, the cursor position, selection, who is viewing the document.
|
|
|
|
Future<FlowyResult<void, FlowyError>> syncAwarenessStates({
|
|
|
|
required String documentId,
|
|
|
|
Selection? selection,
|
|
|
|
String? metadata,
|
|
|
|
}) async {
|
|
|
|
final payload = UpdateDocumentAwarenessStatePB(
|
|
|
|
documentId: documentId,
|
|
|
|
selection: convertSelectionToAwarenessSelection(selection),
|
|
|
|
metadata: metadata,
|
|
|
|
);
|
|
|
|
|
|
|
|
final result = await DocumentEventSetAwarenessState(payload).send();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentAwarenessSelectionPB? convertSelectionToAwarenessSelection(
|
|
|
|
Selection? selection,
|
|
|
|
) {
|
|
|
|
if (selection == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return DocumentAwarenessSelectionPB(
|
|
|
|
start: DocumentAwarenessPositionPB(
|
|
|
|
offset: Int64(selection.startIndex),
|
|
|
|
path: selection.start.path.map((e) => Int64(e)),
|
|
|
|
),
|
|
|
|
end: DocumentAwarenessPositionPB(
|
|
|
|
offset: Int64(selection.endIndex),
|
|
|
|
path: selection.end.path.map((e) => Int64(e)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-07-23 16:45:29 +08:00
|
|
|
}
|