diff --git a/backend/src/services/doc/edit/editor.rs b/backend/src/services/doc/edit/editor.rs index e685af5a96..49b0929769 100644 --- a/backend/src/services/doc/edit/editor.rs +++ b/backend/src/services/doc/edit/editor.rs @@ -13,7 +13,7 @@ use flowy_document_infra::{ entities::ws::{WsDataType, WsDocumentData}, protobuf::{Doc, RevId, RevType, Revision, RevisionRange, UpdateDocParams}, }; -use lib_ot::core::{OperationTransformable, RichTextDelta}; +use lib_ot::{core::OperationTransformable, rich_text::RichTextDelta}; use parking_lot::RwLock; use protobuf::Message; use sqlx::PgPool; diff --git a/backend/tests/document/edit.rs b/backend/tests/document/edit.rs index d821b0d9e6..30d2be0850 100644 --- a/backend/tests/document/edit.rs +++ b/backend/tests/document/edit.rs @@ -1,6 +1,6 @@ use crate::document::helper::{DocScript, DocumentTest}; use flowy_document_infra::core::{Document, FlowyDoc}; -use lib_ot::core::{Interval, RichTextAttribute}; +use lib_ot::{core::Interval, rich_text::RichTextAttribute}; #[rustfmt::skip] // ┌─────────┐ ┌─────────┐ diff --git a/backend/tests/document/helper.rs b/backend/tests/document/helper.rs index 1e93834b9b..3c173b2028 100644 --- a/backend/tests/document/helper.rs +++ b/backend/tests/document/helper.rs @@ -12,8 +12,9 @@ use tokio::time::{sleep, Duration}; // use crate::helper::*; use crate::util::helper::{spawn_server, TestServer}; use flowy_document_infra::{entities::doc::DocIdentifier, protobuf::UpdateDocParams}; -use lib_ot::core::{RichTextAttribute, RichTextDelta, Interval}; +use lib_ot::rich_text::{RichTextAttribute, RichTextDelta}; use parking_lot::RwLock; +use lib_ot::core::Interval; pub struct DocumentTest { server: TestServer, diff --git a/frontend/rust-lib/flowy-document/src/services/doc/edit/editor.rs b/frontend/rust-lib/flowy-document/src/services/doc/edit/editor.rs index 1e8300c781..56560b18d8 100644 --- a/frontend/rust-lib/flowy-document/src/services/doc/edit/editor.rs +++ b/frontend/rust-lib/flowy-document/src/services/doc/edit/editor.rs @@ -17,7 +17,10 @@ use flowy_document_infra::{ errors::DocumentResult, }; use lib_infra::retry::{ExponentialBackoff, Retry}; -use lib_ot::core::{Interval, RichTextAttribute, RichTextDelta}; +use lib_ot::{ + core::Interval, + rich_text::{RichTextAttribute, RichTextDelta}, +}; use lib_ws::WsConnectState; use std::{convert::TryFrom, sync::Arc}; use tokio::sync::{mpsc, mpsc::UnboundedSender, oneshot}; diff --git a/frontend/rust-lib/flowy-document/src/services/doc/edit/queue.rs b/frontend/rust-lib/flowy-document/src/services/doc/edit/queue.rs index caeb062601..d5a2dc10dd 100644 --- a/frontend/rust-lib/flowy-document/src/services/doc/edit/queue.rs +++ b/frontend/rust-lib/flowy-document/src/services/doc/edit/queue.rs @@ -6,7 +6,10 @@ use flowy_document_infra::{ errors::DocumentError, }; use futures::stream::StreamExt; -use lib_ot::core::{Interval, OperationTransformable, RichTextAttribute, RichTextDelta}; +use lib_ot::{ + core::{Interval, OperationTransformable}, + rich_text::{RichTextAttribute, RichTextDelta}, +}; use std::{convert::TryFrom, sync::Arc}; use tokio::sync::{mpsc, oneshot, RwLock}; diff --git a/frontend/rust-lib/flowy-document/src/services/doc/revision/manager.rs b/frontend/rust-lib/flowy-document/src/services/doc/revision/manager.rs index 063c82d664..c765b4bbfa 100644 --- a/frontend/rust-lib/flowy-document/src/services/doc/revision/manager.rs +++ b/frontend/rust-lib/flowy-document/src/services/doc/revision/manager.rs @@ -8,7 +8,7 @@ use flowy_document_infra::{ util::RevIdCounter, }; use lib_infra::future::ResultFuture; -use lib_ot::core::{OperationTransformable, RichTextDelta}; +use lib_ot::{core::OperationTransformable, rich_text::RichTextDelta}; use std::sync::Arc; use tokio::sync::mpsc; diff --git a/frontend/rust-lib/flowy-document/src/services/doc/revision/persistence.rs b/frontend/rust-lib/flowy-document/src/services/doc/revision/persistence.rs index 1b63e33e88..6c0f808726 100644 --- a/frontend/rust-lib/flowy-document/src/services/doc/revision/persistence.rs +++ b/frontend/rust-lib/flowy-document/src/services/doc/revision/persistence.rs @@ -9,7 +9,10 @@ use flowy_database::{ConnectionPool, SqliteConnection}; use flowy_document_infra::entities::doc::{revision_from_doc, Doc, RevId, RevType, Revision, RevisionRange}; use futures::stream::StreamExt; use lib_infra::future::ResultFuture; -use lib_ot::core::{Operation, OperationTransformable, RichTextDelta}; +use lib_ot::{ + core::{Operation, OperationTransformable}, + rich_text::RichTextDelta, +}; use std::{collections::VecDeque, sync::Arc, time::Duration}; use tokio::{ sync::{broadcast, mpsc, RwLock}, diff --git a/frontend/rust-lib/flowy-document/tests/editor/attribute_test.rs b/frontend/rust-lib/flowy-document/tests/editor/attribute_test.rs index 80b987262f..957d03d2ef 100644 --- a/frontend/rust-lib/flowy-document/tests/editor/attribute_test.rs +++ b/frontend/rust-lib/flowy-document/tests/editor/attribute_test.rs @@ -1,8 +1,9 @@ #![cfg_attr(rustfmt, rustfmt::skip)] use crate::editor::{TestBuilder, TestOp::*}; use flowy_document_infra::core::{FlowyDoc, PlainDoc}; -use lib_ot::core::{Interval, OperationTransformable, NEW_LINE, WHITESPACE, FlowyStr, RichTextDelta}; +use lib_ot::core::{Interval, OperationTransformable, NEW_LINE, WHITESPACE, FlowyStr}; use unicode_segmentation::UnicodeSegmentation; +use lib_ot::rich_text::RichTextDelta; #[test] fn attributes_bold_added() { diff --git a/frontend/rust-lib/flowy-document/tests/editor/mod.rs b/frontend/rust-lib/flowy-document/tests/editor/mod.rs index 1447fda4f3..a6fb4624b5 100644 --- a/frontend/rust-lib/flowy-document/tests/editor/mod.rs +++ b/frontend/rust-lib/flowy-document/tests/editor/mod.rs @@ -6,7 +6,10 @@ mod undo_redo_test; use derive_more::Display; use flowy_document_infra::core::{CustomDocument, Document}; -use lib_ot::core::*; +use lib_ot::{ + core::*, + rich_text::{RichTextAttribute, RichTextAttributes, RichTextDelta}, +}; use rand::{prelude::*, Rng as WrappedRng}; use std::{sync::Once, time::Duration}; diff --git a/frontend/rust-lib/flowy-document/tests/editor/op_test.rs b/frontend/rust-lib/flowy-document/tests/editor/op_test.rs index deff6dbd8d..7968de167b 100644 --- a/frontend/rust-lib/flowy-document/tests/editor/op_test.rs +++ b/frontend/rust-lib/flowy-document/tests/editor/op_test.rs @@ -1,7 +1,10 @@ #![allow(clippy::all)] use crate::editor::{Rng, TestBuilder, TestOp::*}; use flowy_document_infra::core::{FlowyDoc, PlainDoc}; -use lib_ot::core::*; +use lib_ot::{ + core::*, + rich_text::{AttributeBuilder, RichTextAttribute, RichTextAttributes, RichTextDelta}, +}; #[test] fn attributes_insert_text() { diff --git a/frontend/rust-lib/flowy-document/tests/editor/serde_test.rs b/frontend/rust-lib/flowy-document/tests/editor/serde_test.rs index 5f634bce5e..a4a0ff3994 100644 --- a/frontend/rust-lib/flowy-document/tests/editor/serde_test.rs +++ b/frontend/rust-lib/flowy-document/tests/editor/serde_test.rs @@ -1,5 +1,8 @@ use flowy_document_infra::core::{Document, PlainDoc}; -use lib_ot::core::*; +use lib_ot::{ + core::*, + rich_text::{AttributeBuilder, RichTextAttribute, RichTextAttributeValue, RichTextDelta}, +}; #[test] fn operation_insert_serialize_test() { diff --git a/shared-lib/flowy-document-infra/src/core/document.rs b/shared-lib/flowy-document-infra/src/core/document.rs index 31a8f40986..0618f56ee4 100644 --- a/shared-lib/flowy-document-infra/src/core/document.rs +++ b/shared-lib/flowy-document-infra/src/core/document.rs @@ -6,7 +6,10 @@ use crate::{ errors::DocumentError, user_default::doc_initial_delta, }; -use lib_ot::core::*; +use lib_ot::{ + core::*, + rich_text::{RichTextAttribute, RichTextDelta}, +}; use tokio::sync::mpsc; pub trait CustomDocument { diff --git a/shared-lib/flowy-document-infra/src/core/extensions/delete/default_delete.rs b/shared-lib/flowy-document-infra/src/core/extensions/delete/default_delete.rs index a96f8c5a34..32c767cd5d 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/delete/default_delete.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/delete/default_delete.rs @@ -1,5 +1,8 @@ use crate::core::extensions::DeleteExt; -use lib_ot::core::{DeltaBuilder, Interval, RichTextDelta}; +use lib_ot::{ + core::{DeltaBuilder, Interval}, + rich_text::RichTextDelta, +}; pub struct DefaultDelete {} impl DeleteExt for DefaultDelete { diff --git a/shared-lib/flowy-document-infra/src/core/extensions/delete/preserve_line_format_merge.rs b/shared-lib/flowy-document-infra/src/core/extensions/delete/preserve_line_format_merge.rs index c49a40dfa5..8b8ed215e8 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/delete/preserve_line_format_merge.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/delete/preserve_line_format_merge.rs @@ -1,13 +1,7 @@ use crate::{core::extensions::DeleteExt, util::is_newline}; -use lib_ot::core::{ - plain_attributes, - Attributes, - CharMetric, - DeltaBuilder, - DeltaIter, - Interval, - RichTextDelta, - NEW_LINE, +use lib_ot::{ + core::{Attributes, CharMetric, DeltaBuilder, DeltaIter, Interval, NEW_LINE}, + rich_text::{plain_attributes, RichTextDelta}, }; pub struct PreserveLineFormatOnMerge {} diff --git a/shared-lib/flowy-document-infra/src/core/extensions/format/helper.rs b/shared-lib/flowy-document-infra/src/core/extensions/format/helper.rs index 1ecb3b3589..d800595ffb 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/format/helper.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/format/helper.rs @@ -1,5 +1,8 @@ use crate::util::find_newline; -use lib_ot::core::{plain_attributes, AttributeScope, RichTextAttribute, RichTextDelta, RichTextOperation}; +use lib_ot::{ + core::RichTextOperation, + rich_text::{plain_attributes, AttributeScope, RichTextAttribute, RichTextDelta}, +}; pub(crate) fn line_break( op: &RichTextOperation, diff --git a/shared-lib/flowy-document-infra/src/core/extensions/format/resolve_block_format.rs b/shared-lib/flowy-document-infra/src/core/extensions/format/resolve_block_format.rs index 0af20a1bac..7854a6fbc5 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/format/resolve_block_format.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/format/resolve_block_format.rs @@ -2,14 +2,9 @@ use crate::{ core::extensions::{format::helper::line_break, FormatExt}, util::find_newline, }; -use lib_ot::core::{ - plain_attributes, - AttributeScope, - DeltaBuilder, - DeltaIter, - Interval, - RichTextAttribute, - RichTextDelta, +use lib_ot::{ + core::{DeltaBuilder, DeltaIter, Interval}, + rich_text::{plain_attributes, AttributeScope, RichTextAttribute, RichTextDelta}, }; pub struct ResolveBlockFormat {} diff --git a/shared-lib/flowy-document-infra/src/core/extensions/format/resolve_inline_format.rs b/shared-lib/flowy-document-infra/src/core/extensions/format/resolve_inline_format.rs index e85839104c..82ae255559 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/format/resolve_inline_format.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/format/resolve_inline_format.rs @@ -2,7 +2,10 @@ use crate::{ core::extensions::{format::helper::line_break, FormatExt}, util::find_newline, }; -use lib_ot::core::{AttributeScope, DeltaBuilder, DeltaIter, Interval, RichTextAttribute, RichTextDelta}; +use lib_ot::{ + core::{DeltaBuilder, DeltaIter, Interval}, + rich_text::{AttributeScope, RichTextAttribute, RichTextDelta}, +}; pub struct ResolveInlineFormat {} impl FormatExt for ResolveInlineFormat { diff --git a/shared-lib/flowy-document-infra/src/core/extensions/insert/auto_exit_block.rs b/shared-lib/flowy-document-infra/src/core/extensions/insert/auto_exit_block.rs index d440cd8060..8b9fa77d2a 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/insert/auto_exit_block.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/insert/auto_exit_block.rs @@ -1,11 +1,7 @@ use crate::{core::extensions::InsertExt, util::is_newline}; -use lib_ot::core::{ - attributes_except_header, - is_empty_line_at_index, - DeltaBuilder, - DeltaIter, - RichTextAttributeKey, - RichTextDelta, +use lib_ot::{ + core::{is_empty_line_at_index, DeltaBuilder, DeltaIter}, + rich_text::{attributes_except_header, RichTextAttributeKey, RichTextDelta}, }; pub struct AutoExitBlock {} diff --git a/shared-lib/flowy-document-infra/src/core/extensions/insert/auto_format.rs b/shared-lib/flowy-document-infra/src/core/extensions/insert/auto_format.rs index 5f517a43c5..4887f80ba4 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/insert/auto_format.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/insert/auto_format.rs @@ -1,12 +1,7 @@ use crate::{core::extensions::InsertExt, util::is_whitespace}; -use lib_ot::core::{ - count_utf16_code_units, - plain_attributes, - DeltaBuilder, - DeltaIter, - RichTextAttribute, - RichTextAttributes, - RichTextDelta, +use lib_ot::{ + core::{count_utf16_code_units, DeltaBuilder, DeltaIter}, + rich_text::{plain_attributes, RichTextAttribute, RichTextAttributes, RichTextDelta}, }; use std::cmp::min; use url::Url; diff --git a/shared-lib/flowy-document-infra/src/core/extensions/insert/default_insert.rs b/shared-lib/flowy-document-infra/src/core/extensions/insert/default_insert.rs index 76f31d84e2..1d3e897427 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/insert/default_insert.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/insert/default_insert.rs @@ -1,12 +1,7 @@ use crate::core::extensions::InsertExt; -use lib_ot::core::{ - Attributes, - DeltaBuilder, - DeltaIter, - RichTextAttributeKey, - RichTextAttributes, - RichTextDelta, - NEW_LINE, +use lib_ot::{ + core::{Attributes, DeltaBuilder, DeltaIter, NEW_LINE}, + rich_text::{RichTextAttributeKey, RichTextAttributes, RichTextDelta}, }; pub struct DefaultInsertAttribute {} diff --git a/shared-lib/flowy-document-infra/src/core/extensions/insert/mod.rs b/shared-lib/flowy-document-infra/src/core/extensions/insert/mod.rs index fe8e167915..8e353018d0 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/insert/mod.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/insert/mod.rs @@ -2,7 +2,7 @@ use crate::core::extensions::InsertExt; pub use auto_exit_block::*; pub use auto_format::*; pub use default_insert::*; -use lib_ot::core::RichTextDelta; +use lib_ot::rich_text::RichTextDelta; pub use preserve_block_format::*; pub use preserve_inline_format::*; pub use reset_format_on_new_line::*; diff --git a/shared-lib/flowy-document-infra/src/core/extensions/insert/preserve_block_format.rs b/shared-lib/flowy-document-infra/src/core/extensions/insert/preserve_block_format.rs index 671b1bbe74..47301f7090 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/insert/preserve_block_format.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/insert/preserve_block_format.rs @@ -1,14 +1,14 @@ use crate::{core::extensions::InsertExt, util::is_newline}; -use lib_ot::core::{ - attributes_except_header, - plain_attributes, - DeltaBuilder, - DeltaIter, - RichTextAttribute, - RichTextAttributeKey, - RichTextAttributes, - RichTextDelta, - NEW_LINE, +use lib_ot::{ + core::{DeltaBuilder, DeltaIter, NEW_LINE}, + rich_text::{ + attributes_except_header, + plain_attributes, + RichTextAttribute, + RichTextAttributeKey, + RichTextAttributes, + RichTextDelta, + }, }; pub struct PreserveBlockFormatOnInsert {} diff --git a/shared-lib/flowy-document-infra/src/core/extensions/insert/preserve_inline_format.rs b/shared-lib/flowy-document-infra/src/core/extensions/insert/preserve_inline_format.rs index 11b4e188fc..d97d913c31 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/insert/preserve_inline_format.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/insert/preserve_inline_format.rs @@ -2,14 +2,9 @@ use crate::{ core::extensions::InsertExt, util::{contain_newline, is_newline}, }; -use lib_ot::core::{ - plain_attributes, - DeltaBuilder, - DeltaIter, - OpNewline, - RichTextAttributeKey, - RichTextDelta, - NEW_LINE, +use lib_ot::{ + core::{DeltaBuilder, DeltaIter, OpNewline, NEW_LINE}, + rich_text::{plain_attributes, RichTextAttributeKey, RichTextDelta}, }; pub struct PreserveInlineFormat {} diff --git a/shared-lib/flowy-document-infra/src/core/extensions/insert/reset_format_on_new_line.rs b/shared-lib/flowy-document-infra/src/core/extensions/insert/reset_format_on_new_line.rs index d1abaf3179..4e22c5767c 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/insert/reset_format_on_new_line.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/insert/reset_format_on_new_line.rs @@ -1,12 +1,7 @@ use crate::{core::extensions::InsertExt, util::is_newline}; -use lib_ot::core::{ - CharMetric, - DeltaBuilder, - DeltaIter, - RichTextAttributeKey, - RichTextAttributes, - RichTextDelta, - NEW_LINE, +use lib_ot::{ + core::{CharMetric, DeltaBuilder, DeltaIter, NEW_LINE}, + rich_text::{RichTextAttributeKey, RichTextAttributes, RichTextDelta}, }; pub struct ResetLineFormatOnNewLine {} diff --git a/shared-lib/flowy-document-infra/src/core/extensions/mod.rs b/shared-lib/flowy-document-infra/src/core/extensions/mod.rs index 31f8b1189c..4c67f6b888 100644 --- a/shared-lib/flowy-document-infra/src/core/extensions/mod.rs +++ b/shared-lib/flowy-document-infra/src/core/extensions/mod.rs @@ -2,7 +2,10 @@ pub use delete::*; pub use format::*; pub use insert::*; -use lib_ot::core::{Interval, RichTextAttribute, RichTextDelta}; +use lib_ot::{ + core::Interval, + rich_text::{RichTextAttribute, RichTextDelta}, +}; mod delete; mod format; diff --git a/shared-lib/flowy-document-infra/src/core/history.rs b/shared-lib/flowy-document-infra/src/core/history.rs index 82c411b109..a233263824 100644 --- a/shared-lib/flowy-document-infra/src/core/history.rs +++ b/shared-lib/flowy-document-infra/src/core/history.rs @@ -1,4 +1,4 @@ -use lib_ot::core::RichTextDelta; +use lib_ot::rich_text::RichTextDelta; const MAX_UNDOS: usize = 20; diff --git a/shared-lib/flowy-document-infra/src/core/view.rs b/shared-lib/flowy-document-infra/src/core/view.rs index cd2d61c73e..9f5335def5 100644 --- a/shared-lib/flowy-document-infra/src/core/view.rs +++ b/shared-lib/flowy-document-infra/src/core/view.rs @@ -1,7 +1,8 @@ use crate::core::extensions::*; use lib_ot::{ - core::{trim, Interval, RichTextAttribute, RichTextDelta}, + core::{trim, Interval}, errors::{ErrorBuilder, OTError, OTErrorCode}, + rich_text::{RichTextAttribute, RichTextDelta}, }; pub const RECORD_THRESHOLD: usize = 400; // in milliseconds diff --git a/shared-lib/flowy-document-infra/src/entities/doc/doc.rs b/shared-lib/flowy-document-infra/src/entities/doc/doc.rs index 90b4c73056..151bef5b57 100644 --- a/shared-lib/flowy-document-infra/src/entities/doc/doc.rs +++ b/shared-lib/flowy-document-infra/src/entities/doc/doc.rs @@ -1,5 +1,5 @@ use flowy_derive::ProtoBuf; -use lib_ot::{core::RichTextDelta, errors::OTError}; +use lib_ot::{errors::OTError, rich_text::RichTextDelta}; #[derive(ProtoBuf, Default, Debug, Clone)] pub struct CreateDocParams { diff --git a/shared-lib/flowy-document-infra/src/entities/doc/revision.rs b/shared-lib/flowy-document-infra/src/entities/doc/revision.rs index ba4a68d86d..cb0ea3ce25 100644 --- a/shared-lib/flowy-document-infra/src/entities/doc/revision.rs +++ b/shared-lib/flowy-document-infra/src/entities/doc/revision.rs @@ -1,6 +1,6 @@ use crate::{entities::doc::Doc, util::md5}; use flowy_derive::{ProtoBuf, ProtoBuf_Enum}; -use lib_ot::core::RichTextDelta; +use lib_ot::rich_text::RichTextDelta; use std::{fmt::Formatter, ops::RangeInclusive}; #[derive(Debug, ProtoBuf_Enum, Clone, Eq, PartialEq)] diff --git a/shared-lib/flowy-document-infra/src/user_default.rs b/shared-lib/flowy-document-infra/src/user_default.rs index 91cf28321d..961d3a377a 100644 --- a/shared-lib/flowy-document-infra/src/user_default.rs +++ b/shared-lib/flowy-document-infra/src/user_default.rs @@ -1,4 +1,4 @@ -use lib_ot::core::{DeltaBuilder, RichTextDelta}; +use lib_ot::{core::DeltaBuilder, rich_text::RichTextDelta}; #[inline] pub fn doc_initial_delta() -> RichTextDelta { DeltaBuilder::new().insert("\n").build() } diff --git a/shared-lib/lib-ot/src/core/attributes/attributes.rs b/shared-lib/lib-ot/src/core/attributes/attributes.rs deleted file mode 100644 index c52c4a7898..0000000000 --- a/shared-lib/lib-ot/src/core/attributes/attributes.rs +++ /dev/null @@ -1,170 +0,0 @@ -use crate::{ - core::{ - Attributes, - OperationTransformable, - RichTextAttribute, - RichTextAttributeKey, - RichTextAttributeValue, - RichTextOperation, - }, - errors::OTError, -}; -use std::{collections::HashMap, fmt}; - -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct RichTextAttributes { - pub(crate) inner: HashMap, -} - -impl std::default::Default for RichTextAttributes { - fn default() -> Self { - Self { - inner: HashMap::with_capacity(0), - } - } -} - -impl fmt::Display for RichTextAttributes { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_fmt(format_args!("{:?}", self.inner)) } -} - -pub fn plain_attributes() -> RichTextAttributes { RichTextAttributes::default() } - -impl RichTextAttributes { - pub fn new() -> Self { RichTextAttributes { inner: HashMap::new() } } - - pub fn is_empty(&self) -> bool { self.inner.is_empty() } - - pub fn add(&mut self, attribute: RichTextAttribute) { - let RichTextAttribute { key, value, scope: _ } = attribute; - self.inner.insert(key, value); - } - - pub fn add_kv(&mut self, key: RichTextAttributeKey, value: RichTextAttributeValue) { - self.inner.insert(key, value); - } - - pub fn delete(&mut self, key: &RichTextAttributeKey) { - self.inner.insert(key.clone(), RichTextAttributeValue(None)); - } - - pub fn mark_all_as_removed_except(&mut self, attribute: Option) { - match attribute { - None => { - self.inner.iter_mut().for_each(|(_k, v)| v.0 = None); - }, - Some(attribute) => { - self.inner.iter_mut().for_each(|(k, v)| { - if k != &attribute { - v.0 = None; - } - }); - }, - } - } - - pub fn remove(&mut self, key: RichTextAttributeKey) { self.inner.retain(|k, _| k != &key); } - - // pub fn block_attributes_except_header(attributes: &Attributes) -> Attributes - // { let mut new_attributes = Attributes::new(); - // attributes.iter().for_each(|(k, v)| { - // if k != &AttributeKey::Header { - // new_attributes.insert(k.clone(), v.clone()); - // } - // }); - // - // new_attributes - // } - - // Update inner by constructing new attributes from the other if it's - // not None and replace the key/value with self key/value. - pub fn merge(&mut self, other: Option) { - if other.is_none() { - return; - } - - let mut new_attributes = other.unwrap().inner; - self.inner.iter().for_each(|(k, v)| { - new_attributes.insert(k.clone(), v.clone()); - }); - self.inner = new_attributes; - } -} - -impl Attributes for RichTextAttributes { - fn is_empty(&self) -> bool { self.inner.is_empty() } - - fn remove_empty(&mut self) { self.inner.retain(|_, v| v.0.is_some()); } - - fn extend_other(&mut self, other: Self) { self.inner.extend(other.inner); } -} - -impl OperationTransformable for RichTextAttributes { - fn compose(&self, other: &Self) -> Result - where - Self: Sized, - { - let mut attributes = self.clone(); - attributes.extend_other(other.clone()); - Ok(attributes) - } - - fn transform(&self, other: &Self) -> Result<(Self, Self), OTError> - where - Self: Sized, - { - let a = self - .iter() - .fold(RichTextAttributes::new(), |mut new_attributes, (k, v)| { - if !other.contains_key(k) { - new_attributes.insert(k.clone(), v.clone()); - } - new_attributes - }); - - let b = other - .iter() - .fold(RichTextAttributes::new(), |mut new_attributes, (k, v)| { - if !self.contains_key(k) { - new_attributes.insert(k.clone(), v.clone()); - } - new_attributes - }); - - Ok((a, b)) - } - - fn invert(&self, other: &Self) -> Self { - let base_inverted = other.iter().fold(RichTextAttributes::new(), |mut attributes, (k, v)| { - if other.get(k) != self.get(k) && self.contains_key(k) { - attributes.insert(k.clone(), v.clone()); - } - attributes - }); - - let inverted = self.iter().fold(base_inverted, |mut attributes, (k, _)| { - if other.get(k) != self.get(k) && !other.contains_key(k) { - attributes.delete(k); - } - attributes - }); - - inverted - } -} - -impl std::ops::Deref for RichTextAttributes { - type Target = HashMap; - - fn deref(&self) -> &Self::Target { &self.inner } -} - -impl std::ops::DerefMut for RichTextAttributes { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner } -} - -pub fn attributes_except_header(op: &RichTextOperation) -> RichTextAttributes { - let mut attributes = op.get_attributes(); - attributes.remove(RichTextAttributeKey::Header); - attributes -} diff --git a/shared-lib/lib-ot/src/core/delta/delta.rs b/shared-lib/lib-ot/src/core/delta/delta.rs index fc78cd3ae3..d219a14288 100644 --- a/shared-lib/lib-ot/src/core/delta/delta.rs +++ b/shared-lib/lib-ot/src/core/delta/delta.rs @@ -1,5 +1,5 @@ use crate::{ - core::{attributes::*, operation::*, DeltaIter, FlowyStr, Interval, OperationTransformable, MAX_IV_LEN}, + core::{operation::*, DeltaIter, FlowyStr, Interval, OperationTransformable, MAX_IV_LEN}, errors::{ErrorBuilder, OTError, OTErrorCode}, }; use bytes::Bytes; @@ -469,8 +469,6 @@ fn transform_op_attribute(left: &Option>, right: &Op left.transform(&right).unwrap().0 } -pub type RichTextDelta = Delta; - impl Delta where T: Attributes + DeserializeOwned, @@ -503,22 +501,31 @@ where } } -impl FromStr for RichTextDelta { +impl FromStr for Delta +where + T: Attributes, +{ type Err = (); - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result, Self::Err> { let mut delta = Delta::with_capacity(1); delta.add(Operation::Insert(s.into())); Ok(delta) } } -impl std::convert::TryFrom> for RichTextDelta { +impl std::convert::TryFrom> for Delta +where + T: Attributes + DeserializeOwned, +{ type Error = OTError; fn try_from(bytes: Vec) -> Result { Delta::from_bytes(bytes) } } -impl std::convert::TryFrom for RichTextDelta { +impl std::convert::TryFrom for Delta +where + T: Attributes + DeserializeOwned, +{ type Error = OTError; fn try_from(bytes: Bytes) -> Result { Delta::from_bytes(&bytes) } diff --git a/shared-lib/lib-ot/src/core/delta/iterator.rs b/shared-lib/lib-ot/src/core/delta/iterator.rs index 478428877c..5a45e21ca8 100644 --- a/shared-lib/lib-ot/src/core/delta/iterator.rs +++ b/shared-lib/lib-ot/src/core/delta/iterator.rs @@ -1,5 +1,8 @@ use super::cursor::*; -use crate::core::{Attributes, Delta, Interval, Operation, RichTextAttributes, NEW_LINE}; +use crate::{ + core::{Attributes, Delta, Interval, Operation, NEW_LINE}, + rich_text::RichTextAttributes, +}; use std::ops::{Deref, DerefMut}; pub(crate) const MAX_IV_LEN: usize = i32::MAX as usize; diff --git a/shared-lib/lib-ot/src/core/mod.rs b/shared-lib/lib-ot/src/core/mod.rs index 6897ef8c63..b5bc594246 100644 --- a/shared-lib/lib-ot/src/core/mod.rs +++ b/shared-lib/lib-ot/src/core/mod.rs @@ -1,11 +1,9 @@ -mod attributes; mod delta; mod flowy_str; mod interval; mod operation; use crate::errors::OTError; -pub use attributes::*; pub use delta::*; pub use flowy_str::*; pub use interval::*; diff --git a/shared-lib/lib-ot/src/core/operation/builder.rs b/shared-lib/lib-ot/src/core/operation/builder.rs index d7484a3613..efd63c2bff 100644 --- a/shared-lib/lib-ot/src/core/operation/builder.rs +++ b/shared-lib/lib-ot/src/core/operation/builder.rs @@ -1,4 +1,7 @@ -use crate::core::{Attributes, Operation, RichTextAttributes}; +use crate::{ + core::{Attributes, Operation}, + rich_text::RichTextAttributes, +}; pub type RichTextOpBuilder = OpBuilder; diff --git a/shared-lib/lib-ot/src/core/operation/operation.rs b/shared-lib/lib-ot/src/core/operation/operation.rs index c8f74370e2..13a775e356 100644 --- a/shared-lib/lib-ot/src/core/operation/operation.rs +++ b/shared-lib/lib-ot/src/core/operation/operation.rs @@ -1,4 +1,7 @@ -use crate::core::{FlowyStr, Interval, OpBuilder, OperationTransformable, RichTextAttribute, RichTextAttributes}; +use crate::{ + core::{FlowyStr, Interval, OpBuilder, OperationTransformable}, + rich_text::{RichTextAttribute, RichTextAttributes}, +}; use serde::__private::Formatter; use std::{ cmp::min, diff --git a/shared-lib/lib-ot/src/lib.rs b/shared-lib/lib-ot/src/lib.rs index fec82dd9cf..5a3be0ede7 100644 --- a/shared-lib/lib-ot/src/lib.rs +++ b/shared-lib/lib-ot/src/lib.rs @@ -1,2 +1,3 @@ pub mod core; pub mod errors; +pub mod rich_text; diff --git a/shared-lib/lib-ot/src/core/attributes/attribute.rs b/shared-lib/lib-ot/src/rich_text/attributes.rs similarity index 51% rename from shared-lib/lib-ot/src/core/attributes/attribute.rs rename to shared-lib/lib-ot/src/rich_text/attributes.rs index 45fe5fbec9..5af66a817a 100644 --- a/shared-lib/lib-ot/src/core/attributes/attribute.rs +++ b/shared-lib/lib-ot/src/rich_text/attributes.rs @@ -1,11 +1,179 @@ #![allow(non_snake_case)] - -use crate::{block_attribute, core::RichTextAttributes, ignore_attribute, inline_attribute, list_attribute}; +use crate::{ + block_attribute, + core::{Attributes, OperationTransformable, RichTextOperation}, + errors::OTError, + ignore_attribute, + inline_attribute, + list_attribute, +}; use lazy_static::lazy_static; - -use std::{collections::HashSet, fmt, fmt::Formatter, iter::FromIterator}; +use std::{ + collections::{HashMap, HashSet}, + fmt, + fmt::Formatter, + iter::FromIterator, +}; use strum_macros::Display; +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct RichTextAttributes { + pub(crate) inner: HashMap, +} + +impl std::default::Default for RichTextAttributes { + fn default() -> Self { + Self { + inner: HashMap::with_capacity(0), + } + } +} + +impl fmt::Display for RichTextAttributes { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_fmt(format_args!("{:?}", self.inner)) } +} + +pub fn plain_attributes() -> RichTextAttributes { RichTextAttributes::default() } + +impl RichTextAttributes { + pub fn new() -> Self { RichTextAttributes { inner: HashMap::new() } } + + pub fn is_empty(&self) -> bool { self.inner.is_empty() } + + pub fn add(&mut self, attribute: RichTextAttribute) { + let RichTextAttribute { key, value, scope: _ } = attribute; + self.inner.insert(key, value); + } + + pub fn add_kv(&mut self, key: RichTextAttributeKey, value: RichTextAttributeValue) { + self.inner.insert(key, value); + } + + pub fn delete(&mut self, key: &RichTextAttributeKey) { + self.inner.insert(key.clone(), RichTextAttributeValue(None)); + } + + pub fn mark_all_as_removed_except(&mut self, attribute: Option) { + match attribute { + None => { + self.inner.iter_mut().for_each(|(_k, v)| v.0 = None); + }, + Some(attribute) => { + self.inner.iter_mut().for_each(|(k, v)| { + if k != &attribute { + v.0 = None; + } + }); + }, + } + } + + pub fn remove(&mut self, key: RichTextAttributeKey) { self.inner.retain(|k, _| k != &key); } + + // pub fn block_attributes_except_header(attributes: &Attributes) -> Attributes + // { let mut new_attributes = Attributes::new(); + // attributes.iter().for_each(|(k, v)| { + // if k != &AttributeKey::Header { + // new_attributes.insert(k.clone(), v.clone()); + // } + // }); + // + // new_attributes + // } + + // Update inner by constructing new attributes from the other if it's + // not None and replace the key/value with self key/value. + pub fn merge(&mut self, other: Option) { + if other.is_none() { + return; + } + + let mut new_attributes = other.unwrap().inner; + self.inner.iter().for_each(|(k, v)| { + new_attributes.insert(k.clone(), v.clone()); + }); + self.inner = new_attributes; + } +} + +impl Attributes for RichTextAttributes { + fn is_empty(&self) -> bool { self.inner.is_empty() } + + fn remove_empty(&mut self) { self.inner.retain(|_, v| v.0.is_some()); } + + fn extend_other(&mut self, other: Self) { self.inner.extend(other.inner); } +} + +impl OperationTransformable for RichTextAttributes { + fn compose(&self, other: &Self) -> Result + where + Self: Sized, + { + let mut attributes = self.clone(); + attributes.extend_other(other.clone()); + Ok(attributes) + } + + fn transform(&self, other: &Self) -> Result<(Self, Self), OTError> + where + Self: Sized, + { + let a = self + .iter() + .fold(RichTextAttributes::new(), |mut new_attributes, (k, v)| { + if !other.contains_key(k) { + new_attributes.insert(k.clone(), v.clone()); + } + new_attributes + }); + + let b = other + .iter() + .fold(RichTextAttributes::new(), |mut new_attributes, (k, v)| { + if !self.contains_key(k) { + new_attributes.insert(k.clone(), v.clone()); + } + new_attributes + }); + + Ok((a, b)) + } + + fn invert(&self, other: &Self) -> Self { + let base_inverted = other.iter().fold(RichTextAttributes::new(), |mut attributes, (k, v)| { + if other.get(k) != self.get(k) && self.contains_key(k) { + attributes.insert(k.clone(), v.clone()); + } + attributes + }); + + let inverted = self.iter().fold(base_inverted, |mut attributes, (k, _)| { + if other.get(k) != self.get(k) && !other.contains_key(k) { + attributes.delete(k); + } + attributes + }); + + inverted + } +} + +impl std::ops::Deref for RichTextAttributes { + type Target = HashMap; + + fn deref(&self) -> &Self::Target { &self.inner } +} + +impl std::ops::DerefMut for RichTextAttributes { + fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner } +} + +pub fn attributes_except_header(op: &RichTextOperation) -> RichTextAttributes { + let mut attributes = op.get_attributes(); + attributes.remove(RichTextAttributeKey::Header); + attributes +} + #[derive(Debug, Clone)] pub struct RichTextAttribute { pub key: RichTextAttributeKey, diff --git a/shared-lib/lib-ot/src/core/attributes/attributes_serde.rs b/shared-lib/lib-ot/src/rich_text/attributes_serde.rs similarity index 98% rename from shared-lib/lib-ot/src/core/attributes/attributes_serde.rs rename to shared-lib/lib-ot/src/rich_text/attributes_serde.rs index 5552297b72..87506ecd99 100644 --- a/shared-lib/lib-ot/src/core/attributes/attributes_serde.rs +++ b/shared-lib/lib-ot/src/rich_text/attributes_serde.rs @@ -1,6 +1,5 @@ #[rustfmt::skip] -use crate::core::RichTextAttributeValue; -use crate::core::{RichTextAttribute, RichTextAttributeKey, RichTextAttributes}; +use crate::rich_text::{RichTextAttribute, RichTextAttributeKey, RichTextAttributes, RichTextAttributeValue}; use serde::{ de, de::{MapAccess, Visitor}, diff --git a/shared-lib/lib-ot/src/core/attributes/builder.rs b/shared-lib/lib-ot/src/rich_text/builder.rs similarity index 89% rename from shared-lib/lib-ot/src/core/attributes/builder.rs rename to shared-lib/lib-ot/src/rich_text/builder.rs index 98c0721371..40d9666d1d 100644 --- a/shared-lib/lib-ot/src/core/attributes/builder.rs +++ b/shared-lib/lib-ot/src/rich_text/builder.rs @@ -1,5 +1,6 @@ #![allow(non_snake_case)] -use crate::core::{RichTextAttribute, RichTextAttributes}; +use crate::rich_text::{RichTextAttribute, RichTextAttributes}; + pub struct AttributeBuilder { inner: RichTextAttributes, } diff --git a/shared-lib/lib-ot/src/rich_text/delta.rs b/shared-lib/lib-ot/src/rich_text/delta.rs new file mode 100644 index 0000000000..03f3b2479f --- /dev/null +++ b/shared-lib/lib-ot/src/rich_text/delta.rs @@ -0,0 +1,3 @@ +use crate::{core::Delta, rich_text::RichTextAttributes}; + +pub type RichTextDelta = Delta; diff --git a/shared-lib/lib-ot/src/core/attributes/macros.rs b/shared-lib/lib-ot/src/rich_text/macros.rs similarity index 100% rename from shared-lib/lib-ot/src/core/attributes/macros.rs rename to shared-lib/lib-ot/src/rich_text/macros.rs diff --git a/shared-lib/lib-ot/src/core/attributes/mod.rs b/shared-lib/lib-ot/src/rich_text/mod.rs similarity index 62% rename from shared-lib/lib-ot/src/core/attributes/mod.rs rename to shared-lib/lib-ot/src/rich_text/mod.rs index 8df7b064a3..c4f09161ac 100644 --- a/shared-lib/lib-ot/src/core/attributes/mod.rs +++ b/shared-lib/lib-ot/src/rich_text/mod.rs @@ -1,12 +1,11 @@ -#![allow(clippy::module_inception)] -mod attribute; mod attributes; mod attributes_serde; mod builder; #[macro_use] mod macros; +mod delta; -pub use attribute::*; pub use attributes::*; pub use builder::*; +pub use delta::*; diff --git a/shared-lib/lib-ot/tests/main.rs b/shared-lib/lib-ot/tests/main.rs new file mode 100644 index 0000000000..e69de29bb2