Merge pull request #1039 from AppFlowy-IO/refactor/lib_ot_attribute

Refactor/lib ot attribute
This commit is contained in:
Nathan.fooo
2022-09-13 12:14:32 +08:00
committed by GitHub
62 changed files with 914 additions and 1347 deletions

View File

@ -1172,6 +1172,7 @@ dependencies = [
"strum_macros", "strum_macros",
"tokio", "tokio",
"tracing", "tracing",
"tracing-subscriber",
"unicode-segmentation", "unicode-segmentation",
"url", "url",
] ]

View File

@ -49,8 +49,8 @@ pub fn make_default_board() -> BuildGridContext {
let done_option = SelectOptionPB::with_color("Done", SelectOptionColorPB::Yellow); let done_option = SelectOptionPB::with_color("Done", SelectOptionColorPB::Yellow);
let single_select_type_option = SingleSelectTypeOptionBuilder::default() let single_select_type_option = SingleSelectTypeOptionBuilder::default()
.add_option(to_do_option.clone()) .add_option(to_do_option.clone())
.add_option(doing_option.clone()) .add_option(doing_option)
.add_option(done_option.clone()); .add_option(done_option);
let single_select_field = FieldBuilder::new(single_select_type_option) let single_select_field = FieldBuilder::new(single_select_type_option)
.name("Status") .name("Status")
.visibility(true) .visibility(true)

View File

@ -9,8 +9,8 @@ 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, EmptyAttributes, Operations}; use lib_ot::core::{Attributes, EmptyAttributes, OperationAttributes, Operations};
use lib_ot::text_delta::TextAttributes;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use std::{convert::TryFrom, sync::Arc}; use std::{convert::TryFrom, sync::Arc};
@ -18,7 +18,7 @@ pub type DeltaMD5 = String;
pub trait ConflictResolver<T> pub trait ConflictResolver<T>
where where
T: Attributes + Send + Sync, T: OperationAttributes + Send + Sync,
{ {
fn compose_delta(&self, delta: Operations<T>) -> BoxResultFuture<DeltaMD5, FlowyError>; fn compose_delta(&self, delta: Operations<T>) -> BoxResultFuture<DeltaMD5, FlowyError>;
fn transform_delta(&self, delta: Operations<T>) -> BoxResultFuture<TransformDeltas<T>, FlowyError>; fn transform_delta(&self, delta: Operations<T>) -> BoxResultFuture<TransformDeltas<T>, FlowyError>;
@ -30,12 +30,12 @@ pub trait ConflictRevisionSink: Send + Sync + 'static {
fn ack(&self, rev_id: String, ty: ServerRevisionWSDataType) -> BoxResultFuture<(), FlowyError>; fn ack(&self, rev_id: String, ty: ServerRevisionWSDataType) -> BoxResultFuture<(), FlowyError>;
} }
pub type RichTextConflictController = ConflictController<TextAttributes>; pub type RichTextConflictController = ConflictController<Attributes>;
pub type PlainTextConflictController = ConflictController<EmptyAttributes>; pub type PlainTextConflictController = ConflictController<EmptyAttributes>;
pub struct ConflictController<T> pub struct ConflictController<T>
where where
T: Attributes + Send + Sync, T: OperationAttributes + Send + Sync,
{ {
user_id: String, user_id: String,
resolver: Arc<dyn ConflictResolver<T> + Send + Sync>, resolver: Arc<dyn ConflictResolver<T> + Send + Sync>,
@ -45,7 +45,7 @@ where
impl<T> ConflictController<T> impl<T> ConflictController<T>
where where
T: Attributes + Send + Sync + DeserializeOwned + serde::Serialize, T: OperationAttributes + Send + Sync + DeserializeOwned + serde::Serialize,
{ {
pub fn new( pub fn new(
user_id: &str, user_id: &str,
@ -147,7 +147,7 @@ fn make_client_and_server_revision<T>(
md5: String, md5: String,
) -> (Revision, Option<Revision>) ) -> (Revision, Option<Revision>)
where where
T: Attributes + serde::Serialize, T: OperationAttributes + serde::Serialize,
{ {
let (base_rev_id, rev_id) = rev_manager.next_rev_id_pair(); let (base_rev_id, rev_id) = rev_manager.next_rev_id_pair();
let client_revision = Revision::new( let client_revision = Revision::new(
@ -175,11 +175,11 @@ where
} }
} }
pub type RichTextTransformDeltas = TransformDeltas<TextAttributes>; pub type RichTextTransformDeltas = TransformDeltas<Attributes>;
pub struct TransformDeltas<T> pub struct TransformDeltas<T>
where where
T: Attributes, T: OperationAttributes,
{ {
pub client_prime: Operations<T>, pub client_prime: Operations<T>,
pub server_prime: Option<Operations<T>>, pub server_prime: Option<Operations<T>>,

View File

@ -26,6 +26,7 @@ unicode-segmentation = "1.8"
log = "0.4.14" log = "0.4.14"
tokio = {version = "1", features = ["sync"]} tokio = {version = "1", features = ["sync"]}
tracing = { version = "0.1", features = ["log"] } tracing = { version = "0.1", features = ["log"] }
bytes = { version = "1.1" } bytes = { version = "1.1" }
strum = "0.21" strum = "0.21"
strum_macros = "0.21" strum_macros = "0.21"
@ -42,6 +43,7 @@ futures = "0.3.15"
flowy-test = { path = "../flowy-test" } flowy-test = { path = "../flowy-test" }
flowy-text-block = { path = "../flowy-text-block", features = ["flowy_unit_test"]} flowy-text-block = { path = "../flowy-text-block", features = ["flowy_unit_test"]}
derive_more = {version = "0.99", features = ["display"]} derive_more = {version = "0.99", features = ["display"]}
tracing-subscriber = "0.2.0"
color-eyre = { version = "0.5", default-features = false } color-eyre = { version = "0.5", default-features = false }
criterion = "0.3" criterion = "0.3"

View File

@ -13,9 +13,10 @@ use flowy_sync::{
errors::CollaborateResult, errors::CollaborateResult,
util::make_delta_from_revisions, util::make_delta_from_revisions,
}; };
use lib_ot::core::AttributeEntry;
use lib_ot::{ use lib_ot::{
core::{Interval, Operation}, core::{Interval, Operation},
text_delta::{TextAttribute, TextDelta}, text_delta::TextDelta,
}; };
use lib_ws::WSConnectState; use lib_ws::WSConnectState;
use std::sync::Arc; use std::sync::Arc;
@ -85,7 +86,7 @@ impl TextBlockEditor {
Ok(()) Ok(())
} }
pub async fn format(&self, interval: Interval, attribute: TextAttribute) -> Result<(), FlowyError> { pub async fn format(&self, interval: Interval, attribute: AttributeEntry) -> Result<(), FlowyError> {
let (ret, rx) = oneshot::channel::<CollaborateResult<()>>(); let (ret, rx) = oneshot::channel::<CollaborateResult<()>>();
let msg = EditorCommand::Format { let msg = EditorCommand::Format {
interval, interval,

View File

@ -11,9 +11,10 @@ use flowy_sync::{
errors::CollaborateError, errors::CollaborateError,
}; };
use futures::stream::StreamExt; use futures::stream::StreamExt;
use lib_ot::core::{AttributeEntry, Attributes};
use lib_ot::{ use lib_ot::{
core::{Interval, OperationTransform}, core::{Interval, OperationTransform},
text_delta::{TextAttribute, TextAttributes, TextDelta}, text_delta::TextDelta,
}; };
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::{oneshot, RwLock}; use tokio::sync::{oneshot, RwLock};
@ -194,7 +195,7 @@ impl EditBlockQueue {
pub(crate) struct TextBlockRevisionCompactor(); 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::<TextAttributes>(revisions)?; let delta = make_delta_from_revisions::<Attributes>(revisions)?;
Ok(delta.json_bytes()) Ok(delta.json_bytes())
} }
} }
@ -229,7 +230,7 @@ pub(crate) enum EditorCommand {
}, },
Format { Format {
interval: Interval, interval: Interval,
attribute: TextAttribute, attribute: AttributeEntry,
ret: Ret<()>, ret: Ret<()>,
}, },
Replace { Replace {

View File

@ -10,7 +10,8 @@ use flowy_sync::{
errors::CollaborateResult, errors::CollaborateResult,
}; };
use lib_infra::future::{BoxResultFuture, FutureResult}; use lib_infra::future::{BoxResultFuture, FutureResult};
use lib_ot::text_delta::TextAttributes; use lib_ot::core::Attributes;
use lib_ot::text_delta::TextDelta; use lib_ot::text_delta::TextDelta;
use lib_ws::WSConnectState; use lib_ws::WSConnectState;
use std::{sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
@ -111,7 +112,7 @@ struct TextBlockConflictResolver {
edit_cmd_tx: EditorCommandSender, edit_cmd_tx: EditorCommandSender,
} }
impl ConflictResolver<TextAttributes> for TextBlockConflictResolver { impl ConflictResolver<Attributes> for TextBlockConflictResolver {
fn compose_delta(&self, delta: TextDelta) -> BoxResultFuture<DeltaMD5, FlowyError> { fn compose_delta(&self, delta: TextDelta) -> BoxResultFuture<DeltaMD5, FlowyError> {
let tx = self.edit_cmd_tx.clone(); let tx = self.edit_cmd_tx.clone();
Box::pin(async move { Box::pin(async move {

View File

@ -1,6 +1,6 @@
#![cfg_attr(rustfmt, rustfmt::skip)] #![cfg_attr(rustfmt, rustfmt::skip)]
use crate::editor::{TestBuilder, TestOp::*}; use crate::editor::{TestBuilder, TestOp::*};
use flowy_sync::client_document::{NewlineDoc, PlainDoc}; use flowy_sync::client_document::{NewlineDoc, EmptyDoc};
use lib_ot::core::{Interval, OperationTransform, NEW_LINE, WHITESPACE, OTString}; use lib_ot::core::{Interval, OperationTransform, NEW_LINE, WHITESPACE, OTString};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use lib_ot::text_delta::TextDelta; use lib_ot::text_delta::TextDelta;
@ -14,12 +14,12 @@ fn attributes_bold_added() {
0, 0,
r#"[ r#"[
{"insert":"123"}, {"insert":"123"},
{"insert":"45","attributes":{"bold":"true"}}, {"insert":"45","attributes":{"bold":true}},
{"insert":"6"} {"insert":"6"}
]"#, ]"#,
), ),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -27,11 +27,11 @@ fn attributes_bold_added_and_invert_all() {
let ops = vec![ let ops = vec![
Insert(0, "123", 0), Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true), Bold(0, Interval::new(0, 3), true),
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":true}}]"#),
Bold(0, Interval::new(0, 3), false), Bold(0, Interval::new(0, 3), false),
AssertDocJson(0, r#"[{"insert":"123"}]"#), AssertDocJson(0, r#"[{"insert":"123"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -39,11 +39,11 @@ fn attributes_bold_added_and_invert_partial_suffix() {
let ops = vec![ let ops = vec![
Insert(0, "1234", 0), Insert(0, "1234", 0),
Bold(0, Interval::new(0, 4), true), Bold(0, Interval::new(0, 4), true),
AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":true}}]"#),
Bold(0, Interval::new(2, 4), false), Bold(0, Interval::new(2, 4), false),
AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34"}]"#), AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":true}},{"insert":"34"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -51,13 +51,13 @@ fn attributes_bold_added_and_invert_partial_suffix2() {
let ops = vec![ let ops = vec![
Insert(0, "1234", 0), Insert(0, "1234", 0),
Bold(0, Interval::new(0, 4), true), Bold(0, Interval::new(0, 4), true),
AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":true}}]"#),
Bold(0, Interval::new(2, 4), false), Bold(0, Interval::new(2, 4), false),
AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34"}]"#), AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":true}},{"insert":"34"}]"#),
Bold(0, Interval::new(2, 4), true), Bold(0, Interval::new(2, 4), true),
AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -67,22 +67,22 @@ fn attributes_bold_added_with_new_line() {
Bold(0, Interval::new(0, 6), true), Bold(0, Interval::new(0, 6), true),
AssertDocJson( AssertDocJson(
0, 0,
r#"[{"insert":"123456","attributes":{"bold":"true"}},{"insert":"\n"}]"#, r#"[{"insert":"123456","attributes":{"bold":true}},{"insert":"\n"}]"#,
), ),
Insert(0, "\n", 3), Insert(0, "\n", 3),
AssertDocJson( AssertDocJson(
0, 0,
r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"},{"insert":"456","attributes":{"bold":"true"}},{"insert":"\n"}]"#, r#"[{"insert":"123","attributes":{"bold":true}},{"insert":"\n"},{"insert":"456","attributes":{"bold":true}},{"insert":"\n"}]"#,
), ),
Insert(0, "\n", 4), Insert(0, "\n", 4),
AssertDocJson( AssertDocJson(
0, 0,
r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n\n"},{"insert":"456","attributes":{"bold":"true"}},{"insert":"\n"}]"#, r#"[{"insert":"123","attributes":{"bold":true}},{"insert":"\n\n"},{"insert":"456","attributes":{"bold":true}},{"insert":"\n"}]"#,
), ),
Insert(0, "a", 4), Insert(0, "a", 4),
AssertDocJson( AssertDocJson(
0, 0,
r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\na\n"},{"insert":"456","attributes":{"bold":"true"}},{"insert":"\n"}]"#, r#"[{"insert":"123","attributes":{"bold":true}},{"insert":"\na\n"},{"insert":"456","attributes":{"bold":true}},{"insert":"\n"}]"#,
), ),
]; ];
TestBuilder::new().run_scripts::<NewlineDoc>(ops); TestBuilder::new().run_scripts::<NewlineDoc>(ops);
@ -93,11 +93,11 @@ fn attributes_bold_added_and_invert_partial_prefix() {
let ops = vec![ let ops = vec![
Insert(0, "1234", 0), Insert(0, "1234", 0),
Bold(0, Interval::new(0, 4), true), Bold(0, Interval::new(0, 4), true),
AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":true}}]"#),
Bold(0, Interval::new(0, 2), false), Bold(0, Interval::new(0, 2), false),
AssertDocJson(0, r#"[{"insert":"12"},{"insert":"34","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"12"},{"insert":"34","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -105,11 +105,11 @@ fn attributes_bold_added_consecutive() {
let ops = vec![ let ops = vec![
Insert(0, "1234", 0), Insert(0, "1234", 0),
Bold(0, Interval::new(0, 1), true), Bold(0, Interval::new(0, 1), true),
AssertDocJson(0, r#"[{"insert":"1","attributes":{"bold":"true"}},{"insert":"234"}]"#), AssertDocJson(0, r#"[{"insert":"1","attributes":{"bold":true}},{"insert":"234"}]"#),
Bold(0, Interval::new(1, 2), true), Bold(0, Interval::new(1, 2), true),
AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34"}]"#), AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":true}},{"insert":"34"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -120,12 +120,12 @@ fn attributes_bold_added_italic() {
Italic(0, Interval::new(0, 4), true), Italic(0, Interval::new(0, 4), true),
AssertDocJson( AssertDocJson(
0, 0,
r#"[{"insert":"1234","attributes":{"italic":"true","bold":"true"}},{"insert":"\n"}]"#, r#"[{"insert":"1234","attributes":{"italic":true,"bold":true}},{"insert":"\n"}]"#,
), ),
Insert(0, "5678", 4), Insert(0, "5678", 4),
AssertDocJson( AssertDocJson(
0, 0,
r#"[{"insert":"12345678","attributes":{"bold":"true","italic":"true"}},{"insert":"\n"}]"#, r#"[{"insert":"12345678","attributes":{"bold":true,"italic":true}},{"insert":"\n"}]"#,
), ),
]; ];
TestBuilder::new().run_scripts::<NewlineDoc>(ops); TestBuilder::new().run_scripts::<NewlineDoc>(ops);
@ -136,27 +136,27 @@ fn attributes_bold_added_italic2() {
let ops = vec![ let ops = vec![
Insert(0, "123456", 0), Insert(0, "123456", 0),
Bold(0, Interval::new(0, 6), true), Bold(0, Interval::new(0, 6), true),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
Italic(0, Interval::new(0, 2), true), Italic(0, Interval::new(0, 2), true),
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}}, {"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"3456","attributes":{"bold":"true"}}] {"insert":"3456","attributes":{"bold":true}}]
"#, "#,
), ),
Italic(0, Interval::new(4, 6), true), Italic(0, Interval::new(4, 6), true),
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}}, {"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"34","attributes":{"bold":"true"}}, {"insert":"34","attributes":{"bold":true}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}}] {"insert":"56","attributes":{"italic":true,"bold":true}}]
"#, "#,
), ),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -168,16 +168,16 @@ fn attributes_bold_added_italic3() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"bold":"true","italic":"true"}}, {"insert":"12","attributes":{"bold":true,"italic":true}},
{"insert":"345","attributes":{"bold":"true"}},{"insert":"6789"}] {"insert":"345","attributes":{"bold":true}},{"insert":"6789"}]
"#, "#,
), ),
Italic(0, Interval::new(2, 4), true), Italic(0, Interval::new(2, 4), true),
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}}, {"insert":"1234","attributes":{"bold":true,"italic":true}},
{"insert":"5","attributes":{"bold":"true"}}, {"insert":"5","attributes":{"bold":true}},
{"insert":"6789"}] {"insert":"6789"}]
"#, "#,
), ),
@ -185,15 +185,15 @@ fn attributes_bold_added_italic3() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}}, {"insert":"1234","attributes":{"bold":true,"italic":true}},
{"insert":"5","attributes":{"bold":"true"}}, {"insert":"5","attributes":{"bold":true}},
{"insert":"67"}, {"insert":"67"},
{"insert":"89","attributes":{"bold":"true"}}] {"insert":"89","attributes":{"bold":true}}]
"#, "#,
), ),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -205,57 +205,57 @@ fn attributes_bold_added_italic_delete() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}}, {"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"345","attributes":{"bold":"true"}},{"insert":"6789"}] {"insert":"345","attributes":{"bold":true}},{"insert":"6789"}]
"#, "#,
), ),
Italic(0, Interval::new(2, 4), true), Italic(0, Interval::new(2, 4), true),
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}} {"insert":"1234","attributes":{"bold":true,"italic":true}}
,{"insert":"5","attributes":{"bold":"true"}},{"insert":"6789"}]"#, ,{"insert":"5","attributes":{"bold":true}},{"insert":"6789"}]"#,
), ),
Bold(0, Interval::new(7, 9), true), Bold(0, Interval::new(7, 9), true),
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}}, {"insert":"1234","attributes":{"bold":true,"italic":true}},
{"insert":"5","attributes":{"bold":"true"}},{"insert":"67"}, {"insert":"5","attributes":{"bold":true}},{"insert":"67"},
{"insert":"89","attributes":{"bold":"true"}}] {"insert":"89","attributes":{"bold":true}}]
"#, "#,
), ),
Delete(0, Interval::new(0, 5)), Delete(0, Interval::new(0, 5)),
AssertDocJson(0, r#"[{"insert":"67"},{"insert":"89","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"67"},{"insert":"89","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
fn attributes_merge_inserted_text_with_same_attribute() { fn attributes_merge_inserted_text_with_same_attribute() {
let ops = vec![ let ops = vec![
InsertBold(0, "123", Interval::new(0, 3)), InsertBold(0, "123", Interval::new(0, 3)),
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":true}}]"#),
InsertBold(0, "456", Interval::new(3, 6)), InsertBold(0, "456", Interval::new(3, 6)),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
fn attributes_compose_attr_attributes_with_attr_attributes_test() { fn attributes_compose_attr_attributes_with_attr_attributes_test() {
let ops = vec![ let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)), InsertBold(0, "123456", Interval::new(0, 6)),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
InsertBold(1, "7", Interval::new(0, 1)), InsertBold(1, "7", Interval::new(0, 1)),
AssertDocJson(1, r#"[{"insert":"7","attributes":{"bold":"true"}}]"#), AssertDocJson(1, r#"[{"insert":"7","attributes":{"bold":true}}]"#),
Transform(0, 1), Transform(0, 1),
AssertDocJson(0, r#"[{"insert":"1234567","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"1234567","attributes":{"bold":true}}]"#),
AssertDocJson(1, r#"[{"insert":"1234567","attributes":{"bold":"true"}}]"#), AssertDocJson(1, r#"[{"insert":"1234567","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -268,113 +268,113 @@ fn attributes_compose_attr_attributes_with_attr_attributes_test2() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"bold":"true","italic":"true"}}, {"insert":"12","attributes":{"bold":true,"italic":true}},
{"insert":"34","attributes":{"bold":"true"}}, {"insert":"34","attributes":{"bold":true}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}}] {"insert":"56","attributes":{"italic":true,"bold":true}}]
"#, "#,
), ),
InsertBold(1, "7", Interval::new(0, 1)), InsertBold(1, "7", Interval::new(0, 1)),
AssertDocJson(1, r#"[{"insert":"7","attributes":{"bold":"true"}}]"#), AssertDocJson(1, r#"[{"insert":"7","attributes":{"bold":true}}]"#),
Transform(0, 1), Transform(0, 1),
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}}, {"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"34","attributes":{"bold":"true"}}, {"insert":"34","attributes":{"bold":true}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}}, {"insert":"56","attributes":{"italic":true,"bold":true}},
{"insert":"7","attributes":{"bold":"true"}}] {"insert":"7","attributes":{"bold":true}}]
"#, "#,
), ),
AssertDocJson( AssertDocJson(
1, 1,
r#"[ r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}}, {"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"34","attributes":{"bold":"true"}}, {"insert":"34","attributes":{"bold":true}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}}, {"insert":"56","attributes":{"italic":true,"bold":true}},
{"insert":"7","attributes":{"bold":"true"}}] {"insert":"7","attributes":{"bold":true}}]
"#, "#,
), ),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
fn attributes_compose_attr_attributes_with_no_attr_attributes_test() { fn attributes_compose_attr_attributes_with_no_attr_attributes_test() {
let expected = r#"[{"insert":"123456","attributes":{"bold":"true"}},{"insert":"7"}]"#; let expected = r#"[{"insert":"123456","attributes":{"bold":true}},{"insert":"7"}]"#;
let ops = vec![ let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)), InsertBold(0, "123456", Interval::new(0, 6)),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
Insert(1, "7", 0), Insert(1, "7", 0),
AssertDocJson(1, r#"[{"insert":"7"}]"#), AssertDocJson(1, r#"[{"insert":"7"}]"#),
Transform(0, 1), Transform(0, 1),
AssertDocJson(0, expected), AssertDocJson(0, expected),
AssertDocJson(1, expected), AssertDocJson(1, expected),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
fn attributes_replace_heading() { fn attributes_replace_heading() {
let ops = vec![ let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)), InsertBold(0, "123456", Interval::new(0, 6)),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
Delete(0, Interval::new(0, 2)), Delete(0, Interval::new(0, 2)),
AssertDocJson(0, r#"[{"insert":"3456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"3456","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
fn attributes_replace_trailing() { fn attributes_replace_trailing() {
let ops = vec![ let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)), InsertBold(0, "123456", Interval::new(0, 6)),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
Delete(0, Interval::new(5, 6)), Delete(0, Interval::new(5, 6)),
AssertDocJson(0, r#"[{"insert":"12345","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"12345","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
fn attributes_replace_middle() { fn attributes_replace_middle() {
let ops = vec![ let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)), InsertBold(0, "123456", Interval::new(0, 6)),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
Delete(0, Interval::new(0, 2)), Delete(0, Interval::new(0, 2)),
AssertDocJson(0, r#"[{"insert":"3456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"3456","attributes":{"bold":true}}]"#),
Delete(0, Interval::new(2, 4)), Delete(0, Interval::new(2, 4)),
AssertDocJson(0, r#"[{"insert":"34","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"34","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
fn attributes_replace_all() { fn attributes_replace_all() {
let ops = vec![ let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)), InsertBold(0, "123456", Interval::new(0, 6)),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
Delete(0, Interval::new(0, 6)), Delete(0, Interval::new(0, 6)),
AssertDocJson(0, r#"[]"#), AssertDocJson(0, r#"[]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
fn attributes_replace_with_text() { fn attributes_replace_with_text() {
let ops = vec![ let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)), InsertBold(0, "123456", Interval::new(0, 6)),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
Replace(0, Interval::new(0, 3), "ab"), Replace(0, Interval::new(0, 3), "ab"),
AssertDocJson(0, r#"[{"insert":"ab"},{"insert":"456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"ab"},{"insert":"456","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -472,7 +472,7 @@ fn attributes_link_format_with_bold() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"123","attributes":{"bold":"true","link":"https://appflowy.io"}}, {"insert":"123","attributes":{"bold":true,"link":"https://appflowy.io"}},
{"insert":"456","attributes":{"link":"https://appflowy.io"}}, {"insert":"456","attributes":{"link":"https://appflowy.io"}},
{"insert":"\n"}] {"insert":"\n"}]
"#, "#,

View File

@ -8,13 +8,11 @@ use derive_more::Display;
use flowy_sync::client_document::{ClientDocument, InitialDocumentText}; use flowy_sync::client_document::{ClientDocument, InitialDocumentText};
use lib_ot::{ use lib_ot::{
core::*, core::*,
text_delta::{TextAttribute, TextAttributes, TextDelta}, text_delta::{BuildInTextAttribute, TextDelta},
}; };
use rand::{prelude::*, Rng as WrappedRng}; use rand::{prelude::*, Rng as WrappedRng};
use std::{sync::Once, time::Duration}; use std::{sync::Once, time::Duration};
const LEVEL: &str = "info";
#[derive(Clone, Debug, Display)] #[derive(Clone, Debug, Display)]
pub enum TestOp { pub enum TestOp {
#[display(fmt = "Insert")] #[display(fmt = "Insert")]
@ -92,7 +90,8 @@ impl TestBuilder {
static INIT: Once = Once::new(); static INIT: Once = Once::new();
INIT.call_once(|| { INIT.call_once(|| {
let _ = color_eyre::install(); let _ = color_eyre::install();
std::env::set_var("RUST_LOG", LEVEL); // let subscriber = FmtSubscriber::builder().with_max_level(Level::INFO).finish();
// tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
}); });
Self { Self {
@ -127,11 +126,11 @@ impl TestBuilder {
TestOp::InsertBold(delta_i, s, iv) => { TestOp::InsertBold(delta_i, s, iv) => {
let document = &mut self.documents[*delta_i]; let document = &mut self.documents[*delta_i];
document.insert(iv.start, s).unwrap(); document.insert(iv.start, s).unwrap();
document.format(*iv, TextAttribute::Bold(true)).unwrap(); document.format(*iv, BuildInTextAttribute::Bold(true)).unwrap();
} }
TestOp::Bold(delta_i, iv, enable) => { TestOp::Bold(delta_i, iv, enable) => {
let document = &mut self.documents[*delta_i]; let document = &mut self.documents[*delta_i];
let attribute = TextAttribute::Bold(*enable); let attribute = BuildInTextAttribute::Bold(*enable);
let delta = document.format(*iv, attribute).unwrap(); let delta = document.format(*iv, attribute).unwrap();
tracing::trace!("Bold delta: {}", delta.json_str()); tracing::trace!("Bold delta: {}", delta.json_str());
self.deltas.insert(*delta_i, Some(delta)); self.deltas.insert(*delta_i, Some(delta));
@ -139,8 +138,8 @@ impl TestBuilder {
TestOp::Italic(delta_i, iv, enable) => { TestOp::Italic(delta_i, iv, enable) => {
let document = &mut self.documents[*delta_i]; let document = &mut self.documents[*delta_i];
let attribute = match *enable { let attribute = match *enable {
true => TextAttribute::Italic(true), true => BuildInTextAttribute::Italic(true),
false => TextAttribute::Italic(false), false => BuildInTextAttribute::Italic(false),
}; };
let delta = document.format(*iv, attribute).unwrap(); let delta = document.format(*iv, attribute).unwrap();
tracing::trace!("Italic delta: {}", delta.json_str()); tracing::trace!("Italic delta: {}", delta.json_str());
@ -148,21 +147,21 @@ impl TestBuilder {
} }
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 = TextAttribute::Header(*level); let attribute = BuildInTextAttribute::Header(*level);
let delta = document.format(*iv, attribute).unwrap(); let delta = document.format(*iv, attribute).unwrap();
tracing::trace!("Header delta: {}", delta.json_str()); tracing::trace!("Header delta: {}", delta.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 = TextAttribute::Link(link.to_owned()); let attribute = BuildInTextAttribute::Link(link.to_owned());
let delta = document.format(*iv, attribute).unwrap(); let delta = document.format(*iv, attribute).unwrap();
tracing::trace!("Link delta: {}", delta.json_str()); tracing::trace!("Link delta: {}", delta.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 = TextAttribute::Bullet(*enable); let attribute = BuildInTextAttribute::Bullet(*enable);
let delta = document.format(*iv, attribute).unwrap(); let delta = document.format(*iv, attribute).unwrap();
tracing::debug!("Bullet delta: {}", delta.json_str()); tracing::debug!("Bullet delta: {}", delta.json_str());
@ -313,18 +312,18 @@ impl Rng {
}; };
match self.0.gen_range(0.0..1.0) { match self.0.gen_range(0.0..1.0) {
f if f < 0.2 => { f if f < 0.2 => {
delta.insert(&self.gen_string(i), TextAttributes::default()); delta.insert(&self.gen_string(i), Attributes::default());
} }
f if f < 0.4 => { f if f < 0.4 => {
delta.delete(i); delta.delete(i);
} }
_ => { _ => {
delta.retain(i, TextAttributes::default()); delta.retain(i, Attributes::default());
} }
} }
} }
if self.0.gen_range(0.0..1.0) < 0.3 { if self.0.gen_range(0.0..1.0) < 0.3 {
delta.insert(&("1".to_owned() + &self.gen_string(10)), TextAttributes::default()); delta.insert(&("1".to_owned() + &self.gen_string(10)), Attributes::default());
} }
delta delta
} }

View File

@ -1,12 +1,8 @@
#![allow(clippy::all)] #![allow(clippy::all)]
use crate::editor::{Rng, TestBuilder, TestOp::*}; use crate::editor::{Rng, TestBuilder, TestOp::*};
use flowy_sync::client_document::{NewlineDoc, PlainDoc}; use flowy_sync::client_document::{EmptyDoc, NewlineDoc};
use lib_ot::text_delta::TextDeltaBuilder; use lib_ot::text_delta::TextDeltaBuilder;
use lib_ot::{ use lib_ot::{core::Interval, core::*, text_delta::TextDelta};
core::Interval,
core::*,
text_delta::{AttributeBuilder, TextAttribute, TextAttributes, TextDelta},
};
#[test] #[test]
fn attributes_insert_text() { fn attributes_insert_text() {
@ -15,7 +11,7 @@ fn attributes_insert_text() {
Insert(0, "456", 3), Insert(0, "456", 3),
AssertDocJson(0, r#"[{"insert":"123456"}]"#), AssertDocJson(0, r#"[{"insert":"123456"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -25,7 +21,7 @@ fn attributes_insert_text_at_head() {
Insert(0, "456", 0), Insert(0, "456", 0),
AssertDocJson(0, r#"[{"insert":"456123"}]"#), AssertDocJson(0, r#"[{"insert":"456123"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -35,7 +31,7 @@ fn attributes_insert_text_at_middle() {
Insert(0, "456", 1), Insert(0, "456", 1),
AssertDocJson(0, r#"[{"insert":"145623"}]"#), AssertDocJson(0, r#"[{"insert":"145623"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -207,8 +203,8 @@ fn delta_utf16_code_unit_seek() {
fn delta_utf16_code_unit_seek_with_attributes() { fn delta_utf16_code_unit_seek_with_attributes() {
let mut delta = TextDelta::default(); let mut delta = TextDelta::default();
let attributes = AttributeBuilder::new() let attributes = AttributeBuilder::new()
.add_attr(TextAttribute::Bold(true)) .insert("bold", true)
.add_attr(TextAttribute::Italic(true)) .insert("italic", true)
.build(); .build();
delta.add(Operation::insert_with_attributes("1234", attributes.clone())); delta.add(Operation::insert_with_attributes("1234", attributes.clone()));
@ -304,13 +300,13 @@ fn lengths() {
let mut delta = TextDelta::default(); let mut delta = TextDelta::default();
assert_eq!(delta.utf16_base_len, 0); assert_eq!(delta.utf16_base_len, 0);
assert_eq!(delta.utf16_target_len, 0); assert_eq!(delta.utf16_target_len, 0);
delta.retain(5, TextAttributes::default()); delta.retain(5, Attributes::default());
assert_eq!(delta.utf16_base_len, 5); assert_eq!(delta.utf16_base_len, 5);
assert_eq!(delta.utf16_target_len, 5); assert_eq!(delta.utf16_target_len, 5);
delta.insert("abc", TextAttributes::default()); delta.insert("abc", Attributes::default());
assert_eq!(delta.utf16_base_len, 5); assert_eq!(delta.utf16_base_len, 5);
assert_eq!(delta.utf16_target_len, 8); assert_eq!(delta.utf16_target_len, 8);
delta.retain(2, TextAttributes::default()); delta.retain(2, Attributes::default());
assert_eq!(delta.utf16_base_len, 7); assert_eq!(delta.utf16_base_len, 7);
assert_eq!(delta.utf16_target_len, 10); assert_eq!(delta.utf16_target_len, 10);
delta.delete(2); delta.delete(2);
@ -320,10 +316,10 @@ fn lengths() {
#[test] #[test]
fn sequence() { fn sequence() {
let mut delta = TextDelta::default(); let mut delta = TextDelta::default();
delta.retain(5, TextAttributes::default()); delta.retain(5, Attributes::default());
delta.retain(0, TextAttributes::default()); delta.retain(0, Attributes::default());
delta.insert("appflowy", TextAttributes::default()); delta.insert("appflowy", Attributes::default());
delta.insert("", TextAttributes::default()); delta.insert("", Attributes::default());
delta.delete(3); delta.delete(3);
delta.delete(0); delta.delete(0);
assert_eq!(delta.ops.len(), 3); assert_eq!(delta.ops.len(), 3);
@ -353,15 +349,15 @@ fn apply_test() {
#[test] #[test]
fn base_len_test() { fn base_len_test() {
let mut delta_a = TextDelta::default(); let mut delta_a = TextDelta::default();
delta_a.insert("a", TextAttributes::default()); delta_a.insert("a", Attributes::default());
delta_a.insert("b", TextAttributes::default()); delta_a.insert("b", Attributes::default());
delta_a.insert("c", TextAttributes::default()); delta_a.insert("c", Attributes::default());
let s = "hello world,".to_owned(); let s = "hello world,".to_owned();
delta_a.delete(s.len()); delta_a.delete(s.len());
let after_a = delta_a.apply(&s).unwrap(); let after_a = delta_a.apply(&s).unwrap();
delta_a.insert("d", TextAttributes::default()); delta_a.insert("d", Attributes::default());
assert_eq!("abc", &after_a); assert_eq!("abc", &after_a);
} }
@ -392,8 +388,8 @@ fn invert_test() {
#[test] #[test]
fn empty_ops() { fn empty_ops() {
let mut delta = TextDelta::default(); let mut delta = TextDelta::default();
delta.retain(0, TextAttributes::default()); delta.retain(0, Attributes::default());
delta.insert("", TextAttributes::default()); delta.insert("", Attributes::default());
delta.delete(0); delta.delete(0);
assert_eq!(delta.ops.len(), 0); assert_eq!(delta.ops.len(), 0);
} }
@ -401,33 +397,33 @@ fn empty_ops() {
fn eq() { fn eq() {
let mut delta_a = TextDelta::default(); let mut delta_a = TextDelta::default();
delta_a.delete(1); delta_a.delete(1);
delta_a.insert("lo", TextAttributes::default()); delta_a.insert("lo", Attributes::default());
delta_a.retain(2, TextAttributes::default()); delta_a.retain(2, Attributes::default());
delta_a.retain(3, TextAttributes::default()); delta_a.retain(3, Attributes::default());
let mut delta_b = TextDelta::default(); let mut delta_b = TextDelta::default();
delta_b.delete(1); delta_b.delete(1);
delta_b.insert("l", TextAttributes::default()); delta_b.insert("l", Attributes::default());
delta_b.insert("o", TextAttributes::default()); delta_b.insert("o", Attributes::default());
delta_b.retain(5, TextAttributes::default()); delta_b.retain(5, Attributes::default());
assert_eq!(delta_a, delta_b); assert_eq!(delta_a, delta_b);
delta_a.delete(1); delta_a.delete(1);
delta_b.retain(1, TextAttributes::default()); delta_b.retain(1, Attributes::default());
assert_ne!(delta_a, delta_b); assert_ne!(delta_a, delta_b);
} }
#[test] #[test]
fn ops_merging() { fn ops_merging() {
let mut delta = TextDelta::default(); let mut delta = TextDelta::default();
assert_eq!(delta.ops.len(), 0); assert_eq!(delta.ops.len(), 0);
delta.retain(2, TextAttributes::default()); delta.retain(2, Attributes::default());
assert_eq!(delta.ops.len(), 1); assert_eq!(delta.ops.len(), 1);
assert_eq!(delta.ops.last(), Some(&Operation::retain(2))); assert_eq!(delta.ops.last(), Some(&Operation::retain(2)));
delta.retain(3, TextAttributes::default()); delta.retain(3, Attributes::default());
assert_eq!(delta.ops.len(), 1); assert_eq!(delta.ops.len(), 1);
assert_eq!(delta.ops.last(), Some(&Operation::retain(5))); assert_eq!(delta.ops.last(), Some(&Operation::retain(5)));
delta.insert("abc", TextAttributes::default()); delta.insert("abc", Attributes::default());
assert_eq!(delta.ops.len(), 2); assert_eq!(delta.ops.len(), 2);
assert_eq!(delta.ops.last(), Some(&Operation::insert("abc"))); assert_eq!(delta.ops.last(), Some(&Operation::insert("abc")));
delta.insert("xyz", TextAttributes::default()); delta.insert("xyz", Attributes::default());
assert_eq!(delta.ops.len(), 2); assert_eq!(delta.ops.len(), 2);
assert_eq!(delta.ops.last(), Some(&Operation::insert("abcxyz"))); assert_eq!(delta.ops.last(), Some(&Operation::insert("abcxyz")));
delta.delete(1); delta.delete(1);
@ -442,11 +438,11 @@ fn ops_merging() {
fn is_noop() { fn is_noop() {
let mut delta = TextDelta::default(); let mut delta = TextDelta::default();
assert!(delta.is_noop()); assert!(delta.is_noop());
delta.retain(5, TextAttributes::default()); delta.retain(5, Attributes::default());
assert!(delta.is_noop()); assert!(delta.is_noop());
delta.retain(3, TextAttributes::default()); delta.retain(3, Attributes::default());
assert!(delta.is_noop()); assert!(delta.is_noop());
delta.insert("lorem", TextAttributes::default()); delta.insert("lorem", Attributes::default());
assert!(!delta.is_noop()); assert!(!delta.is_noop());
} }
#[test] #[test]
@ -490,16 +486,13 @@ fn transform_random_delta() {
fn transform_with_two_delta() { fn transform_with_two_delta() {
let mut a = TextDelta::default(); let mut a = TextDelta::default();
let mut a_s = String::new(); let mut a_s = String::new();
a.insert( a.insert("123", AttributeBuilder::new().insert("bold", true).build());
"123",
AttributeBuilder::new().add_attr(TextAttribute::Bold(true)).build(),
);
a_s = a.apply(&a_s).unwrap(); a_s = a.apply(&a_s).unwrap();
assert_eq!(&a_s, "123"); assert_eq!(&a_s, "123");
let mut b = TextDelta::default(); let mut b = TextDelta::default();
let mut b_s = String::new(); let mut b_s = String::new();
b.insert("456", TextAttributes::default()); b.insert("456", Attributes::default());
b_s = b.apply(&b_s).unwrap(); b_s = b.apply(&b_s).unwrap();
assert_eq!(&b_s, "456"); assert_eq!(&b_s, "456");
@ -535,7 +528,7 @@ fn transform_two_plain_delta() {
AssertDocJson(0, r#"[{"insert":"123456"}]"#), AssertDocJson(0, r#"[{"insert":"123456"}]"#),
AssertDocJson(1, r#"[{"insert":"123456"}]"#), AssertDocJson(1, r#"[{"insert":"123456"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -549,7 +542,7 @@ fn transform_two_plain_delta2() {
AssertDocJson(0, r#"[{"insert":"123456"}]"#), AssertDocJson(0, r#"[{"insert":"123456"}]"#),
AssertDocJson(1, r#"[{"insert":"123456"}]"#), AssertDocJson(1, r#"[{"insert":"123456"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -567,7 +560,7 @@ fn transform_two_non_seq_delta() {
AssertDocJson(0, r#"[{"insert":"123456"}]"#), AssertDocJson(0, r#"[{"insert":"123456"}]"#),
AssertDocJson(1, r#"[{"insert":"123456789"}]"#), AssertDocJson(1, r#"[{"insert":"123456789"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -582,7 +575,7 @@ fn transform_two_conflict_non_seq_delta() {
AssertDocJson(0, r#"[{"insert":"123456"}]"#), AssertDocJson(0, r#"[{"insert":"123456"}]"#),
AssertDocJson(1, r#"[{"insert":"12378456"}]"#), AssertDocJson(1, r#"[{"insert":"12378456"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -609,7 +602,7 @@ fn delta_invert_no_attribute_delta2() {
Invert(0, 1), Invert(0, 1),
AssertDocJson(0, r#"[{"insert":"123"}]"#), AssertDocJson(0, r#"[{"insert":"123"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -617,12 +610,12 @@ fn delta_invert_attribute_delta_with_no_attribute_delta() {
let ops = vec![ let ops = vec![
Insert(0, "123", 0), Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true), Bold(0, Interval::new(0, 3), true),
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":true}}]"#),
Insert(1, "4567", 0), Insert(1, "4567", 0),
Invert(0, 1), Invert(0, 1),
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":true}}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -634,16 +627,16 @@ fn delta_invert_attribute_delta_with_no_attribute_delta2() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"123456","attributes":{"bold":"true"}}] {"insert":"123456","attributes":{"bold":true}}]
"#, "#,
), ),
Italic(0, Interval::new(2, 4), true), Italic(0, Interval::new(2, 4), true),
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"bold":"true"}}, {"insert":"12","attributes":{"bold":true}},
{"insert":"34","attributes":{"bold":"true","italic":"true"}}, {"insert":"34","attributes":{"bold":true,"italic":true}},
{"insert":"56","attributes":{"bold":"true"}} {"insert":"56","attributes":{"bold":true}}
]"#, ]"#,
), ),
Insert(1, "abc", 0), Insert(1, "abc", 0),
@ -651,13 +644,13 @@ fn delta_invert_attribute_delta_with_no_attribute_delta2() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"bold":"true"}}, {"insert":"12","attributes":{"bold":true}},
{"insert":"34","attributes":{"bold":"true","italic":"true"}}, {"insert":"34","attributes":{"bold":true,"italic":true}},
{"insert":"56","attributes":{"bold":"true"}} {"insert":"56","attributes":{"bold":true}}
]"#, ]"#,
), ),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -666,11 +659,11 @@ fn delta_invert_no_attribute_delta_with_attribute_delta() {
Insert(0, "123", 0), Insert(0, "123", 0),
Insert(1, "4567", 0), Insert(1, "4567", 0),
Bold(1, Interval::new(0, 3), true), Bold(1, Interval::new(0, 3), true),
AssertDocJson(1, r#"[{"insert":"456","attributes":{"bold":"true"}},{"insert":"7"}]"#), AssertDocJson(1, r#"[{"insert":"456","attributes":{"bold":true}},{"insert":"7"}]"#),
Invert(0, 1), Invert(0, 1),
AssertDocJson(0, r#"[{"insert":"123"}]"#), AssertDocJson(0, r#"[{"insert":"123"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -684,12 +677,12 @@ fn delta_invert_no_attribute_delta_with_attribute_delta2() {
Italic(1, Interval::new(1, 3), true), Italic(1, Interval::new(1, 3), true),
AssertDocJson( AssertDocJson(
1, 1,
r#"[{"insert":"a","attributes":{"bold":"true"}},{"insert":"bc","attributes":{"bold":"true","italic":"true"}},{"insert":"d","attributes":{"bold":"true"}}]"#, r#"[{"insert":"a","attributes":{"bold":true}},{"insert":"bc","attributes":{"bold":true,"italic":true}},{"insert":"d","attributes":{"bold":true}}]"#,
), ),
Invert(0, 1), Invert(0, 1),
AssertDocJson(0, r#"[{"insert":"123"}]"#), AssertDocJson(0, r#"[{"insert":"123"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -698,14 +691,14 @@ fn delta_invert_attribute_delta_with_attribute_delta() {
Insert(0, "123", 0), Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true), Bold(0, Interval::new(0, 3), true),
Insert(0, "456", 3), Insert(0, "456", 3),
AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#), AssertDocJson(0, r#"[{"insert":"123456","attributes":{"bold":true}}]"#),
Italic(0, Interval::new(2, 4), true), Italic(0, Interval::new(2, 4), true),
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"bold":"true"}}, {"insert":"12","attributes":{"bold":true}},
{"insert":"34","attributes":{"bold":"true","italic":"true"}}, {"insert":"34","attributes":{"bold":true,"italic":true}},
{"insert":"56","attributes":{"bold":"true"}} {"insert":"56","attributes":{"bold":true}}
]"#, ]"#,
), ),
Insert(1, "abc", 0), Insert(1, "abc", 0),
@ -715,22 +708,22 @@ fn delta_invert_attribute_delta_with_attribute_delta() {
AssertDocJson( AssertDocJson(
1, 1,
r#"[ r#"[
{"insert":"a","attributes":{"bold":"true"}}, {"insert":"a","attributes":{"bold":true}},
{"insert":"bc","attributes":{"bold":"true","italic":"true"}}, {"insert":"bc","attributes":{"bold":true,"italic":true}},
{"insert":"d","attributes":{"bold":"true"}} {"insert":"d","attributes":{"bold":true}}
]"#, ]"#,
), ),
Invert(0, 1), Invert(0, 1),
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"12","attributes":{"bold":"true"}}, {"insert":"12","attributes":{"bold":true}},
{"insert":"34","attributes":{"bold":"true","italic":"true"}}, {"insert":"34","attributes":{"bold":true,"italic":true}},
{"insert":"56","attributes":{"bold":"true"}} {"insert":"56","attributes":{"bold":true}}
]"#, ]"#,
), ),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]

View File

@ -1,21 +1,21 @@
use flowy_sync::client_document::{ClientDocument, PlainDoc}; use flowy_sync::client_document::{ClientDocument, EmptyDoc};
use lib_ot::text_delta::RichTextOperation; use lib_ot::text_delta::TextOperation;
use lib_ot::{ use lib_ot::{
core::*, core::*,
text_delta::{AttributeBuilder, TextAttribute, TextAttributeValue, TextDelta}, text_delta::{BuildInTextAttribute, TextDelta},
}; };
#[test] #[test]
fn operation_insert_serialize_test() { fn operation_insert_serialize_test() {
let attributes = AttributeBuilder::new() let attributes = AttributeBuilder::new()
.add_attr(TextAttribute::Bold(true)) .insert("bold", true)
.add_attr(TextAttribute::Italic(true)) .insert("italic", true)
.build(); .build();
let operation = Operation::insert_with_attributes("123", attributes); let operation = Operation::insert_with_attributes("123", attributes);
let json = serde_json::to_string(&operation).unwrap(); let json = serde_json::to_string(&operation).unwrap();
eprintln!("{}", json); eprintln!("{}", json);
let insert_op: RichTextOperation = serde_json::from_str(&json).unwrap(); let insert_op: TextOperation = serde_json::from_str(&json).unwrap();
assert_eq!(insert_op, operation); assert_eq!(insert_op, operation);
} }
@ -24,23 +24,23 @@ fn operation_retain_serialize_test() {
let operation = Operation::Retain(12.into()); let operation = Operation::Retain(12.into());
let json = serde_json::to_string(&operation).unwrap(); let json = serde_json::to_string(&operation).unwrap();
eprintln!("{}", json); eprintln!("{}", json);
let insert_op: RichTextOperation = serde_json::from_str(&json).unwrap(); let insert_op: TextOperation = serde_json::from_str(&json).unwrap();
assert_eq!(insert_op, operation); assert_eq!(insert_op, operation);
} }
#[test] #[test]
fn operation_delete_serialize_test() { fn operation_delete_serialize_test() {
let operation = RichTextOperation::Delete(2); let operation = TextOperation::Delete(2);
let json = serde_json::to_string(&operation).unwrap(); let json = serde_json::to_string(&operation).unwrap();
let insert_op: RichTextOperation = serde_json::from_str(&json).unwrap(); let insert_op: TextOperation = serde_json::from_str(&json).unwrap();
assert_eq!(insert_op, operation); assert_eq!(insert_op, operation);
} }
#[test] #[test]
fn attributes_serialize_test() { fn attributes_serialize_test() {
let attributes = AttributeBuilder::new() let attributes = AttributeBuilder::new()
.add_attr(TextAttribute::Bold(true)) .insert_entry(BuildInTextAttribute::Bold(true))
.add_attr(TextAttribute::Italic(true)) .insert_entry(BuildInTextAttribute::Italic(true))
.build(); .build();
let retain = Operation::insert_with_attributes("123", attributes); let retain = Operation::insert_with_attributes("123", attributes);
@ -53,8 +53,8 @@ fn delta_serialize_multi_attribute_test() {
let mut delta = Operations::default(); let mut delta = Operations::default();
let attributes = AttributeBuilder::new() let attributes = AttributeBuilder::new()
.add_attr(TextAttribute::Bold(true)) .insert_entry(BuildInTextAttribute::Bold(true))
.add_attr(TextAttribute::Italic(true)) .insert_entry(BuildInTextAttribute::Italic(true))
.build(); .build();
let retain = Operation::insert_with_attributes("123", attributes); let retain = Operation::insert_with_attributes("123", attributes);
@ -74,7 +74,7 @@ fn delta_deserialize_test() {
let json = r#"[ let json = r#"[
{"retain":2,"attributes":{"italic":true}}, {"retain":2,"attributes":{"italic":true}},
{"retain":2,"attributes":{"italic":123}}, {"retain":2,"attributes":{"italic":123}},
{"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 = TextDelta::from_json(json).unwrap(); let delta = TextDelta::from_json(json).unwrap();
@ -88,26 +88,20 @@ fn delta_deserialize_null_test() {
]"#; ]"#;
let delta1 = TextDelta::from_json(json).unwrap(); let delta1 = TextDelta::from_json(json).unwrap();
let mut attribute = TextAttribute::Bold(true); let mut attribute = BuildInTextAttribute::Bold(true);
attribute.value = TextAttributeValue(None); attribute.remove_value();
let delta2 = OperationBuilder::new() let delta2 = OperationBuilder::new()
.retain_with_attributes(7, attribute.into()) .retain_with_attributes(7, attribute.into())
.build(); .build();
assert_eq!(delta2.json_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#); assert_eq!(delta2.json_str(), r#"[{"retain":7,"attributes":{"bold":null}}]"#);
assert_eq!(delta1, delta2); assert_eq!(delta1, delta2);
} }
#[test]
fn delta_serde_null_test() {
let mut attribute = TextAttribute::Bold(true);
attribute.value = TextAttributeValue(None);
assert_eq!(attribute.to_json(), r#"{"bold":""}"#);
}
#[test] #[test]
fn document_insert_serde_test() { fn document_insert_serde_test() {
let mut document = ClientDocument::new::<PlainDoc>(); let mut document = ClientDocument::new::<EmptyDoc>();
document.insert(0, "\n").unwrap(); document.insert(0, "\n").unwrap();
document.insert(0, "123").unwrap(); document.insert(0, "123").unwrap();
let json = document.delta_str(); let json = document.delta_str();

View File

@ -1,5 +1,5 @@
use crate::editor::{TestBuilder, TestOp::*}; use crate::editor::{TestBuilder, TestOp::*};
use flowy_sync::client_document::{NewlineDoc, PlainDoc, RECORD_THRESHOLD}; use flowy_sync::client_document::{EmptyDoc, NewlineDoc, RECORD_THRESHOLD};
use lib_ot::core::{Interval, NEW_LINE, WHITESPACE}; use lib_ot::core::{Interval, NEW_LINE, WHITESPACE};
#[test] #[test]
@ -85,7 +85,7 @@ fn history_bold_redo() {
Undo(0), Undo(0),
AssertDocJson(0, r#"[{"insert":"\n"}]"#), AssertDocJson(0, r#"[{"insert":"\n"}]"#),
Redo(0), Redo(0),
AssertDocJson(0, r#" [{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"}]"#), AssertDocJson(0, r#" [{"insert":"123","attributes":{"bold":true}},{"insert":"\n"}]"#),
]; ];
TestBuilder::new().run_scripts::<NewlineDoc>(ops); TestBuilder::new().run_scripts::<NewlineDoc>(ops);
} }
@ -99,7 +99,7 @@ fn history_bold_redo_with_lagging() {
Undo(0), Undo(0),
AssertDocJson(0, r#"[{"insert":"123\n"}]"#), AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
Redo(0), Redo(0),
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"}]"#), AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":true}},{"insert":"\n"}]"#),
]; ];
TestBuilder::new().run_scripts::<NewlineDoc>(ops); TestBuilder::new().run_scripts::<NewlineDoc>(ops);
} }
@ -115,7 +115,7 @@ fn history_delete_undo() {
Undo(0), Undo(0),
AssertDocJson(0, r#"[{"insert":"123"}]"#), AssertDocJson(0, r#"[{"insert":"123"}]"#),
]; ];
TestBuilder::new().run_scripts::<PlainDoc>(ops); TestBuilder::new().run_scripts::<EmptyDoc>(ops);
} }
#[test] #[test]
@ -127,7 +127,7 @@ fn history_delete_undo_2() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"23","attributes":{"bold":"true"}}, {"insert":"23","attributes":{"bold":true}},
{"insert":"\n"}] {"insert":"\n"}]
"#, "#,
), ),
@ -148,7 +148,7 @@ fn history_delete_undo_with_lagging() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"23","attributes":{"bold":"true"}}, {"insert":"23","attributes":{"bold":true}},
{"insert":"\n"}] {"insert":"\n"}]
"#, "#,
), ),
@ -156,7 +156,7 @@ fn history_delete_undo_with_lagging() {
AssertDocJson( AssertDocJson(
0, 0,
r#"[ r#"[
{"insert":"123","attributes":{"bold":"true"}}, {"insert":"123","attributes":{"bold":true}},
{"insert":"\n"}] {"insert":"\n"}]
"#, "#,
), ),
@ -188,7 +188,7 @@ fn history_replace_undo() {
0, 0,
r#"[ r#"[
{"insert":"ab"}, {"insert":"ab"},
{"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}] {"insert":"3","attributes":{"bold":true}},{"insert":"\n"}]
"#, "#,
), ),
Undo(0), Undo(0),
@ -209,11 +209,11 @@ fn history_replace_undo_with_lagging() {
0, 0,
r#"[ r#"[
{"insert":"ab"}, {"insert":"ab"},
{"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}] {"insert":"3","attributes":{"bold":true}},{"insert":"\n"}]
"#, "#,
), ),
Undo(0), Undo(0),
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"}]"#), AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":true}},{"insert":"\n"}]"#),
]; ];
TestBuilder::new().run_scripts::<NewlineDoc>(ops); TestBuilder::new().run_scripts::<NewlineDoc>(ops);
} }
@ -230,7 +230,7 @@ fn history_replace_redo() {
0, 0,
r#"[ r#"[
{"insert":"ab"}, {"insert":"ab"},
{"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}] {"insert":"3","attributes":{"bold":true}},{"insert":"\n"}]
"#, "#,
), ),
]; ];

View File

@ -7,18 +7,15 @@ use crate::{
errors::CollaborateError, errors::CollaborateError,
}; };
use bytes::Bytes; use bytes::Bytes;
use lib_ot::{ use lib_ot::{core::*, text_delta::TextDelta};
core::*,
text_delta::{TextAttribute, TextDelta},
};
use tokio::sync::mpsc; use tokio::sync::mpsc;
pub trait InitialDocumentText { pub trait InitialDocumentText {
fn initial_delta() -> TextDelta; fn initial_delta() -> TextDelta;
} }
pub struct PlainDoc(); pub struct EmptyDoc();
impl InitialDocumentText for PlainDoc { impl InitialDocumentText for EmptyDoc {
fn initial_delta() -> TextDelta { fn initial_delta() -> TextDelta {
TextDelta::new() TextDelta::new()
} }
@ -141,9 +138,9 @@ impl ClientDocument {
Ok(delete) Ok(delete)
} }
pub fn format(&mut self, interval: Interval, attribute: TextAttribute) -> Result<TextDelta, CollaborateError> { pub fn format(&mut self, interval: Interval, attribute: AttributeEntry) -> Result<TextDelta, CollaborateError> {
let _ = validate_interval(&self.delta, &interval)?; let _ = validate_interval(&self.delta, &interval)?;
tracing::trace!("format {} with {}", interval, attribute); tracing::trace!("format {} with {:?}", interval, attribute);
let format_delta = self.view.format(&self.delta, attribute, interval).unwrap(); let format_delta = self.view.format(&self.delta, attribute, interval).unwrap();
self.compose_delta(format_delta.clone())?; self.compose_delta(format_delta.clone())?;
Ok(format_delta) Ok(format_delta)

View File

@ -1,7 +1,7 @@
use crate::{client_document::DeleteExt, util::is_newline}; use crate::{client_document::DeleteExt, util::is_newline};
use lib_ot::{ use lib_ot::{
core::{Attributes, Interval, OperationBuilder, OperationIterator, Utf16CodeUnitMetric, NEW_LINE}, core::{Interval, OperationAttributes, OperationBuilder, OperationIterator, Utf16CodeUnitMetric, NEW_LINE},
text_delta::{plain_attributes, TextDelta}, text_delta::{empty_attributes, TextDelta},
}; };
pub struct PreserveLineFormatOnMerge {} pub struct PreserveLineFormatOnMerge {}
@ -37,18 +37,18 @@ impl DeleteExt for PreserveLineFormatOnMerge {
// //
match op.get_data().find(NEW_LINE) { match op.get_data().find(NEW_LINE) {
None => { None => {
new_delta.retain(op.len(), plain_attributes()); new_delta.retain(op.len(), empty_attributes());
continue; continue;
} }
Some(line_break) => { Some(line_break) => {
let mut attributes = op.get_attributes(); let mut attributes = op.get_attributes();
attributes.mark_all_as_removed_except(None); attributes.remove_all_value();
if newline_op.has_attribute() { if newline_op.has_attribute() {
attributes.extend_other(newline_op.get_attributes()); attributes.extend_other(newline_op.get_attributes());
} }
new_delta.retain(line_break, plain_attributes()); new_delta.retain(line_break, empty_attributes());
new_delta.retain(1, attributes); new_delta.retain(1, attributes);
break; break;
} }

View File

@ -1,6 +1,8 @@
use lib_ot::core::AttributeEntry;
use lib_ot::text_delta::is_block;
use lib_ot::{ use lib_ot::{
core::{Interval, OperationBuilder, OperationIterator}, core::{Interval, OperationBuilder, OperationIterator},
text_delta::{plain_attributes, AttributeScope, TextAttribute, TextDelta}, text_delta::{empty_attributes, AttributeScope, TextDelta},
}; };
use crate::{ use crate::{
@ -14,8 +16,8 @@ impl FormatExt for ResolveBlockFormat {
"ResolveBlockFormat" "ResolveBlockFormat"
} }
fn apply(&self, delta: &TextDelta, interval: Interval, attribute: &TextAttribute) -> Option<TextDelta> { fn apply(&self, delta: &TextDelta, interval: Interval, attribute: &AttributeEntry) -> Option<TextDelta> {
if attribute.scope != AttributeScope::Block { if !is_block(&attribute.key) {
return None; return None;
} }
@ -26,7 +28,7 @@ impl FormatExt for ResolveBlockFormat {
while start < end && iter.has_next() { while start < end && iter.has_next() {
let next_op = iter.next_op_with_len(end - start).unwrap(); let next_op = iter.next_op_with_len(end - start).unwrap();
match find_newline(next_op.get_data()) { match find_newline(next_op.get_data()) {
None => new_delta.retain(next_op.len(), plain_attributes()), None => new_delta.retain(next_op.len(), empty_attributes()),
Some(_) => { Some(_) => {
let tmp_delta = line_break(&next_op, attribute, AttributeScope::Block); let tmp_delta = line_break(&next_op, attribute, AttributeScope::Block);
new_delta.extend(tmp_delta); new_delta.extend(tmp_delta);
@ -40,9 +42,9 @@ impl FormatExt for ResolveBlockFormat {
let op = iter.next_op().expect("Unexpected None, iter.has_next() must return op"); let op = iter.next_op().expect("Unexpected None, iter.has_next() must return op");
match find_newline(op.get_data()) { match find_newline(op.get_data()) {
None => new_delta.retain(op.len(), plain_attributes()), None => new_delta.retain(op.len(), empty_attributes()),
Some(line_break) => { Some(line_break) => {
new_delta.retain(line_break, plain_attributes()); new_delta.retain(line_break, empty_attributes());
new_delta.retain(1, attribute.clone().into()); new_delta.retain(1, attribute.clone().into());
break; break;
} }

View File

@ -1,6 +1,8 @@
use lib_ot::core::AttributeEntry;
use lib_ot::text_delta::is_inline;
use lib_ot::{ use lib_ot::{
core::{Interval, OperationBuilder, OperationIterator}, core::{Interval, OperationBuilder, OperationIterator},
text_delta::{AttributeScope, TextAttribute, TextDelta}, text_delta::{AttributeScope, TextDelta},
}; };
use crate::{ use crate::{
@ -14,8 +16,8 @@ impl FormatExt for ResolveInlineFormat {
"ResolveInlineFormat" "ResolveInlineFormat"
} }
fn apply(&self, delta: &TextDelta, interval: Interval, attribute: &TextAttribute) -> Option<TextDelta> { fn apply(&self, delta: &TextDelta, interval: Interval, attribute: &AttributeEntry) -> Option<TextDelta> {
if attribute.scope != AttributeScope::Inline { if !is_inline(&attribute.key) {
return None; return None;
} }
let mut new_delta = OperationBuilder::new().retain(interval.start).build(); let mut new_delta = OperationBuilder::new().retain(interval.start).build();

View File

@ -1,7 +1,8 @@
use crate::util::find_newline; use crate::util::find_newline;
use lib_ot::text_delta::{plain_attributes, AttributeScope, RichTextOperation, TextAttribute, TextDelta}; use lib_ot::core::AttributeEntry;
use lib_ot::text_delta::{empty_attributes, AttributeScope, TextDelta, TextOperation};
pub(crate) fn line_break(op: &RichTextOperation, attribute: &TextAttribute, scope: AttributeScope) -> TextDelta { pub(crate) fn line_break(op: &TextOperation, attribute: &AttributeEntry, scope: AttributeScope) -> TextDelta {
let mut new_delta = TextDelta::new(); let mut new_delta = TextDelta::new();
let mut start = 0; let mut start = 0;
let end = op.len(); let end = op.len();
@ -11,10 +12,10 @@ pub(crate) fn line_break(op: &RichTextOperation, attribute: &TextAttribute, scop
match scope { match scope {
AttributeScope::Inline => { AttributeScope::Inline => {
new_delta.retain(line_break - start, attribute.clone().into()); new_delta.retain(line_break - start, attribute.clone().into());
new_delta.retain(1, plain_attributes()); new_delta.retain(1, empty_attributes());
} }
AttributeScope::Block => { AttributeScope::Block => {
new_delta.retain(line_break - start, plain_attributes()); new_delta.retain(line_break - start, empty_attributes());
new_delta.retain(1, attribute.clone().into()); new_delta.retain(1, attribute.clone().into());
} }
_ => { _ => {
@ -29,7 +30,7 @@ pub(crate) fn line_break(op: &RichTextOperation, attribute: &TextAttribute, scop
if start < end { if start < end {
match scope { match scope {
AttributeScope::Inline => new_delta.retain(end - start, attribute.clone().into()), AttributeScope::Inline => new_delta.retain(end - start, attribute.clone().into()),
AttributeScope::Block => new_delta.retain(end - start, plain_attributes()), AttributeScope::Block => new_delta.retain(end - start, empty_attributes()),
_ => log::error!("Unsupported parser line break for {:?}", scope), _ => log::error!("Unsupported parser line break for {:?}", scope),
} }
} }

View File

@ -1,6 +1,6 @@
use crate::{client_document::InsertExt, util::is_newline}; use crate::{client_document::InsertExt, util::is_newline};
use lib_ot::core::{is_empty_line_at_index, OperationBuilder, OperationIterator}; use lib_ot::core::{is_empty_line_at_index, OperationBuilder, OperationIterator};
use lib_ot::text_delta::{attributes_except_header, TextAttributeKey, TextDelta}; use lib_ot::text_delta::{attributes_except_header, BuildInTextAttributeKey, TextDelta};
pub struct AutoExitBlock {} pub struct AutoExitBlock {}
@ -42,7 +42,7 @@ impl InsertExt for AutoExitBlock {
} }
} }
attributes.mark_all_as_removed_except(Some(TextAttributeKey::Header)); attributes.retain_values(&[BuildInTextAttributeKey::Header.as_ref()]);
Some( Some(
OperationBuilder::new() OperationBuilder::new()

View File

@ -1,7 +1,8 @@
use crate::{client_document::InsertExt, util::is_whitespace}; use crate::{client_document::InsertExt, util::is_whitespace};
use lib_ot::core::Attributes;
use lib_ot::{ use lib_ot::{
core::{count_utf16_code_units, OperationBuilder, OperationIterator}, core::{count_utf16_code_units, OperationBuilder, OperationIterator},
text_delta::{plain_attributes, TextAttribute, TextAttributes, TextDelta}, text_delta::{empty_attributes, BuildInTextAttribute, TextDelta},
}; };
use std::cmp::min; use std::cmp::min;
use url::Url; use url::Url;
@ -36,7 +37,7 @@ impl InsertExt for AutoFormatExt {
}); });
let next_attributes = match iter.next_op() { let next_attributes = match iter.next_op() {
None => plain_attributes(), None => empty_attributes(),
Some(op) => op.get_attributes(), Some(op) => op.get_attributes(),
}; };
@ -60,9 +61,9 @@ pub enum AutoFormatter {
} }
impl AutoFormatter { impl AutoFormatter {
pub fn to_attributes(&self) -> TextAttributes { pub fn to_attributes(&self) -> Attributes {
match self { match self {
AutoFormatter::Url(url) => TextAttribute::Link(url.as_str()).into(), AutoFormatter::Url(url) => BuildInTextAttribute::Link(url.as_str()).into(),
} }
} }

View File

@ -1,7 +1,8 @@
use crate::client_document::InsertExt; use crate::client_document::InsertExt;
use lib_ot::core::Attributes;
use lib_ot::{ use lib_ot::{
core::{Attributes, OperationBuilder, OperationIterator, NEW_LINE}, core::{OperationAttributes, OperationBuilder, OperationIterator, NEW_LINE},
text_delta::{TextAttributeKey, TextAttributes, TextDelta}, text_delta::{BuildInTextAttributeKey, TextDelta},
}; };
pub struct DefaultInsertAttribute {} pub struct DefaultInsertAttribute {}
@ -12,7 +13,7 @@ impl InsertExt for DefaultInsertAttribute {
fn apply(&self, delta: &TextDelta, replace_len: usize, text: &str, index: usize) -> Option<TextDelta> { fn apply(&self, delta: &TextDelta, replace_len: usize, text: &str, index: usize) -> Option<TextDelta> {
let iter = OperationIterator::new(delta); let iter = OperationIterator::new(delta);
let mut attributes = TextAttributes::new(); let mut attributes = Attributes::new();
// Enable each line split by "\n" remains the block attributes. for example: // Enable each line split by "\n" remains the block attributes. for example:
// insert "\n" to "123456" at index 3 // insert "\n" to "123456" at index 3
@ -23,7 +24,10 @@ impl InsertExt for DefaultInsertAttribute {
match iter.last() { match iter.last() {
None => {} None => {}
Some(op) => { Some(op) => {
if op.get_attributes().contains_key(&TextAttributeKey::Header) { if op
.get_attributes()
.contains_key(BuildInTextAttributeKey::Header.as_ref())
{
attributes.extend_other(op.get_attributes()); attributes.extend_other(op.get_attributes());
} }
} }

View File

@ -1,9 +1,8 @@
use crate::{client_document::InsertExt, util::is_newline}; use crate::{client_document::InsertExt, util::is_newline};
use lib_ot::core::Attributes;
use lib_ot::{ use lib_ot::{
core::{OperationBuilder, OperationIterator, NEW_LINE}, core::{OperationBuilder, OperationIterator, NEW_LINE},
text_delta::{ text_delta::{attributes_except_header, empty_attributes, BuildInTextAttributeKey, TextDelta},
attributes_except_header, plain_attributes, TextAttribute, TextAttributeKey, TextAttributes, TextDelta,
},
}; };
pub struct PreserveBlockFormatOnInsert {} pub struct PreserveBlockFormatOnInsert {}
@ -27,16 +26,16 @@ impl InsertExt for PreserveBlockFormatOnInsert {
return None; return None;
} }
let mut reset_attribute = TextAttributes::new(); let mut reset_attribute = Attributes::new();
if newline_attributes.contains_key(&TextAttributeKey::Header) { if newline_attributes.contains_key(BuildInTextAttributeKey::Header.as_ref()) {
reset_attribute.add(TextAttribute::Header(1)); reset_attribute.insert(BuildInTextAttributeKey::Header, 1);
} }
let lines: Vec<_> = text.split(NEW_LINE).collect(); let lines: Vec<_> = text.split(NEW_LINE).collect();
let mut new_delta = OperationBuilder::new().retain(index + replace_len).build(); let mut new_delta = OperationBuilder::new().retain(index + replace_len).build();
lines.iter().enumerate().for_each(|(i, line)| { lines.iter().enumerate().for_each(|(i, line)| {
if !line.is_empty() { if !line.is_empty() {
new_delta.insert(line, plain_attributes()); new_delta.insert(line, empty_attributes());
} }
if i == 0 { if i == 0 {
@ -48,9 +47,9 @@ impl InsertExt for PreserveBlockFormatOnInsert {
} }
}); });
if !reset_attribute.is_empty() { if !reset_attribute.is_empty() {
new_delta.retain(offset, plain_attributes()); new_delta.retain(offset, empty_attributes());
let len = newline_op.get_data().find(NEW_LINE).unwrap(); let len = newline_op.get_data().find(NEW_LINE).unwrap();
new_delta.retain(len, plain_attributes()); new_delta.retain(len, empty_attributes());
new_delta.retain(1, reset_attribute); new_delta.retain(1, reset_attribute);
} }

View File

@ -4,7 +4,7 @@ use crate::{
}; };
use lib_ot::{ use lib_ot::{
core::{OpNewline, OperationBuilder, OperationIterator, NEW_LINE}, core::{OpNewline, OperationBuilder, OperationIterator, NEW_LINE},
text_delta::{plain_attributes, TextAttributeKey, TextDelta}, text_delta::{empty_attributes, BuildInTextAttributeKey, TextDelta},
}; };
pub struct PreserveInlineFormat {} pub struct PreserveInlineFormat {}
@ -25,7 +25,7 @@ impl InsertExt for PreserveInlineFormat {
} }
let mut attributes = prev.get_attributes(); let mut attributes = prev.get_attributes();
if attributes.is_empty() || !attributes.contains_key(&TextAttributeKey::Link) { if attributes.is_empty() || !attributes.contains_key(BuildInTextAttributeKey::Link.as_ref()) {
return Some( return Some(
OperationBuilder::new() OperationBuilder::new()
.retain(index + replace_len) .retain(index + replace_len)
@ -36,10 +36,10 @@ impl InsertExt for PreserveInlineFormat {
let next = iter.next_op(); let next = iter.next_op();
match &next { match &next {
None => attributes = plain_attributes(), None => attributes = empty_attributes(),
Some(next) => { Some(next) => {
if OpNewline::parse(next).is_equal() { if OpNewline::parse(next).is_equal() {
attributes = plain_attributes(); attributes = empty_attributes();
} }
} }
} }
@ -77,11 +77,11 @@ impl InsertExt for PreserveLineFormatOnSplit {
} }
let mut new_delta = TextDelta::new(); let mut new_delta = TextDelta::new();
new_delta.retain(index + replace_len, plain_attributes()); new_delta.retain(index + replace_len, empty_attributes());
if newline_status.is_contain() { if newline_status.is_contain() {
debug_assert!(!next.has_attribute()); debug_assert!(!next.has_attribute());
new_delta.insert(NEW_LINE, plain_attributes()); new_delta.insert(NEW_LINE, empty_attributes());
return Some(new_delta); return Some(new_delta);
} }

View File

@ -1,7 +1,8 @@
use crate::{client_document::InsertExt, util::is_newline}; use crate::{client_document::InsertExt, util::is_newline};
use lib_ot::core::Attributes;
use lib_ot::{ use lib_ot::{
core::{OperationBuilder, OperationIterator, Utf16CodeUnitMetric, NEW_LINE}, core::{OperationBuilder, OperationIterator, Utf16CodeUnitMetric, NEW_LINE},
text_delta::{TextAttributeKey, TextAttributes, TextDelta}, text_delta::{BuildInTextAttributeKey, TextDelta},
}; };
pub struct ResetLineFormatOnNewLine {} pub struct ResetLineFormatOnNewLine {}
@ -22,9 +23,12 @@ impl InsertExt for ResetLineFormatOnNewLine {
return None; return None;
} }
let mut reset_attribute = TextAttributes::new(); let mut reset_attribute = Attributes::new();
if next_op.get_attributes().contains_key(&TextAttributeKey::Header) { if next_op
reset_attribute.delete(&TextAttributeKey::Header); .get_attributes()
.contains_key(BuildInTextAttributeKey::Header.as_ref())
{
reset_attribute.remove_value(BuildInTextAttributeKey::Header);
} }
let len = index + replace_len; let len = index + replace_len;

View File

@ -1,10 +1,8 @@
pub use delete::*; pub use delete::*;
pub use format::*; pub use format::*;
pub use insert::*; pub use insert::*;
use lib_ot::{ use lib_ot::core::AttributeEntry;
core::Interval, use lib_ot::{core::Interval, text_delta::TextDelta};
text_delta::{TextAttribute, TextDelta},
};
mod delete; mod delete;
mod format; mod format;
@ -22,7 +20,7 @@ pub trait InsertExt {
pub trait FormatExt { pub trait FormatExt {
fn ext_name(&self) -> &str; fn ext_name(&self) -> &str;
fn apply(&self, delta: &TextDelta, interval: Interval, attribute: &TextAttribute) -> Option<TextDelta>; fn apply(&self, delta: &TextDelta, interval: Interval, attribute: &AttributeEntry) -> Option<TextDelta>;
} }
pub trait DeleteExt { pub trait DeleteExt {

View File

@ -1,8 +1,9 @@
use crate::client_document::*; use crate::client_document::*;
use lib_ot::core::AttributeEntry;
use lib_ot::{ use lib_ot::{
core::{trim, Interval}, core::{trim, Interval},
errors::{ErrorBuilder, OTError, OTErrorCode}, errors::{ErrorBuilder, OTError, OTErrorCode},
text_delta::{TextAttribute, TextDelta}, text_delta::TextDelta,
}; };
pub const RECORD_THRESHOLD: usize = 400; // in milliseconds pub const RECORD_THRESHOLD: usize = 400; // in milliseconds
@ -27,7 +28,7 @@ impl ViewExtensions {
for ext in &self.insert_exts { for ext in &self.insert_exts {
if let Some(mut delta) = ext.apply(delta, interval.size(), text, interval.start) { if let Some(mut delta) = ext.apply(delta, interval.size(), text, interval.start) {
trim(&mut delta); trim(&mut delta);
tracing::debug!("[{} extension]: process: {}", ext.ext_name(), delta); tracing::trace!("[{}] applied, delta: {}", ext.ext_name(), delta);
new_delta = Some(delta); new_delta = Some(delta);
break; break;
} }
@ -44,7 +45,7 @@ impl ViewExtensions {
for ext in &self.delete_exts { for ext in &self.delete_exts {
if let Some(mut delta) = ext.apply(delta, interval) { if let Some(mut delta) = ext.apply(delta, interval) {
trim(&mut delta); trim(&mut delta);
tracing::trace!("[{}]: applied, delta: {}", ext.ext_name(), delta); tracing::trace!("[{}] applied, delta: {}", ext.ext_name(), delta);
new_delta = Some(delta); new_delta = Some(delta);
break; break;
} }
@ -59,14 +60,14 @@ impl ViewExtensions {
pub(crate) fn format( pub(crate) fn format(
&self, &self,
delta: &TextDelta, delta: &TextDelta,
attribute: TextAttribute, attribute: AttributeEntry,
interval: Interval, interval: Interval,
) -> Result<TextDelta, OTError> { ) -> Result<TextDelta, OTError> {
let mut new_delta = None; let mut new_delta = None;
for ext in &self.format_exts { for ext in &self.format_exts {
if let Some(mut delta) = ext.apply(delta, interval, &attribute) { if let Some(mut delta) = ext.apply(delta, interval, &attribute) {
trim(&mut delta); trim(&mut delta);
tracing::trace!("[{}]: applied, delta: {}", ext.ext_name(), delta); tracing::trace!("[{}] applied, delta: {}", ext.ext_name(), delta);
new_delta = Some(delta); new_delta = Some(delta);
break; break;
} }

View File

@ -11,7 +11,8 @@ use async_stream::stream;
use dashmap::DashMap; use dashmap::DashMap;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use lib_infra::future::BoxResultFuture; use lib_infra::future::BoxResultFuture;
use lib_ot::text_delta::{TextAttributes, TextDelta}; use lib_ot::core::Attributes;
use lib_ot::text_delta::TextDelta;
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},
@ -198,7 +199,7 @@ impl std::ops::Drop for ServerDocumentManager {
} }
} }
type DocumentRevisionSynchronizer = RevisionSynchronizer<TextAttributes>; type DocumentRevisionSynchronizer = RevisionSynchronizer<Attributes>;
struct OpenDocumentHandler { struct OpenDocumentHandler {
doc_id: String, doc_id: String,

View File

@ -1,8 +1,5 @@
use crate::{client_document::InitialDocumentText, errors::CollaborateError, synchronizer::RevisionSyncObject}; use crate::{client_document::InitialDocumentText, errors::CollaborateError, synchronizer::RevisionSyncObject};
use lib_ot::{ use lib_ot::{core::*, text_delta::TextDelta};
core::*,
text_delta::{TextAttributes, TextDelta},
};
pub struct ServerDocument { pub struct ServerDocument {
doc_id: String, doc_id: String,
@ -21,7 +18,7 @@ impl ServerDocument {
} }
} }
impl RevisionSyncObject<TextAttributes> for ServerDocument { impl RevisionSyncObject<Attributes> for ServerDocument {
fn id(&self) -> &str { fn id(&self) -> &str {
&self.doc_id &self.doc_id
} }
@ -42,7 +39,7 @@ impl RevisionSyncObject<TextAttributes> for ServerDocument {
self.delta.json_str() self.delta.json_str()
} }
fn set_delta(&mut self, new_delta: Operations<TextAttributes>) { fn set_delta(&mut self, new_delta: Operations<Attributes>) {
self.delta = new_delta; self.delta = new_delta;
} }
} }

View File

@ -9,7 +9,7 @@ use crate::{
util::*, util::*,
}; };
use lib_infra::future::BoxResultFuture; use lib_infra::future::BoxResultFuture;
use lib_ot::core::{Attributes, Operations}; use lib_ot::core::{OperationAttributes, Operations};
use parking_lot::RwLock; use parking_lot::RwLock;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use std::{ use std::{
@ -43,7 +43,7 @@ pub trait RevisionSyncPersistence: Send + Sync + 'static {
) -> BoxResultFuture<(), CollaborateError>; ) -> BoxResultFuture<(), CollaborateError>;
} }
pub trait RevisionSyncObject<T: Attributes>: Send + Sync + 'static { pub trait RevisionSyncObject<T: OperationAttributes>: Send + Sync + 'static {
fn id(&self) -> &str; fn id(&self) -> &str;
fn compose(&mut self, other: &Operations<T>) -> Result<(), CollaborateError>; fn compose(&mut self, other: &Operations<T>) -> Result<(), CollaborateError>;
fn transform(&self, other: &Operations<T>) -> Result<(Operations<T>, Operations<T>), CollaborateError>; fn transform(&self, other: &Operations<T>) -> Result<(Operations<T>, Operations<T>), CollaborateError>;
@ -57,7 +57,7 @@ pub enum RevisionSyncResponse {
Ack(ServerRevisionWSData), Ack(ServerRevisionWSData),
} }
pub struct RevisionSynchronizer<T: Attributes> { pub struct RevisionSynchronizer<T: OperationAttributes> {
object_id: String, object_id: String,
rev_id: AtomicI64, rev_id: AtomicI64,
object: Arc<RwLock<dyn RevisionSyncObject<T>>>, object: Arc<RwLock<dyn RevisionSyncObject<T>>>,
@ -66,7 +66,7 @@ pub struct RevisionSynchronizer<T: Attributes> {
impl<T> RevisionSynchronizer<T> impl<T> RevisionSynchronizer<T>
where where
T: Attributes + DeserializeOwned + serde::Serialize + 'static, T: OperationAttributes + DeserializeOwned + serde::Serialize + 'static,
{ {
pub fn new<S, P>(rev_id: i64, sync_object: S, persistence: P) -> RevisionSynchronizer<T> pub fn new<S, P>(rev_id: i64, sync_object: S, persistence: P) -> RevisionSynchronizer<T>
where where

View File

@ -7,9 +7,9 @@ use crate::{
errors::{CollaborateError, CollaborateResult}, errors::{CollaborateError, CollaborateResult},
}; };
use dissimilar::Chunk; use dissimilar::Chunk;
use lib_ot::core::{Delta, EmptyAttributes, OTString, OperationBuilder}; use lib_ot::core::{Delta, EmptyAttributes, OTString, OperationAttributes, OperationBuilder};
use lib_ot::{ use lib_ot::{
core::{Attributes, OperationTransform, Operations, NEW_LINE, WHITESPACE}, core::{OperationTransform, Operations, NEW_LINE, WHITESPACE},
text_delta::TextDelta, text_delta::TextDelta,
}; };
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
@ -64,7 +64,7 @@ impl RevIdCounter {
#[tracing::instrument(level = "trace", skip(revisions), err)] #[tracing::instrument(level = "trace", skip(revisions), err)]
pub fn make_delta_from_revisions<T>(revisions: Vec<Revision>) -> CollaborateResult<Operations<T>> pub fn make_delta_from_revisions<T>(revisions: Vec<Revision>) -> CollaborateResult<Operations<T>>
where where
T: Attributes + DeserializeOwned, T: OperationAttributes + DeserializeOwned,
{ {
let mut delta = Operations::<T>::new(); let mut delta = Operations::<T>::new();
for revision in revisions { for revision in revisions {
@ -87,7 +87,7 @@ pub fn make_text_delta_from_revisions(revisions: Vec<Revision>) -> CollaborateRe
pub fn make_delta_from_revision_pb<T>(revisions: Vec<Revision>) -> CollaborateResult<Operations<T>> pub fn make_delta_from_revision_pb<T>(revisions: Vec<Revision>) -> CollaborateResult<Operations<T>>
where where
T: Attributes + DeserializeOwned, T: OperationAttributes + DeserializeOwned,
{ {
let mut new_delta = Operations::<T>::new(); let mut new_delta = Operations::<T>::new();
for revision in revisions { for revision in revisions {
@ -206,7 +206,7 @@ pub fn rev_id_from_str(s: &str) -> Result<i64, CollaborateError> {
Ok(rev_id) Ok(rev_id)
} }
pub fn cal_diff<T: Attributes>(old: String, new: String) -> Option<Operations<T>> { pub fn cal_diff<T: OperationAttributes>(old: String, new: String) -> Option<Operations<T>> {
let chunks = dissimilar::diff(&old, &new); let chunks = dissimilar::diff(&old, &new);
let mut delta_builder = OperationBuilder::<T>::new(); let mut delta_builder = OperationBuilder::<T>::new();
for chunk in &chunks { for chunk in &chunks {

View File

@ -1,6 +1,8 @@
use crate::core::{OperationIterator, Operations}; use crate::core::{AttributeKey, AttributeValue, Attributes, OperationIterator, Operations};
use crate::text_delta::{is_block, TextAttributeKey, TextAttributeValue, TextAttributes}; use crate::text_delta::{is_block, TextAttributeKey};
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr;
use strum_macros::EnumString;
const LINEFEEDASCIICODE: i32 = 0x0A; const LINEFEEDASCIICODE: i32 = 0x0A;
@ -98,14 +100,14 @@ mod tests {
} }
struct Attribute { struct Attribute {
key: TextAttributeKey, key: AttributeKey,
value: TextAttributeValue, value: AttributeValue,
} }
pub fn markdown_encoder(delta: &Operations<TextAttributes>) -> String { pub fn markdown_encoder(delta: &Operations<Attributes>) -> String {
let mut markdown_buffer = String::new(); let mut markdown_buffer = String::new();
let mut line_buffer = String::new(); let mut line_buffer = String::new();
let mut current_inline_style = TextAttributes::default(); let mut current_inline_style = Attributes::default();
let mut current_block_lines: Vec<String> = Vec::new(); let mut current_block_lines: Vec<String> = Vec::new();
let mut iterator = OperationIterator::new(delta); let mut iterator = OperationIterator::new(delta);
let mut current_block_style: Option<Attribute> = None; let mut current_block_style: Option<Attribute> = None;
@ -113,7 +115,7 @@ pub fn markdown_encoder(delta: &Operations<TextAttributes>) -> String {
while iterator.has_next() { while iterator.has_next() {
let operation = iterator.next().unwrap(); let operation = iterator.next().unwrap();
let operation_data = operation.get_data(); let operation_data = operation.get_data();
if !operation_data.contains("\n") { if !operation_data.contains('\n') {
handle_inline( handle_inline(
&mut current_inline_style, &mut current_inline_style,
&mut line_buffer, &mut line_buffer,
@ -137,31 +139,28 @@ pub fn markdown_encoder(delta: &Operations<TextAttributes>) -> String {
markdown_buffer markdown_buffer
} }
fn handle_inline( fn handle_inline(current_inline_style: &mut Attributes, buffer: &mut String, mut text: String, attributes: Attributes) {
current_inline_style: &mut TextAttributes, let mut marked_for_removal: HashMap<AttributeKey, AttributeValue> = HashMap::new();
buffer: &mut String,
mut text: String,
attributes: TextAttributes,
) {
let mut marked_for_removal: HashMap<TextAttributeKey, TextAttributeValue> = HashMap::new();
for key in current_inline_style for key in current_inline_style
.clone() .clone()
.keys() .keys()
.collect::<Vec<&TextAttributeKey>>() .collect::<Vec<&AttributeKey>>()
.into_iter() .into_iter()
.rev() .rev()
{ {
if is_block(key) { if let Some(attribute) = TextAttributeKey::from_str(key) {
if is_block(&attribute) {
continue; continue;
} }
}
if attributes.contains_key(key) { if attributes.contains_key(key) {
continue; continue;
} }
let padding = trim_right(buffer); let padding = trim_right(buffer);
write_attribute(buffer, key, current_inline_style.get(key).unwrap(), true); write_attribute(buffer, key, current_inline_style.get(key.as_ref()).unwrap(), true);
if !padding.is_empty() { if !padding.is_empty() {
buffer.push_str(&padding) buffer.push_str(&padding)
} }
@ -175,9 +174,11 @@ fn handle_inline(
} }
for (key, value) in attributes.iter() { for (key, value) in attributes.iter() {
if is_block(key) { if let Some(attribute) = TextAttributeKey::from_str(key) {
if is_block(&attribute) {
continue; continue;
} }
}
if current_inline_style.contains_key(key) { if current_inline_style.contains_key(key) {
continue; continue;
} }
@ -196,7 +197,7 @@ fn handle_inline(
fn trim_right(buffer: &mut String) -> String { fn trim_right(buffer: &mut String) -> String {
let text = buffer.clone(); let text = buffer.clone();
if !text.ends_with(" ") { if !text.ends_with(' ') {
return String::from(""); return String::from("");
} }
let result = text.trim_end(); let result = text.trim_end();
@ -205,10 +206,11 @@ fn trim_right(buffer: &mut String) -> String {
" ".repeat(text.len() - result.len()) " ".repeat(text.len() - result.len())
} }
fn write_attribute(buffer: &mut String, key: &TextAttributeKey, value: &TextAttributeValue, close: bool) { fn write_attribute(buffer: &mut String, key: &AttributeKey, value: &AttributeValue, close: bool) {
let key = TextAttributeKey::from_str(key);
match key { match key {
TextAttributeKey::Bold => buffer.push_str("**"), TextAttributeKey::Bold => buffer.push_str("**"),
TextAttributeKey::Italic => buffer.push_str("_"), TextAttributeKey::Italic => buffer.push('_'),
TextAttributeKey::Underline => { TextAttributeKey::Underline => {
if close { if close {
buffer.push_str("</u>") buffer.push_str("</u>")
@ -216,18 +218,12 @@ fn write_attribute(buffer: &mut String, key: &TextAttributeKey, value: &TextAttr
buffer.push_str("<u>") buffer.push_str("<u>")
} }
} }
TextAttributeKey::StrikeThrough => { TextAttributeKey::StrikeThrough => buffer.push_str("~~"),
if close {
buffer.push_str("~~")
} else {
buffer.push_str("~~")
}
}
TextAttributeKey::Link => { TextAttributeKey::Link => {
if close { if close {
buffer.push_str(format!("]({})", value.0.as_ref().unwrap()).as_str()) buffer.push_str(format!("]({})", value.0.as_ref().unwrap()).as_str())
} else { } else {
buffer.push_str("[") buffer.push('[')
} }
} }
TextAttributeKey::Background => { TextAttributeKey::Background => {
@ -244,13 +240,7 @@ fn write_attribute(buffer: &mut String, key: &TextAttributeKey, value: &TextAttr
buffer.push_str("```\n") buffer.push_str("```\n")
} }
} }
TextAttributeKey::InlineCode => { TextAttributeKey::InlineCode => buffer.push('`'),
if close {
buffer.push_str("`")
} else {
buffer.push_str("`")
}
}
_ => {} _ => {}
} }
} }
@ -259,10 +249,10 @@ fn handle_line(
buffer: &mut String, buffer: &mut String,
markdown_buffer: &mut String, markdown_buffer: &mut String,
data: String, data: String,
attributes: TextAttributes, attributes: Attributes,
current_block_style: &mut Option<Attribute>, current_block_style: &mut Option<Attribute>,
current_block_lines: &mut Vec<String>, current_block_lines: &mut Vec<String>,
current_inline_style: &mut TextAttributes, current_inline_style: &mut Attributes,
) { ) {
let mut span = String::new(); let mut span = String::new();
for c in data.chars() { for c in data.chars() {
@ -270,18 +260,13 @@ fn handle_line(
if !span.is_empty() { if !span.is_empty() {
handle_inline(current_inline_style, buffer, span.clone(), attributes.clone()); handle_inline(current_inline_style, buffer, span.clone(), attributes.clone());
} }
handle_inline( handle_inline(current_inline_style, buffer, String::from(""), Attributes::default());
current_inline_style,
buffer,
String::from(""),
TextAttributes::default(),
);
let line_block_key = attributes.keys().find(|key| { let line_block_key = attributes.keys().find(|key| {
if is_block(*key) { if let Some(attribute) = TextAttributeKey::from_str(key) {
return true; is_block(&attribute)
} else { } else {
return false; false
} }
}); });
@ -339,7 +324,7 @@ fn handle_block(
markdown_buffer.push_str(&current_block_lines.join("\n")); markdown_buffer.push_str(&current_block_lines.join("\n"));
markdown_buffer.push('\n'); markdown_buffer.push('\n');
} }
Some(block_style) if block_style.key == TextAttributeKey::CodeBlock => { Some(block_style) if block_style.key == AttributeKey::CodeBlock => {
write_attribute(markdown_buffer, &block_style.key, &block_style.value, false); write_attribute(markdown_buffer, &block_style.key, &block_style.value, false);
markdown_buffer.push_str(&current_block_lines.join("\n")); markdown_buffer.push_str(&current_block_lines.join("\n"));
write_attribute(markdown_buffer, &block_style.key, &block_style.value, true); write_attribute(markdown_buffer, &block_style.key, &block_style.value, true);
@ -347,7 +332,7 @@ fn handle_block(
} }
Some(block_style) => { Some(block_style) => {
for line in current_block_lines { for line in current_block_lines {
write_block_tag(markdown_buffer, &block_style, false); write_block_tag(markdown_buffer, block_style, false);
markdown_buffer.push_str(line); markdown_buffer.push_str(line);
markdown_buffer.push('\n'); markdown_buffer.push('\n');
} }
@ -360,9 +345,9 @@ fn write_block_tag(buffer: &mut String, block: &Attribute, close: bool) {
return; return;
} }
if block.key == TextAttributeKey::BlockQuote { if block.key == AttributeKey::BlockQuote {
buffer.push_str("> "); buffer.push_str("> ");
} else if block.key == TextAttributeKey::List { } else if block.key == AttributeKey::List {
if block.value.0.as_ref().unwrap().eq("bullet") { if block.value.0.as_ref().unwrap().eq("bullet") {
buffer.push_str("* "); buffer.push_str("* ");
} else if block.value.0.as_ref().unwrap().eq("checked") { } else if block.value.0.as_ref().unwrap().eq("checked") {
@ -374,14 +359,14 @@ fn write_block_tag(buffer: &mut String, block: &Attribute, close: bool) {
} else { } else {
buffer.push_str("* "); buffer.push_str("* ");
} }
} else if block.key == TextAttributeKey::Header { } else if block.key == AttributeKey::Header {
if block.value.0.as_ref().unwrap().eq("1") { if block.value.0.as_ref().unwrap().eq("1") {
buffer.push_str("# "); buffer.push_str("# ");
} else if block.value.0.as_ref().unwrap().eq("2") { } else if block.value.0.as_ref().unwrap().eq("2") {
buffer.push_str("## "); buffer.push_str("## ");
} else if block.value.0.as_ref().unwrap().eq("3") { } else if block.value.0.as_ref().unwrap().eq("3") {
buffer.push_str("### "); buffer.push_str("### ");
} else if block.key == TextAttributeKey::List { } else if block.key == AttributeKey::List {
} }
} }
} }

View File

@ -1 +1 @@
pub mod markdown_encoder; // pub mod markdown_encoder;

View File

@ -0,0 +1,304 @@
use crate::core::{OperationAttributes, OperationTransform};
use crate::errors::OTError;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::fmt::Display;
pub type AttributeMap = HashMap<AttributeKey, AttributeValue>;
#[derive(Debug, Clone)]
pub struct AttributeEntry {
pub key: AttributeKey,
pub value: AttributeValue,
}
impl AttributeEntry {
pub fn remove_value(&mut self) {
self.value.ty = None;
self.value.value = None;
}
}
impl std::convert::From<AttributeEntry> for Attributes {
fn from(entry: AttributeEntry) -> Self {
let mut attributes = Attributes::new();
attributes.insert_entry(entry);
attributes
}
}
#[derive(Default, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct Attributes(AttributeMap);
impl std::ops::Deref for Attributes {
type Target = AttributeMap;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for Attributes {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Attributes {
pub fn new() -> Attributes {
Attributes(HashMap::new())
}
pub fn from_value(attribute_map: AttributeMap) -> Self {
Self(attribute_map)
}
pub fn insert<K: ToString, V: Into<AttributeValue>>(&mut self, key: K, value: V) {
self.0.insert(key.to_string(), value.into());
}
pub fn insert_entry(&mut self, entry: AttributeEntry) {
self.insert(entry.key, entry.value)
}
/// Set the key's value to None
pub fn remove_value<K: AsRef<str>>(&mut self, key: K) {
// if let Some(mut_value) = self.0.get_mut(key.as_ref()) {
// mut_value.value = None;
// }
self.insert(key.as_ref().to_string(), AttributeValue::none());
}
/// Set all key's value to None
pub fn remove_all_value(&mut self) {
self.0.iter_mut().for_each(|(_, v)| {
*v = AttributeValue::none();
})
}
pub fn retain_values(&mut self, retain_keys: &[&str]) {
self.0.iter_mut().for_each(|(k, v)| {
if !retain_keys.contains(&k.as_str()) {
*v = AttributeValue::none();
}
})
}
pub fn remove_key<K: AsRef<str>>(&mut self, key: K) {
self.0.remove(key.as_ref());
}
/// Create a new key/value map 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<Attributes>) {
if other.is_none() {
return;
}
let mut new_attributes = other.unwrap().0;
self.0.iter().for_each(|(k, v)| {
new_attributes.insert(k.clone(), v.clone());
});
self.0 = new_attributes;
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl Display for Attributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (key, value) in self.0.iter() {
let _ = f.write_str(&format!("{:?}:{:?}", key, value))?;
}
Ok(())
}
}
impl OperationAttributes for Attributes {
fn is_empty(&self) -> bool {
self.is_empty()
}
fn remove_empty(&mut self) {
self.retain(|_, v| v.value.is_some());
}
fn extend_other(&mut self, other: Self) {
self.0.extend(other.0);
}
}
impl OperationTransform for Attributes {
fn compose(&self, other: &Self) -> Result<Self, OTError>
where
Self: Sized,
{
let mut attributes = self.clone();
attributes.0.extend(other.clone().0);
Ok(attributes)
}
fn transform(&self, other: &Self) -> Result<(Self, Self), OTError>
where
Self: Sized,
{
let a = self.iter().fold(Attributes::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(Attributes::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(Attributes::new(), |mut attributes, (k, v)| {
if other.get(k) != self.get(k) && self.contains_key(k) {
attributes.insert(k.clone(), v.clone());
}
attributes
});
self.iter().fold(base_inverted, |mut attributes, (k, _)| {
if other.get(k) != self.get(k) && !other.contains_key(k) {
attributes.remove_value(k);
}
attributes
})
}
}
pub type AttributeKey = String;
#[derive(Eq, PartialEq, Hash, Debug, Clone)]
pub enum ValueType {
IntType = 0,
FloatType = 1,
StrType = 2,
BoolType = 3,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AttributeValue {
pub ty: Option<ValueType>,
pub value: Option<String>,
}
impl AttributeValue {
pub fn none() -> Self {
Self { ty: None, value: None }
}
pub fn from_int(val: usize) -> Self {
let value = if val > 0_usize { Some(val.to_string()) } else { None };
Self {
ty: Some(ValueType::IntType),
value,
}
}
pub fn from_float(val: f64) -> Self {
Self {
ty: Some(ValueType::FloatType),
value: Some(val.to_string()),
}
}
pub fn from_bool(val: bool) -> Self {
let value = if val { Some(val.to_string()) } else { None };
Self {
ty: Some(ValueType::BoolType),
value,
}
}
pub fn from_string(s: &str) -> Self {
let value = if s.is_empty() { None } else { Some(s.to_string()) };
Self {
ty: Some(ValueType::StrType),
value,
}
}
pub fn int_value(&self) -> Option<i64> {
let value = self.value.as_ref()?;
Some(value.parse::<i64>().unwrap_or(0))
}
pub fn bool_value(&self) -> Option<bool> {
let value = self.value.as_ref()?;
Some(value.parse::<bool>().unwrap_or(false))
}
pub fn str_value(&self) -> Option<String> {
self.value.clone()
}
pub fn float_value(&self) -> Option<f64> {
let value = self.value.as_ref()?;
Some(value.parse::<f64>().unwrap_or(0.0))
}
}
impl std::convert::From<bool> for AttributeValue {
fn from(value: bool) -> Self {
AttributeValue::from_bool(value)
}
}
impl std::convert::From<usize> for AttributeValue {
fn from(value: usize) -> Self {
AttributeValue::from_int(value)
}
}
impl std::convert::From<&str> for AttributeValue {
fn from(value: &str) -> Self {
AttributeValue::from_string(value)
}
}
impl std::convert::From<String> for AttributeValue {
fn from(value: String) -> Self {
AttributeValue::from_string(&value)
}
}
#[derive(Default)]
pub struct AttributeBuilder {
attributes: Attributes,
}
impl AttributeBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn insert<K: ToString, V: Into<AttributeValue>>(mut self, key: K, value: V) -> Self {
self.attributes.insert(key, value);
self
}
pub fn insert_entry(mut self, entry: AttributeEntry) -> Self {
self.attributes.insert_entry(entry);
self
}
pub fn delete<K: AsRef<str>>(mut self, key: K) -> Self {
self.attributes.remove_value(key);
self
}
pub fn build(self) -> Attributes {
self.attributes
}
}

View File

@ -1,20 +1,20 @@
use std::fmt; use crate::core::attributes::AttributeValue;
use serde::{ use serde::{
de::{self, MapAccess, Visitor}, de::{self, MapAccess, Visitor},
Deserialize, Deserializer, Serialize, Serializer, Deserialize, Deserializer, Serialize, Serializer,
}; };
use std::fmt;
use super::AttributeValue;
impl Serialize for AttributeValue { impl Serialize for AttributeValue {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
{ {
match self.ty { match &self.ty {
None => serializer.serialize_none(),
Some(ty) => match ty {
super::ValueType::IntType => { super::ValueType::IntType => {
//
if let Some(value) = self.int_value() { if let Some(value) = self.int_value() {
serializer.serialize_i64(value) serializer.serialize_i64(value)
} else { } else {
@ -42,6 +42,7 @@ impl Serialize for AttributeValue {
serializer.serialize_none() serializer.serialize_none()
} }
} }
},
} }
} }
} }
@ -125,14 +126,14 @@ impl<'de> Deserialize<'de> for AttributeValue {
where where
E: de::Error, E: de::Error,
{ {
Ok(AttributeValue::from_str(s)) Ok(AttributeValue::from_string(s))
} }
fn visit_none<E>(self) -> Result<Self::Value, E> fn visit_none<E>(self) -> Result<Self::Value, E>
where where
E: de::Error, E: de::Error,
{ {
Ok(AttributeValue::empty()) Ok(AttributeValue::none())
} }
fn visit_unit<E>(self) -> Result<Self::Value, E> fn visit_unit<E>(self) -> Result<Self::Value, E>
@ -140,7 +141,7 @@ impl<'de> Deserialize<'de> for AttributeValue {
E: de::Error, E: de::Error,
{ {
// the value that contains null will be processed here. // the value that contains null will be processed here.
Ok(AttributeValue::empty()) Ok(AttributeValue::none())
} }
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error> fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>

View File

@ -0,0 +1,4 @@
mod attribute;
mod attribute_serde;
pub use attribute::*;

View File

@ -1,4 +1,4 @@
use crate::core::delta::operation::Attributes; use crate::core::delta::operation::OperationAttributes;
use crate::core::delta::{trim, Operations}; use crate::core::delta::{trim, Operations};
use crate::core::Operation; use crate::core::Operation;
@ -16,13 +16,13 @@ use crate::core::Operation;
/// .build(); /// .build();
/// assert_eq!(delta.content().unwrap(), "AppFlowy"); /// assert_eq!(delta.content().unwrap(), "AppFlowy");
/// ``` /// ```
pub struct OperationBuilder<T: Attributes> { pub struct OperationBuilder<T: OperationAttributes> {
delta: Operations<T>, delta: Operations<T>,
} }
impl<T> std::default::Default for OperationBuilder<T> impl<T> std::default::Default for OperationBuilder<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -33,7 +33,7 @@ where
impl<T> OperationBuilder<T> impl<T> OperationBuilder<T>
where where
T: Attributes, T: OperationAttributes,
{ {
pub fn new() -> Self { pub fn new() -> Self {
OperationBuilder::default() OperationBuilder::default()
@ -52,9 +52,9 @@ where
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use lib_ot::text_delta::{TextAttribute, TextDelta, TextDeltaBuilder}; /// use lib_ot::text_delta::{BuildInTextAttribute, TextDelta, TextDeltaBuilder};
/// ///
/// let mut attribute = TextAttribute::Bold(true); /// let mut attribute = BuildInTextAttribute::Bold(true);
/// let delta = TextDeltaBuilder::new().retain_with_attributes(7, attribute.into()).build(); /// let delta = TextDeltaBuilder::new().retain_with_attributes(7, attribute.into()).build();
/// ///
/// assert_eq!(delta.json_str(), r#"[{"retain":7,"attributes":{"bold":true}}]"#); /// assert_eq!(delta.json_str(), r#"[{"retain":7,"attributes":{"bold":true}}]"#);
@ -111,7 +111,7 @@ where
/// ///
/// ``` /// ```
/// use lib_ot::core::{OperationTransform, DeltaBuilder}; /// use lib_ot::core::{OperationTransform, DeltaBuilder};
/// use lib_ot::text_delta::{TextAttribute, TextDeltaBuilder}; /// use lib_ot::text_delta::{BuildInTextAttribute, TextDeltaBuilder};
/// let delta = DeltaBuilder::new() /// let delta = DeltaBuilder::new()
/// .retain(3) /// .retain(3)
/// .trim() /// .trim()
@ -119,7 +119,7 @@ where
/// assert_eq!(delta.ops.len(), 0); /// assert_eq!(delta.ops.len(), 0);
/// ///
/// let delta = TextDeltaBuilder::new() /// let delta = TextDeltaBuilder::new()
/// .retain_with_attributes(3, TextAttribute::Bold(true).into()) /// .retain_with_attributes(3, BuildInTextAttribute::Bold(true).into())
/// .trim() /// .trim()
/// .build(); /// .build();
/// assert_eq!(delta.ops.len(), 1); /// assert_eq!(delta.ops.len(), 1);

View File

@ -1,5 +1,5 @@
#![allow(clippy::while_let_on_iterator)] #![allow(clippy::while_let_on_iterator)]
use crate::core::delta::operation::{Attributes, Operation}; use crate::core::delta::operation::{Operation, OperationAttributes};
use crate::core::delta::Operations; use crate::core::delta::Operations;
use crate::core::interval::Interval; use crate::core::interval::Interval;
use crate::errors::{ErrorBuilder, OTError, OTErrorCode}; use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
@ -7,7 +7,7 @@ use std::{cmp::min, iter::Enumerate, slice::Iter};
/// A [OperationsCursor] is used to iterate the delta and return the corresponding delta. /// A [OperationsCursor] is used to iterate the delta and return the corresponding delta.
#[derive(Debug)] #[derive(Debug)]
pub struct OperationsCursor<'a, T: Attributes> { pub struct OperationsCursor<'a, T: OperationAttributes> {
pub(crate) delta: &'a Operations<T>, pub(crate) delta: &'a Operations<T>,
pub(crate) origin_iv: Interval, pub(crate) origin_iv: Interval,
pub(crate) consume_iv: Interval, pub(crate) consume_iv: Interval,
@ -19,7 +19,7 @@ pub struct OperationsCursor<'a, T: Attributes> {
impl<'a, T> OperationsCursor<'a, T> impl<'a, T> OperationsCursor<'a, T>
where where
T: Attributes, T: OperationAttributes,
{ {
/// # Arguments /// # Arguments
/// ///
@ -184,7 +184,7 @@ where
fn find_next<'a, T>(cursor: &mut OperationsCursor<'a, T>) -> Option<&'a Operation<T>> fn find_next<'a, T>(cursor: &mut OperationsCursor<'a, T>) -> Option<&'a Operation<T>>
where where
T: Attributes, T: OperationAttributes,
{ {
match cursor.iter.next() { match cursor.iter.next() {
None => None, None => None,
@ -197,7 +197,7 @@ where
type SeekResult = Result<(), OTError>; type SeekResult = Result<(), OTError>;
pub trait Metric { pub trait Metric {
fn seek<T: Attributes>(cursor: &mut OperationsCursor<T>, offset: usize) -> SeekResult; fn seek<T: OperationAttributes>(cursor: &mut OperationsCursor<T>, offset: usize) -> SeekResult;
} }
/// [OpMetric] is used by [DeltaIterator] for seeking operations /// [OpMetric] is used by [DeltaIterator] for seeking operations
@ -205,7 +205,7 @@ pub trait Metric {
pub struct OpMetric(); pub struct OpMetric();
impl Metric for OpMetric { impl Metric for OpMetric {
fn seek<T: Attributes>(cursor: &mut OperationsCursor<T>, op_offset: usize) -> SeekResult { fn seek<T: OperationAttributes>(cursor: &mut OperationsCursor<T>, op_offset: usize) -> SeekResult {
let _ = check_bound(cursor.op_offset, op_offset)?; let _ = check_bound(cursor.op_offset, op_offset)?;
let mut seek_cursor = OperationsCursor::new(cursor.delta, cursor.origin_iv); let mut seek_cursor = OperationsCursor::new(cursor.delta, cursor.origin_iv);
@ -224,7 +224,7 @@ impl Metric for OpMetric {
pub struct Utf16CodeUnitMetric(); pub struct Utf16CodeUnitMetric();
impl Metric for Utf16CodeUnitMetric { impl Metric for Utf16CodeUnitMetric {
fn seek<T: Attributes>(cursor: &mut OperationsCursor<T>, offset: usize) -> SeekResult { fn seek<T: OperationAttributes>(cursor: &mut OperationsCursor<T>, offset: usize) -> SeekResult {
if offset > 0 { if offset > 0 {
let _ = check_bound(cursor.consume_count, offset)?; let _ = check_bound(cursor.consume_count, offset)?;
let _ = cursor.next_with_len(Some(offset)); let _ = cursor.next_with_len(Some(offset));

View File

@ -1,8 +1,9 @@
use super::cursor::*; use super::cursor::*;
use crate::core::delta::operation::{Attributes, Operation}; use crate::core::delta::operation::{Operation, OperationAttributes};
use crate::core::delta::{Operations, NEW_LINE}; use crate::core::delta::{Operations, NEW_LINE};
use crate::core::interval::Interval; use crate::core::interval::Interval;
use crate::text_delta::TextAttributes; use crate::core::Attributes;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
pub(crate) const MAX_IV_LEN: usize = i32::MAX as usize; pub(crate) const MAX_IV_LEN: usize = i32::MAX as usize;
@ -28,13 +29,13 @@ pub(crate) const MAX_IV_LEN: usize = i32::MAX as usize;
/// vec![Operation::insert("23")] /// vec![Operation::insert("23")]
/// ); /// );
/// ``` /// ```
pub struct OperationIterator<'a, T: Attributes> { pub struct OperationIterator<'a, T: OperationAttributes> {
cursor: OperationsCursor<'a, T>, cursor: OperationsCursor<'a, T>,
} }
impl<'a, T> OperationIterator<'a, T> impl<'a, T> OperationIterator<'a, T>
where where
T: Attributes, T: OperationAttributes,
{ {
pub fn new(delta: &'a Operations<T>) -> Self { pub fn new(delta: &'a Operations<T>) -> Self {
let interval = Interval::new(0, MAX_IV_LEN); let interval = Interval::new(0, MAX_IV_LEN);
@ -124,7 +125,7 @@ where
impl<'a, T> Iterator for OperationIterator<'a, T> impl<'a, T> Iterator for OperationIterator<'a, T>
where where
T: Attributes, T: OperationAttributes,
{ {
type Item = Operation<T>; type Item = Operation<T>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@ -132,7 +133,7 @@ where
} }
} }
pub fn is_empty_line_at_index(delta: &Operations<TextAttributes>, index: usize) -> bool { pub fn is_empty_line_at_index(delta: &Operations<Attributes>, index: usize) -> bool {
let mut iter = OperationIterator::new(delta); let mut iter = OperationIterator::new(delta);
let (prev, next) = (iter.next_op_with_len(index), iter.next_op()); let (prev, next) = (iter.next_op_with_len(index), iter.next_op());
if prev.is_none() { if prev.is_none() {
@ -148,13 +149,13 @@ pub fn is_empty_line_at_index(delta: &Operations<TextAttributes>, index: usize)
OpNewline::parse(&prev).is_end() && OpNewline::parse(&next).is_start() OpNewline::parse(&prev).is_end() && OpNewline::parse(&next).is_start()
} }
pub struct AttributesIter<'a, T: Attributes> { pub struct AttributesIter<'a, T: OperationAttributes> {
delta_iter: OperationIterator<'a, T>, delta_iter: OperationIterator<'a, T>,
} }
impl<'a, T> AttributesIter<'a, T> impl<'a, T> AttributesIter<'a, T>
where where
T: Attributes, T: OperationAttributes,
{ {
pub fn new(delta: &'a Operations<T>) -> Self { pub fn new(delta: &'a Operations<T>) -> Self {
let interval = Interval::new(0, usize::MAX); let interval = Interval::new(0, usize::MAX);
@ -176,7 +177,7 @@ where
impl<'a, T> Deref for AttributesIter<'a, T> impl<'a, T> Deref for AttributesIter<'a, T>
where where
T: Attributes, T: OperationAttributes,
{ {
type Target = OperationIterator<'a, T>; type Target = OperationIterator<'a, T>;
@ -187,7 +188,7 @@ where
impl<'a, T> DerefMut for AttributesIter<'a, T> impl<'a, T> DerefMut for AttributesIter<'a, T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.delta_iter &mut self.delta_iter
@ -196,7 +197,7 @@ where
impl<'a, T> Iterator for AttributesIter<'a, T> impl<'a, T> Iterator for AttributesIter<'a, T>
where where
T: Attributes, T: OperationAttributes,
{ {
type Item = (usize, T); type Item = (usize, T);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@ -234,7 +235,7 @@ pub enum OpNewline {
} }
impl OpNewline { impl OpNewline {
pub fn parse<T: Attributes>(op: &Operation<T>) -> OpNewline { pub fn parse<T: OperationAttributes>(op: &Operation<T>) -> OpNewline {
let s = op.get_data(); let s = op.get_data();
if s == NEW_LINE { if s == NEW_LINE {

View File

@ -1,17 +1,16 @@
use crate::core::delta::operation::{Attributes, EmptyAttributes, Operation}; use crate::core::delta::operation::{EmptyAttributes, Operation, OperationAttributes};
use crate::text_delta::TextAttributes;
pub type RichTextOpBuilder = OperationsBuilder<TextAttributes>; // pub type RichTextOpBuilder = OperationsBuilder<TextAttributes>;
pub type PlainTextOpBuilder = OperationsBuilder<EmptyAttributes>; pub type PlainTextOpBuilder = OperationsBuilder<EmptyAttributes>;
#[derive(Default)] #[derive(Default)]
pub struct OperationsBuilder<T: Attributes> { pub struct OperationsBuilder<T: OperationAttributes> {
operations: Vec<Operation<T>>, operations: Vec<Operation<T>>,
} }
impl<T> OperationsBuilder<T> impl<T> OperationsBuilder<T>
where where
T: Attributes, T: OperationAttributes,
{ {
pub fn new() -> OperationsBuilder<T> { pub fn new() -> OperationsBuilder<T> {
OperationsBuilder::default() OperationsBuilder::default()

View File

@ -70,7 +70,7 @@ pub trait OperationTransform {
///Because [Operation] is generic over the T, so you must specify the T. For example, the [Delta] uses ///Because [Operation] is generic over the T, so you must specify the T. For example, the [Delta] uses
///[EmptyAttributes] as the T. [EmptyAttributes] does nothing, just a phantom. ///[EmptyAttributes] as the T. [EmptyAttributes] does nothing, just a phantom.
/// ///
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransform { pub trait OperationAttributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransform {
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
true true
} }
@ -96,7 +96,7 @@ pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + Opera
/// to json string. You could check out the operation_serde.rs for more information. /// 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: OperationAttributes> {
Delete(usize), Delete(usize),
Retain(Retain<T>), Retain(Retain<T>),
Insert(Insert<T>), Insert(Insert<T>),
@ -104,7 +104,7 @@ pub enum Operation<T: Attributes> {
impl<T> Operation<T> impl<T> Operation<T>
where where
T: Attributes, T: OperationAttributes,
{ {
pub fn delete(n: usize) -> Self { pub fn delete(n: usize) -> Self {
Self::Delete(n) Self::Delete(n)
@ -281,7 +281,7 @@ where
impl<T> fmt::Display for Operation<T> impl<T> fmt::Display for Operation<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("{")?; f.write_str("{")?;
@ -302,14 +302,14 @@ where
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Retain<T: Attributes> { pub struct Retain<T: OperationAttributes> {
pub n: usize, pub n: usize,
pub attributes: T, pub attributes: T,
} }
impl<T> fmt::Display for Retain<T> impl<T> fmt::Display for Retain<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.attributes.is_empty() { if self.attributes.is_empty() {
@ -322,7 +322,7 @@ where
impl<T> Retain<T> impl<T> Retain<T>
where where
T: Attributes, T: OperationAttributes,
{ {
pub fn merge_or_new(&mut self, n: usize, attributes: T) -> Option<Operation<T>> { pub fn merge_or_new(&mut self, n: usize, attributes: T) -> Option<Operation<T>> {
// tracing::trace!( // tracing::trace!(
@ -346,7 +346,7 @@ where
impl<T> std::convert::From<usize> for Retain<T> impl<T> std::convert::From<usize> for Retain<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn from(n: usize) -> Self { fn from(n: usize) -> Self {
Retain { Retain {
@ -358,7 +358,7 @@ where
impl<T> Deref for Retain<T> impl<T> Deref for Retain<T>
where where
T: Attributes, T: OperationAttributes,
{ {
type Target = usize; type Target = usize;
@ -369,7 +369,7 @@ where
impl<T> DerefMut for Retain<T> impl<T> DerefMut for Retain<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.n &mut self.n
@ -377,14 +377,14 @@ where
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Insert<T: Attributes> { pub struct Insert<T: OperationAttributes> {
pub s: OTString, pub s: OTString,
pub attributes: T, pub attributes: T,
} }
impl<T> fmt::Display for Insert<T> impl<T> fmt::Display for Insert<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut s = self.s.clone(); let mut s = self.s.clone();
@ -405,7 +405,7 @@ where
impl<T> Insert<T> impl<T> Insert<T>
where where
T: Attributes, T: OperationAttributes,
{ {
pub fn utf16_size(&self) -> usize { pub fn utf16_size(&self) -> usize {
self.s.utf16_len() self.s.utf16_len()
@ -427,7 +427,7 @@ where
impl<T> std::convert::From<String> for Insert<T> impl<T> std::convert::From<String> for Insert<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn from(s: String) -> Self { fn from(s: String) -> Self {
Insert { Insert {
@ -439,7 +439,7 @@ where
impl<T> std::convert::From<&str> for Insert<T> impl<T> std::convert::From<&str> for Insert<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn from(s: &str) -> Self { fn from(s: &str) -> Self {
Insert::from(s.to_owned()) Insert::from(s.to_owned())
@ -448,7 +448,7 @@ where
impl<T> std::convert::From<OTString> for Insert<T> impl<T> std::convert::From<OTString> for Insert<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn from(s: OTString) -> Self { fn from(s: OTString) -> Self {
Insert { Insert {
@ -466,7 +466,7 @@ impl fmt::Display for EmptyAttributes {
} }
} }
impl Attributes for EmptyAttributes {} impl OperationAttributes for EmptyAttributes {}
impl OperationTransform for EmptyAttributes { impl OperationTransform for EmptyAttributes {
fn compose(&self, _other: &Self) -> Result<Self, OTError> { fn compose(&self, _other: &Self) -> Result<Self, OTError> {

View File

@ -1,4 +1,4 @@
use crate::core::delta::operation::{Attributes, Insert, Operation, Retain}; use crate::core::delta::operation::{Insert, Operation, OperationAttributes, Retain};
use crate::core::ot_str::OTString; use crate::core::ot_str::OTString;
use serde::{ use serde::{
de, de,
@ -10,7 +10,7 @@ use std::{fmt, marker::PhantomData};
impl<T> Serialize for Operation<T> impl<T> Serialize for Operation<T>
where where
T: Attributes + Serialize, T: OperationAttributes + Serialize,
{ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@ -30,7 +30,7 @@ where
impl<'de, T> Deserialize<'de> for Operation<T> impl<'de, T> Deserialize<'de> for Operation<T>
where where
T: Attributes + Deserialize<'de>, T: OperationAttributes + Deserialize<'de>,
{ {
fn deserialize<D>(deserializer: D) -> Result<Operation<T>, D::Error> fn deserialize<D>(deserializer: D) -> Result<Operation<T>, D::Error>
where where
@ -40,7 +40,7 @@ where
impl<'de, T> Visitor<'de> for OperationVisitor<T> impl<'de, T> Visitor<'de> for OperationVisitor<T>
where where
T: Attributes + Deserialize<'de>, T: OperationAttributes + Deserialize<'de>,
{ {
type Value = Operation<T>; type Value = Operation<T>;
@ -105,7 +105,7 @@ where
impl<T> Serialize for Retain<T> impl<T> Serialize for Retain<T>
where where
T: Attributes + Serialize, T: OperationAttributes + Serialize,
{ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@ -123,7 +123,7 @@ where
impl<'de, T> Deserialize<'de> for Retain<T> impl<'de, T> Deserialize<'de> for Retain<T>
where where
T: Attributes + Deserialize<'de>, T: OperationAttributes + Deserialize<'de>,
{ {
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error> fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
where where
@ -133,7 +133,7 @@ where
impl<'de, T> Visitor<'de> for RetainVisitor<T> impl<'de, T> Visitor<'de> for RetainVisitor<T>
where where
T: Attributes + Deserialize<'de>, T: OperationAttributes + Deserialize<'de>,
{ {
type Value = Retain<T>; type Value = Retain<T>;
@ -208,7 +208,7 @@ where
impl<T> Serialize for Insert<T> impl<T> Serialize for Insert<T>
where where
T: Attributes + Serialize, T: OperationAttributes + Serialize,
{ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@ -226,7 +226,7 @@ where
impl<'de, T> Deserialize<'de> for Insert<T> impl<'de, T> Deserialize<'de> for Insert<T>
where where
T: Attributes + Deserialize<'de>, T: OperationAttributes + Deserialize<'de>,
{ {
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error> fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
where where
@ -236,7 +236,7 @@ where
impl<'de, T> Visitor<'de> for InsertVisitor<T> impl<'de, T> Visitor<'de> for InsertVisitor<T>
where where
T: Attributes + Deserialize<'de>, T: OperationAttributes + Deserialize<'de>,
{ {
type Value = Insert<T>; type Value = Insert<T>;

View File

@ -1,6 +1,6 @@
use crate::errors::{ErrorBuilder, OTError, OTErrorCode}; use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
use crate::core::delta::operation::{Attributes, EmptyAttributes, Operation, OperationTransform}; use crate::core::delta::operation::{EmptyAttributes, Operation, OperationAttributes, OperationTransform};
use crate::core::delta::{OperationIterator, MAX_IV_LEN}; use crate::core::delta::{OperationIterator, MAX_IV_LEN};
use crate::core::interval::Interval; use crate::core::interval::Interval;
use crate::core::ot_str::OTString; use crate::core::ot_str::OTString;
@ -28,7 +28,7 @@ pub type DeltaBuilder = OperationBuilder<EmptyAttributes>;
/// a JSON string. /// a JSON string.
/// ///
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Operations<T: Attributes> { pub struct Operations<T: OperationAttributes> {
pub ops: Vec<Operation<T>>, pub ops: Vec<Operation<T>>,
/// 'Delete' and 'Retain' operation will update the [utf16_base_len] /// 'Delete' and 'Retain' operation will update the [utf16_base_len]
@ -42,7 +42,7 @@ pub struct Operations<T: Attributes> {
impl<T> Default for Operations<T> impl<T> Default for Operations<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -55,7 +55,7 @@ where
impl<T> fmt::Display for Operations<T> impl<T> fmt::Display for Operations<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// f.write_str(&serde_json::to_string(self).unwrap_or("".to_owned()))?; // f.write_str(&serde_json::to_string(self).unwrap_or("".to_owned()))?;
@ -70,7 +70,7 @@ where
impl<T> FromIterator<Operation<T>> for Operations<T> impl<T> FromIterator<Operation<T>> for Operations<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn from_iter<I: IntoIterator<Item = Operation<T>>>(ops: I) -> Self { fn from_iter<I: IntoIterator<Item = Operation<T>>>(ops: I) -> Self {
let mut operations = Operations::default(); let mut operations = Operations::default();
@ -83,7 +83,7 @@ where
impl<T> Operations<T> impl<T> Operations<T>
where where
T: Attributes, T: OperationAttributes,
{ {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
@ -294,7 +294,7 @@ where
impl<T> OperationTransform for Operations<T> impl<T> OperationTransform for Operations<T>
where where
T: Attributes, T: OperationAttributes,
{ {
fn compose(&self, other: &Self) -> Result<Self, OTError> fn compose(&self, other: &Self) -> Result<Self, OTError>
where where
@ -509,7 +509,7 @@ where
/// Removes trailing retain operation with empty attributes, if present. /// Removes trailing retain operation with empty attributes, if present.
pub fn trim<T>(delta: &mut Operations<T>) pub fn trim<T>(delta: &mut Operations<T>)
where where
T: Attributes, T: OperationAttributes,
{ {
if let Some(last) = delta.ops.last() { if let Some(last) = delta.ops.last() {
if last.is_retain() && last.is_plain() { if last.is_retain() && last.is_plain() {
@ -518,7 +518,7 @@ where
} }
} }
fn invert_other<T: Attributes>( fn invert_other<T: OperationAttributes>(
base: &mut Operations<T>, base: &mut Operations<T>,
other: &Operations<T>, other: &Operations<T>,
operation: &Operation<T>, operation: &Operation<T>,
@ -547,7 +547,7 @@ fn invert_other<T: Attributes>(
}); });
} }
fn transform_op_attribute<T: Attributes>( fn transform_op_attribute<T: OperationAttributes>(
left: &Option<Operation<T>>, left: &Option<Operation<T>>,
right: &Option<Operation<T>>, right: &Option<Operation<T>>,
) -> Result<T, OTError> { ) -> Result<T, OTError> {
@ -565,7 +565,7 @@ fn transform_op_attribute<T: Attributes>(
impl<T> Operations<T> impl<T> Operations<T>
where where
T: Attributes + DeserializeOwned, T: OperationAttributes + DeserializeOwned,
{ {
/// # Examples /// # Examples
/// ///
@ -576,7 +576,7 @@ where
/// {"retain":7,"attributes":{"bold":null}} /// {"retain":7,"attributes":{"bold":null}}
/// ]"#; /// ]"#;
/// let delta = TextDelta::from_json(json).unwrap(); /// let delta = TextDelta::from_json(json).unwrap();
/// assert_eq!(delta.json_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#); /// assert_eq!(delta.json_str(), r#"[{"retain":7,"attributes":{"bold":null}}]"#);
/// ``` /// ```
pub fn from_json(json: &str) -> Result<Self, OTError> { pub fn from_json(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| {
@ -597,7 +597,7 @@ where
impl<T> Operations<T> impl<T> Operations<T>
where where
T: Attributes + serde::Serialize, T: OperationAttributes + serde::Serialize,
{ {
/// Serialize the [Delta] into a String in JSON format /// Serialize the [Delta] into a String in JSON format
pub fn json_str(&self) -> String { pub fn json_str(&self) -> String {
@ -618,7 +618,7 @@ where
impl<T> FromStr for Operations<T> impl<T> FromStr for Operations<T>
where where
T: Attributes, T: OperationAttributes,
{ {
type Err = (); type Err = ();
@ -631,7 +631,7 @@ where
impl<T> std::convert::TryFrom<Vec<u8>> for Operations<T> impl<T> std::convert::TryFrom<Vec<u8>> for Operations<T>
where where
T: Attributes + DeserializeOwned, T: OperationAttributes + DeserializeOwned,
{ {
type Error = OTError; type Error = OTError;
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> { fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
@ -641,7 +641,7 @@ where
impl<T> std::convert::TryFrom<Bytes> for Operations<T> impl<T> std::convert::TryFrom<Bytes> for Operations<T>
where where
T: Attributes + DeserializeOwned, T: OperationAttributes + DeserializeOwned,
{ {
type Error = OTError; type Error = OTError;

View File

@ -1,4 +1,4 @@
use crate::core::delta::operation::Attributes; use crate::core::delta::operation::OperationAttributes;
use crate::core::delta::Operations; use crate::core::delta::Operations;
use serde::{ use serde::{
de::{SeqAccess, Visitor}, de::{SeqAccess, Visitor},
@ -9,7 +9,7 @@ use std::{fmt, marker::PhantomData};
impl<T> Serialize for Operations<T> impl<T> Serialize for Operations<T>
where where
T: Attributes + Serialize, T: OperationAttributes + Serialize,
{ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@ -25,7 +25,7 @@ where
impl<'de, T> Deserialize<'de> for Operations<T> impl<'de, T> Deserialize<'de> for Operations<T>
where where
T: Attributes + Deserialize<'de>, T: OperationAttributes + Deserialize<'de>,
{ {
fn deserialize<D>(deserializer: D) -> Result<Operations<T>, D::Error> fn deserialize<D>(deserializer: D) -> Result<Operations<T>, D::Error>
where where
@ -35,7 +35,7 @@ where
impl<'de, T> Visitor<'de> for OperationSeqVisitor<T> impl<'de, T> Visitor<'de> for OperationSeqVisitor<T>
where where
T: Attributes + Deserialize<'de>, T: OperationAttributes + Deserialize<'de>,
{ {
type Value = Operations<T>; type Value = Operations<T>;

View File

@ -1,197 +0,0 @@
use crate::core::OperationTransform;
use crate::errors::OTError;
use serde::{Deserialize, Serialize};
use serde_repr::*;
use std::collections::HashMap;
pub type AttributeMap = HashMap<AttributeKey, AttributeValue>;
#[derive(Default, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct NodeAttributes(AttributeMap);
impl std::ops::Deref for NodeAttributes {
type Target = AttributeMap;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for NodeAttributes {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl NodeAttributes {
pub fn new() -> NodeAttributes {
NodeAttributes(HashMap::new())
}
pub fn from_value(attribute_map: AttributeMap) -> Self {
Self(attribute_map)
}
pub fn insert<K: ToString, V: Into<AttributeValue>>(&mut self, key: K, value: V) {
self.0.insert(key.to_string(), value.into());
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn delete<K: ToString>(&mut self, key: K) {
self.insert(key.to_string(), AttributeValue::empty());
}
}
impl OperationTransform for NodeAttributes {
fn compose(&self, other: &Self) -> Result<Self, OTError>
where
Self: Sized,
{
let mut attributes = self.clone();
attributes.0.extend(other.clone().0);
Ok(attributes)
}
fn transform(&self, other: &Self) -> Result<(Self, Self), OTError>
where
Self: Sized,
{
let a = self.iter().fold(NodeAttributes::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(NodeAttributes::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(NodeAttributes::new(), |mut attributes, (k, v)| {
if other.get(k) != self.get(k) && self.contains_key(k) {
attributes.insert(k.clone(), v.clone());
}
attributes
});
self.iter().fold(base_inverted, |mut attributes, (k, _)| {
if other.get(k) != self.get(k) && !other.contains_key(k) {
attributes.delete(k);
}
attributes
})
}
}
pub type AttributeKey = String;
#[derive(Eq, PartialEq, Hash, Debug, Clone, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum ValueType {
IntType = 0,
FloatType = 1,
StrType = 2,
BoolType = 3,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AttributeValue {
pub ty: ValueType,
pub value: Option<String>,
}
impl AttributeValue {
pub fn empty() -> Self {
Self {
ty: ValueType::StrType,
value: None,
}
}
pub fn from_int(val: usize) -> Self {
Self {
ty: ValueType::IntType,
value: Some(val.to_string()),
}
}
pub fn from_float(val: f64) -> Self {
Self {
ty: ValueType::FloatType,
value: Some(val.to_string()),
}
}
pub fn from_bool(val: bool) -> Self {
Self {
ty: ValueType::BoolType,
value: Some(val.to_string()),
}
}
pub fn from_str(s: &str) -> Self {
let value = if s.is_empty() { None } else { Some(s.to_string()) };
Self {
ty: ValueType::StrType,
value,
}
}
pub fn int_value(&self) -> Option<i64> {
let value = self.value.as_ref()?;
Some(value.parse::<i64>().unwrap_or(0))
}
pub fn bool_value(&self) -> Option<bool> {
let value = self.value.as_ref()?;
Some(value.parse::<bool>().unwrap_or(false))
}
pub fn str_value(&self) -> Option<String> {
self.value.clone()
}
pub fn float_value(&self) -> Option<f64> {
let value = self.value.as_ref()?;
Some(value.parse::<f64>().unwrap_or(0.0))
}
}
impl std::convert::From<bool> for AttributeValue {
fn from(value: bool) -> Self {
AttributeValue::from_bool(value)
}
}
pub struct NodeAttributeBuilder {
attributes: NodeAttributes,
}
impl NodeAttributeBuilder {
pub fn new() -> Self {
Self {
attributes: NodeAttributes::default(),
}
}
pub fn insert<K: ToString, V: Into<AttributeValue>>(mut self, key: K, value: V) -> Self {
self.attributes.insert(key, value);
self
}
pub fn delete<K: ToString>(mut self, key: K) -> Self {
self.attributes.delete(key);
self
}
pub fn build(self) -> NodeAttributes {
self.attributes
}
}

View File

@ -1,6 +1,5 @@
#![allow(clippy::module_inception)] #![allow(clippy::module_inception)]
mod attributes;
mod attributes_serde;
mod node; mod node;
mod node_serde; mod node_serde;
mod node_tree; mod node_tree;
@ -9,7 +8,6 @@ mod operation_serde;
mod path; mod path;
mod transaction; mod transaction;
pub use attributes::*;
pub use node::*; pub use node::*;
pub use node_tree::*; pub use node_tree::*;
pub use operation::*; pub use operation::*;

View File

@ -1,6 +1,7 @@
use super::node_serde::*; use super::node_serde::*;
use crate::core::attributes::{AttributeKey, AttributeValue, Attributes};
use crate::core::NodeBody::Delta; use crate::core::NodeBody::Delta;
use crate::core::{AttributeKey, AttributeValue, NodeAttributes, OperationTransform}; use crate::core::OperationTransform;
use crate::errors::OTError; use crate::errors::OTError;
use crate::text_delta::TextDelta; use crate::text_delta::TextDelta;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -10,9 +11,9 @@ pub struct NodeData {
#[serde(rename = "type")] #[serde(rename = "type")]
pub node_type: String, pub node_type: String,
#[serde(skip_serializing_if = "NodeAttributes::is_empty")] #[serde(skip_serializing_if = "Attributes::is_empty")]
#[serde(default)] #[serde(default)]
pub attributes: NodeAttributes, pub attributes: Attributes,
#[serde(serialize_with = "serialize_body")] #[serde(serialize_with = "serialize_body")]
#[serde(deserialize_with = "deserialize_body")] #[serde(deserialize_with = "deserialize_body")]
@ -104,10 +105,7 @@ impl std::default::Default for NodeBody {
impl NodeBody { impl NodeBody {
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
match self { matches!(self, NodeBody::Empty)
NodeBody::Empty => true,
_ => false,
}
} }
} }
@ -118,7 +116,7 @@ impl OperationTransform for NodeBody {
Self: Sized, Self: Sized,
{ {
match (self, other) { match (self, other) {
(Delta(a), Delta(b)) => a.compose(b).map(|delta| Delta(delta)), (Delta(a), Delta(b)) => a.compose(b).map(Delta),
(NodeBody::Empty, NodeBody::Empty) => Ok(NodeBody::Empty), (NodeBody::Empty, NodeBody::Empty) => Ok(NodeBody::Empty),
(l, r) => { (l, r) => {
let msg = format!("{:?} can not compose {:?}", l, r); let msg = format!("{:?} can not compose {:?}", l, r);
@ -181,21 +179,21 @@ impl NodeBodyChangeset {
pub struct Node { pub struct Node {
pub node_type: String, pub node_type: String,
pub body: NodeBody, pub body: NodeBody,
pub attributes: NodeAttributes, pub attributes: Attributes,
} }
impl Node { impl Node {
pub fn new(node_type: &str) -> Node { pub fn new(node_type: &str) -> Node {
Node { Node {
node_type: node_type.into(), node_type: node_type.into(),
attributes: NodeAttributes::new(), attributes: Attributes::new(),
body: NodeBody::Empty, body: NodeBody::Empty,
} }
} }
pub fn apply_body_changeset(&mut self, changeset: NodeBodyChangeset) { pub fn apply_body_changeset(&mut self, changeset: NodeBodyChangeset) {
match changeset { match changeset {
NodeBodyChangeset::Delta { delta, inverted: _ } => match self.body.compose(&Delta(delta.clone())) { NodeBodyChangeset::Delta { delta, inverted: _ } => match self.body.compose(&Delta(delta)) {
Ok(new_body) => self.body = new_body, Ok(new_body) => self.body = new_body,
Err(e) => tracing::error!("{:?}", e), Err(e) => tracing::error!("{:?}", e),
}, },

View File

@ -64,8 +64,8 @@ where
} }
} }
if delta.is_some() { if let Some(delta) = delta {
return Ok(NodeBody::Delta(delta.unwrap())); return Ok(NodeBody::Delta(delta));
} }
Err(de::Error::missing_field("delta")) Err(de::Error::missing_field("delta"))

View File

@ -1,5 +1,6 @@
use crate::core::attributes::Attributes;
use crate::core::document::path::Path; use crate::core::document::path::Path;
use crate::core::{Node, NodeAttributes, NodeBodyChangeset, NodeData, NodeOperation, OperationTransform, Transaction}; use crate::core::{Node, NodeBodyChangeset, NodeData, NodeOperation, OperationTransform, Transaction};
use crate::errors::{ErrorBuilder, OTError, OTErrorCode}; use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
use indextree::{Arena, Children, FollowingSiblings, NodeId}; use indextree::{Arena, Children, FollowingSiblings, NodeId};
@ -81,8 +82,8 @@ impl NodeTree {
let mut path = vec![]; let mut path = vec![];
let mut current_node = node_id; let mut current_node = node_id;
// Use .skip(1) on the ancestors iterator to skip the root node. // Use .skip(1) on the ancestors iterator to skip the root node.
let mut ancestors = node_id.ancestors(&self.arena).skip(1); let ancestors = node_id.ancestors(&self.arena).skip(1);
while let Some(parent_node) = ancestors.next() { for parent_node in ancestors {
let counter = self.index_of_node(parent_node, current_node); let counter = self.index_of_node(parent_node, current_node);
path.push(counter); path.push(counter);
current_node = parent_node; current_node = parent_node;
@ -93,8 +94,8 @@ impl NodeTree {
fn index_of_node(&self, parent_node: NodeId, child_node: NodeId) -> usize { fn index_of_node(&self, parent_node: NodeId, child_node: NodeId) -> usize {
let mut counter: usize = 0; let mut counter: usize = 0;
let mut iter = parent_node.children(&self.arena); let iter = parent_node.children(&self.arena);
while let Some(node) = iter.next() { for node in iter {
if node == child_node { if node == child_node {
return counter; return counter;
} }
@ -198,7 +199,7 @@ impl NodeTree {
fn insert_nodes_before(&mut self, node_id: &NodeId, nodes: Vec<NodeData>) { fn insert_nodes_before(&mut self, node_id: &NodeId, nodes: Vec<NodeData>) {
for node in nodes { for node in nodes {
let (node, children) = node.split(); let (node, children) = node.split();
let new_node_id = self.arena.new_node(node.into()); let new_node_id = self.arena.new_node(node);
if node_id.is_removed(&self.arena) { if node_id.is_removed(&self.arena) {
tracing::warn!("Node:{:?} is remove before insert", node_id); tracing::warn!("Node:{:?} is remove before insert", node_id);
return; return;
@ -231,16 +232,16 @@ impl NodeTree {
fn append_nodes(&mut self, parent: &NodeId, nodes: Vec<NodeData>) { fn append_nodes(&mut self, parent: &NodeId, nodes: Vec<NodeData>) {
for node in nodes { for node in nodes {
let (node, children) = node.split(); let (node, children) = node.split();
let new_node_id = self.arena.new_node(node.into()); let new_node_id = self.arena.new_node(node);
parent.append(new_node_id, &mut self.arena); parent.append(new_node_id, &mut self.arena);
self.append_nodes(&new_node_id, children); self.append_nodes(&new_node_id, children);
} }
} }
fn update_attributes(&mut self, path: &Path, attributes: NodeAttributes) -> Result<(), OTError> { fn update_attributes(&mut self, path: &Path, attributes: Attributes) -> Result<(), OTError> {
self.mut_node_at_path(path, |node| { self.mut_node_at_path(path, |node| {
let new_attributes = NodeAttributes::compose(&node.attributes, &attributes)?; let new_attributes = Attributes::compose(&node.attributes, &attributes)?;
node.attributes = new_attributes; node.attributes = new_attributes;
Ok(()) Ok(())
}) })

View File

@ -1,5 +1,6 @@
use crate::core::attributes::Attributes;
use crate::core::document::path::Path; use crate::core::document::path::Path;
use crate::core::{NodeAttributes, NodeBodyChangeset, NodeData}; use crate::core::{NodeBodyChangeset, NodeData};
use crate::errors::OTError; use crate::errors::OTError;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -12,9 +13,9 @@ pub enum NodeOperation {
#[serde(rename = "update")] #[serde(rename = "update")]
UpdateAttributes { UpdateAttributes {
path: Path, path: Path,
attributes: NodeAttributes, attributes: Attributes,
#[serde(rename = "oldAttributes")] #[serde(rename = "oldAttributes")]
old_attributes: NodeAttributes, old_attributes: Attributes,
}, },
#[serde(rename = "update-body")] #[serde(rename = "update-body")]

View File

@ -11,21 +11,21 @@ impl std::ops::Deref for Path {
} }
} }
impl std::convert::Into<Path> for usize { impl std::convert::From<usize> for Path {
fn into(self) -> Path { fn from(val: usize) -> Self {
Path(vec![self]) Path(vec![val])
} }
} }
impl std::convert::Into<Path> for &usize { impl std::convert::From<&usize> for Path {
fn into(self) -> Path { fn from(val: &usize) -> Self {
Path(vec![*self]) Path(vec![*val])
} }
} }
impl std::convert::Into<Path> for &Path { impl std::convert::From<&Path> for Path {
fn into(self) -> Path { fn from(path: &Path) -> Self {
self.clone() path.clone()
} }
} }

View File

@ -1,5 +1,6 @@
use crate::core::attributes::Attributes;
use crate::core::document::path::Path; use crate::core::document::path::Path;
use crate::core::{NodeAttributes, NodeData, NodeOperation, NodeTree}; use crate::core::{NodeData, NodeOperation, NodeTree};
use indextree::NodeId; use indextree::NodeId;
use super::{NodeBodyChangeset, NodeOperationList}; use super::{NodeBodyChangeset, NodeOperationList};
@ -100,10 +101,10 @@ impl<'a> TransactionBuilder<'a> {
self.insert_nodes_at_path(path, vec![node]) self.insert_nodes_at_path(path, vec![node])
} }
pub fn update_attributes_at_path(mut self, path: &Path, attributes: NodeAttributes) -> Self { pub fn update_attributes_at_path(mut self, path: &Path, attributes: Attributes) -> Self {
match self.node_tree.get_node_at_path(path) { match self.node_tree.get_node_at_path(path) {
Some(node) => { Some(node) => {
let mut old_attributes = NodeAttributes::new(); let mut old_attributes = Attributes::new();
for key in attributes.keys() { for key in attributes.keys() {
let old_attrs = &node.attributes; let old_attrs = &node.attributes;
if let Some(value) = old_attrs.get(key.as_str()) { if let Some(value) = old_attrs.get(key.as_str()) {

View File

@ -1,8 +1,10 @@
pub mod attributes;
mod delta; mod delta;
mod document; mod document;
mod interval; mod interval;
mod ot_str; mod ot_str;
pub use attributes::*;
pub use delta::operation::*; pub use delta::operation::*;
pub use delta::*; pub use delta::*;
pub use document::*; pub use document::*;

View File

@ -1,30 +0,0 @@
#![allow(non_snake_case)]
#![allow(clippy::derivable_impls)]
use crate::text_delta::{TextAttribute, TextAttributes};
pub struct AttributeBuilder {
inner: TextAttributes,
}
impl std::default::Default for AttributeBuilder {
fn default() -> Self {
Self {
inner: TextAttributes::default(),
}
}
}
impl AttributeBuilder {
pub fn new() -> Self {
AttributeBuilder::default()
}
pub fn add_attr(mut self, attribute: TextAttribute) -> Self {
self.inner.add(attribute);
self
}
pub fn build(self) -> TextAttributes {
self.inner
}
}

View File

@ -1,253 +1,58 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use crate::core::{Attributes, Operation, OperationTransform}; use crate::core::{AttributeEntry, AttributeKey, Attributes};
use crate::{block_attribute, errors::OTError, ignore_attribute, inline_attribute, list_attribute}; use crate::text_delta::TextOperation;
use crate::{inline_attribute_entry, inline_list_attribute_entry};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::{ use std::str::FromStr;
collections::{HashMap, HashSet}, use std::{collections::HashSet, iter::FromIterator};
fmt, use strum_macros::{AsRefStr, Display, EnumString};
fmt::Formatter,
iter::FromIterator,
};
use strum_macros::Display;
pub type RichTextOperation = Operation<TextAttributes>;
impl RichTextOperation {
pub fn contain_attribute(&self, attribute: &TextAttribute) -> bool {
self.get_attributes().contains_key(&attribute.key)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TextAttributes {
pub(crate) inner: HashMap<TextAttributeKey, TextAttributeValue>,
}
impl std::default::Default for TextAttributes {
fn default() -> Self {
Self {
inner: HashMap::with_capacity(0),
}
}
}
impl fmt::Display for TextAttributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{:?}", self.inner))
}
}
#[inline(always)] #[inline(always)]
pub fn plain_attributes() -> TextAttributes { pub fn empty_attributes() -> Attributes {
TextAttributes::default() Attributes::default()
} }
impl TextAttributes { pub fn attributes_except_header(op: &TextOperation) -> Attributes {
pub fn new() -> Self {
TextAttributes { inner: HashMap::new() }
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn add(&mut self, attribute: TextAttribute) {
let TextAttribute { key, value, scope: _ } = attribute;
self.inner.insert(key, value);
}
pub fn insert(&mut self, key: TextAttributeKey, value: TextAttributeValue) {
self.inner.insert(key, value);
}
pub fn delete(&mut self, key: &TextAttributeKey) {
self.inner.insert(key.clone(), TextAttributeValue(None));
}
pub fn mark_all_as_removed_except(&mut self, attribute: Option<TextAttributeKey>) {
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: TextAttributeKey) {
self.inner.retain(|k, _| k != &key);
}
// 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<TextAttributes>) {
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 TextAttributes {
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 OperationTransform for TextAttributes {
fn compose(&self, other: &Self) -> Result<Self, OTError>
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(TextAttributes::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(TextAttributes::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(TextAttributes::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 TextAttributes {
type Target = HashMap<TextAttributeKey, TextAttributeValue>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl std::ops::DerefMut for TextAttributes {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
pub fn attributes_except_header(op: &RichTextOperation) -> TextAttributes {
let mut attributes = op.get_attributes(); let mut attributes = op.get_attributes();
attributes.remove(TextAttributeKey::Header); attributes.remove_key(BuildInTextAttributeKey::Header);
attributes attributes
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TextAttribute { pub struct BuildInTextAttribute();
pub key: TextAttributeKey,
pub value: TextAttributeValue,
pub scope: AttributeScope,
}
impl TextAttribute { impl BuildInTextAttribute {
// inline inline_attribute_entry!(Bold, bool);
inline_attribute!(Bold, bool); inline_attribute_entry!(Italic, bool);
inline_attribute!(Italic, bool); inline_attribute_entry!(Underline, bool);
inline_attribute!(Underline, bool); inline_attribute_entry!(StrikeThrough, bool);
inline_attribute!(StrikeThrough, bool); inline_attribute_entry!(Link, &str);
inline_attribute!(Link, &str); inline_attribute_entry!(Color, String);
inline_attribute!(Color, String); inline_attribute_entry!(Font, usize);
inline_attribute!(Font, usize); inline_attribute_entry!(Size, usize);
inline_attribute!(Size, usize); inline_attribute_entry!(Background, String);
inline_attribute!(Background, String); inline_attribute_entry!(InlineCode, bool);
inline_attribute!(InlineCode, bool);
// block inline_attribute_entry!(Header, usize);
block_attribute!(Header, usize); inline_attribute_entry!(Indent, usize);
block_attribute!(Indent, usize); inline_attribute_entry!(Align, String);
block_attribute!(Align, String); inline_attribute_entry!(List, &str);
block_attribute!(List, &str); inline_attribute_entry!(CodeBlock, bool);
block_attribute!(CodeBlock, bool); inline_attribute_entry!(BlockQuote, bool);
block_attribute!(BlockQuote, bool);
// ignore inline_attribute_entry!(Width, usize);
ignore_attribute!(Width, usize); inline_attribute_entry!(Height, usize);
ignore_attribute!(Height, usize);
// List extension // List extension
list_attribute!(Bullet, "bullet"); inline_list_attribute_entry!(Bullet, "bullet");
list_attribute!(Ordered, "ordered"); inline_list_attribute_entry!(Ordered, "ordered");
list_attribute!(Checked, "checked"); inline_list_attribute_entry!(Checked, "checked");
list_attribute!(UnChecked, "unchecked"); inline_list_attribute_entry!(UnChecked, "unchecked");
pub fn to_json(&self) -> String {
match serde_json::to_string(self) {
Ok(json) => json,
Err(e) => {
log::error!("Attribute serialize to str failed: {}", e);
"".to_owned()
}
}
}
} }
impl fmt::Display for TextAttribute { #[derive(Clone, Debug, Display, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize, AsRefStr, EnumString)]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { #[strum(serialize_all = "snake_case")]
let s = format!("{:?}:{:?} {:?}", self.key, self.value.0, self.scope); pub enum BuildInTextAttributeKey {
f.write_str(&s)
}
}
impl std::convert::From<TextAttribute> for TextAttributes {
fn from(attr: TextAttribute) -> Self {
let mut attributes = TextAttributes::new();
attributes.add(attr);
attributes
}
}
#[derive(Clone, Debug, Display, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
// #[serde(rename_all = "snake_case")]
pub enum TextAttributeKey {
#[serde(rename = "bold")] #[serde(rename = "bold")]
Bold, Bold,
#[serde(rename = "italic")] #[serde(rename = "italic")]
@ -286,91 +91,45 @@ pub enum TextAttributeKey {
Header, Header,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub fn is_block(k: &AttributeKey) -> bool {
pub struct TextAttributeValue(pub Option<String>); if let Ok(key) = BuildInTextAttributeKey::from_str(k) {
BLOCK_KEYS.contains(&key)
impl std::convert::From<&usize> for TextAttributeValue {
fn from(val: &usize) -> Self {
TextAttributeValue::from(*val)
}
}
impl std::convert::From<usize> for TextAttributeValue {
fn from(val: usize) -> Self {
if val > 0_usize {
TextAttributeValue(Some(format!("{}", val)))
} else { } else {
TextAttributeValue(None) false
}
} }
} }
impl std::convert::From<&str> for TextAttributeValue { pub fn is_inline(k: &AttributeKey) -> bool {
fn from(val: &str) -> Self { if let Ok(key) = BuildInTextAttributeKey::from_str(k) {
val.to_owned().into() INLINE_KEYS.contains(&key)
}
}
impl std::convert::From<String> for TextAttributeValue {
fn from(val: String) -> Self {
if val.is_empty() {
TextAttributeValue(None)
} else { } else {
TextAttributeValue(Some(val)) false
} }
}
}
impl std::convert::From<&bool> for TextAttributeValue {
fn from(val: &bool) -> Self {
TextAttributeValue::from(*val)
}
}
impl std::convert::From<bool> for TextAttributeValue {
fn from(val: bool) -> Self {
let val = match val {
true => Some("true".to_owned()),
false => None,
};
TextAttributeValue(val)
}
}
pub fn is_block_except_header(k: &TextAttributeKey) -> bool {
if k == &TextAttributeKey::Header {
return false;
}
BLOCK_KEYS.contains(k)
}
pub fn is_block(k: &TextAttributeKey) -> bool {
BLOCK_KEYS.contains(k)
} }
lazy_static! { lazy_static! {
static ref BLOCK_KEYS: HashSet<TextAttributeKey> = HashSet::from_iter(vec![ static ref BLOCK_KEYS: HashSet<BuildInTextAttributeKey> = HashSet::from_iter(vec![
TextAttributeKey::Header, BuildInTextAttributeKey::Header,
TextAttributeKey::Indent, BuildInTextAttributeKey::Indent,
TextAttributeKey::Align, BuildInTextAttributeKey::Align,
TextAttributeKey::CodeBlock, BuildInTextAttributeKey::CodeBlock,
TextAttributeKey::List, BuildInTextAttributeKey::List,
TextAttributeKey::BlockQuote, BuildInTextAttributeKey::BlockQuote,
]); ]);
static ref INLINE_KEYS: HashSet<TextAttributeKey> = HashSet::from_iter(vec![ static ref INLINE_KEYS: HashSet<BuildInTextAttributeKey> = HashSet::from_iter(vec![
TextAttributeKey::Bold, BuildInTextAttributeKey::Bold,
TextAttributeKey::Italic, BuildInTextAttributeKey::Italic,
TextAttributeKey::Underline, BuildInTextAttributeKey::Underline,
TextAttributeKey::StrikeThrough, BuildInTextAttributeKey::StrikeThrough,
TextAttributeKey::Link, BuildInTextAttributeKey::Link,
TextAttributeKey::Color, BuildInTextAttributeKey::Color,
TextAttributeKey::Font, BuildInTextAttributeKey::Font,
TextAttributeKey::Size, BuildInTextAttributeKey::Size,
TextAttributeKey::Background, BuildInTextAttributeKey::Background,
TextAttributeKey::InlineCode, BuildInTextAttributeKey::InlineCode,
]); ]);
static ref INGORE_KEYS: HashSet<TextAttributeKey> = static ref INGORE_KEYS: HashSet<BuildInTextAttributeKey> =
HashSet::from_iter(vec![TextAttributeKey::Width, TextAttributeKey::Height,]); HashSet::from_iter(vec![BuildInTextAttributeKey::Width, BuildInTextAttributeKey::Height,]);
} }
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]

View File

@ -1,231 +0,0 @@
#[rustfmt::skip]
use crate::text_delta::{TextAttribute, TextAttributeKey, TextAttributes, TextAttributeValue};
use serde::{
de,
de::{MapAccess, Visitor},
ser::SerializeMap,
Deserialize, Deserializer, Serialize, Serializer,
};
use std::fmt;
impl Serialize for TextAttribute {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(1))?;
let _ = serial_attribute(&mut map, &self.key, &self.value)?;
map.end()
}
}
impl Serialize for TextAttributes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if self.is_empty() {
return serializer.serialize_none();
}
let mut map = serializer.serialize_map(Some(self.inner.len()))?;
for (k, v) in &self.inner {
let _ = serial_attribute(&mut map, k, v)?;
}
map.end()
}
}
fn serial_attribute<S, E>(map_serializer: &mut S, key: &TextAttributeKey, value: &TextAttributeValue) -> Result<(), E>
where
S: SerializeMap,
E: From<<S as SerializeMap>::Error>,
{
if let Some(v) = &value.0 {
match key {
TextAttributeKey::Bold
| TextAttributeKey::Italic
| TextAttributeKey::Underline
| TextAttributeKey::StrikeThrough
| TextAttributeKey::CodeBlock
| TextAttributeKey::InlineCode
| TextAttributeKey::BlockQuote => match &v.parse::<bool>() {
Ok(value) => map_serializer.serialize_entry(&key, value)?,
Err(e) => log::error!("Serial {:?} failed. {:?}", &key, e),
},
TextAttributeKey::Font
| TextAttributeKey::Size
| TextAttributeKey::Header
| TextAttributeKey::Indent
| TextAttributeKey::Width
| TextAttributeKey::Height => match &v.parse::<i32>() {
Ok(value) => map_serializer.serialize_entry(&key, value)?,
Err(e) => log::error!("Serial {:?} failed. {:?}", &key, e),
},
TextAttributeKey::Link
| TextAttributeKey::Color
| TextAttributeKey::Background
| TextAttributeKey::Align
| TextAttributeKey::List => {
map_serializer.serialize_entry(&key, v)?;
}
}
} else {
map_serializer.serialize_entry(&key, "")?;
}
Ok(())
}
impl<'de> Deserialize<'de> for TextAttributes {
fn deserialize<D>(deserializer: D) -> Result<TextAttributes, D::Error>
where
D: Deserializer<'de>,
{
struct AttributesVisitor;
impl<'de> Visitor<'de> for AttributesVisitor {
type Value = TextAttributes;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Expect map")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut attributes = TextAttributes::new();
while let Some(key) = map.next_key::<TextAttributeKey>()? {
let value = map.next_value::<TextAttributeValue>()?;
attributes.insert(key, value);
}
Ok(attributes)
}
}
deserializer.deserialize_map(AttributesVisitor {})
}
}
impl Serialize for TextAttributeValue {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match &self.0 {
None => serializer.serialize_none(),
Some(val) => serializer.serialize_str(val),
}
}
}
impl<'de> Deserialize<'de> for TextAttributeValue {
fn deserialize<D>(deserializer: D) -> Result<TextAttributeValue, D::Error>
where
D: Deserializer<'de>,
{
struct AttributeValueVisitor;
impl<'de> Visitor<'de> for AttributeValueVisitor {
type Value = TextAttributeValue;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("bool, usize or string")
}
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(value.into())
}
fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(TextAttributeValue(Some(format!("{}", value))))
}
fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(TextAttributeValue(Some(format!("{}", value))))
}
fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(TextAttributeValue(Some(format!("{}", value))))
}
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(TextAttributeValue(Some(format!("{}", value))))
}
fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(TextAttributeValue(Some(format!("{}", value))))
}
fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(TextAttributeValue(Some(format!("{}", value))))
}
fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(TextAttributeValue(Some(format!("{}", value))))
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(TextAttributeValue(Some(format!("{}", value))))
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(s.into())
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(TextAttributeValue(None))
}
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
// the value that contains null will be processed here.
Ok(TextAttributeValue(None))
}
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
// https://github.com/serde-rs/json/issues/505
let mut map = map;
let value = map.next_value::<TextAttributeValue>()?;
Ok(value)
}
}
deserializer.deserialize_any(AttributeValueVisitor)
}
}

View File

@ -0,0 +1,6 @@
use crate::core::{Attributes, Operation, OperationBuilder, Operations};
pub type TextDelta = Operations<Attributes>;
pub type TextDeltaBuilder = OperationBuilder<Attributes>;
pub type TextOperation = Operation<Attributes>;

View File

@ -1,62 +1,33 @@
#[macro_export] #[macro_export]
macro_rules! inline_attribute { macro_rules! inline_attribute_entry {
( (
$key: ident, $key: ident,
$value: ty $value: ty
) => { ) => {
pub fn $key(value: $value) -> Self { pub fn $key(value: $value) -> crate::core::AttributeEntry {
Self { AttributeEntry {
key: TextAttributeKey::$key, key: BuildInTextAttributeKey::$key.as_ref().to_string(),
value: value.into(), value: value.into(),
scope: AttributeScope::Inline,
} }
} }
}; };
} }
#[macro_export] #[macro_export]
macro_rules! block_attribute { macro_rules! inline_list_attribute_entry {
(
$key: ident,
$value: ty
) => {
pub fn $key(value: $value) -> Self {
Self {
key: TextAttributeKey::$key,
value: value.into(),
scope: AttributeScope::Block,
}
}
};
}
#[macro_export]
macro_rules! list_attribute {
( (
$key: ident, $key: ident,
$value: expr $value: expr
) => { ) => {
pub fn $key(b: bool) -> Self { pub fn $key(b: bool) -> crate::core::AttributeEntry {
let value = match b { let value = match b {
true => $value, true => $value,
false => "", false => "",
}; };
TextAttribute::List(value)
}
};
}
#[macro_export] AttributeEntry {
macro_rules! ignore_attribute { key: BuildInTextAttributeKey::List.as_ref().to_string(),
(
$key: ident,
$value: ident
) => {
pub fn $key(value: $value) -> Self {
Self {
key: TextAttributeKey::$key,
value: value.into(), value: value.into(),
scope: AttributeScope::Ignore,
} }
} }
}; };

View File

@ -1,11 +1,8 @@
mod attribute_builder;
mod attributes; mod attributes;
mod attributes_serde;
#[macro_use] #[macro_use]
mod macros; mod macros;
mod text_delta; mod delta;
pub use attribute_builder::*;
pub use attributes::*; pub use attributes::*;
pub use text_delta::*; pub use delta::*;

View File

@ -1,5 +0,0 @@
use crate::core::{OperationBuilder, Operations};
use crate::text_delta::TextAttributes;
pub type TextDelta = Operations<TextAttributes>;
pub type TextDeltaBuilder = OperationBuilder<TextAttributes>;

View File

@ -1,7 +1,8 @@
use super::script::{NodeScript::*, *}; use super::script::{NodeScript::*, *};
use lib_ot::core::AttributeBuilder;
use lib_ot::{ use lib_ot::{
core::{NodeData, Path}, core::{NodeData, Path},
text_delta::{AttributeBuilder, TextAttribute, TextAttributes, TextDeltaBuilder}, text_delta::TextDeltaBuilder,
}; };
#[test] #[test]
@ -14,17 +15,17 @@ fn editor_deserialize_node_test() {
.insert("👋 ") .insert("👋 ")
.insert_with_attributes( .insert_with_attributes(
"Welcome to ", "Welcome to ",
AttributeBuilder::new().add_attr(TextAttribute::Bold(true)).build(), AttributeBuilder::new().insert("href", "appflowy.io").build(),
) )
.insert_with_attributes( .insert_with_attributes(
"AppFlowy Editor", "AppFlowy Editor",
AttributeBuilder::new().add_attr(TextAttribute::Italic(true)).build(), AttributeBuilder::new().insert("italic", true).build(),
) )
.build(); .build();
test.run_scripts(vec![ test.run_scripts(vec![
InsertNode { InsertNode {
path: path.clone(), path,
node: node.clone(), node: node.clone(),
}, },
AssertNumberOfNodesAtPath { path: None, len: 1 }, AssertNumberOfNodesAtPath { path: None, len: 1 },
@ -77,7 +78,7 @@ const EXAMPLE_JSON: &str = r#"
{ {
"insert": "Welcome to ", "insert": "Welcome to ",
"attributes": { "attributes": {
"bold": true "href": "appflowy.io"
} }
}, },
{ {

View File

@ -1,5 +1,6 @@
use lib_ot::core::AttributeBuilder;
use lib_ot::{ use lib_ot::{
core::{NodeAttributeBuilder, NodeBodyChangeset, NodeData, NodeDataBuilder, NodeOperation, Path}, core::{NodeBodyChangeset, NodeData, NodeDataBuilder, NodeOperation, Path},
text_delta::TextDeltaBuilder, text_delta::TextDeltaBuilder,
}; };
@ -32,15 +33,15 @@ fn operation_insert_node_with_children_serde_test() {
fn operation_update_node_attributes_serde_test() { fn operation_update_node_attributes_serde_test() {
let operation = NodeOperation::UpdateAttributes { let operation = NodeOperation::UpdateAttributes {
path: Path(vec![0, 1]), path: Path(vec![0, 1]),
attributes: NodeAttributeBuilder::new().insert("bold", true).build(), attributes: AttributeBuilder::new().insert("bold", true).build(),
old_attributes: NodeAttributeBuilder::new().insert("bold", false).build(), old_attributes: AttributeBuilder::new().insert("bold", false).build(),
}; };
let result = serde_json::to_string(&operation).unwrap(); let result = serde_json::to_string(&operation).unwrap();
assert_eq!( assert_eq!(
result, result,
r#"{"op":"update","path":[0,1],"attributes":{"bold":true},"oldAttributes":{"bold":false}}"# r#"{"op":"update","path":[0,1],"attributes":{"bold":true},"oldAttributes":{"bold":null}}"#
); );
} }

View File

@ -1,11 +1,12 @@
use lib_ot::{ use lib_ot::{
core::{NodeAttributes, NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder}, core::attributes::Attributes,
core::{NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
text_delta::TextDelta, text_delta::TextDelta,
}; };
pub enum NodeScript { pub enum NodeScript {
InsertNode { path: Path, node: NodeData }, InsertNode { path: Path, node: NodeData },
UpdateAttributes { path: Path, attributes: NodeAttributes }, UpdateAttributes { path: Path, attributes: Attributes },
UpdateBody { path: Path, changeset: NodeBodyChangeset }, UpdateBody { path: Path, changeset: NodeBodyChangeset },
DeleteNode { path: Path }, DeleteNode { path: Path },
AssertNumberOfNodesAtPath { path: Option<Path>, len: usize }, AssertNumberOfNodesAtPath { path: Option<Path>, len: usize },
@ -65,7 +66,7 @@ impl NodeTest {
None => assert!(node_id.is_none()), None => assert!(node_id.is_none()),
Some(node_id) => { Some(node_id) => {
let node_data = self.node_tree.get_node(node_id).cloned(); let node_data = self.node_tree.get_node(node_id).cloned();
assert_eq!(node_data, expected.and_then(|e| Some(e.into()))); assert_eq!(node_data, expected.map(|e| e.into()));
} }
} }
} }

View File

@ -172,7 +172,7 @@ fn node_delete_test() {
let scripts = vec![ let scripts = vec![
InsertNode { InsertNode {
path: path.clone(), path: path.clone(),
node: inserted_node.clone(), node: inserted_node,
}, },
DeleteNode { path: path.clone() }, DeleteNode { path: path.clone() },
AssertNode { path, expected: None }, AssertNode { path, expected: None },
@ -198,7 +198,7 @@ fn node_update_body_test() {
let scripts = vec![ let scripts = vec![
InsertNode { InsertNode {
path: path.clone(), path: path.clone(),
node: node.clone(), node,
}, },
UpdateBody { UpdateBody {
path: path.clone(), path: path.clone(),