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,
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
use collab_document::blocks::{
|
2023-04-18 11:06:21 +00:00
|
|
|
json_str_to_hashmap, Block, BlockAction, BlockActionPayload, BlockActionType, BlockEvent,
|
2023-04-24 06:25:00 +00:00
|
|
|
BlockEventPayload, DeltaType,
|
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-24 06:25:00 +00:00
|
|
|
fn from(payload: BlockEvent) -> Self {
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocEventPB {
|
|
|
|
pub(crate) fn get_from(events: &Vec<BlockEvent>, is_remote: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
events: events.iter().map(|e| e.to_owned().into()).collect(),
|
|
|
|
is_remote,
|
2023-04-17 02:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|