From dd71acf843f6aa4d5697b0480926e882e136dccd Mon Sep 17 00:00:00 2001 From: appflowy Date: Sun, 24 Jul 2022 18:10:29 +0800 Subject: [PATCH] chore: rename some function & add more documentation --- frontend/rust-lib/flowy-folder/src/manager.rs | 2 +- .../src/services/folder_editor.rs | 8 ++--- .../src/services/persistence/mod.rs | 2 +- .../flowy-folder/src/services/web_socket.rs | 8 ++--- frontend/rust-lib/flowy-grid/src/manager.rs | 4 +-- .../src/services/block_revision_editor.rs | 8 ++--- .../flowy-grid/src/services/grid_editor.rs | 8 ++--- .../src/services/persistence/migration.rs | 2 +- .../flowy-revision/src/conflict_resolve.rs | 8 ++--- .../rust-lib/flowy-text-block/src/editor.rs | 2 +- .../rust-lib/flowy-text-block/src/queue.rs | 4 +-- .../flowy-text-block/tests/document/script.rs | 2 +- .../tests/editor/attribute_test.rs | 12 +++---- .../flowy-text-block/tests/editor/mod.rs | 26 ++++++++-------- .../flowy-text-block/tests/editor/op_test.rs | 2 +- .../tests/editor/serde_test.rs | 8 ++--- .../src/client_document/default/mod.rs | 6 ++-- .../src/client_document/document_pad.rs | 10 +++--- .../flowy-sync/src/client_folder/builder.rs | 10 +++--- .../src/client_folder/folder_pad.rs | 6 ++-- .../src/client_grid/grid_block_revsion_pad.rs | 30 +++++++++--------- .../src/client_grid/grid_revision_pad.rs | 18 +++++------ .../flowy-sync/src/entities/revision.rs | 2 +- .../flowy-sync/src/entities/text_block.rs | 2 +- .../src/server_document/document_pad.rs | 2 +- .../src/server_folder/folder_manager.rs | 4 +-- .../src/server_folder/folder_pad.rs | 6 ++-- shared-lib/flowy-sync/src/util.rs | 4 +-- shared-lib/lib-ot/src/core/delta/builder.rs | 4 +-- shared-lib/lib-ot/src/core/delta/delta.rs | 21 +++++++------ .../lib-ot/src/core/operation/builder.rs | 4 +-- .../lib-ot/src/core/operation/operation.rs | 31 ++++++++++++------- 32 files changed, 139 insertions(+), 127 deletions(-) diff --git a/frontend/rust-lib/flowy-folder/src/manager.rs b/frontend/rust-lib/flowy-folder/src/manager.rs index 356ff15897..8850024f25 100644 --- a/frontend/rust-lib/flowy-folder/src/manager.rs +++ b/frontend/rust-lib/flowy-folder/src/manager.rs @@ -215,7 +215,7 @@ impl DefaultFolderBuilder { for app in workspace_rev.apps.iter() { for (index, view) in app.belongings.iter().enumerate() { let view_data = if index == 0 { - initial_read_me().to_delta_str() + initial_read_me().to_json_str() } else { initial_quill_delta_string() }; diff --git a/frontend/rust-lib/flowy-folder/src/services/folder_editor.rs b/frontend/rust-lib/flowy-folder/src/services/folder_editor.rs index b69b303fa1..6fb0ebf565 100644 --- a/frontend/rust-lib/flowy-folder/src/services/folder_editor.rs +++ b/frontend/rust-lib/flowy-folder/src/services/folder_editor.rs @@ -10,7 +10,7 @@ use flowy_sync::{ entities::{revision::Revision, ws_data::ServerRevisionWSData}, }; use lib_infra::future::FutureResult; -use lib_ot::core::PlainTextAttributes; +use lib_ot::core::PhantomAttributes; use parking_lot::RwLock; use std::sync::Arc; @@ -80,7 +80,7 @@ impl FolderEditor { pub(crate) fn apply_change(&self, change: FolderChange) -> FlowyResult<()> { let FolderChange { delta, md5 } = change; 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( &self.rev_manager.object_id, base_rev_id, @@ -132,7 +132,7 @@ impl FolderEditor { pub struct FolderRevisionCompactor(); impl RevisionCompactor for FolderRevisionCompactor { fn bytes_from_revisions(&self, revisions: Vec) -> FlowyResult { - let delta = make_delta_from_revisions::(revisions)?; - Ok(delta.to_delta_bytes()) + let delta = make_delta_from_revisions::(revisions)?; + Ok(delta.to_json_bytes()) } } diff --git a/frontend/rust-lib/flowy-folder/src/services/persistence/mod.rs b/frontend/rust-lib/flowy-folder/src/services/persistence/mod.rs index 5676db54da..ee930bd6ff 100644 --- a/frontend/rust-lib/flowy-folder/src/services/persistence/mod.rs +++ b/frontend/rust-lib/flowy-folder/src/services/persistence/mod.rs @@ -110,7 +110,7 @@ impl FolderPersistence { pub async fn save_folder(&self, user_id: &str, folder_id: &FolderId, folder: FolderPad) -> FlowyResult<()> { let pool = self.database.db_pool()?; 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 record = RevisionRecord { revision, diff --git a/frontend/rust-lib/flowy-folder/src/services/web_socket.rs b/frontend/rust-lib/flowy-folder/src/services/web_socket.rs index 75db905d63..eac429dc1f 100644 --- a/frontend/rust-lib/flowy-folder/src/services/web_socket.rs +++ b/frontend/rust-lib/flowy-folder/src/services/web_socket.rs @@ -10,7 +10,7 @@ use flowy_sync::{ }, }; 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 std::{sync::Arc, time::Duration}; @@ -24,7 +24,7 @@ pub(crate) async fn make_folder_ws_manager( ) -> Arc { let ws_data_provider = Arc::new(WSDataProvider::new(folder_id, Arc::new(rev_manager.clone()))); let resolver = Arc::new(FolderConflictResolver { folder_pad }); - let conflict_controller = ConflictController::::new( + let conflict_controller = ConflictController::::new( user_id, resolver, Arc::new(ws_data_provider.clone()), @@ -55,7 +55,7 @@ struct FolderConflictResolver { folder_pad: Arc>, } -impl ConflictResolver for FolderConflictResolver { +impl ConflictResolver for FolderConflictResolver { fn compose_delta(&self, delta: PlainTextDelta) -> BoxResultFuture { let folder_pad = self.folder_pad.clone(); Box::pin(async move { @@ -67,7 +67,7 @@ impl ConflictResolver for FolderConflictResolver { fn transform_delta( &self, delta: PlainTextDelta, - ) -> BoxResultFuture, FlowyError> { + ) -> BoxResultFuture, FlowyError> { let folder_pad = self.folder_pad.clone(); Box::pin(async move { let read_guard = folder_pad.read(); diff --git a/frontend/rust-lib/flowy-grid/src/manager.rs b/frontend/rust-lib/flowy-grid/src/manager.rs index 320f18b560..eadd1e47fd 100644 --- a/frontend/rust-lib/flowy-grid/src/manager.rs +++ b/frontend/rust-lib/flowy-grid/src/manager.rs @@ -192,7 +192,7 @@ pub async fn make_grid_view_data( // Create grid's block 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 = Revision::initial_revision(user_id, block_id, block_delta_data).into(); let _ = grid_manager.create_grid_block(&block_id, repeated_revision).await?; @@ -202,7 +202,7 @@ pub async fn make_grid_view_data( // Create grid 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 = Revision::initial_revision(user_id, view_id, grid_delta_data.clone()).into(); let _ = grid_manager.create_grid(view_id, repeated_revision).await?; diff --git a/frontend/rust-lib/flowy-grid/src/services/block_revision_editor.rs b/frontend/rust-lib/flowy-grid/src/services/block_revision_editor.rs index f74075ec6e..19f3a885cb 100644 --- a/frontend/rust-lib/flowy-grid/src/services/block_revision_editor.rs +++ b/frontend/rust-lib/flowy-grid/src/services/block_revision_editor.rs @@ -7,7 +7,7 @@ use flowy_sync::client_grid::{GridBlockMetaChange, GridBlockRevisionPad}; use flowy_sync::entities::revision::Revision; use flowy_sync::util::make_delta_from_revisions; use lib_infra::future::FutureResult; -use lib_ot::core::PlainTextAttributes; +use lib_ot::core::PhantomAttributes; use std::borrow::Cow; use std::sync::Arc; use tokio::sync::RwLock; @@ -161,7 +161,7 @@ impl GridBlockRevisionEditor { let GridBlockMetaChange { delta, md5 } = change; let user_id = self.user_id.clone(); 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( &self.rev_manager.object_id, base_rev_id, @@ -200,7 +200,7 @@ impl RevisionObjectBuilder for GridBlockMetaPadBuilder { pub struct GridBlockRevisionCompactor(); impl RevisionCompactor for GridBlockRevisionCompactor { fn bytes_from_revisions(&self, revisions: Vec) -> FlowyResult { - let delta = make_delta_from_revisions::(revisions)?; - Ok(delta.to_delta_bytes()) + let delta = make_delta_from_revisions::(revisions)?; + Ok(delta.to_json_bytes()) } } diff --git a/frontend/rust-lib/flowy-grid/src/services/grid_editor.rs b/frontend/rust-lib/flowy-grid/src/services/grid_editor.rs index 572665bd0c..f185c907a7 100644 --- a/frontend/rust-lib/flowy-grid/src/services/grid_editor.rs +++ b/frontend/rust-lib/flowy-grid/src/services/grid_editor.rs @@ -21,7 +21,7 @@ use flowy_sync::entities::revision::Revision; use flowy_sync::errors::CollaborateResult; use flowy_sync::util::make_delta_from_revisions; use lib_infra::future::FutureResult; -use lib_ot::core::PlainTextAttributes; +use lib_ot::core::PhantomAttributes; use std::collections::HashMap; use std::sync::Arc; use tokio::sync::RwLock; @@ -573,7 +573,7 @@ impl GridRevisionEditor { let GridChangeset { delta, md5 } = change; let user_id = self.user.user_id()?; 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( &self.rev_manager.object_id, base_rev_id, @@ -664,8 +664,8 @@ impl RevisionCloudService for GridRevisionCloudService { pub struct GridRevisionCompactor(); impl RevisionCompactor for GridRevisionCompactor { fn bytes_from_revisions(&self, revisions: Vec) -> FlowyResult { - let delta = make_delta_from_revisions::(revisions)?; - Ok(delta.to_delta_bytes()) + let delta = make_delta_from_revisions::(revisions)?; + Ok(delta.to_json_bytes()) } } diff --git a/frontend/rust-lib/flowy-grid/src/services/persistence/migration.rs b/frontend/rust-lib/flowy-grid/src/services/persistence/migration.rs index 026ba3bc20..9b24d03fda 100644 --- a/frontend/rust-lib/flowy-grid/src/services/persistence/migration.rs +++ b/frontend/rust-lib/flowy-grid/src/services/persistence/migration.rs @@ -48,7 +48,7 @@ impl GridMigration { let pool = self.database.db_pool()?; let grid_rev_pad = self.get_grid_revision_pad(grid_id).await?; 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 record = RevisionRecord::new(revision); // diff --git a/frontend/rust-lib/flowy-revision/src/conflict_resolve.rs b/frontend/rust-lib/flowy-revision/src/conflict_resolve.rs index 0a63f37f3e..5e89e7db32 100644 --- a/frontend/rust-lib/flowy-revision/src/conflict_resolve.rs +++ b/frontend/rust-lib/flowy-revision/src/conflict_resolve.rs @@ -9,7 +9,7 @@ use flowy_sync::{ util::make_delta_from_revisions, }; 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 serde::de::DeserializeOwned; use std::{convert::TryFrom, sync::Arc}; @@ -31,7 +31,7 @@ pub trait ConflictRevisionSink: Send + Sync + 'static { } pub type RichTextConflictController = ConflictController; -pub type PlainTextConflictController = ConflictController; +pub type PlainTextConflictController = ConflictController; pub struct ConflictController where @@ -154,7 +154,7 @@ where &rev_manager.object_id, base_rev_id, rev_id, - client_delta.to_delta_bytes(), + client_delta.to_json_bytes(), user_id, md5.clone(), ); @@ -166,7 +166,7 @@ where &rev_manager.object_id, base_rev_id, rev_id, - server_delta.to_delta_bytes(), + server_delta.to_json_bytes(), user_id, md5, ); diff --git a/frontend/rust-lib/flowy-text-block/src/editor.rs b/frontend/rust-lib/flowy-text-block/src/editor.rs index 6fed85c913..2a52f43778 100644 --- a/frontend/rust-lib/flowy-text-block/src/editor.rs +++ b/frontend/rust-lib/flowy-text-block/src/editor.rs @@ -238,7 +238,7 @@ impl RevisionObjectBuilder for TextBlockInfoBuilder { Result::::Ok(DocumentPB { block_id: object_id.to_owned(), - text: delta.to_delta_str(), + text: delta.to_json_str(), rev_id, base_rev_id, }) diff --git a/frontend/rust-lib/flowy-text-block/src/queue.rs b/frontend/rust-lib/flowy-text-block/src/queue.rs index 343ad11e7b..072c810d9d 100644 --- a/frontend/rust-lib/flowy-text-block/src/queue.rs +++ b/frontend/rust-lib/flowy-text-block/src/queue.rs @@ -175,7 +175,7 @@ impl EditBlockQueue { } async fn save_local_delta(&self, delta: RichTextDelta, md5: String) -> Result { - 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 user_id = self.user.user_id()?; let revision = Revision::new( @@ -195,7 +195,7 @@ pub(crate) struct TextBlockRevisionCompactor(); impl RevisionCompactor for TextBlockRevisionCompactor { fn bytes_from_revisions(&self, revisions: Vec) -> FlowyResult { let delta = make_delta_from_revisions::(revisions)?; - Ok(delta.to_delta_bytes()) + Ok(delta.to_json_bytes()) } } diff --git a/frontend/rust-lib/flowy-text-block/tests/document/script.rs b/frontend/rust-lib/flowy-text-block/tests/document/script.rs index 1722724c3f..b3ce1cb9bd 100644 --- a/frontend/rust-lib/flowy-text-block/tests/document/script.rs +++ b/frontend/rust-lib/flowy-text-block/tests/document/script.rs @@ -75,7 +75,7 @@ impl TextBlockEditorTest { let delta = self.editor.text_block_delta().await.unwrap(); if expected_delta != delta { eprintln!("✅ expect: {}", expected,); - eprintln!("❌ receive: {}", delta.to_delta_str()); + eprintln!("❌ receive: {}", delta.to_json_str()); } assert_eq!(expected_delta, delta); } diff --git a/frontend/rust-lib/flowy-text-block/tests/editor/attribute_test.rs b/frontend/rust-lib/flowy-text-block/tests/editor/attribute_test.rs index 037b36970b..6dcd372d47 100644 --- a/frontend/rust-lib/flowy-text-block/tests/editor/attribute_test.rs +++ b/frontend/rust-lib/flowy-text-block/tests/editor/attribute_test.rs @@ -762,19 +762,19 @@ fn attributes_preserve_list_format_on_merge() { #[test] 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![ - RichTextDelta::from_delta_str(r#"[{"retain":1,"attributes":{"list":"unchecked"}}]"#).unwrap(), - RichTextDelta::from_delta_str(r#"[{"insert":"a"}]"#).unwrap(), - RichTextDelta::from_delta_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":1,"attributes":{"list":"unchecked"}}]"#).unwrap(), + RichTextDelta::from_json_str(r#"[{"insert":"a"}]"#).unwrap(), + RichTextDelta::from_json_str(r#"[{"retain":1},{"insert":"\n","attributes":{"list":"unchecked"}}]"#).unwrap(), + RichTextDelta::from_json_str(r#"[{"retain":2},{"retain":1,"attributes":{"list":""}}]"#).unwrap(), ]; for d in deltas { delta = delta.compose(&d).unwrap(); } assert_eq!( - delta.to_delta_str(), + delta.to_json_str(), r#"[{"insert":"a"},{"insert":"\n","attributes":{"list":"unchecked"}},{"insert":"\n"}]"# ); diff --git a/frontend/rust-lib/flowy-text-block/tests/editor/mod.rs b/frontend/rust-lib/flowy-text-block/tests/editor/mod.rs index 676b9a1cb0..a6b58ab39b 100644 --- a/frontend/rust-lib/flowy-text-block/tests/editor/mod.rs +++ b/frontend/rust-lib/flowy-text-block/tests/editor/mod.rs @@ -108,20 +108,20 @@ impl TestBuilder { TestOp::Insert(delta_i, s, index) => { let document = &mut self.documents[*delta_i]; 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)); } TestOp::Delete(delta_i, iv) => { let document = &mut self.documents[*delta_i]; 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)); } TestOp::Replace(delta_i, iv, s) => { let document = &mut self.documents[*delta_i]; 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)); } TestOp::InsertBold(delta_i, s, iv) => { @@ -133,7 +133,7 @@ impl TestBuilder { let document = &mut self.documents[*delta_i]; let attribute = RichTextAttribute::Bold(*enable); 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)); } TestOp::Italic(delta_i, iv, enable) => { @@ -143,28 +143,28 @@ impl TestBuilder { false => RichTextAttribute::Italic(false), }; 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)); } TestOp::Header(delta_i, iv, level) => { let document = &mut self.documents[*delta_i]; let attribute = RichTextAttribute::Header(*level); 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)); } TestOp::Link(delta_i, iv, link) => { let document = &mut self.documents[*delta_i]; let attribute = RichTextAttribute::Link(link.to_owned()); 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)); } TestOp::Bullet(delta_i, iv, enable) => { let document = &mut self.documents[*delta_i]; let attribute = RichTextAttribute::Bullet(*enable); 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)); } @@ -194,15 +194,15 @@ impl TestBuilder { let delta_a = &self.documents[*delta_a_i].delta(); let delta_b = &self.documents[*delta_b_i].delta(); tracing::debug!("Invert: "); - tracing::debug!("a: {}", delta_a.to_delta_str()); - tracing::debug!("b: {}", delta_b.to_delta_str()); + tracing::debug!("a: {}", delta_a.to_json_str()); + tracing::debug!("b: {}", delta_b.to_json_str()); let (_, b_prime) = delta_a.transform(delta_b).unwrap(); let undo = b_prime.invert(delta_a); let new_delta = delta_a.compose(&b_prime).unwrap(); - tracing::debug!("new delta: {}", new_delta.to_delta_str()); - tracing::debug!("undo delta: {}", undo.to_delta_str()); + tracing::debug!("new delta: {}", new_delta.to_json_str()); + tracing::debug!("undo delta: {}", undo.to_json_str()); let new_delta_after_undo = new_delta.compose(&undo).unwrap(); @@ -238,7 +238,7 @@ impl TestBuilder { } 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 target_prime: RichTextDelta = serde_json::from_str(&prime_json).unwrap(); diff --git a/frontend/rust-lib/flowy-text-block/tests/editor/op_test.rs b/frontend/rust-lib/flowy-text-block/tests/editor/op_test.rs index 47d3c5ebfe..f32e70942f 100644 --- a/frontend/rust-lib/flowy-text-block/tests/editor/op_test.rs +++ b/frontend/rust-lib/flowy-text-block/tests/editor/op_test.rs @@ -378,7 +378,7 @@ fn apply_test() { .insert(", AppFlowy") .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(); assert_eq!("hello, AppFlowy", &after_b); } diff --git a/frontend/rust-lib/flowy-text-block/tests/editor/serde_test.rs b/frontend/rust-lib/flowy-text-block/tests/editor/serde_test.rs index ae9e57b3c6..6c578be6c3 100644 --- a/frontend/rust-lib/flowy-text-block/tests/editor/serde_test.rs +++ b/frontend/rust-lib/flowy-text-block/tests/editor/serde_test.rs @@ -65,7 +65,7 @@ fn delta_serialize_multi_attribute_test() { let json = serde_json::to_string(&delta).unwrap(); 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); } @@ -77,7 +77,7 @@ fn delta_deserialize_test() { {"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); } @@ -86,13 +86,13 @@ fn delta_deserialize_null_test() { let json = r#"[ {"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); attribute.value = RichTextAttributeValue(None); 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); } diff --git a/shared-lib/flowy-sync/src/client_document/default/mod.rs b/shared-lib/flowy-sync/src/client_document/default/mod.rs index f5fb180571..582e3eac2e 100644 --- a/shared-lib/flowy-sync/src/client_document/default/mod.rs +++ b/shared-lib/flowy-sync/src/client_document/default/mod.rs @@ -7,13 +7,13 @@ pub fn initial_quill_delta() -> RichTextDelta { #[inline] pub fn initial_quill_delta_string() -> String { - initial_quill_delta().to_delta_str() + initial_quill_delta().to_json_str() } #[inline] pub fn initial_read_me() -> RichTextDelta { let json = include_str!("READ_ME.json"); - RichTextDelta::from_delta_str(json).unwrap() + RichTextDelta::from_json_str(json).unwrap() } #[cfg(test)] @@ -22,6 +22,6 @@ mod tests { #[test] fn load_read_me() { - println!("{}", initial_read_me().to_delta_str()); + println!("{}", initial_read_me().to_json_str()); } } diff --git a/shared-lib/flowy-sync/src/client_document/document_pad.rs b/shared-lib/flowy-sync/src/client_document/document_pad.rs index 72b52f6415..d63d2194ad 100644 --- a/shared-lib/flowy-sync/src/client_document/document_pad.rs +++ b/shared-lib/flowy-sync/src/client_document/document_pad.rs @@ -55,16 +55,16 @@ impl ClientDocument { } pub fn from_json(json: &str) -> Result { - let delta = RichTextDelta::from_delta_str(json)?; + let delta = RichTextDelta::from_json_str(json)?; Ok(Self::from_delta(delta)) } pub fn delta_str(&self) -> String { - self.delta.to_delta_str() + self.delta.to_json_str() } pub fn to_bytes(&self) -> Bytes { - self.delta.to_delta_bytes() + self.delta.to_json_bytes() } pub fn to_plain_string(&self) -> String { @@ -85,7 +85,7 @@ impl ClientDocument { } 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; match &self.notify { @@ -97,7 +97,7 @@ impl ClientDocument { } 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 mut undo_delta = delta.invert(&self.delta); diff --git a/shared-lib/flowy-sync/src/client_folder/builder.rs b/shared-lib/flowy-sync/src/client_folder/builder.rs index 4c27f278f5..b055f91fb8 100644 --- a/shared-lib/flowy-sync/src/client_folder/builder.rs +++ b/shared-lib/flowy-sync/src/client_folder/builder.rs @@ -7,7 +7,7 @@ use crate::{ }; 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 std::sync::Arc; @@ -41,9 +41,9 @@ impl FolderPadBuilder { } // TODO: Reconvert from history if delta.to_str() failed. - let folder_json = delta.to_str()?; - let mut folder: FolderPad = serde_json::from_str(&folder_json).map_err(|e| { - tracing::error!("Deserialize folder from json failed: {}", folder_json); + let content = delta.content_str()?; + let mut folder: FolderPad = serde_json::from_str(&content).map_err(|e| { + tracing::error!("Deserialize folder from {} failed", content); return CollaborateError::internal().context(format!("Deserialize delta to folder failed: {}", e)); })?; folder.delta = delta; @@ -51,7 +51,7 @@ impl FolderPadBuilder { } pub(crate) fn build_with_revisions(self, revisions: Vec) -> CollaborateResult { - let folder_delta: FolderDelta = make_delta_from_revisions::(revisions)?; + let folder_delta: FolderDelta = make_delta_from_revisions::(revisions)?; self.build_with_delta(folder_delta) } diff --git a/shared-lib/flowy-sync/src/client_folder/folder_pad.rs b/shared-lib/flowy-sync/src/client_folder/folder_pad.rs index f821c03e6f..0cbc33598e 100644 --- a/shared-lib/flowy-sync/src/client_folder/folder_pad.rs +++ b/shared-lib/flowy-sync/src/client_folder/folder_pad.rs @@ -295,7 +295,7 @@ impl FolderPad { } pub fn md5(&self) -> String { - md5(&self.delta.to_delta_bytes()) + md5(&self.delta.to_json_bytes()) } pub fn to_json(&self) -> CollaborateResult { @@ -315,7 +315,7 @@ impl FolderPad { Some(_) => { let old = cloned_self.to_json()?; let new = self.to_json()?; - match cal_diff::(old, new) { + match cal_diff::(old, new) { None => Ok(None), Some(delta) => { self.delta = self.delta.compose(&delta)?; @@ -350,7 +350,7 @@ impl FolderPad { Some(_) => { let old = cloned_self.to_json()?; let new = self.to_json()?; - match cal_diff::(old, new) { + match cal_diff::(old, new) { None => Ok(None), Some(delta) => { self.delta = self.delta.compose(&delta)?; diff --git a/shared-lib/flowy-sync/src/client_grid/grid_block_revsion_pad.rs b/shared-lib/flowy-sync/src/client_grid/grid_block_revsion_pad.rs index 16b98dc7fc..cea6d7fd03 100644 --- a/shared-lib/flowy-sync/src/client_grid/grid_block_revsion_pad.rs +++ b/shared-lib/flowy-sync/src/client_grid/grid_block_revsion_pad.rs @@ -4,7 +4,7 @@ use crate::util::{cal_diff, make_delta_from_revisions}; use flowy_grid_data_model::revision::{ 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::collections::HashMap; use std::sync::Arc; @@ -46,7 +46,7 @@ impl GridBlockRevisionPad { } pub fn from_delta(delta: GridBlockRevisionDelta) -> CollaborateResult { - let s = delta.to_str()?; + let s = delta.content_str()?; let block_revision: GridBlockRevision = serde_json::from_str(&s).map_err(|e| { let msg = format!("Deserialize delta to block meta failed: {}", e); tracing::error!("{}", s); @@ -56,7 +56,7 @@ impl GridBlockRevisionPad { } pub fn from_revisions(_grid_id: &str, revisions: Vec) -> CollaborateResult { - let block_delta: GridBlockRevisionDelta = make_delta_from_revisions::(revisions)?; + let block_delta: GridBlockRevisionDelta = make_delta_from_revisions::(revisions)?; Self::from_delta(block_delta) } @@ -195,10 +195,10 @@ impl GridBlockRevisionPad { Some(_) => { let old = cloned_self.to_json()?; let new = self.to_json()?; - match cal_diff::(old, new) { + match cal_diff::(old, new) { None => Ok(None), Some(delta) => { - tracing::trace!("[GridBlockMeta] Composing delta {}", delta.to_delta_str()); + tracing::trace!("[GridBlockMeta] Composing delta {}", delta.to_json_str()); // tracing::debug!( // "[GridBlockMeta] current delta: {}", // self.delta.to_str().unwrap_or_else(|_| "".to_string()) @@ -231,11 +231,11 @@ impl GridBlockRevisionPad { } pub fn md5(&self) -> String { - md5(&self.delta.to_delta_bytes()) + md5(&self.delta.to_json_bytes()) } 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 { 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); revision.into() } @@ -289,7 +289,7 @@ mod tests { let change = pad.add_row_rev(row.clone(), None).unwrap().unwrap(); assert_eq!(pad.rows.first().unwrap().as_ref(), &row); 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}]"# ); } @@ -303,19 +303,19 @@ mod tests { let change = pad.add_row_rev(row_1.clone(), None).unwrap().unwrap(); 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}]"# ); let change = pad.add_row_rev(row_2.clone(), None).unwrap().unwrap(); 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}]"# ); let change = pad.add_row_rev(row_3.clone(), Some("2".to_string())).unwrap().unwrap(); 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}]"# ); @@ -381,7 +381,7 @@ mod tests { let _ = pad.add_row_rev(row.clone(), None).unwrap().unwrap(); let change = pad.delete_rows(vec![Cow::Borrowed(&row.id)]).unwrap().unwrap(); assert_eq!( - change.delta.to_delta_str(), + change.delta.to_json_str(), r#"[{"retain":24},{"delete":66},{"retain":2}]"# ); @@ -410,7 +410,7 @@ mod tests { let change = pad.update_row(changeset).unwrap().unwrap(); 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}]"# ); @@ -422,7 +422,7 @@ mod tests { fn test_pad() -> GridBlockRevisionPad { 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() } } diff --git a/shared-lib/flowy-sync/src/client_grid/grid_revision_pad.rs b/shared-lib/flowy-sync/src/client_grid/grid_revision_pad.rs index eaa6f1823a..d1232a261d 100644 --- a/shared-lib/flowy-sync/src/client_grid/grid_revision_pad.rs +++ b/shared-lib/flowy-sync/src/client_grid/grid_revision_pad.rs @@ -9,7 +9,7 @@ use flowy_grid_data_model::revision::{ GridLayoutRevision, GridRevision, GridSettingRevision, GridSortRevision, }; 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::sync::Arc; @@ -52,8 +52,8 @@ impl GridRevisionPad { } pub fn from_delta(delta: GridRevisionDelta) -> CollaborateResult { - let s = delta.to_str()?; - let grid: GridRevision = serde_json::from_str(&s) + let content = delta.content_str()?; + let grid: GridRevision = serde_json::from_str(&content) .map_err(|e| CollaborateError::internal().context(format!("Deserialize delta to grid failed: {}", e)))?; Ok(Self { @@ -63,7 +63,7 @@ impl GridRevisionPad { } pub fn from_revisions(revisions: Vec) -> CollaborateResult { - let grid_delta: GridRevisionDelta = make_delta_from_revisions::(revisions)?; + let grid_delta: GridRevisionDelta = make_delta_from_revisions::(revisions)?; Self::from_delta(grid_delta) } @@ -457,15 +457,15 @@ impl GridRevisionPad { } pub fn md5(&self) -> String { - md5(&self.delta.to_delta_bytes()) + md5(&self.delta.to_json_bytes()) } pub fn delta_str(&self) -> String { - self.delta.to_delta_str() + self.delta.to_json_str() } pub fn delta_bytes(&self) -> Bytes { - self.delta.to_delta_bytes() + self.delta.to_json_bytes() } pub fn fields(&self) -> &[Arc] { @@ -482,7 +482,7 @@ impl GridRevisionPad { Some(_) => { let old = make_grid_rev_json_str(&cloned_grid)?; let new = self.json_str()?; - match cal_diff::(old, new) { + match cal_diff::(old, new) { None => Ok(None), Some(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 { 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); revision.into() } diff --git a/shared-lib/flowy-sync/src/entities/revision.rs b/shared-lib/flowy-sync/src/entities/revision.rs index d909efa3d5..408026607c 100644 --- a/shared-lib/flowy-sync/src/entities/revision.rs +++ b/shared-lib/flowy-sync/src/entities/revision.rs @@ -89,7 +89,7 @@ impl std::fmt::Debug for Revision { let _ = f.write_fmt(format_args!("rev_id {}, ", self.rev_id))?; match RichTextDelta::from_bytes(&self.delta_data) { 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) => { let _ = f.write_fmt(format_args!("delta {:?}", e))?; diff --git a/shared-lib/flowy-sync/src/entities/text_block.rs b/shared-lib/flowy-sync/src/entities/text_block.rs index 8753103369..91ff7e65c4 100644 --- a/shared-lib/flowy-sync/src/entities/text_block.rs +++ b/shared-lib/flowy-sync/src/entities/text_block.rs @@ -46,7 +46,7 @@ impl std::convert::TryFrom for DocumentPB { } let delta = RichTextDelta::from_bytes(&revision.delta_data)?; - let doc_json = delta.to_delta_str(); + let doc_json = delta.to_json_str(); Ok(DocumentPB { block_id: revision.object_id, diff --git a/shared-lib/flowy-sync/src/server_document/document_pad.rs b/shared-lib/flowy-sync/src/server_document/document_pad.rs index 1f4fa7bda1..d51919f048 100644 --- a/shared-lib/flowy-sync/src/server_document/document_pad.rs +++ b/shared-lib/flowy-sync/src/server_document/document_pad.rs @@ -39,7 +39,7 @@ impl RevisionSyncObject for ServerDocument { } fn to_json(&self) -> String { - self.delta.to_delta_str() + self.delta.to_json_str() } fn set_delta(&mut self, new_delta: Delta) { diff --git a/shared-lib/flowy-sync/src/server_folder/folder_manager.rs b/shared-lib/flowy-sync/src/server_folder/folder_manager.rs index 16b753f50b..95e3c2330f 100644 --- a/shared-lib/flowy-sync/src/server_folder/folder_manager.rs +++ b/shared-lib/flowy-sync/src/server_folder/folder_manager.rs @@ -13,7 +13,7 @@ use crate::{ use async_stream::stream; use futures::stream::StreamExt; use lib_infra::future::BoxResultFuture; -use lib_ot::core::PlainTextAttributes; +use lib_ot::core::PhantomAttributes; use std::{collections::HashMap, fmt::Debug, sync::Arc}; use tokio::{ sync::{mpsc, oneshot, RwLock}, @@ -188,7 +188,7 @@ impl ServerFolderManager { } } -type FolderRevisionSynchronizer = RevisionSynchronizer; +type FolderRevisionSynchronizer = RevisionSynchronizer; struct OpenFolderHandler { folder_id: String, diff --git a/shared-lib/flowy-sync/src/server_folder/folder_pad.rs b/shared-lib/flowy-sync/src/server_folder/folder_pad.rs index 09b4c9d048..29457eb65e 100644 --- a/shared-lib/flowy-sync/src/server_folder/folder_pad.rs +++ b/shared-lib/flowy-sync/src/server_folder/folder_pad.rs @@ -1,5 +1,5 @@ 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 { folder_id: String, @@ -15,7 +15,7 @@ impl ServerFolder { } } -impl RevisionSyncObject for ServerFolder { +impl RevisionSyncObject for ServerFolder { fn id(&self) -> &str { &self.folder_id } @@ -32,7 +32,7 @@ impl RevisionSyncObject for ServerFolder { } fn to_json(&self) -> String { - self.delta.to_delta_str() + self.delta.to_json_str() } fn set_delta(&mut self, new_delta: PlainTextDelta) { diff --git a/shared-lib/flowy-sync/src/util.rs b/shared-lib/flowy-sync/src/util.rs index c4504a806a..b845550137 100644 --- a/shared-lib/flowy-sync/src/util.rs +++ b/shared-lib/flowy-sync/src/util.rs @@ -149,7 +149,7 @@ pub fn make_folder_from_revisions_pb( folder_delta = folder_delta.compose(&delta)?; } - let text = folder_delta.to_delta_str(); + let text = folder_delta.to_json_str(); Ok(Some(FolderInfo { folder_id: folder_id.to_string(), text, @@ -183,7 +183,7 @@ pub fn make_document_from_revision_pbs( delta = delta.compose(&new_delta)?; } - let text = delta.to_delta_str(); + let text = delta.to_json_str(); Ok(Some(DocumentPB { block_id: doc_id.to_owned(), diff --git a/shared-lib/lib-ot/src/core/delta/builder.rs b/shared-lib/lib-ot/src/core/delta/builder.rs index 184284393a..9e67495da0 100644 --- a/shared-lib/lib-ot/src/core/delta/builder.rs +++ b/shared-lib/lib-ot/src/core/delta/builder.rs @@ -1,7 +1,7 @@ use crate::core::delta::{trim, Delta}; -use crate::core::operation::{Attributes, PlainTextAttributes}; +use crate::core::operation::{Attributes, PhantomAttributes}; -pub type PlainTextDeltaBuilder = DeltaBuilder; +pub type PlainTextDeltaBuilder = DeltaBuilder; /// A builder for creating new [Delta] objects. /// diff --git a/shared-lib/lib-ot/src/core/delta/delta.rs b/shared-lib/lib-ot/src/core/delta/delta.rs index 6edf817bdf..2bff1a3edc 100644 --- a/shared-lib/lib-ot/src/core/delta/delta.rs +++ b/shared-lib/lib-ot/src/core/delta/delta.rs @@ -3,7 +3,7 @@ use crate::errors::{ErrorBuilder, OTError, OTErrorCode}; use crate::core::delta::{DeltaIterator, MAX_IV_LEN}; use crate::core::flowy_str::FlowyStr; 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 serde::de::DeserializeOwned; use std::{ @@ -14,12 +14,15 @@ use std::{ str::FromStr, }; -pub type PlainTextDelta = Delta; +pub type PlainTextDelta = Delta; /// 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 /// 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)] pub struct Delta { pub ops: Vec>, @@ -182,7 +185,7 @@ where /// .insert(", AppFlowy") /// .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(); /// assert_eq!("hello, AppFlowy", &after_b); /// ``` @@ -565,7 +568,7 @@ impl Delta where T: Attributes + DeserializeOwned, { - pub fn from_delta_str(json: &str) -> Result { + pub fn from_json_str(json: &str) -> Result { let delta = serde_json::from_str(json).map_err(|e| { tracing::trace!("Deserialize failed: {:?}", e); tracing::trace!("{:?}", json); @@ -576,7 +579,7 @@ where pub fn from_bytes>(bytes: B) -> Result { 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) } } @@ -585,16 +588,16 @@ impl Delta where 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()) } - pub fn to_str(&self) -> Result { + pub fn content_str(&self) -> Result { self.apply("") } - pub fn to_delta_bytes(&self) -> Bytes { - let json = self.to_delta_str(); + pub fn to_json_bytes(&self) -> Bytes { + let json = self.to_json_str(); Bytes::from(json.into_bytes()) } } diff --git a/shared-lib/lib-ot/src/core/operation/builder.rs b/shared-lib/lib-ot/src/core/operation/builder.rs index a42ebdecb3..c2cf774617 100644 --- a/shared-lib/lib-ot/src/core/operation/builder.rs +++ b/shared-lib/lib-ot/src/core/operation/builder.rs @@ -1,8 +1,8 @@ -use crate::core::operation::{Attributes, Operation, PlainTextAttributes}; +use crate::core::operation::{Attributes, Operation, PhantomAttributes}; use crate::rich_text::RichTextAttributes; pub type RichTextOpBuilder = OperationBuilder; -pub type PlainTextOpBuilder = OperationBuilder; +pub type PlainTextOpBuilder = OperationBuilder; pub struct OperationBuilder { ty: Operation, diff --git a/shared-lib/lib-ot/src/core/operation/operation.rs b/shared-lib/lib-ot/src/core/operation/operation.rs index 8ff47d95e8..ac56025621 100644 --- a/shared-lib/lib-ot/src/core/operation/operation.rs +++ b/shared-lib/lib-ot/src/core/operation/operation.rs @@ -26,7 +26,7 @@ pub trait OperationTransformable { /// let document = PlainTextDeltaBuilder::new().build(); /// let delta = PlainTextDeltaBuilder::new().insert("abc").build(); /// 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 where @@ -65,6 +65,12 @@ pub trait OperationTransformable { 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 { fn is_empty(&self) -> bool { 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)] pub enum Operation { Delete(usize), @@ -241,9 +255,7 @@ where #[derive(Clone, Debug, Eq, PartialEq)] pub struct Retain { - // #[serde(rename(serialize = "retain", deserialize = "retain"))] pub n: usize, - // #[serde(skip_serializing_if = "is_empty")] pub attributes: T, } @@ -318,10 +330,7 @@ where #[derive(Clone, Debug, Eq, PartialEq)] pub struct Insert { - // #[serde(rename(serialize = "insert", deserialize = "insert"))] pub s: FlowyStr, - - // #[serde(skip_serializing_if = "is_empty")] pub attributes: T, } @@ -402,16 +411,16 @@ where } #[derive(Debug, Clone, Eq, PartialEq, Default, Serialize, Deserialize)] -pub struct PlainTextAttributes(); -impl fmt::Display for PlainTextAttributes { +pub struct PhantomAttributes(); +impl fmt::Display for PhantomAttributes { 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 { Ok(self.clone()) }