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,
|
2023-06-03 12:43:46 +00:00
|
|
|
BlockEventPayload, DeltaType,
|
2023-05-15 14:16:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};
|
|
|
|
|
2023-06-15 02:37:51 +00:00
|
|
|
use crate::entities::{
|
|
|
|
ApplyActionParams, CloseDocumentParams, ConvertDataParams, CreateDocumentParams,
|
|
|
|
DocumentRedoUndoParams, OpenDocumentParams,
|
|
|
|
};
|
2023-04-13 10:53:51 +00:00
|
|
|
use crate::{
|
|
|
|
entities::{
|
2023-05-23 08:13:12 +00:00
|
|
|
ApplyActionPayloadPB, BlockActionPB, BlockActionPayloadPB, BlockActionTypePB, BlockEventPB,
|
2023-06-03 12:43:46 +00:00
|
|
|
BlockEventPayloadPB, BlockPB, CloseDocumentPayloadPB, ConvertDataPayloadPB, ConvertType,
|
2023-06-15 02:37:51 +00:00
|
|
|
CreateDocumentPayloadPB, DeltaTypePB, DocEventPB, DocumentDataPB, DocumentRedoUndoPayloadPB,
|
|
|
|
DocumentRedoUndoResponsePB, OpenDocumentPayloadPB,
|
2023-04-13 10:53:51 +00:00
|
|
|
},
|
|
|
|
manager::DocumentManager,
|
2023-06-03 12:43:46 +00:00
|
|
|
parser::json::parser::JsonToDocumentParser,
|
2023-04-13 10:53:51 +00:00
|
|
|
};
|
|
|
|
|
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(
|
2023-05-23 08:13:12 +00:00
|
|
|
data: AFPluginData<CreateDocumentPayloadPB>,
|
2023-04-17 02:12:04 +00:00
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> FlowyResult<()> {
|
2023-06-15 02:37:51 +00:00
|
|
|
let params: CreateDocumentParams = data.into_inner().try_into()?;
|
2023-06-17 06:25:30 +00:00
|
|
|
manager.create_document(¶ms.document_id, params.initial_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(
|
2023-05-23 08:13:12 +00:00
|
|
|
data: AFPluginData<OpenDocumentPayloadPB>,
|
2023-05-16 06:58:24 +00:00
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
2023-05-23 08:13:12 +00:00
|
|
|
) -> DataResult<DocumentDataPB, FlowyError> {
|
2023-06-15 02:37:51 +00:00
|
|
|
let params: OpenDocumentParams = data.into_inner().try_into()?;
|
|
|
|
let doc_id = params.document_id;
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id)?;
|
2023-05-16 06:58:24 +00:00
|
|
|
let document_data = document.lock().get_document()?;
|
2023-06-03 05:55:43 +00:00
|
|
|
data_result_ok(DocumentDataPB::from(document_data))
|
2023-05-16 06:58:24 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 10:53:51 +00:00
|
|
|
pub(crate) async fn close_document_handler(
|
2023-05-23 08:13:12 +00:00
|
|
|
data: AFPluginData<CloseDocumentPayloadPB>,
|
2023-04-13 10:53:51 +00:00
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> FlowyResult<()> {
|
2023-06-15 02:37:51 +00:00
|
|
|
let params: CloseDocumentParams = data.into_inner().try_into()?;
|
|
|
|
let doc_id = params.document_id;
|
|
|
|
manager.close_document(&doc_id)?;
|
2023-04-13 10:53:51 +00:00
|
|
|
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(
|
2023-05-23 08:13:12 +00:00
|
|
|
data: AFPluginData<OpenDocumentPayloadPB>,
|
2023-05-16 06:58:24 +00:00
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
2023-05-23 08:13:12 +00:00
|
|
|
) -> DataResult<DocumentDataPB, FlowyError> {
|
2023-06-15 02:37:51 +00:00
|
|
|
let params: OpenDocumentParams = data.into_inner().try_into()?;
|
|
|
|
let doc_id = params.document_id;
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_document_from_disk(&doc_id)?;
|
2023-05-16 06:58:24 +00:00
|
|
|
let document_data = document.lock().get_document()?;
|
2023-06-03 05:55:43 +00:00
|
|
|
data_result_ok(DocumentDataPB::from(document_data))
|
2023-05-16 06:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for applying an action to a document
|
2023-04-13 10:53:51 +00:00
|
|
|
pub(crate) async fn apply_action_handler(
|
2023-05-23 08:13:12 +00:00
|
|
|
data: AFPluginData<ApplyActionPayloadPB>,
|
2023-04-13 10:53:51 +00:00
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> FlowyResult<()> {
|
2023-06-15 02:37:51 +00:00
|
|
|
let params: ApplyActionParams = data.into_inner().try_into()?;
|
|
|
|
let doc_id = params.document_id;
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id)?;
|
2023-06-15 02:37:51 +00:00
|
|
|
let actions = params.actions;
|
2023-04-13 10:53:51 +00:00
|
|
|
document.lock().apply_action(actions);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-03 12:43:46 +00:00
|
|
|
pub(crate) async fn convert_data_to_document(
|
|
|
|
data: AFPluginData<ConvertDataPayloadPB>,
|
|
|
|
_manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> DataResult<DocumentDataPB, FlowyError> {
|
|
|
|
let payload = data.into_inner();
|
|
|
|
let document = convert_data_to_document_internal(payload)?;
|
|
|
|
data_result_ok(document)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn convert_data_to_document_internal(
|
|
|
|
payload: ConvertDataPayloadPB,
|
|
|
|
) -> Result<DocumentDataPB, FlowyError> {
|
2023-06-15 02:37:51 +00:00
|
|
|
let params: ConvertDataParams = payload.try_into()?;
|
|
|
|
let convert_type = params.convert_type;
|
|
|
|
let data = params.data;
|
2023-06-03 12:43:46 +00:00
|
|
|
match convert_type {
|
|
|
|
ConvertType::Json => {
|
|
|
|
let json_str = String::from_utf8(data).map_err(|_| FlowyError::invalid_data())?;
|
|
|
|
let document = JsonToDocumentParser::json_str_to_document(&json_str)?;
|
|
|
|
Ok(document)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:37:51 +00:00
|
|
|
pub(crate) async fn redo_handler(
|
|
|
|
data: AFPluginData<DocumentRedoUndoPayloadPB>,
|
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> DataResult<DocumentRedoUndoResponsePB, FlowyError> {
|
|
|
|
let params: DocumentRedoUndoParams = data.into_inner().try_into()?;
|
|
|
|
let doc_id = params.document_id;
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id)?;
|
2023-06-15 02:37:51 +00:00
|
|
|
let document = document.lock();
|
|
|
|
let redo = document.redo();
|
|
|
|
let can_redo = document.can_redo();
|
|
|
|
let can_undo = document.can_undo();
|
|
|
|
data_result_ok(DocumentRedoUndoResponsePB {
|
|
|
|
can_redo,
|
|
|
|
can_undo,
|
|
|
|
is_success: redo,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn undo_handler(
|
|
|
|
data: AFPluginData<DocumentRedoUndoPayloadPB>,
|
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> DataResult<DocumentRedoUndoResponsePB, FlowyError> {
|
|
|
|
let params: DocumentRedoUndoParams = data.into_inner().try_into()?;
|
|
|
|
let doc_id = params.document_id;
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id)?;
|
2023-06-15 02:37:51 +00:00
|
|
|
let document = document.lock();
|
|
|
|
let undo = document.undo();
|
|
|
|
let can_redo = document.can_redo();
|
|
|
|
let can_undo = document.can_undo();
|
|
|
|
data_result_ok(DocumentRedoUndoResponsePB {
|
|
|
|
can_redo,
|
|
|
|
can_undo,
|
|
|
|
is_success: undo,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn can_undo_redo_handler(
|
|
|
|
data: AFPluginData<DocumentRedoUndoPayloadPB>,
|
|
|
|
manager: AFPluginState<Arc<DocumentManager>>,
|
|
|
|
) -> DataResult<DocumentRedoUndoResponsePB, FlowyError> {
|
|
|
|
let params: DocumentRedoUndoParams = data.into_inner().try_into()?;
|
|
|
|
let doc_id = params.document_id;
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id)?;
|
2023-06-15 02:37:51 +00:00
|
|
|
let document = document.lock();
|
|
|
|
let can_redo = document.can_redo();
|
|
|
|
let can_undo = document.can_undo();
|
|
|
|
drop(document);
|
|
|
|
data_result_ok(DocumentRedoUndoResponsePB {
|
|
|
|
can_redo,
|
|
|
|
can_undo,
|
|
|
|
is_success: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:53:51 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|