118 lines
2.6 KiB
Rust
Raw Normal View History

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)]
pub id: String,
2021-07-22 21:43:01 +08:00
#[pb(index = 2)]
pub revisions: RepeatedRevision,
2021-09-11 14:26:30 +08:00
}
#[derive(ProtoBuf, Default, Debug, Clone, Eq, PartialEq)]
pub struct TextBlockInfoPB {
#[pb(index = 1)]
2022-03-02 21:12:21 +08:00
pub block_id: String,
#[pb(index = 2)]
pub text: String,
#[pb(index = 3)]
pub rev_id: i64,
2021-10-06 15:23:38 +08:00
#[pb(index = 4)]
pub base_rev_id: i64,
}
impl TextBlockInfoPB {
2021-12-07 10:39:01 +08:00
pub fn delta(&self) -> Result<RichTextDelta, OTError> {
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
}
impl std::convert::TryFrom<Revision> for TextBlockInfoPB {
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-03-05 22:30:42 +08:00
let doc_json = delta.to_delta_str();
2021-12-13 22:46:35 +08:00
Ok(TextBlockInfoPB {
2022-03-02 21:12:21 +08:00
block_id: revision.object_id,
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)]
pub revisions: RepeatedRevision,
2021-09-14 16:22:44 +08:00
}
#[derive(ProtoBuf, Default, Debug, Clone)]
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
#[derive(ProtoBuf, Default, Debug, Clone)]
pub struct NewDocUserPB {
#[pb(index = 1)]
pub user_id: String,
#[pb(index = 2)]
pub rev_id: i64,
#[pb(index = 3)]
pub doc_id: String,
}
#[derive(ProtoBuf, Default, Debug, Clone)]
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
}
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
impl std::convert::From<String> for TextBlockIdPB {
2022-02-25 22:27:44 +08:00
fn from(value: String) -> Self {
TextBlockIdPB { value }
2022-01-23 12:14:00 +08:00
}
2021-10-15 13:42:52 +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
}
}
impl std::convert::From<&String> for TextBlockIdPB {
2022-03-06 21:22:42 +08:00
fn from(s: &String) -> Self {
TextBlockIdPB { value: s.to_owned() }
}
}