mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: rename some function & add more documentation
This commit is contained in:
@ -215,7 +215,7 @@ impl DefaultFolderBuilder {
|
|||||||
for app in workspace_rev.apps.iter() {
|
for app in workspace_rev.apps.iter() {
|
||||||
for (index, view) in app.belongings.iter().enumerate() {
|
for (index, view) in app.belongings.iter().enumerate() {
|
||||||
let view_data = if index == 0 {
|
let view_data = if index == 0 {
|
||||||
initial_read_me().to_delta_str()
|
initial_read_me().to_json_str()
|
||||||
} else {
|
} else {
|
||||||
initial_quill_delta_string()
|
initial_quill_delta_string()
|
||||||
};
|
};
|
||||||
|
@ -10,7 +10,7 @@ use flowy_sync::{
|
|||||||
entities::{revision::Revision, ws_data::ServerRevisionWSData},
|
entities::{revision::Revision, ws_data::ServerRevisionWSData},
|
||||||
};
|
};
|
||||||
use lib_infra::future::FutureResult;
|
use lib_infra::future::FutureResult;
|
||||||
use lib_ot::core::PlainTextAttributes;
|
use lib_ot::core::PhantomAttributes;
|
||||||
|
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -80,7 +80,7 @@ impl FolderEditor {
|
|||||||
pub(crate) fn apply_change(&self, change: FolderChange) -> FlowyResult<()> {
|
pub(crate) fn apply_change(&self, change: FolderChange) -> FlowyResult<()> {
|
||||||
let FolderChange { delta, md5 } = change;
|
let FolderChange { delta, md5 } = change;
|
||||||
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
||||||
let delta_data = delta.to_delta_bytes();
|
let delta_data = delta.to_json_bytes();
|
||||||
let revision = Revision::new(
|
let revision = Revision::new(
|
||||||
&self.rev_manager.object_id,
|
&self.rev_manager.object_id,
|
||||||
base_rev_id,
|
base_rev_id,
|
||||||
@ -132,7 +132,7 @@ impl FolderEditor {
|
|||||||
pub struct FolderRevisionCompactor();
|
pub struct FolderRevisionCompactor();
|
||||||
impl RevisionCompactor for FolderRevisionCompactor {
|
impl RevisionCompactor for FolderRevisionCompactor {
|
||||||
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
||||||
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
|
let delta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
|
||||||
Ok(delta.to_delta_bytes())
|
Ok(delta.to_json_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@ impl FolderPersistence {
|
|||||||
pub async fn save_folder(&self, user_id: &str, folder_id: &FolderId, folder: FolderPad) -> FlowyResult<()> {
|
pub async fn save_folder(&self, user_id: &str, folder_id: &FolderId, folder: FolderPad) -> FlowyResult<()> {
|
||||||
let pool = self.database.db_pool()?;
|
let pool = self.database.db_pool()?;
|
||||||
let json = folder.to_json()?;
|
let json = folder.to_json()?;
|
||||||
let delta_data = PlainTextDeltaBuilder::new().insert(&json).build().to_delta_bytes();
|
let delta_data = PlainTextDeltaBuilder::new().insert(&json).build().to_json_bytes();
|
||||||
let revision = Revision::initial_revision(user_id, folder_id.as_ref(), delta_data);
|
let revision = Revision::initial_revision(user_id, folder_id.as_ref(), delta_data);
|
||||||
let record = RevisionRecord {
|
let record = RevisionRecord {
|
||||||
revision,
|
revision,
|
||||||
|
@ -10,7 +10,7 @@ use flowy_sync::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
use lib_infra::future::{BoxResultFuture, FutureResult};
|
use lib_infra::future::{BoxResultFuture, FutureResult};
|
||||||
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta};
|
use lib_ot::core::{OperationTransformable, PhantomAttributes, PlainTextDelta};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ pub(crate) async fn make_folder_ws_manager(
|
|||||||
) -> Arc<RevisionWebSocketManager> {
|
) -> Arc<RevisionWebSocketManager> {
|
||||||
let ws_data_provider = Arc::new(WSDataProvider::new(folder_id, Arc::new(rev_manager.clone())));
|
let ws_data_provider = Arc::new(WSDataProvider::new(folder_id, Arc::new(rev_manager.clone())));
|
||||||
let resolver = Arc::new(FolderConflictResolver { folder_pad });
|
let resolver = Arc::new(FolderConflictResolver { folder_pad });
|
||||||
let conflict_controller = ConflictController::<PlainTextAttributes>::new(
|
let conflict_controller = ConflictController::<PhantomAttributes>::new(
|
||||||
user_id,
|
user_id,
|
||||||
resolver,
|
resolver,
|
||||||
Arc::new(ws_data_provider.clone()),
|
Arc::new(ws_data_provider.clone()),
|
||||||
@ -55,7 +55,7 @@ struct FolderConflictResolver {
|
|||||||
folder_pad: Arc<RwLock<FolderPad>>,
|
folder_pad: Arc<RwLock<FolderPad>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConflictResolver<PlainTextAttributes> for FolderConflictResolver {
|
impl ConflictResolver<PhantomAttributes> for FolderConflictResolver {
|
||||||
fn compose_delta(&self, delta: PlainTextDelta) -> BoxResultFuture<DeltaMD5, FlowyError> {
|
fn compose_delta(&self, delta: PlainTextDelta) -> BoxResultFuture<DeltaMD5, FlowyError> {
|
||||||
let folder_pad = self.folder_pad.clone();
|
let folder_pad = self.folder_pad.clone();
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
@ -67,7 +67,7 @@ impl ConflictResolver<PlainTextAttributes> for FolderConflictResolver {
|
|||||||
fn transform_delta(
|
fn transform_delta(
|
||||||
&self,
|
&self,
|
||||||
delta: PlainTextDelta,
|
delta: PlainTextDelta,
|
||||||
) -> BoxResultFuture<TransformDeltas<PlainTextAttributes>, FlowyError> {
|
) -> BoxResultFuture<TransformDeltas<PhantomAttributes>, FlowyError> {
|
||||||
let folder_pad = self.folder_pad.clone();
|
let folder_pad = self.folder_pad.clone();
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let read_guard = folder_pad.read();
|
let read_guard = folder_pad.read();
|
||||||
|
@ -192,7 +192,7 @@ pub async fn make_grid_view_data(
|
|||||||
|
|
||||||
// Create grid's block
|
// Create grid's block
|
||||||
let grid_block_delta = make_grid_block_delta(block_meta_data);
|
let grid_block_delta = make_grid_block_delta(block_meta_data);
|
||||||
let block_delta_data = grid_block_delta.to_delta_bytes();
|
let block_delta_data = grid_block_delta.to_json_bytes();
|
||||||
let repeated_revision: RepeatedRevision =
|
let repeated_revision: RepeatedRevision =
|
||||||
Revision::initial_revision(user_id, block_id, block_delta_data).into();
|
Revision::initial_revision(user_id, block_id, block_delta_data).into();
|
||||||
let _ = grid_manager.create_grid_block(&block_id, repeated_revision).await?;
|
let _ = grid_manager.create_grid_block(&block_id, repeated_revision).await?;
|
||||||
@ -202,7 +202,7 @@ pub async fn make_grid_view_data(
|
|||||||
|
|
||||||
// Create grid
|
// Create grid
|
||||||
let grid_meta_delta = make_grid_delta(&grid_rev);
|
let grid_meta_delta = make_grid_delta(&grid_rev);
|
||||||
let grid_delta_data = grid_meta_delta.to_delta_bytes();
|
let grid_delta_data = grid_meta_delta.to_json_bytes();
|
||||||
let repeated_revision: RepeatedRevision =
|
let repeated_revision: RepeatedRevision =
|
||||||
Revision::initial_revision(user_id, view_id, grid_delta_data.clone()).into();
|
Revision::initial_revision(user_id, view_id, grid_delta_data.clone()).into();
|
||||||
let _ = grid_manager.create_grid(view_id, repeated_revision).await?;
|
let _ = grid_manager.create_grid(view_id, repeated_revision).await?;
|
||||||
|
@ -7,7 +7,7 @@ use flowy_sync::client_grid::{GridBlockMetaChange, GridBlockRevisionPad};
|
|||||||
use flowy_sync::entities::revision::Revision;
|
use flowy_sync::entities::revision::Revision;
|
||||||
use flowy_sync::util::make_delta_from_revisions;
|
use flowy_sync::util::make_delta_from_revisions;
|
||||||
use lib_infra::future::FutureResult;
|
use lib_infra::future::FutureResult;
|
||||||
use lib_ot::core::PlainTextAttributes;
|
use lib_ot::core::PhantomAttributes;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
@ -161,7 +161,7 @@ impl GridBlockRevisionEditor {
|
|||||||
let GridBlockMetaChange { delta, md5 } = change;
|
let GridBlockMetaChange { delta, md5 } = change;
|
||||||
let user_id = self.user_id.clone();
|
let user_id = self.user_id.clone();
|
||||||
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
||||||
let delta_data = delta.to_delta_bytes();
|
let delta_data = delta.to_json_bytes();
|
||||||
let revision = Revision::new(
|
let revision = Revision::new(
|
||||||
&self.rev_manager.object_id,
|
&self.rev_manager.object_id,
|
||||||
base_rev_id,
|
base_rev_id,
|
||||||
@ -200,7 +200,7 @@ impl RevisionObjectBuilder for GridBlockMetaPadBuilder {
|
|||||||
pub struct GridBlockRevisionCompactor();
|
pub struct GridBlockRevisionCompactor();
|
||||||
impl RevisionCompactor for GridBlockRevisionCompactor {
|
impl RevisionCompactor for GridBlockRevisionCompactor {
|
||||||
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
||||||
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
|
let delta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
|
||||||
Ok(delta.to_delta_bytes())
|
Ok(delta.to_json_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ use flowy_sync::entities::revision::Revision;
|
|||||||
use flowy_sync::errors::CollaborateResult;
|
use flowy_sync::errors::CollaborateResult;
|
||||||
use flowy_sync::util::make_delta_from_revisions;
|
use flowy_sync::util::make_delta_from_revisions;
|
||||||
use lib_infra::future::FutureResult;
|
use lib_infra::future::FutureResult;
|
||||||
use lib_ot::core::PlainTextAttributes;
|
use lib_ot::core::PhantomAttributes;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
@ -573,7 +573,7 @@ impl GridRevisionEditor {
|
|||||||
let GridChangeset { delta, md5 } = change;
|
let GridChangeset { delta, md5 } = change;
|
||||||
let user_id = self.user.user_id()?;
|
let user_id = self.user.user_id()?;
|
||||||
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
||||||
let delta_data = delta.to_delta_bytes();
|
let delta_data = delta.to_json_bytes();
|
||||||
let revision = Revision::new(
|
let revision = Revision::new(
|
||||||
&self.rev_manager.object_id,
|
&self.rev_manager.object_id,
|
||||||
base_rev_id,
|
base_rev_id,
|
||||||
@ -664,8 +664,8 @@ impl RevisionCloudService for GridRevisionCloudService {
|
|||||||
pub struct GridRevisionCompactor();
|
pub struct GridRevisionCompactor();
|
||||||
impl RevisionCompactor for GridRevisionCompactor {
|
impl RevisionCompactor for GridRevisionCompactor {
|
||||||
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
||||||
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
|
let delta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
|
||||||
Ok(delta.to_delta_bytes())
|
Ok(delta.to_json_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ impl GridMigration {
|
|||||||
let pool = self.database.db_pool()?;
|
let pool = self.database.db_pool()?;
|
||||||
let grid_rev_pad = self.get_grid_revision_pad(grid_id).await?;
|
let grid_rev_pad = self.get_grid_revision_pad(grid_id).await?;
|
||||||
let json = grid_rev_pad.json_str()?;
|
let json = grid_rev_pad.json_str()?;
|
||||||
let delta_data = PlainTextDeltaBuilder::new().insert(&json).build().to_delta_bytes();
|
let delta_data = PlainTextDeltaBuilder::new().insert(&json).build().to_json_bytes();
|
||||||
let revision = Revision::initial_revision(&user_id, grid_id, delta_data);
|
let revision = Revision::initial_revision(&user_id, grid_id, delta_data);
|
||||||
let record = RevisionRecord::new(revision);
|
let record = RevisionRecord::new(revision);
|
||||||
//
|
//
|
||||||
|
@ -9,7 +9,7 @@ use flowy_sync::{
|
|||||||
util::make_delta_from_revisions,
|
util::make_delta_from_revisions,
|
||||||
};
|
};
|
||||||
use lib_infra::future::BoxResultFuture;
|
use lib_infra::future::BoxResultFuture;
|
||||||
use lib_ot::core::{Attributes, Delta, PlainTextAttributes};
|
use lib_ot::core::{Attributes, Delta, PhantomAttributes};
|
||||||
use lib_ot::rich_text::RichTextAttributes;
|
use lib_ot::rich_text::RichTextAttributes;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::{convert::TryFrom, sync::Arc};
|
use std::{convert::TryFrom, sync::Arc};
|
||||||
@ -31,7 +31,7 @@ pub trait ConflictRevisionSink: Send + Sync + 'static {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub type RichTextConflictController = ConflictController<RichTextAttributes>;
|
pub type RichTextConflictController = ConflictController<RichTextAttributes>;
|
||||||
pub type PlainTextConflictController = ConflictController<PlainTextAttributes>;
|
pub type PlainTextConflictController = ConflictController<PhantomAttributes>;
|
||||||
|
|
||||||
pub struct ConflictController<T>
|
pub struct ConflictController<T>
|
||||||
where
|
where
|
||||||
@ -154,7 +154,7 @@ where
|
|||||||
&rev_manager.object_id,
|
&rev_manager.object_id,
|
||||||
base_rev_id,
|
base_rev_id,
|
||||||
rev_id,
|
rev_id,
|
||||||
client_delta.to_delta_bytes(),
|
client_delta.to_json_bytes(),
|
||||||
user_id,
|
user_id,
|
||||||
md5.clone(),
|
md5.clone(),
|
||||||
);
|
);
|
||||||
@ -166,7 +166,7 @@ where
|
|||||||
&rev_manager.object_id,
|
&rev_manager.object_id,
|
||||||
base_rev_id,
|
base_rev_id,
|
||||||
rev_id,
|
rev_id,
|
||||||
server_delta.to_delta_bytes(),
|
server_delta.to_json_bytes(),
|
||||||
user_id,
|
user_id,
|
||||||
md5,
|
md5,
|
||||||
);
|
);
|
||||||
|
@ -238,7 +238,7 @@ impl RevisionObjectBuilder for TextBlockInfoBuilder {
|
|||||||
|
|
||||||
Result::<DocumentPB, FlowyError>::Ok(DocumentPB {
|
Result::<DocumentPB, FlowyError>::Ok(DocumentPB {
|
||||||
block_id: object_id.to_owned(),
|
block_id: object_id.to_owned(),
|
||||||
text: delta.to_delta_str(),
|
text: delta.to_json_str(),
|
||||||
rev_id,
|
rev_id,
|
||||||
base_rev_id,
|
base_rev_id,
|
||||||
})
|
})
|
||||||
|
@ -175,7 +175,7 @@ impl EditBlockQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn save_local_delta(&self, delta: RichTextDelta, md5: String) -> Result<RevId, FlowyError> {
|
async fn save_local_delta(&self, delta: RichTextDelta, md5: String) -> Result<RevId, FlowyError> {
|
||||||
let delta_data = delta.to_delta_bytes();
|
let delta_data = delta.to_json_bytes();
|
||||||
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
||||||
let user_id = self.user.user_id()?;
|
let user_id = self.user.user_id()?;
|
||||||
let revision = Revision::new(
|
let revision = Revision::new(
|
||||||
@ -195,7 +195,7 @@ pub(crate) struct TextBlockRevisionCompactor();
|
|||||||
impl RevisionCompactor for TextBlockRevisionCompactor {
|
impl RevisionCompactor for TextBlockRevisionCompactor {
|
||||||
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
||||||
let delta = make_delta_from_revisions::<RichTextAttributes>(revisions)?;
|
let delta = make_delta_from_revisions::<RichTextAttributes>(revisions)?;
|
||||||
Ok(delta.to_delta_bytes())
|
Ok(delta.to_json_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ impl TextBlockEditorTest {
|
|||||||
let delta = self.editor.text_block_delta().await.unwrap();
|
let delta = self.editor.text_block_delta().await.unwrap();
|
||||||
if expected_delta != delta {
|
if expected_delta != delta {
|
||||||
eprintln!("✅ expect: {}", expected,);
|
eprintln!("✅ expect: {}", expected,);
|
||||||
eprintln!("❌ receive: {}", delta.to_delta_str());
|
eprintln!("❌ receive: {}", delta.to_json_str());
|
||||||
}
|
}
|
||||||
assert_eq!(expected_delta, delta);
|
assert_eq!(expected_delta, delta);
|
||||||
}
|
}
|
||||||
|
@ -762,19 +762,19 @@ fn attributes_preserve_list_format_on_merge() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn delta_compose() {
|
fn delta_compose() {
|
||||||
let mut delta = RichTextDelta::from_delta_str(r#"[{"insert":"\n"}]"#).unwrap();
|
let mut delta = RichTextDelta::from_json_str(r#"[{"insert":"\n"}]"#).unwrap();
|
||||||
let deltas = vec![
|
let deltas = vec![
|
||||||
RichTextDelta::from_delta_str(r#"[{"retain":1,"attributes":{"list":"unchecked"}}]"#).unwrap(),
|
RichTextDelta::from_json_str(r#"[{"retain":1,"attributes":{"list":"unchecked"}}]"#).unwrap(),
|
||||||
RichTextDelta::from_delta_str(r#"[{"insert":"a"}]"#).unwrap(),
|
RichTextDelta::from_json_str(r#"[{"insert":"a"}]"#).unwrap(),
|
||||||
RichTextDelta::from_delta_str(r#"[{"retain":1},{"insert":"\n","attributes":{"list":"unchecked"}}]"#).unwrap(),
|
RichTextDelta::from_json_str(r#"[{"retain":1},{"insert":"\n","attributes":{"list":"unchecked"}}]"#).unwrap(),
|
||||||
RichTextDelta::from_delta_str(r#"[{"retain":2},{"retain":1,"attributes":{"list":""}}]"#).unwrap(),
|
RichTextDelta::from_json_str(r#"[{"retain":2},{"retain":1,"attributes":{"list":""}}]"#).unwrap(),
|
||||||
];
|
];
|
||||||
|
|
||||||
for d in deltas {
|
for d in deltas {
|
||||||
delta = delta.compose(&d).unwrap();
|
delta = delta.compose(&d).unwrap();
|
||||||
}
|
}
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
delta.to_delta_str(),
|
delta.to_json_str(),
|
||||||
r#"[{"insert":"a"},{"insert":"\n","attributes":{"list":"unchecked"}},{"insert":"\n"}]"#
|
r#"[{"insert":"a"},{"insert":"\n","attributes":{"list":"unchecked"}},{"insert":"\n"}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -108,20 +108,20 @@ impl TestBuilder {
|
|||||||
TestOp::Insert(delta_i, s, index) => {
|
TestOp::Insert(delta_i, s, index) => {
|
||||||
let document = &mut self.documents[*delta_i];
|
let document = &mut self.documents[*delta_i];
|
||||||
let delta = document.insert(*index, s).unwrap();
|
let delta = document.insert(*index, s).unwrap();
|
||||||
tracing::debug!("Insert delta: {}", delta.to_delta_str());
|
tracing::debug!("Insert delta: {}", delta.to_json_str());
|
||||||
|
|
||||||
self.deltas.insert(*delta_i, Some(delta));
|
self.deltas.insert(*delta_i, Some(delta));
|
||||||
}
|
}
|
||||||
TestOp::Delete(delta_i, iv) => {
|
TestOp::Delete(delta_i, iv) => {
|
||||||
let document = &mut self.documents[*delta_i];
|
let document = &mut self.documents[*delta_i];
|
||||||
let delta = document.replace(*iv, "").unwrap();
|
let delta = document.replace(*iv, "").unwrap();
|
||||||
tracing::trace!("Delete delta: {}", delta.to_delta_str());
|
tracing::trace!("Delete delta: {}", delta.to_json_str());
|
||||||
self.deltas.insert(*delta_i, Some(delta));
|
self.deltas.insert(*delta_i, Some(delta));
|
||||||
}
|
}
|
||||||
TestOp::Replace(delta_i, iv, s) => {
|
TestOp::Replace(delta_i, iv, s) => {
|
||||||
let document = &mut self.documents[*delta_i];
|
let document = &mut self.documents[*delta_i];
|
||||||
let delta = document.replace(*iv, s).unwrap();
|
let delta = document.replace(*iv, s).unwrap();
|
||||||
tracing::trace!("Replace delta: {}", delta.to_delta_str());
|
tracing::trace!("Replace delta: {}", delta.to_json_str());
|
||||||
self.deltas.insert(*delta_i, Some(delta));
|
self.deltas.insert(*delta_i, Some(delta));
|
||||||
}
|
}
|
||||||
TestOp::InsertBold(delta_i, s, iv) => {
|
TestOp::InsertBold(delta_i, s, iv) => {
|
||||||
@ -133,7 +133,7 @@ impl TestBuilder {
|
|||||||
let document = &mut self.documents[*delta_i];
|
let document = &mut self.documents[*delta_i];
|
||||||
let attribute = RichTextAttribute::Bold(*enable);
|
let attribute = RichTextAttribute::Bold(*enable);
|
||||||
let delta = document.format(*iv, attribute).unwrap();
|
let delta = document.format(*iv, attribute).unwrap();
|
||||||
tracing::trace!("Bold delta: {}", delta.to_delta_str());
|
tracing::trace!("Bold delta: {}", delta.to_json_str());
|
||||||
self.deltas.insert(*delta_i, Some(delta));
|
self.deltas.insert(*delta_i, Some(delta));
|
||||||
}
|
}
|
||||||
TestOp::Italic(delta_i, iv, enable) => {
|
TestOp::Italic(delta_i, iv, enable) => {
|
||||||
@ -143,28 +143,28 @@ impl TestBuilder {
|
|||||||
false => RichTextAttribute::Italic(false),
|
false => RichTextAttribute::Italic(false),
|
||||||
};
|
};
|
||||||
let delta = document.format(*iv, attribute).unwrap();
|
let delta = document.format(*iv, attribute).unwrap();
|
||||||
tracing::trace!("Italic delta: {}", delta.to_delta_str());
|
tracing::trace!("Italic delta: {}", delta.to_json_str());
|
||||||
self.deltas.insert(*delta_i, Some(delta));
|
self.deltas.insert(*delta_i, Some(delta));
|
||||||
}
|
}
|
||||||
TestOp::Header(delta_i, iv, level) => {
|
TestOp::Header(delta_i, iv, level) => {
|
||||||
let document = &mut self.documents[*delta_i];
|
let document = &mut self.documents[*delta_i];
|
||||||
let attribute = RichTextAttribute::Header(*level);
|
let attribute = RichTextAttribute::Header(*level);
|
||||||
let delta = document.format(*iv, attribute).unwrap();
|
let delta = document.format(*iv, attribute).unwrap();
|
||||||
tracing::trace!("Header delta: {}", delta.to_delta_str());
|
tracing::trace!("Header delta: {}", delta.to_json_str());
|
||||||
self.deltas.insert(*delta_i, Some(delta));
|
self.deltas.insert(*delta_i, Some(delta));
|
||||||
}
|
}
|
||||||
TestOp::Link(delta_i, iv, link) => {
|
TestOp::Link(delta_i, iv, link) => {
|
||||||
let document = &mut self.documents[*delta_i];
|
let document = &mut self.documents[*delta_i];
|
||||||
let attribute = RichTextAttribute::Link(link.to_owned());
|
let attribute = RichTextAttribute::Link(link.to_owned());
|
||||||
let delta = document.format(*iv, attribute).unwrap();
|
let delta = document.format(*iv, attribute).unwrap();
|
||||||
tracing::trace!("Link delta: {}", delta.to_delta_str());
|
tracing::trace!("Link delta: {}", delta.to_json_str());
|
||||||
self.deltas.insert(*delta_i, Some(delta));
|
self.deltas.insert(*delta_i, Some(delta));
|
||||||
}
|
}
|
||||||
TestOp::Bullet(delta_i, iv, enable) => {
|
TestOp::Bullet(delta_i, iv, enable) => {
|
||||||
let document = &mut self.documents[*delta_i];
|
let document = &mut self.documents[*delta_i];
|
||||||
let attribute = RichTextAttribute::Bullet(*enable);
|
let attribute = RichTextAttribute::Bullet(*enable);
|
||||||
let delta = document.format(*iv, attribute).unwrap();
|
let delta = document.format(*iv, attribute).unwrap();
|
||||||
tracing::debug!("Bullet delta: {}", delta.to_delta_str());
|
tracing::debug!("Bullet delta: {}", delta.to_json_str());
|
||||||
|
|
||||||
self.deltas.insert(*delta_i, Some(delta));
|
self.deltas.insert(*delta_i, Some(delta));
|
||||||
}
|
}
|
||||||
@ -194,15 +194,15 @@ impl TestBuilder {
|
|||||||
let delta_a = &self.documents[*delta_a_i].delta();
|
let delta_a = &self.documents[*delta_a_i].delta();
|
||||||
let delta_b = &self.documents[*delta_b_i].delta();
|
let delta_b = &self.documents[*delta_b_i].delta();
|
||||||
tracing::debug!("Invert: ");
|
tracing::debug!("Invert: ");
|
||||||
tracing::debug!("a: {}", delta_a.to_delta_str());
|
tracing::debug!("a: {}", delta_a.to_json_str());
|
||||||
tracing::debug!("b: {}", delta_b.to_delta_str());
|
tracing::debug!("b: {}", delta_b.to_json_str());
|
||||||
|
|
||||||
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
|
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
|
||||||
let undo = b_prime.invert(delta_a);
|
let undo = b_prime.invert(delta_a);
|
||||||
|
|
||||||
let new_delta = delta_a.compose(&b_prime).unwrap();
|
let new_delta = delta_a.compose(&b_prime).unwrap();
|
||||||
tracing::debug!("new delta: {}", new_delta.to_delta_str());
|
tracing::debug!("new delta: {}", new_delta.to_json_str());
|
||||||
tracing::debug!("undo delta: {}", undo.to_delta_str());
|
tracing::debug!("undo delta: {}", undo.to_json_str());
|
||||||
|
|
||||||
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
|
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
|
||||||
|
|
||||||
@ -238,7 +238,7 @@ impl TestBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TestOp::AssertPrimeJson(doc_i, expected) => {
|
TestOp::AssertPrimeJson(doc_i, expected) => {
|
||||||
let prime_json = self.primes[*doc_i].as_ref().unwrap().to_delta_str();
|
let prime_json = self.primes[*doc_i].as_ref().unwrap().to_json_str();
|
||||||
let expected_prime: RichTextDelta = serde_json::from_str(expected).unwrap();
|
let expected_prime: RichTextDelta = serde_json::from_str(expected).unwrap();
|
||||||
let target_prime: RichTextDelta = serde_json::from_str(&prime_json).unwrap();
|
let target_prime: RichTextDelta = serde_json::from_str(&prime_json).unwrap();
|
||||||
|
|
||||||
|
@ -378,7 +378,7 @@ fn apply_test() {
|
|||||||
.insert(", AppFlowy")
|
.insert(", AppFlowy")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let after_a = delta_a.to_str().unwrap();
|
let after_a = delta_a.content_str().unwrap();
|
||||||
let after_b = delta_b.apply(&after_a).unwrap();
|
let after_b = delta_b.apply(&after_a).unwrap();
|
||||||
assert_eq!("hello, AppFlowy", &after_b);
|
assert_eq!("hello, AppFlowy", &after_b);
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@ fn delta_serialize_multi_attribute_test() {
|
|||||||
let json = serde_json::to_string(&delta).unwrap();
|
let json = serde_json::to_string(&delta).unwrap();
|
||||||
eprintln!("{}", json);
|
eprintln!("{}", json);
|
||||||
|
|
||||||
let delta_from_json = Delta::from_delta_str(&json).unwrap();
|
let delta_from_json = Delta::from_json_str(&json).unwrap();
|
||||||
assert_eq!(delta_from_json, delta);
|
assert_eq!(delta_from_json, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ fn delta_deserialize_test() {
|
|||||||
{"retain":2,"attributes":{"italic":"true","bold":"true"}},
|
{"retain":2,"attributes":{"italic":"true","bold":"true"}},
|
||||||
{"retain":2,"attributes":{"italic":true,"bold":true}}
|
{"retain":2,"attributes":{"italic":true,"bold":true}}
|
||||||
]"#;
|
]"#;
|
||||||
let delta = RichTextDelta::from_delta_str(json).unwrap();
|
let delta = RichTextDelta::from_json_str(json).unwrap();
|
||||||
eprintln!("{}", delta);
|
eprintln!("{}", delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,13 +86,13 @@ fn delta_deserialize_null_test() {
|
|||||||
let json = r#"[
|
let json = r#"[
|
||||||
{"retain":7,"attributes":{"bold":null}}
|
{"retain":7,"attributes":{"bold":null}}
|
||||||
]"#;
|
]"#;
|
||||||
let delta1 = RichTextDelta::from_delta_str(json).unwrap();
|
let delta1 = RichTextDelta::from_json_str(json).unwrap();
|
||||||
|
|
||||||
let mut attribute = RichTextAttribute::Bold(true);
|
let mut attribute = RichTextAttribute::Bold(true);
|
||||||
attribute.value = RichTextAttributeValue(None);
|
attribute.value = RichTextAttributeValue(None);
|
||||||
let delta2 = DeltaBuilder::new().retain_with_attributes(7, attribute.into()).build();
|
let delta2 = DeltaBuilder::new().retain_with_attributes(7, attribute.into()).build();
|
||||||
|
|
||||||
assert_eq!(delta2.to_delta_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#);
|
assert_eq!(delta2.to_json_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#);
|
||||||
assert_eq!(delta1, delta2);
|
assert_eq!(delta1, delta2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,13 +7,13 @@ pub fn initial_quill_delta() -> RichTextDelta {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn initial_quill_delta_string() -> String {
|
pub fn initial_quill_delta_string() -> String {
|
||||||
initial_quill_delta().to_delta_str()
|
initial_quill_delta().to_json_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn initial_read_me() -> RichTextDelta {
|
pub fn initial_read_me() -> RichTextDelta {
|
||||||
let json = include_str!("READ_ME.json");
|
let json = include_str!("READ_ME.json");
|
||||||
RichTextDelta::from_delta_str(json).unwrap()
|
RichTextDelta::from_json_str(json).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -22,6 +22,6 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_read_me() {
|
fn load_read_me() {
|
||||||
println!("{}", initial_read_me().to_delta_str());
|
println!("{}", initial_read_me().to_json_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,16 +55,16 @@ impl ClientDocument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_json(json: &str) -> Result<Self, CollaborateError> {
|
pub fn from_json(json: &str) -> Result<Self, CollaborateError> {
|
||||||
let delta = RichTextDelta::from_delta_str(json)?;
|
let delta = RichTextDelta::from_json_str(json)?;
|
||||||
Ok(Self::from_delta(delta))
|
Ok(Self::from_delta(delta))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delta_str(&self) -> String {
|
pub fn delta_str(&self) -> String {
|
||||||
self.delta.to_delta_str()
|
self.delta.to_json_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_bytes(&self) -> Bytes {
|
pub fn to_bytes(&self) -> Bytes {
|
||||||
self.delta.to_delta_bytes()
|
self.delta.to_json_bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_plain_string(&self) -> String {
|
pub fn to_plain_string(&self) -> String {
|
||||||
@ -85,7 +85,7 @@ impl ClientDocument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_delta(&mut self, data: RichTextDelta) {
|
pub fn set_delta(&mut self, data: RichTextDelta) {
|
||||||
tracing::trace!("document: {}", data.to_delta_str());
|
tracing::trace!("document: {}", data.to_json_str());
|
||||||
self.delta = data;
|
self.delta = data;
|
||||||
|
|
||||||
match &self.notify {
|
match &self.notify {
|
||||||
@ -97,7 +97,7 @@ impl ClientDocument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn compose_delta(&mut self, delta: RichTextDelta) -> Result<(), CollaborateError> {
|
pub fn compose_delta(&mut self, delta: RichTextDelta) -> Result<(), CollaborateError> {
|
||||||
tracing::trace!("{} compose {}", &self.delta.to_delta_str(), delta.to_delta_str());
|
tracing::trace!("{} compose {}", &self.delta.to_json_str(), delta.to_json_str());
|
||||||
let composed_delta = self.delta.compose(&delta)?;
|
let composed_delta = self.delta.compose(&delta)?;
|
||||||
let mut undo_delta = delta.invert(&self.delta);
|
let mut undo_delta = delta.invert(&self.delta);
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use flowy_folder_data_model::revision::{TrashRevision, WorkspaceRevision};
|
use flowy_folder_data_model::revision::{TrashRevision, WorkspaceRevision};
|
||||||
use lib_ot::core::{PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
|
use lib_ot::core::{PhantomAttributes, PlainTextDelta, PlainTextDeltaBuilder};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@ -41,9 +41,9 @@ impl FolderPadBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Reconvert from history if delta.to_str() failed.
|
// TODO: Reconvert from history if delta.to_str() failed.
|
||||||
let folder_json = delta.to_str()?;
|
let content = delta.content_str()?;
|
||||||
let mut folder: FolderPad = serde_json::from_str(&folder_json).map_err(|e| {
|
let mut folder: FolderPad = serde_json::from_str(&content).map_err(|e| {
|
||||||
tracing::error!("Deserialize folder from json failed: {}", folder_json);
|
tracing::error!("Deserialize folder from {} failed", content);
|
||||||
return CollaborateError::internal().context(format!("Deserialize delta to folder failed: {}", e));
|
return CollaborateError::internal().context(format!("Deserialize delta to folder failed: {}", e));
|
||||||
})?;
|
})?;
|
||||||
folder.delta = delta;
|
folder.delta = delta;
|
||||||
@ -51,7 +51,7 @@ impl FolderPadBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn build_with_revisions(self, revisions: Vec<Revision>) -> CollaborateResult<FolderPad> {
|
pub(crate) fn build_with_revisions(self, revisions: Vec<Revision>) -> CollaborateResult<FolderPad> {
|
||||||
let folder_delta: FolderDelta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
|
let folder_delta: FolderDelta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
|
||||||
self.build_with_delta(folder_delta)
|
self.build_with_delta(folder_delta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,7 +295,7 @@ impl FolderPad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn md5(&self) -> String {
|
pub fn md5(&self) -> String {
|
||||||
md5(&self.delta.to_delta_bytes())
|
md5(&self.delta.to_json_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_json(&self) -> CollaborateResult<String> {
|
pub fn to_json(&self) -> CollaborateResult<String> {
|
||||||
@ -315,7 +315,7 @@ impl FolderPad {
|
|||||||
Some(_) => {
|
Some(_) => {
|
||||||
let old = cloned_self.to_json()?;
|
let old = cloned_self.to_json()?;
|
||||||
let new = self.to_json()?;
|
let new = self.to_json()?;
|
||||||
match cal_diff::<PlainTextAttributes>(old, new) {
|
match cal_diff::<PhantomAttributes>(old, new) {
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
Some(delta) => {
|
Some(delta) => {
|
||||||
self.delta = self.delta.compose(&delta)?;
|
self.delta = self.delta.compose(&delta)?;
|
||||||
@ -350,7 +350,7 @@ impl FolderPad {
|
|||||||
Some(_) => {
|
Some(_) => {
|
||||||
let old = cloned_self.to_json()?;
|
let old = cloned_self.to_json()?;
|
||||||
let new = self.to_json()?;
|
let new = self.to_json()?;
|
||||||
match cal_diff::<PlainTextAttributes>(old, new) {
|
match cal_diff::<PhantomAttributes>(old, new) {
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
Some(delta) => {
|
Some(delta) => {
|
||||||
self.delta = self.delta.compose(&delta)?;
|
self.delta = self.delta.compose(&delta)?;
|
||||||
|
@ -4,7 +4,7 @@ use crate::util::{cal_diff, make_delta_from_revisions};
|
|||||||
use flowy_grid_data_model::revision::{
|
use flowy_grid_data_model::revision::{
|
||||||
gen_block_id, gen_row_id, CellRevision, GridBlockRevision, RowMetaChangeset, RowRevision,
|
gen_block_id, gen_row_id, CellRevision, GridBlockRevision, RowMetaChangeset, RowRevision,
|
||||||
};
|
};
|
||||||
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
|
use lib_ot::core::{OperationTransformable, PhantomAttributes, PlainTextDelta, PlainTextDeltaBuilder};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -46,7 +46,7 @@ impl GridBlockRevisionPad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_delta(delta: GridBlockRevisionDelta) -> CollaborateResult<Self> {
|
pub fn from_delta(delta: GridBlockRevisionDelta) -> CollaborateResult<Self> {
|
||||||
let s = delta.to_str()?;
|
let s = delta.content_str()?;
|
||||||
let block_revision: GridBlockRevision = serde_json::from_str(&s).map_err(|e| {
|
let block_revision: GridBlockRevision = serde_json::from_str(&s).map_err(|e| {
|
||||||
let msg = format!("Deserialize delta to block meta failed: {}", e);
|
let msg = format!("Deserialize delta to block meta failed: {}", e);
|
||||||
tracing::error!("{}", s);
|
tracing::error!("{}", s);
|
||||||
@ -56,7 +56,7 @@ impl GridBlockRevisionPad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_revisions(_grid_id: &str, revisions: Vec<Revision>) -> CollaborateResult<Self> {
|
pub fn from_revisions(_grid_id: &str, revisions: Vec<Revision>) -> CollaborateResult<Self> {
|
||||||
let block_delta: GridBlockRevisionDelta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
|
let block_delta: GridBlockRevisionDelta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
|
||||||
Self::from_delta(block_delta)
|
Self::from_delta(block_delta)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,10 +195,10 @@ impl GridBlockRevisionPad {
|
|||||||
Some(_) => {
|
Some(_) => {
|
||||||
let old = cloned_self.to_json()?;
|
let old = cloned_self.to_json()?;
|
||||||
let new = self.to_json()?;
|
let new = self.to_json()?;
|
||||||
match cal_diff::<PlainTextAttributes>(old, new) {
|
match cal_diff::<PhantomAttributes>(old, new) {
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
Some(delta) => {
|
Some(delta) => {
|
||||||
tracing::trace!("[GridBlockMeta] Composing delta {}", delta.to_delta_str());
|
tracing::trace!("[GridBlockMeta] Composing delta {}", delta.to_json_str());
|
||||||
// tracing::debug!(
|
// tracing::debug!(
|
||||||
// "[GridBlockMeta] current delta: {}",
|
// "[GridBlockMeta] current delta: {}",
|
||||||
// self.delta.to_str().unwrap_or_else(|_| "".to_string())
|
// self.delta.to_str().unwrap_or_else(|_| "".to_string())
|
||||||
@ -231,11 +231,11 @@ impl GridBlockRevisionPad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn md5(&self) -> String {
|
pub fn md5(&self) -> String {
|
||||||
md5(&self.delta.to_delta_bytes())
|
md5(&self.delta.to_json_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delta_str(&self) -> String {
|
pub fn delta_str(&self) -> String {
|
||||||
self.delta.to_delta_str()
|
self.delta.to_json_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,7 +252,7 @@ pub fn make_grid_block_delta(block_rev: &GridBlockRevision) -> GridBlockRevision
|
|||||||
|
|
||||||
pub fn make_grid_block_revisions(user_id: &str, grid_block_meta_data: &GridBlockRevision) -> RepeatedRevision {
|
pub fn make_grid_block_revisions(user_id: &str, grid_block_meta_data: &GridBlockRevision) -> RepeatedRevision {
|
||||||
let delta = make_grid_block_delta(grid_block_meta_data);
|
let delta = make_grid_block_delta(grid_block_meta_data);
|
||||||
let bytes = delta.to_delta_bytes();
|
let bytes = delta.to_json_bytes();
|
||||||
let revision = Revision::initial_revision(user_id, &grid_block_meta_data.block_id, bytes);
|
let revision = Revision::initial_revision(user_id, &grid_block_meta_data.block_id, bytes);
|
||||||
revision.into()
|
revision.into()
|
||||||
}
|
}
|
||||||
@ -289,7 +289,7 @@ mod tests {
|
|||||||
let change = pad.add_row_rev(row.clone(), None).unwrap().unwrap();
|
let change = pad.add_row_rev(row.clone(), None).unwrap().unwrap();
|
||||||
assert_eq!(pad.rows.first().unwrap().as_ref(), &row);
|
assert_eq!(pad.rows.first().unwrap().as_ref(), &row);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
change.delta.to_delta_str(),
|
change.delta.to_json_str(),
|
||||||
r#"[{"retain":24},{"insert":"{\"id\":\"1\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
r#"[{"retain":24},{"insert":"{\"id\":\"1\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -303,19 +303,19 @@ mod tests {
|
|||||||
|
|
||||||
let change = pad.add_row_rev(row_1.clone(), None).unwrap().unwrap();
|
let change = pad.add_row_rev(row_1.clone(), None).unwrap().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
change.delta.to_delta_str(),
|
change.delta.to_json_str(),
|
||||||
r#"[{"retain":24},{"insert":"{\"id\":\"1\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
r#"[{"retain":24},{"insert":"{\"id\":\"1\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
let change = pad.add_row_rev(row_2.clone(), None).unwrap().unwrap();
|
let change = pad.add_row_rev(row_2.clone(), None).unwrap().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
change.delta.to_delta_str(),
|
change.delta.to_json_str(),
|
||||||
r#"[{"retain":90},{"insert":",{\"id\":\"2\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
r#"[{"retain":90},{"insert":",{\"id\":\"2\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
let change = pad.add_row_rev(row_3.clone(), Some("2".to_string())).unwrap().unwrap();
|
let change = pad.add_row_rev(row_3.clone(), Some("2".to_string())).unwrap().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
change.delta.to_delta_str(),
|
change.delta.to_json_str(),
|
||||||
r#"[{"retain":157},{"insert":",{\"id\":\"3\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
r#"[{"retain":157},{"insert":",{\"id\":\"3\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -381,7 +381,7 @@ mod tests {
|
|||||||
let _ = pad.add_row_rev(row.clone(), None).unwrap().unwrap();
|
let _ = pad.add_row_rev(row.clone(), None).unwrap().unwrap();
|
||||||
let change = pad.delete_rows(vec![Cow::Borrowed(&row.id)]).unwrap().unwrap();
|
let change = pad.delete_rows(vec![Cow::Borrowed(&row.id)]).unwrap().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
change.delta.to_delta_str(),
|
change.delta.to_json_str(),
|
||||||
r#"[{"retain":24},{"delete":66},{"retain":2}]"#
|
r#"[{"retain":24},{"delete":66},{"retain":2}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -410,7 +410,7 @@ mod tests {
|
|||||||
let change = pad.update_row(changeset).unwrap().unwrap();
|
let change = pad.update_row(changeset).unwrap().unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
change.delta.to_delta_str(),
|
change.delta.to_json_str(),
|
||||||
r#"[{"retain":69},{"insert":"10"},{"retain":15},{"insert":"tru"},{"delete":4},{"retain":4}]"#
|
r#"[{"retain":69},{"insert":"10"},{"retain":15},{"insert":"tru"},{"delete":4},{"retain":4}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -422,7 +422,7 @@ mod tests {
|
|||||||
|
|
||||||
fn test_pad() -> GridBlockRevisionPad {
|
fn test_pad() -> GridBlockRevisionPad {
|
||||||
let delta =
|
let delta =
|
||||||
GridBlockRevisionDelta::from_delta_str(r#"[{"insert":"{\"block_id\":\"1\",\"rows\":[]}"}]"#).unwrap();
|
GridBlockRevisionDelta::from_json_str(r#"[{"insert":"{\"block_id\":\"1\",\"rows\":[]}"}]"#).unwrap();
|
||||||
GridBlockRevisionPad::from_delta(delta).unwrap()
|
GridBlockRevisionPad::from_delta(delta).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ use flowy_grid_data_model::revision::{
|
|||||||
GridLayoutRevision, GridRevision, GridSettingRevision, GridSortRevision,
|
GridLayoutRevision, GridRevision, GridSettingRevision, GridSortRevision,
|
||||||
};
|
};
|
||||||
use lib_infra::util::move_vec_element;
|
use lib_infra::util::move_vec_element;
|
||||||
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
|
use lib_ot::core::{OperationTransformable, PhantomAttributes, PlainTextDelta, PlainTextDeltaBuilder};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@ -52,8 +52,8 @@ impl GridRevisionPad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_delta(delta: GridRevisionDelta) -> CollaborateResult<Self> {
|
pub fn from_delta(delta: GridRevisionDelta) -> CollaborateResult<Self> {
|
||||||
let s = delta.to_str()?;
|
let content = delta.content_str()?;
|
||||||
let grid: GridRevision = serde_json::from_str(&s)
|
let grid: GridRevision = serde_json::from_str(&content)
|
||||||
.map_err(|e| CollaborateError::internal().context(format!("Deserialize delta to grid failed: {}", e)))?;
|
.map_err(|e| CollaborateError::internal().context(format!("Deserialize delta to grid failed: {}", e)))?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@ -63,7 +63,7 @@ impl GridRevisionPad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_revisions(revisions: Vec<Revision>) -> CollaborateResult<Self> {
|
pub fn from_revisions(revisions: Vec<Revision>) -> CollaborateResult<Self> {
|
||||||
let grid_delta: GridRevisionDelta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
|
let grid_delta: GridRevisionDelta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
|
||||||
Self::from_delta(grid_delta)
|
Self::from_delta(grid_delta)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -457,15 +457,15 @@ impl GridRevisionPad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn md5(&self) -> String {
|
pub fn md5(&self) -> String {
|
||||||
md5(&self.delta.to_delta_bytes())
|
md5(&self.delta.to_json_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delta_str(&self) -> String {
|
pub fn delta_str(&self) -> String {
|
||||||
self.delta.to_delta_str()
|
self.delta.to_json_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delta_bytes(&self) -> Bytes {
|
pub fn delta_bytes(&self) -> Bytes {
|
||||||
self.delta.to_delta_bytes()
|
self.delta.to_json_bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fields(&self) -> &[Arc<FieldRevision>] {
|
pub fn fields(&self) -> &[Arc<FieldRevision>] {
|
||||||
@ -482,7 +482,7 @@ impl GridRevisionPad {
|
|||||||
Some(_) => {
|
Some(_) => {
|
||||||
let old = make_grid_rev_json_str(&cloned_grid)?;
|
let old = make_grid_rev_json_str(&cloned_grid)?;
|
||||||
let new = self.json_str()?;
|
let new = self.json_str()?;
|
||||||
match cal_diff::<PlainTextAttributes>(old, new) {
|
match cal_diff::<PhantomAttributes>(old, new) {
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
Some(delta) => {
|
Some(delta) => {
|
||||||
self.delta = self.delta.compose(&delta)?;
|
self.delta = self.delta.compose(&delta)?;
|
||||||
@ -553,7 +553,7 @@ pub fn make_grid_delta(grid_rev: &GridRevision) -> GridRevisionDelta {
|
|||||||
|
|
||||||
pub fn make_grid_revisions(user_id: &str, grid_rev: &GridRevision) -> RepeatedRevision {
|
pub fn make_grid_revisions(user_id: &str, grid_rev: &GridRevision) -> RepeatedRevision {
|
||||||
let delta = make_grid_delta(grid_rev);
|
let delta = make_grid_delta(grid_rev);
|
||||||
let bytes = delta.to_delta_bytes();
|
let bytes = delta.to_json_bytes();
|
||||||
let revision = Revision::initial_revision(user_id, &grid_rev.grid_id, bytes);
|
let revision = Revision::initial_revision(user_id, &grid_rev.grid_id, bytes);
|
||||||
revision.into()
|
revision.into()
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ impl std::fmt::Debug for Revision {
|
|||||||
let _ = f.write_fmt(format_args!("rev_id {}, ", self.rev_id))?;
|
let _ = f.write_fmt(format_args!("rev_id {}, ", self.rev_id))?;
|
||||||
match RichTextDelta::from_bytes(&self.delta_data) {
|
match RichTextDelta::from_bytes(&self.delta_data) {
|
||||||
Ok(delta) => {
|
Ok(delta) => {
|
||||||
let _ = f.write_fmt(format_args!("delta {:?}", delta.to_delta_str()))?;
|
let _ = f.write_fmt(format_args!("delta {:?}", delta.to_json_str()))?;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let _ = f.write_fmt(format_args!("delta {:?}", e))?;
|
let _ = f.write_fmt(format_args!("delta {:?}", e))?;
|
||||||
|
@ -46,7 +46,7 @@ impl std::convert::TryFrom<Revision> for DocumentPB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let delta = RichTextDelta::from_bytes(&revision.delta_data)?;
|
let delta = RichTextDelta::from_bytes(&revision.delta_data)?;
|
||||||
let doc_json = delta.to_delta_str();
|
let doc_json = delta.to_json_str();
|
||||||
|
|
||||||
Ok(DocumentPB {
|
Ok(DocumentPB {
|
||||||
block_id: revision.object_id,
|
block_id: revision.object_id,
|
||||||
|
@ -39,7 +39,7 @@ impl RevisionSyncObject<RichTextAttributes> for ServerDocument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn to_json(&self) -> String {
|
fn to_json(&self) -> String {
|
||||||
self.delta.to_delta_str()
|
self.delta.to_json_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_delta(&mut self, new_delta: Delta<RichTextAttributes>) {
|
fn set_delta(&mut self, new_delta: Delta<RichTextAttributes>) {
|
||||||
|
@ -13,7 +13,7 @@ use crate::{
|
|||||||
use async_stream::stream;
|
use async_stream::stream;
|
||||||
use futures::stream::StreamExt;
|
use futures::stream::StreamExt;
|
||||||
use lib_infra::future::BoxResultFuture;
|
use lib_infra::future::BoxResultFuture;
|
||||||
use lib_ot::core::PlainTextAttributes;
|
use lib_ot::core::PhantomAttributes;
|
||||||
use std::{collections::HashMap, fmt::Debug, sync::Arc};
|
use std::{collections::HashMap, fmt::Debug, sync::Arc};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
sync::{mpsc, oneshot, RwLock},
|
sync::{mpsc, oneshot, RwLock},
|
||||||
@ -188,7 +188,7 @@ impl ServerFolderManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type FolderRevisionSynchronizer = RevisionSynchronizer<PlainTextAttributes>;
|
type FolderRevisionSynchronizer = RevisionSynchronizer<PhantomAttributes>;
|
||||||
|
|
||||||
struct OpenFolderHandler {
|
struct OpenFolderHandler {
|
||||||
folder_id: String,
|
folder_id: String,
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{entities::folder::FolderDelta, errors::CollaborateError, synchronizer::RevisionSyncObject};
|
use crate::{entities::folder::FolderDelta, errors::CollaborateError, synchronizer::RevisionSyncObject};
|
||||||
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta};
|
use lib_ot::core::{OperationTransformable, PhantomAttributes, PlainTextDelta};
|
||||||
|
|
||||||
pub struct ServerFolder {
|
pub struct ServerFolder {
|
||||||
folder_id: String,
|
folder_id: String,
|
||||||
@ -15,7 +15,7 @@ impl ServerFolder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RevisionSyncObject<PlainTextAttributes> for ServerFolder {
|
impl RevisionSyncObject<PhantomAttributes> for ServerFolder {
|
||||||
fn id(&self) -> &str {
|
fn id(&self) -> &str {
|
||||||
&self.folder_id
|
&self.folder_id
|
||||||
}
|
}
|
||||||
@ -32,7 +32,7 @@ impl RevisionSyncObject<PlainTextAttributes> for ServerFolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn to_json(&self) -> String {
|
fn to_json(&self) -> String {
|
||||||
self.delta.to_delta_str()
|
self.delta.to_json_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_delta(&mut self, new_delta: PlainTextDelta) {
|
fn set_delta(&mut self, new_delta: PlainTextDelta) {
|
||||||
|
@ -149,7 +149,7 @@ pub fn make_folder_from_revisions_pb(
|
|||||||
folder_delta = folder_delta.compose(&delta)?;
|
folder_delta = folder_delta.compose(&delta)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let text = folder_delta.to_delta_str();
|
let text = folder_delta.to_json_str();
|
||||||
Ok(Some(FolderInfo {
|
Ok(Some(FolderInfo {
|
||||||
folder_id: folder_id.to_string(),
|
folder_id: folder_id.to_string(),
|
||||||
text,
|
text,
|
||||||
@ -183,7 +183,7 @@ pub fn make_document_from_revision_pbs(
|
|||||||
delta = delta.compose(&new_delta)?;
|
delta = delta.compose(&new_delta)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let text = delta.to_delta_str();
|
let text = delta.to_json_str();
|
||||||
|
|
||||||
Ok(Some(DocumentPB {
|
Ok(Some(DocumentPB {
|
||||||
block_id: doc_id.to_owned(),
|
block_id: doc_id.to_owned(),
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::core::delta::{trim, Delta};
|
use crate::core::delta::{trim, Delta};
|
||||||
use crate::core::operation::{Attributes, PlainTextAttributes};
|
use crate::core::operation::{Attributes, PhantomAttributes};
|
||||||
|
|
||||||
pub type PlainTextDeltaBuilder = DeltaBuilder<PlainTextAttributes>;
|
pub type PlainTextDeltaBuilder = DeltaBuilder<PhantomAttributes>;
|
||||||
|
|
||||||
/// A builder for creating new [Delta] objects.
|
/// A builder for creating new [Delta] objects.
|
||||||
///
|
///
|
||||||
|
@ -3,7 +3,7 @@ use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
|
|||||||
use crate::core::delta::{DeltaIterator, MAX_IV_LEN};
|
use crate::core::delta::{DeltaIterator, MAX_IV_LEN};
|
||||||
use crate::core::flowy_str::FlowyStr;
|
use crate::core::flowy_str::FlowyStr;
|
||||||
use crate::core::interval::Interval;
|
use crate::core::interval::Interval;
|
||||||
use crate::core::operation::{Attributes, Operation, OperationBuilder, OperationTransformable, PlainTextAttributes};
|
use crate::core::operation::{Attributes, Operation, OperationBuilder, OperationTransformable, PhantomAttributes};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::{
|
use std::{
|
||||||
@ -14,12 +14,15 @@ use std::{
|
|||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type PlainTextDelta = Delta<PlainTextAttributes>;
|
pub type PlainTextDelta = Delta<PhantomAttributes>;
|
||||||
|
|
||||||
/// A [Delta] contains list of operations that consists of 'Retain', 'Delete' and 'Insert' operation.
|
/// A [Delta] contains list of operations that consists of 'Retain', 'Delete' and 'Insert' operation.
|
||||||
/// Check out the [Operation] for more details. It describes the document as a sequence of
|
/// Check out the [Operation] for more details. It describes the document as a sequence of
|
||||||
/// operations.
|
/// operations.
|
||||||
///
|
///
|
||||||
|
/// If the [T] supports 'serde', that will enable delta to serialize to JSON or deserialize from
|
||||||
|
/// a JSON string.
|
||||||
|
///
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct Delta<T: Attributes> {
|
pub struct Delta<T: Attributes> {
|
||||||
pub ops: Vec<Operation<T>>,
|
pub ops: Vec<Operation<T>>,
|
||||||
@ -182,7 +185,7 @@ where
|
|||||||
/// .insert(", AppFlowy")
|
/// .insert(", AppFlowy")
|
||||||
/// .build();
|
/// .build();
|
||||||
///
|
///
|
||||||
/// let after_a = delta_a.to_str().unwrap();
|
/// let after_a = delta_a.content_str().unwrap();
|
||||||
/// let after_b = delta_b.apply(&after_a).unwrap();
|
/// let after_b = delta_b.apply(&after_a).unwrap();
|
||||||
/// assert_eq!("hello, AppFlowy", &after_b);
|
/// assert_eq!("hello, AppFlowy", &after_b);
|
||||||
/// ```
|
/// ```
|
||||||
@ -565,7 +568,7 @@ impl<T> Delta<T>
|
|||||||
where
|
where
|
||||||
T: Attributes + DeserializeOwned,
|
T: Attributes + DeserializeOwned,
|
||||||
{
|
{
|
||||||
pub fn from_delta_str(json: &str) -> Result<Self, OTError> {
|
pub fn from_json_str(json: &str) -> Result<Self, OTError> {
|
||||||
let delta = serde_json::from_str(json).map_err(|e| {
|
let delta = serde_json::from_str(json).map_err(|e| {
|
||||||
tracing::trace!("Deserialize failed: {:?}", e);
|
tracing::trace!("Deserialize failed: {:?}", e);
|
||||||
tracing::trace!("{:?}", json);
|
tracing::trace!("{:?}", json);
|
||||||
@ -576,7 +579,7 @@ where
|
|||||||
|
|
||||||
pub fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> Result<Self, OTError> {
|
pub fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> Result<Self, OTError> {
|
||||||
let json = str::from_utf8(bytes.as_ref())?.to_owned();
|
let json = str::from_utf8(bytes.as_ref())?.to_owned();
|
||||||
let val = Self::from_delta_str(&json)?;
|
let val = Self::from_json_str(&json)?;
|
||||||
Ok(val)
|
Ok(val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -585,16 +588,16 @@ impl<T> Delta<T>
|
|||||||
where
|
where
|
||||||
T: Attributes + serde::Serialize,
|
T: Attributes + serde::Serialize,
|
||||||
{
|
{
|
||||||
pub fn to_delta_str(&self) -> String {
|
pub fn to_json_str(&self) -> String {
|
||||||
serde_json::to_string(self).unwrap_or_else(|_| "".to_owned())
|
serde_json::to_string(self).unwrap_or_else(|_| "".to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_str(&self) -> Result<String, OTError> {
|
pub fn content_str(&self) -> Result<String, OTError> {
|
||||||
self.apply("")
|
self.apply("")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_delta_bytes(&self) -> Bytes {
|
pub fn to_json_bytes(&self) -> Bytes {
|
||||||
let json = self.to_delta_str();
|
let json = self.to_json_str();
|
||||||
Bytes::from(json.into_bytes())
|
Bytes::from(json.into_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use crate::core::operation::{Attributes, Operation, PlainTextAttributes};
|
use crate::core::operation::{Attributes, Operation, PhantomAttributes};
|
||||||
use crate::rich_text::RichTextAttributes;
|
use crate::rich_text::RichTextAttributes;
|
||||||
|
|
||||||
pub type RichTextOpBuilder = OperationBuilder<RichTextAttributes>;
|
pub type RichTextOpBuilder = OperationBuilder<RichTextAttributes>;
|
||||||
pub type PlainTextOpBuilder = OperationBuilder<PlainTextAttributes>;
|
pub type PlainTextOpBuilder = OperationBuilder<PhantomAttributes>;
|
||||||
|
|
||||||
pub struct OperationBuilder<T: Attributes> {
|
pub struct OperationBuilder<T: Attributes> {
|
||||||
ty: Operation<T>,
|
ty: Operation<T>,
|
||||||
|
@ -26,7 +26,7 @@ pub trait OperationTransformable {
|
|||||||
/// let document = PlainTextDeltaBuilder::new().build();
|
/// let document = PlainTextDeltaBuilder::new().build();
|
||||||
/// let delta = PlainTextDeltaBuilder::new().insert("abc").build();
|
/// let delta = PlainTextDeltaBuilder::new().insert("abc").build();
|
||||||
/// let new_document = document.compose(&delta).unwrap();
|
/// let new_document = document.compose(&delta).unwrap();
|
||||||
/// assert_eq!(new_document.to_str().unwrap(), "abc".to_owned());
|
/// assert_eq!(new_document.content_str().unwrap(), "abc".to_owned());
|
||||||
/// ```
|
/// ```
|
||||||
fn compose(&self, other: &Self) -> Result<Self, OTError>
|
fn compose(&self, other: &Self) -> Result<Self, OTError>
|
||||||
where
|
where
|
||||||
@ -65,6 +65,12 @@ pub trait OperationTransformable {
|
|||||||
fn invert(&self, other: &Self) -> Self;
|
fn invert(&self, other: &Self) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Each operation can carry attributes. For example, the [RichTextAttributes] has a list of key/value attributes.
|
||||||
|
/// Such as { bold: true, italic: true }.
|
||||||
|
///
|
||||||
|
/// Because [Operation] is generic over the T, so you must specify the T. For example, the [PlainTextDelta]. It use
|
||||||
|
/// use [PhantomAttributes] as the T. [PhantomAttributes] does nothing, just a phantom.
|
||||||
|
///
|
||||||
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransformable {
|
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransformable {
|
||||||
fn is_empty(&self) -> bool {
|
fn is_empty(&self) -> bool {
|
||||||
true
|
true
|
||||||
@ -80,6 +86,14 @@ pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + Opera
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// [Operation] consists of three types.
|
||||||
|
/// * Delete
|
||||||
|
/// * Retain
|
||||||
|
/// * Insert
|
||||||
|
///
|
||||||
|
/// The [T] should support serde if you want to serialize/deserialize the operation
|
||||||
|
/// to json string. You could check out the operation_serde.rs for more information.
|
||||||
|
///
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum Operation<T: Attributes> {
|
pub enum Operation<T: Attributes> {
|
||||||
Delete(usize),
|
Delete(usize),
|
||||||
@ -241,9 +255,7 @@ where
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Retain<T: Attributes> {
|
pub struct Retain<T: Attributes> {
|
||||||
// #[serde(rename(serialize = "retain", deserialize = "retain"))]
|
|
||||||
pub n: usize,
|
pub n: usize,
|
||||||
// #[serde(skip_serializing_if = "is_empty")]
|
|
||||||
pub attributes: T,
|
pub attributes: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,10 +330,7 @@ where
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Insert<T: Attributes> {
|
pub struct Insert<T: Attributes> {
|
||||||
// #[serde(rename(serialize = "insert", deserialize = "insert"))]
|
|
||||||
pub s: FlowyStr,
|
pub s: FlowyStr,
|
||||||
|
|
||||||
// #[serde(skip_serializing_if = "is_empty")]
|
|
||||||
pub attributes: T,
|
pub attributes: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -402,16 +411,16 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default, Serialize, Deserialize)]
|
||||||
pub struct PlainTextAttributes();
|
pub struct PhantomAttributes();
|
||||||
impl fmt::Display for PlainTextAttributes {
|
impl fmt::Display for PhantomAttributes {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.write_str("PlainAttributes")
|
f.write_str("PhantomAttributes")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Attributes for PlainTextAttributes {}
|
impl Attributes for PhantomAttributes {}
|
||||||
|
|
||||||
impl OperationTransformable for PlainTextAttributes {
|
impl OperationTransformable for PhantomAttributes {
|
||||||
fn compose(&self, _other: &Self) -> Result<Self, OTError> {
|
fn compose(&self, _other: &Self) -> Result<Self, OTError> {
|
||||||
Ok(self.clone())
|
Ok(self.clone())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user