2021-12-22 21:13:52 +08:00
|
|
|
use crate::{
|
|
|
|
|
entities::revision::{RepeatedRevision, Revision},
|
2021-12-29 00:34:00 +08:00
|
|
|
errors::CollaborateError,
|
2021-12-22 21:13:52 +08:00
|
|
|
};
|
2021-07-22 21:43:01 +08:00
|
|
|
use flowy_derive::ProtoBuf;
|
2022-01-21 21:41:24 +08:00
|
|
|
use lib_ot::{errors::OTError, rich_text::RichTextDelta};
|
2021-07-22 21:43:01 +08:00
|
|
|
|
2021-09-11 14:26:30 +08:00
|
|
|
#[derive(ProtoBuf, Default, Debug, Clone)]
|
2022-03-10 17:14:10 +08:00
|
|
|
pub struct CreateTextBlockParams {
|
2021-07-22 21:43:01 +08:00
|
|
|
#[pb(index = 1)]
|
2021-07-23 14:37:18 +08:00
|
|
|
pub id: String,
|
2021-07-22 21:43:01 +08:00
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2021-12-23 22:52:38 +08:00
|
|
|
pub revisions: RepeatedRevision,
|
2021-09-11 14:26:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(ProtoBuf, Default, Debug, Clone, Eq, PartialEq)]
|
2022-07-19 11:31:04 +08:00
|
|
|
pub struct DocumentPB {
|
2021-09-09 15:43:05 +08:00
|
|
|
#[pb(index = 1)]
|
2022-03-02 21:12:21 +08:00
|
|
|
pub block_id: String,
|
2021-09-09 15:43:05 +08:00
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2021-12-23 22:52:38 +08:00
|
|
|
pub text: String,
|
2021-09-22 23:21:44 +08:00
|
|
|
|
|
|
|
|
#[pb(index = 3)]
|
2021-09-25 21:47:02 +08:00
|
|
|
pub rev_id: i64,
|
2021-10-06 15:23:38 +08:00
|
|
|
|
|
|
|
|
#[pb(index = 4)]
|
|
|
|
|
pub base_rev_id: i64,
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 11:31:04 +08:00
|
|
|
impl DocumentPB {
|
2021-12-07 10:39:01 +08:00
|
|
|
pub fn delta(&self) -> Result<RichTextDelta, OTError> {
|
2021-12-23 22:52:38 +08:00
|
|
|
let delta = RichTextDelta::from_bytes(&self.text)?;
|
2021-10-06 15:23:38 +08:00
|
|
|
Ok(delta)
|
|
|
|
|
}
|
2021-07-22 21:43:01 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 11:31:04 +08:00
|
|
|
impl std::convert::TryFrom<Revision> for DocumentPB {
|
2021-12-13 22:46:35 +08:00
|
|
|
type Error = CollaborateError;
|
|
|
|
|
|
|
|
|
|
fn try_from(revision: Revision) -> Result<Self, Self::Error> {
|
|
|
|
|
if !revision.is_initial() {
|
2021-12-21 11:13:38 +08:00
|
|
|
return Err(CollaborateError::revision_conflict()
|
|
|
|
|
.context("Revision's rev_id should be 0 when creating the document"));
|
2021-12-13 22:46:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let delta = RichTextDelta::from_bytes(&revision.delta_data)?;
|
2022-07-24 18:10:29 +08:00
|
|
|
let doc_json = delta.to_json_str();
|
2021-12-13 22:46:35 +08:00
|
|
|
|
2022-07-19 11:31:04 +08:00
|
|
|
Ok(DocumentPB {
|
2022-03-02 21:12:21 +08:00
|
|
|
block_id: revision.object_id,
|
2021-12-23 22:52:38 +08:00
|
|
|
text: doc_json,
|
2021-12-13 22:46:35 +08:00
|
|
|
rev_id: revision.rev_id,
|
|
|
|
|
base_rev_id: revision.base_rev_id,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 14:26:30 +08:00
|
|
|
#[derive(ProtoBuf, Default, Debug, Clone)]
|
2022-03-10 17:14:10 +08:00
|
|
|
pub struct ResetTextBlockParams {
|
2021-09-11 14:26:30 +08:00
|
|
|
#[pb(index = 1)]
|
2022-03-02 21:12:21 +08:00
|
|
|
pub block_id: String,
|
2021-07-22 21:43:01 +08:00
|
|
|
|
2021-09-14 16:22:44 +08:00
|
|
|
#[pb(index = 2)]
|
2021-12-23 22:52:38 +08:00
|
|
|
pub revisions: RepeatedRevision,
|
2021-09-14 16:22:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(ProtoBuf, Default, Debug, Clone)]
|
2022-07-18 11:58:39 +08:00
|
|
|
pub struct TextBlockDeltaPB {
|
2021-09-14 16:22:44 +08:00
|
|
|
#[pb(index = 1)]
|
2022-02-25 22:27:44 +08:00
|
|
|
pub block_id: String,
|
2021-09-14 16:22:44 +08:00
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2022-03-05 22:30:42 +08:00
|
|
|
pub delta_str: String,
|
2021-07-22 21:43:01 +08:00
|
|
|
}
|
2021-07-22 22:26:38 +08:00
|
|
|
|
2021-10-03 11:33:19 +08:00
|
|
|
#[derive(ProtoBuf, Default, Debug, Clone)]
|
2022-07-18 11:58:39 +08:00
|
|
|
pub struct NewDocUserPB {
|
2021-10-03 11:33:19 +08:00
|
|
|
#[pb(index = 1)]
|
|
|
|
|
pub user_id: String,
|
|
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
|
pub rev_id: i64,
|
|
|
|
|
|
|
|
|
|
#[pb(index = 3)]
|
|
|
|
|
pub doc_id: String,
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:43:05 +08:00
|
|
|
#[derive(ProtoBuf, Default, Debug, Clone)]
|
2022-07-18 11:58:39 +08:00
|
|
|
pub struct TextBlockIdPB {
|
2021-07-22 22:26:38 +08:00
|
|
|
#[pb(index = 1)]
|
2022-02-24 21:49:18 +08:00
|
|
|
pub value: String,
|
2021-07-22 22:26:38 +08:00
|
|
|
}
|
2022-07-18 11:58:39 +08:00
|
|
|
impl AsRef<str> for TextBlockIdPB {
|
2022-03-06 21:22:42 +08:00
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
|
&self.value
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-15 13:42:52 +08:00
|
|
|
|
2022-07-18 11:58:39 +08:00
|
|
|
impl std::convert::From<String> for TextBlockIdPB {
|
2022-02-25 22:27:44 +08:00
|
|
|
fn from(value: String) -> Self {
|
2022-07-18 11:58:39 +08:00
|
|
|
TextBlockIdPB { value }
|
2022-01-23 12:14:00 +08:00
|
|
|
}
|
2021-10-15 13:42:52 +08:00
|
|
|
}
|
2021-10-19 13:04:09 +08:00
|
|
|
|
2022-07-18 11:58:39 +08:00
|
|
|
impl std::convert::From<TextBlockIdPB> for String {
|
|
|
|
|
fn from(block_id: TextBlockIdPB) -> Self {
|
2022-03-06 21:22:42 +08:00
|
|
|
block_id.value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-18 11:58:39 +08:00
|
|
|
impl std::convert::From<&String> for TextBlockIdPB {
|
2022-03-06 21:22:42 +08:00
|
|
|
fn from(s: &String) -> Self {
|
2022-07-18 11:58:39 +08:00
|
|
|
TextBlockIdPB { value: s.to_owned() }
|
2021-10-19 13:04:09 +08:00
|
|
|
}
|
|
|
|
|
}
|