2023-05-16 06:58:24 +00:00
|
|
|
/*
|
|
|
|
* The following code defines functions that handle creating, opening, and closing documents,
|
|
|
|
* as well as performing actions on documents. These functions make use of a DocumentManager,
|
|
|
|
* which you can think of as a higher-level interface to interact with documents.
|
|
|
|
*/
|
|
|
|
|
2023-04-18 11:06:21 +00:00
|
|
|
use std::sync::Arc;
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-05-15 14:16:05 +00:00
|
|
|
use collab_document::blocks::{
|
|
|
|
json_str_to_hashmap, Block, BlockAction, BlockActionPayload, BlockActionType, BlockEvent,
|
|
|
|
BlockEventPayload, DeltaType,
|
|
|
|
};
|
|
|
|
|
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};
|
|
|
|
|
2023-04-13 10:53:51 +00:00
|
|
|
use crate::{
|
2023-05-16 06:58:24 +00:00
|
|
|
document_data::DocumentDataWrapper,
|
2023-04-13 10:53:51 +00:00
|
|
|
entities::{
|
2023-04-18 11:06:21 +00:00
|
|
|
ApplyActionPayloadPBV2, BlockActionPB, BlockActionPayloadPB, BlockActionTypePB, BlockEventPB,
|
2023-04-24 06:25:00 +00:00
|
|
|
BlockEventPayloadPB, BlockPB, CloseDocumentPayloadPBV2, CreateDocumentPayloadPBV2, DeltaTypePB,
|
|
|
|
DocEventPB, DocumentDataPB2, OpenDocumentPayloadPBV2,
|
2023-04-13 10:53:51 +00:00
|
|
|
},
|
|
|
|
manager::DocumentManager,
|
|
|
|
};
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
// Handler for creating a new document
|
2023-04-17 02:12:04 +00:00
|
|
|
pub(crate) async fn create_document_handler(
|
|
|
|
data: AFPluginData<CreateDocumentPayloadPBV2>,
|
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> FlowyResult<()> {
|
|
|
|
let context = data.into_inner();
|
2023-05-16 06:58:24 +00:00
|
|
|
// Create a new document with a default content, one page block and one text block
|
2023-04-17 02:12:04 +00:00
|
|
|
let data = DocumentDataWrapper::default();
|
2023-05-16 06:58:24 +00:00
|
|
|
manager.create_document(context.document_id, data)?;
|
2023-04-17 02:12:04 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
// Handler for opening an existing document
|
|
|
|
pub(crate) async fn open_document_handler(
|
|
|
|
data: AFPluginData<OpenDocumentPayloadPBV2>,
|
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> DataResult<DocumentDataPB2, FlowyError> {
|
|
|
|
let context = data.into_inner();
|
|
|
|
let document = manager.open_document(context.document_id)?;
|
|
|
|
let document_data = document.lock().get_document()?;
|
|
|
|
data_result_ok(DocumentDataPB2::from(DocumentDataWrapper(document_data)))
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:53:51 +00:00
|
|
|
pub(crate) async fn close_document_handler(
|
|
|
|
data: AFPluginData<CloseDocumentPayloadPBV2>,
|
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> FlowyResult<()> {
|
|
|
|
let context = data.into_inner();
|
|
|
|
manager.close_document(context.document_id)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
// Get the content of the existing document,
|
|
|
|
// if the document does not exist, return an error.
|
|
|
|
pub(crate) async fn get_document_data_handler(
|
|
|
|
data: AFPluginData<OpenDocumentPayloadPBV2>,
|
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> DataResult<DocumentDataPB2, FlowyError> {
|
|
|
|
let context = data.into_inner();
|
|
|
|
let document = manager.get_document(context.document_id)?;
|
|
|
|
let document_data = document.lock().get_document()?;
|
|
|
|
data_result_ok(DocumentDataPB2::from(DocumentDataWrapper(document_data)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for applying an action to a document
|
2023-04-13 10:53:51 +00:00
|
|
|
pub(crate) async fn apply_action_handler(
|
|
|
|
data: AFPluginData<ApplyActionPayloadPBV2>,
|
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> FlowyResult<()> {
|
|
|
|
let context = data.into_inner();
|
|
|
|
let doc_id = context.document_id;
|
2023-05-16 06:58:24 +00:00
|
|
|
let document = manager.open_document(doc_id)?;
|
|
|
|
let actions = context.actions.into_iter().map(BlockAction::from).collect();
|
2023-04-13 10:53:51 +00:00
|
|
|
document.lock().apply_action(actions);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BlockActionPB> for BlockAction {
|
|
|
|
fn from(pb: BlockActionPB) -> Self {
|
|
|
|
Self {
|
|
|
|
action: pb.action.into(),
|
|
|
|
payload: pb.payload.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BlockActionTypePB> for BlockActionType {
|
|
|
|
fn from(pb: BlockActionTypePB) -> Self {
|
|
|
|
match pb {
|
|
|
|
BlockActionTypePB::Insert => Self::Insert,
|
|
|
|
BlockActionTypePB::Update => Self::Update,
|
|
|
|
BlockActionTypePB::Delete => Self::Delete,
|
|
|
|
BlockActionTypePB::Move => Self::Move,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BlockActionPayloadPB> for BlockActionPayload {
|
|
|
|
fn from(pb: BlockActionPayloadPB) -> Self {
|
|
|
|
Self {
|
|
|
|
block: pb.block.into(),
|
|
|
|
parent_id: pb.parent_id,
|
|
|
|
prev_id: pb.prev_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BlockPB> for Block {
|
|
|
|
fn from(pb: BlockPB) -> Self {
|
2023-05-16 06:58:24 +00:00
|
|
|
// Use `json_str_to_hashmap()` from the `collab_document` crate to convert the JSON data to a hashmap
|
2023-04-13 10:53:51 +00:00
|
|
|
let data = json_str_to_hashmap(&pb.data).unwrap_or_default();
|
2023-05-16 06:58:24 +00:00
|
|
|
|
|
|
|
// Convert the protobuf `BlockPB` to our internal `Block` struct
|
2023-04-13 10:53:51 +00:00
|
|
|
Self {
|
|
|
|
id: pb.id,
|
|
|
|
ty: pb.ty,
|
|
|
|
children: pb.children_id,
|
|
|
|
parent: pb.parent_id,
|
|
|
|
data,
|
|
|
|
external_id: None,
|
|
|
|
external_type: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-17 02:12:04 +00:00
|
|
|
|
|
|
|
impl From<BlockEvent> for BlockEventPB {
|
2023-04-24 06:25:00 +00:00
|
|
|
fn from(payload: BlockEvent) -> Self {
|
2023-05-16 06:58:24 +00:00
|
|
|
// Convert each individual `BlockEvent` to a protobuf `BlockEventPB`, and collect the results into a `Vec`
|
2023-04-17 02:12:04 +00:00
|
|
|
Self {
|
2023-04-24 06:25:00 +00:00
|
|
|
event: payload.iter().map(|e| e.to_owned().into()).collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BlockEventPayload> for BlockEventPayloadPB {
|
|
|
|
fn from(payload: BlockEventPayload) -> Self {
|
|
|
|
Self {
|
|
|
|
command: payload.command.into(),
|
|
|
|
path: payload.path,
|
|
|
|
id: payload.id,
|
|
|
|
value: payload.value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DeltaType> for DeltaTypePB {
|
|
|
|
fn from(action: DeltaType) -> Self {
|
|
|
|
match action {
|
|
|
|
DeltaType::Inserted => Self::Inserted,
|
|
|
|
DeltaType::Updated => Self::Updated,
|
|
|
|
DeltaType::Removed => Self::Removed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:52:57 +00:00
|
|
|
impl From<(&Vec<BlockEvent>, bool)> for DocEventPB {
|
|
|
|
fn from((events, is_remote): (&Vec<BlockEvent>, bool)) -> Self {
|
2023-05-16 06:58:24 +00:00
|
|
|
// Convert each individual `BlockEvent` to a protobuf `BlockEventPB`, and collect the results into a `Vec`
|
2023-04-24 06:25:00 +00:00
|
|
|
Self {
|
|
|
|
events: events.iter().map(|e| e.to_owned().into()).collect(),
|
|
|
|
is_remote,
|
2023-04-17 02:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|