2023-04-18 11:06:21 +00:00
|
|
|
use std::sync::Arc;
|
2023-04-13 10:53:51 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
document::DocumentDataWrapper,
|
|
|
|
entities::{
|
2023-04-18 11:06:21 +00:00
|
|
|
ApplyActionPayloadPBV2, BlockActionPB, BlockActionPayloadPB, BlockActionTypePB, BlockEventPB,
|
|
|
|
BlockPB, CloseDocumentPayloadPBV2, CreateDocumentPayloadPBV2, DocumentDataPB2,
|
|
|
|
OpenDocumentPayloadPBV2,
|
2023-04-13 10:53:51 +00:00
|
|
|
},
|
|
|
|
manager::DocumentManager,
|
|
|
|
};
|
|
|
|
|
|
|
|
use collab_document::blocks::{
|
2023-04-18 11:06:21 +00:00
|
|
|
json_str_to_hashmap, Block, BlockAction, BlockActionPayload, BlockActionType, BlockEvent,
|
2023-04-13 10:53:51 +00:00
|
|
|
};
|
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};
|
|
|
|
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()
|
|
|
|
.map_err(|err| FlowyError::internal().context(err))?;
|
|
|
|
data_result_ok(DocumentDataPB2::from(DocumentDataWrapper(document_data)))
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
let data = DocumentDataWrapper::default();
|
|
|
|
manager.create_document(context.document_id, data)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
let actions = context
|
|
|
|
.actions
|
|
|
|
.into_iter()
|
|
|
|
.map(|action| action.into())
|
|
|
|
.collect();
|
|
|
|
let document = manager.open_document(doc_id)?;
|
|
|
|
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 {
|
|
|
|
let data = json_str_to_hashmap(&pb.data).unwrap_or_default();
|
|
|
|
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-21 09:02:26 +00:00
|
|
|
fn from(_block_event: BlockEvent) -> Self {
|
2023-04-18 11:06:21 +00:00
|
|
|
// let delta = serde_json::to_value(&block_event.delta).unwrap();
|
|
|
|
// Self {
|
|
|
|
// path: block_event.path.into(),
|
|
|
|
// delta: delta.to_string(),
|
|
|
|
// }
|
2023-04-17 02:12:04 +00:00
|
|
|
Self {
|
2023-04-18 11:06:21 +00:00
|
|
|
path: vec![],
|
|
|
|
delta: "".to_string(),
|
2023-04-17 02:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|