2023-07-29 01:46:24 +00:00
|
|
|
use std::sync::Weak;
|
|
|
|
|
2023-04-13 10:53:51 +00:00
|
|
|
use strum_macros::Display;
|
|
|
|
|
|
|
|
use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
|
|
|
|
use lib_dispatch::prelude::AFPlugin;
|
|
|
|
|
2023-10-30 04:50:31 +00:00
|
|
|
use crate::event_handler::convert_document;
|
2023-07-05 12:57:09 +00:00
|
|
|
use crate::event_handler::get_snapshot_handler;
|
|
|
|
use crate::{event_handler::*, manager::DocumentManager};
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-07-29 01:46:24 +00:00
|
|
|
pub fn init(document_manager: Weak<DocumentManager>) -> AFPlugin {
|
2023-07-05 12:57:09 +00:00
|
|
|
AFPlugin::new()
|
2023-04-13 10:53:51 +00:00
|
|
|
.name(env!("CARGO_PKG_NAME"))
|
2023-07-05 12:57:09 +00:00
|
|
|
.state(document_manager)
|
|
|
|
.event(DocumentEvent::CreateDocument, create_document_handler)
|
|
|
|
.event(DocumentEvent::OpenDocument, open_document_handler)
|
|
|
|
.event(DocumentEvent::CloseDocument, close_document_handler)
|
|
|
|
.event(DocumentEvent::ApplyAction, apply_action_handler)
|
|
|
|
.event(DocumentEvent::GetDocumentData, get_document_data_handler)
|
|
|
|
.event(
|
|
|
|
DocumentEvent::ConvertDataToDocument,
|
|
|
|
convert_data_to_document,
|
|
|
|
)
|
|
|
|
.event(DocumentEvent::Redo, redo_handler)
|
|
|
|
.event(DocumentEvent::Undo, undo_handler)
|
|
|
|
.event(DocumentEvent::CanUndoRedo, can_undo_redo_handler)
|
|
|
|
.event(DocumentEvent::GetDocumentSnapshots, get_snapshot_handler)
|
2023-09-12 12:49:03 +00:00
|
|
|
.event(DocumentEvent::CreateText, create_text_handler)
|
|
|
|
.event(DocumentEvent::ApplyTextDeltaEvent, apply_text_delta_handler)
|
2023-10-30 04:50:31 +00:00
|
|
|
.event(DocumentEvent::ConvertDocument, convert_document)
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, ProtoBuf_Enum, Flowy_Event)]
|
|
|
|
#[event_err = "FlowyError"]
|
2023-05-23 08:13:12 +00:00
|
|
|
pub enum DocumentEvent {
|
|
|
|
#[event(input = "CreateDocumentPayloadPB")]
|
2023-05-16 06:58:24 +00:00
|
|
|
CreateDocument = 0,
|
|
|
|
|
2023-05-23 08:13:12 +00:00
|
|
|
#[event(input = "OpenDocumentPayloadPB", output = "DocumentDataPB")]
|
2023-05-16 06:58:24 +00:00
|
|
|
OpenDocument = 1,
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-05-23 08:13:12 +00:00
|
|
|
#[event(input = "CloseDocumentPayloadPB")]
|
2023-05-16 06:58:24 +00:00
|
|
|
CloseDocument = 2,
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-05-23 08:13:12 +00:00
|
|
|
#[event(input = "ApplyActionPayloadPB")]
|
2023-05-16 06:58:24 +00:00
|
|
|
ApplyAction = 3,
|
2023-04-17 02:12:04 +00:00
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
#[event(input = "OpenDocumentPayloadPB", output = "DocumentDataPB")]
|
2023-05-16 06:58:24 +00:00
|
|
|
GetDocumentData = 4,
|
2023-06-03 12:43:46 +00:00
|
|
|
|
|
|
|
#[event(input = "ConvertDataPayloadPB", output = "DocumentDataPB")]
|
|
|
|
ConvertDataToDocument = 5,
|
2023-06-15 02:37:51 +00:00
|
|
|
|
|
|
|
#[event(
|
|
|
|
input = "DocumentRedoUndoPayloadPB",
|
|
|
|
output = "DocumentRedoUndoResponsePB"
|
|
|
|
)]
|
|
|
|
Redo = 6,
|
|
|
|
|
|
|
|
#[event(
|
|
|
|
input = "DocumentRedoUndoPayloadPB",
|
|
|
|
output = "DocumentRedoUndoResponsePB"
|
|
|
|
)]
|
|
|
|
Undo = 7,
|
|
|
|
|
|
|
|
#[event(
|
|
|
|
input = "DocumentRedoUndoPayloadPB",
|
|
|
|
output = "DocumentRedoUndoResponsePB"
|
|
|
|
)]
|
|
|
|
CanUndoRedo = 8,
|
2023-07-05 12:57:09 +00:00
|
|
|
|
|
|
|
#[event(input = "OpenDocumentPayloadPB", output = "RepeatedDocumentSnapshotPB")]
|
|
|
|
GetDocumentSnapshots = 9,
|
2023-09-12 12:49:03 +00:00
|
|
|
|
|
|
|
#[event(input = "TextDeltaPayloadPB")]
|
|
|
|
CreateText = 10,
|
|
|
|
|
|
|
|
#[event(input = "TextDeltaPayloadPB")]
|
|
|
|
ApplyTextDeltaEvent = 11,
|
2023-10-30 04:50:31 +00:00
|
|
|
|
|
|
|
/// Handler for converting a document to a JSON string, HTML string, or plain text string.
|
|
|
|
///
|
|
|
|
/// ConvertDocumentPayloadPB is the input of this event.
|
|
|
|
/// ConvertDocumentResponsePB is the output of this event.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```txt
|
|
|
|
/// // document: [{ "block_id": "1", "type": "paragraph", "data": {"delta": [{ "insert": "Hello World!" }] } }, { "block_id": "2", "type": "paragraph", "data": {"delta": [{ "insert": "Hello World!" }] }
|
|
|
|
/// let test = DocumentEventTest::new().await;
|
|
|
|
/// let view = test.create_document().await;
|
|
|
|
/// let payload = ConvertDocumentPayloadPB {
|
|
|
|
/// document_id: view.id,
|
|
|
|
/// range: Some(RangePB {
|
|
|
|
/// start: SelectionPB {
|
|
|
|
/// block_id: "1".to_string(),
|
|
|
|
/// index: 0,
|
|
|
|
/// length: 5,
|
|
|
|
/// },
|
|
|
|
/// end: SelectionPB {
|
|
|
|
/// block_id: "2".to_string(),
|
|
|
|
/// index: 5,
|
|
|
|
/// length: 7,
|
|
|
|
/// }
|
|
|
|
/// }),
|
|
|
|
/// export_types: ConvertTypePB {
|
|
|
|
/// json: true,
|
|
|
|
/// text: true,
|
|
|
|
/// html: true,
|
|
|
|
/// },
|
|
|
|
/// };
|
|
|
|
/// let result = test.convert_document(payload).await;
|
|
|
|
/// assert_eq!(result.json, Some("[{ \"block_id\": \"1\", \"type\": \"paragraph\", \"data\": {\"delta\": [{ \"insert\": \"Hello\" }] } }, { \"block_id\": \"2\", \"type\": \"paragraph\", \"data\": {\"delta\": [{ \"insert\": \" World!\" }] } }".to_string()));
|
|
|
|
/// assert_eq!(result.text, Some("Hello\n World!".to_string()));
|
|
|
|
/// assert_eq!(result.html, Some("<p>Hello</p><p> World!</p>".to_string()));
|
|
|
|
/// ```
|
|
|
|
/// #
|
|
|
|
#[event(
|
|
|
|
input = "ConvertDocumentPayloadPB",
|
|
|
|
output = "ConvertDocumentResponsePB"
|
|
|
|
)]
|
|
|
|
ConvertDocument = 12,
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|