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
commit 9ff0975f7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
62 changed files with 914 additions and 1347 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -13,9 +13,10 @@ use flowy_sync::{
errors::CollaborateResult,
util::make_delta_from_revisions,
};
use lib_ot::core::AttributeEntry;
use lib_ot::{
core::{Interval, Operation},
text_delta::{TextAttribute, TextDelta},
text_delta::TextDelta,
};
use lib_ws::WSConnectState;
use std::sync::Arc;
@ -85,7 +86,7 @@ impl TextBlockEditor {
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 msg = EditorCommand::Format {
interval,

View file

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

View file

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

View file

@ -1,6 +1,6 @@
#![cfg_attr(rustfmt, rustfmt::skip)]
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 unicode_segmentation::UnicodeSegmentation;
use lib_ot::text_delta::TextDelta;
@ -14,12 +14,12 @@ fn attributes_bold_added() {
0,
r#"[
{"insert":"123"},
{"insert":"45","attributes":{"bold":"true"}},
{"insert":"45","attributes":{"bold":true}},
{"insert":"6"}
]"#,
),
];
TestBuilder::new().run_scripts::<PlainDoc>(ops);
TestBuilder::new().run_scripts::<EmptyDoc>(ops);
}
#[test]
@ -27,11 +27,11 @@ fn attributes_bold_added_and_invert_all() {
let ops = vec![
Insert(0, "123", 0),
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),
AssertDocJson(0, r#"[{"insert":"123"}]"#),
];
TestBuilder::new().run_scripts::<PlainDoc>(ops);
TestBuilder::new().run_scripts::<EmptyDoc>(ops);
}
#[test]
@ -39,11 +39,11 @@ fn attributes_bold_added_and_invert_partial_suffix() {
let ops = vec![
Insert(0, "1234", 0),
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),
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]
@ -51,13 +51,13 @@ fn attributes_bold_added_and_invert_partial_suffix2() {
let ops = vec![
Insert(0, "1234", 0),
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),
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),
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]
@ -67,22 +67,22 @@ fn attributes_bold_added_with_new_line() {
Bold(0, Interval::new(0, 6), true),
AssertDocJson(
0,
r#"[{"insert":"123456","attributes":{"bold":"true"}},{"insert":"\n"}]"#,
r#"[{"insert":"123456","attributes":{"bold":true}},{"insert":"\n"}]"#,
),
Insert(0, "\n", 3),
AssertDocJson(
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),
AssertDocJson(
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),
AssertDocJson(
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);
@ -93,11 +93,11 @@ fn attributes_bold_added_and_invert_partial_prefix() {
let ops = vec![
Insert(0, "1234", 0),
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),
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]
@ -105,11 +105,11 @@ fn attributes_bold_added_consecutive() {
let ops = vec![
Insert(0, "1234", 0),
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),
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]
@ -120,12 +120,12 @@ fn attributes_bold_added_italic() {
Italic(0, Interval::new(0, 4), true),
AssertDocJson(
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),
AssertDocJson(
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);
@ -136,27 +136,27 @@ fn attributes_bold_added_italic2() {
let ops = vec![
Insert(0, "123456", 0),
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),
AssertDocJson(
0,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"3456","attributes":{"bold":"true"}}]
{"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"3456","attributes":{"bold":true}}]
"#,
),
Italic(0, Interval::new(4, 6), true),
AssertDocJson(
0,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"34","attributes":{"bold":"true"}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}}]
{"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"34","attributes":{"bold":true}},
{"insert":"56","attributes":{"italic":true,"bold":true}}]
"#,
),
];
TestBuilder::new().run_scripts::<PlainDoc>(ops);
TestBuilder::new().run_scripts::<EmptyDoc>(ops);
}
#[test]
@ -168,16 +168,16 @@ fn attributes_bold_added_italic3() {
AssertDocJson(
0,
r#"[
{"insert":"12","attributes":{"bold":"true","italic":"true"}},
{"insert":"345","attributes":{"bold":"true"}},{"insert":"6789"}]
{"insert":"12","attributes":{"bold":true,"italic":true}},
{"insert":"345","attributes":{"bold":true}},{"insert":"6789"}]
"#,
),
Italic(0, Interval::new(2, 4), true),
AssertDocJson(
0,
r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}},
{"insert":"5","attributes":{"bold":"true"}},
{"insert":"1234","attributes":{"bold":true,"italic":true}},
{"insert":"5","attributes":{"bold":true}},
{"insert":"6789"}]
"#,
),
@ -185,15 +185,15 @@ fn attributes_bold_added_italic3() {
AssertDocJson(
0,
r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}},
{"insert":"5","attributes":{"bold":"true"}},
{"insert":"1234","attributes":{"bold":true,"italic":true}},
{"insert":"5","attributes":{"bold":true}},
{"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]
@ -205,57 +205,57 @@ fn attributes_bold_added_italic_delete() {
AssertDocJson(
0,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"345","attributes":{"bold":"true"}},{"insert":"6789"}]
{"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"345","attributes":{"bold":true}},{"insert":"6789"}]
"#,
),
Italic(0, Interval::new(2, 4), true),
AssertDocJson(
0,
r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}}
,{"insert":"5","attributes":{"bold":"true"}},{"insert":"6789"}]"#,
{"insert":"1234","attributes":{"bold":true,"italic":true}}
,{"insert":"5","attributes":{"bold":true}},{"insert":"6789"}]"#,
),
Bold(0, Interval::new(7, 9), true),
AssertDocJson(
0,
r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}},
{"insert":"5","attributes":{"bold":"true"}},{"insert":"67"},
{"insert":"89","attributes":{"bold":"true"}}]
{"insert":"1234","attributes":{"bold":true,"italic":true}},
{"insert":"5","attributes":{"bold":true}},{"insert":"67"},
{"insert":"89","attributes":{"bold":true}}]
"#,
),
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]
fn attributes_merge_inserted_text_with_same_attribute() {
let ops = vec![
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)),
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]
fn attributes_compose_attr_attributes_with_attr_attributes_test() {
let ops = vec![
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)),
AssertDocJson(1, r#"[{"insert":"7","attributes":{"bold":"true"}}]"#),
AssertDocJson(1, r#"[{"insert":"7","attributes":{"bold":true}}]"#),
Transform(0, 1),
AssertDocJson(0, r#"[{"insert":"1234567","attributes":{"bold":"true"}}]"#),
AssertDocJson(1, r#"[{"insert":"1234567","attributes":{"bold":"true"}}]"#),
AssertDocJson(0, 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]
@ -268,113 +268,113 @@ fn attributes_compose_attr_attributes_with_attr_attributes_test2() {
AssertDocJson(
0,
r#"[
{"insert":"12","attributes":{"bold":"true","italic":"true"}},
{"insert":"34","attributes":{"bold":"true"}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}}]
{"insert":"12","attributes":{"bold":true,"italic":true}},
{"insert":"34","attributes":{"bold":true}},
{"insert":"56","attributes":{"italic":true,"bold":true}}]
"#,
),
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),
AssertDocJson(
0,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"34","attributes":{"bold":"true"}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}},
{"insert":"7","attributes":{"bold":"true"}}]
{"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"34","attributes":{"bold":true}},
{"insert":"56","attributes":{"italic":true,"bold":true}},
{"insert":"7","attributes":{"bold":true}}]
"#,
),
AssertDocJson(
1,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"34","attributes":{"bold":"true"}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}},
{"insert":"7","attributes":{"bold":"true"}}]
{"insert":"12","attributes":{"italic":true,"bold":true}},
{"insert":"34","attributes":{"bold":true}},
{"insert":"56","attributes":{"italic":true,"bold":true}},
{"insert":"7","attributes":{"bold":true}}]
"#,
),
];
TestBuilder::new().run_scripts::<PlainDoc>(ops);
TestBuilder::new().run_scripts::<EmptyDoc>(ops);
}
#[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![
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),
AssertDocJson(1, r#"[{"insert":"7"}]"#),
Transform(0, 1),
AssertDocJson(0, expected),
AssertDocJson(1, expected),
];
TestBuilder::new().run_scripts::<PlainDoc>(ops);
TestBuilder::new().run_scripts::<EmptyDoc>(ops);
}
#[test]
fn attributes_replace_heading() {
let ops = vec![
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)),
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]
fn attributes_replace_trailing() {
let ops = vec![
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)),
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]
fn attributes_replace_middle() {
let ops = vec![
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)),
AssertDocJson(0, r#"[{"insert":"3456","attributes":{"bold":"true"}}]"#),
AssertDocJson(0, r#"[{"insert":"3456","attributes":{"bold":true}}]"#),
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]
fn attributes_replace_all() {
let ops = vec![
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)),
AssertDocJson(0, r#"[]"#),
];
TestBuilder::new().run_scripts::<PlainDoc>(ops);
TestBuilder::new().run_scripts::<EmptyDoc>(ops);
}
#[test]
fn attributes_replace_with_text() {
let ops = vec![
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"),
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]
@ -472,7 +472,7 @@ fn attributes_link_format_with_bold() {
AssertDocJson(
0,
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":"\n"}]
"#,

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
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};
#[test]
@ -85,7 +85,7 @@ fn history_bold_redo() {
Undo(0),
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
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);
}
@ -99,7 +99,7 @@ fn history_bold_redo_with_lagging() {
Undo(0),
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
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);
}
@ -115,7 +115,7 @@ fn history_delete_undo() {
Undo(0),
AssertDocJson(0, r#"[{"insert":"123"}]"#),
];
TestBuilder::new().run_scripts::<PlainDoc>(ops);
TestBuilder::new().run_scripts::<EmptyDoc>(ops);
}
#[test]
@ -127,7 +127,7 @@ fn history_delete_undo_2() {
AssertDocJson(
0,
r#"[
{"insert":"23","attributes":{"bold":"true"}},
{"insert":"23","attributes":{"bold":true}},
{"insert":"\n"}]
"#,
),
@ -148,7 +148,7 @@ fn history_delete_undo_with_lagging() {
AssertDocJson(
0,
r#"[
{"insert":"23","attributes":{"bold":"true"}},
{"insert":"23","attributes":{"bold":true}},
{"insert":"\n"}]
"#,
),
@ -156,7 +156,7 @@ fn history_delete_undo_with_lagging() {
AssertDocJson(
0,
r#"[
{"insert":"123","attributes":{"bold":"true"}},
{"insert":"123","attributes":{"bold":true}},
{"insert":"\n"}]
"#,
),
@ -188,7 +188,7 @@ fn history_replace_undo() {
0,
r#"[
{"insert":"ab"},
{"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}]
{"insert":"3","attributes":{"bold":true}},{"insert":"\n"}]
"#,
),
Undo(0),
@ -209,11 +209,11 @@ fn history_replace_undo_with_lagging() {
0,
r#"[
{"insert":"ab"},
{"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}]
{"insert":"3","attributes":{"bold":true}},{"insert":"\n"}]
"#,
),
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);
}
@ -230,7 +230,7 @@ fn history_replace_redo() {
0,
r#"[
{"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,
};
use bytes::Bytes;
use lib_ot::{
core::*,
text_delta::{TextAttribute, TextDelta},
};
use lib_ot::{core::*, text_delta::TextDelta};
use tokio::sync::mpsc;
pub trait InitialDocumentText {
fn initial_delta() -> TextDelta;
}
pub struct PlainDoc();
impl InitialDocumentText for PlainDoc {
pub struct EmptyDoc();
impl InitialDocumentText for EmptyDoc {
fn initial_delta() -> TextDelta {
TextDelta::new()
}
@ -141,9 +138,9 @@ impl ClientDocument {
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)?;
tracing::trace!("format {} with {}", interval, attribute);
tracing::trace!("format {} with {:?}", interval, attribute);
let format_delta = self.view.format(&self.delta, attribute, interval).unwrap();
self.compose_delta(format_delta.clone())?;
Ok(format_delta)

View file

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

View file

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

View file

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

View file

@ -1,7 +1,8 @@
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 start = 0;
let end = op.len();
@ -11,10 +12,10 @@ pub(crate) fn line_break(op: &RichTextOperation, attribute: &TextAttribute, scop
match scope {
AttributeScope::Inline => {
new_delta.retain(line_break - start, attribute.clone().into());
new_delta.retain(1, plain_attributes());
new_delta.retain(1, empty_attributes());
}
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());
}
_ => {
@ -29,7 +30,7 @@ pub(crate) fn line_break(op: &RichTextOperation, attribute: &TextAttribute, scop
if start < end {
match scope {
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),
}
}

View file

@ -1,6 +1,6 @@
use crate::{client_document::InsertExt, util::is_newline};
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 {}
@ -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(
OperationBuilder::new()

View file

@ -1,7 +1,8 @@
use crate::{client_document::InsertExt, util::is_whitespace};
use lib_ot::core::Attributes;
use lib_ot::{
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 url::Url;
@ -36,7 +37,7 @@ impl InsertExt for AutoFormatExt {
});
let next_attributes = match iter.next_op() {
None => plain_attributes(),
None => empty_attributes(),
Some(op) => op.get_attributes(),
};
@ -60,9 +61,9 @@ pub enum AutoFormatter {
}
impl AutoFormatter {
pub fn to_attributes(&self) -> TextAttributes {
pub fn to_attributes(&self) -> Attributes {
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 lib_ot::core::Attributes;
use lib_ot::{
core::{Attributes, OperationBuilder, OperationIterator, NEW_LINE},
text_delta::{TextAttributeKey, TextAttributes, TextDelta},
core::{OperationAttributes, OperationBuilder, OperationIterator, NEW_LINE},
text_delta::{BuildInTextAttributeKey, TextDelta},
};
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> {
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:
// insert "\n" to "123456" at index 3
@ -23,7 +24,10 @@ impl InsertExt for DefaultInsertAttribute {
match iter.last() {
None => {}
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());
}
}

View file

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

View file

@ -4,7 +4,7 @@ use crate::{
};
use lib_ot::{
core::{OpNewline, OperationBuilder, OperationIterator, NEW_LINE},
text_delta::{plain_attributes, TextAttributeKey, TextDelta},
text_delta::{empty_attributes, BuildInTextAttributeKey, TextDelta},
};
pub struct PreserveInlineFormat {}
@ -25,7 +25,7 @@ impl InsertExt for PreserveInlineFormat {
}
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(
OperationBuilder::new()
.retain(index + replace_len)
@ -36,10 +36,10 @@ impl InsertExt for PreserveInlineFormat {
let next = iter.next_op();
match &next {
None => attributes = plain_attributes(),
None => attributes = empty_attributes(),
Some(next) => {
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();
new_delta.retain(index + replace_len, plain_attributes());
new_delta.retain(index + replace_len, empty_attributes());
if newline_status.is_contain() {
debug_assert!(!next.has_attribute());
new_delta.insert(NEW_LINE, plain_attributes());
new_delta.insert(NEW_LINE, empty_attributes());
return Some(new_delta);
}

View file

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

View file

@ -1,10 +1,8 @@
pub use delete::*;
pub use format::*;
pub use insert::*;
use lib_ot::{
core::Interval,
text_delta::{TextAttribute, TextDelta},
};
use lib_ot::core::AttributeEntry;
use lib_ot::{core::Interval, text_delta::TextDelta};
mod delete;
mod format;
@ -22,7 +20,7 @@ pub trait InsertExt {
pub trait FormatExt {
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 {

View file

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

View file

@ -11,7 +11,8 @@ use async_stream::stream;
use dashmap::DashMap;
use futures::stream::StreamExt;
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 tokio::{
sync::{mpsc, oneshot, RwLock},
@ -198,7 +199,7 @@ impl std::ops::Drop for ServerDocumentManager {
}
}
type DocumentRevisionSynchronizer = RevisionSynchronizer<TextAttributes>;
type DocumentRevisionSynchronizer = RevisionSynchronizer<Attributes>;
struct OpenDocumentHandler {
doc_id: String,

View file

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

View file

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

View file

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

View file

@ -1,6 +1,8 @@
use crate::core::{OperationIterator, Operations};
use crate::text_delta::{is_block, TextAttributeKey, TextAttributeValue, TextAttributes};
use crate::core::{AttributeKey, AttributeValue, Attributes, OperationIterator, Operations};
use crate::text_delta::{is_block, TextAttributeKey};
use std::collections::HashMap;
use std::str::FromStr;
use strum_macros::EnumString;
const LINEFEEDASCIICODE: i32 = 0x0A;
@ -98,14 +100,14 @@ mod tests {
}
struct Attribute {
key: TextAttributeKey,
value: TextAttributeValue,
key: AttributeKey,
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 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 iterator = OperationIterator::new(delta);
let mut current_block_style: Option<Attribute> = None;
@ -113,7 +115,7 @@ pub fn markdown_encoder(delta: &Operations<TextAttributes>) -> String {
while iterator.has_next() {
let operation = iterator.next().unwrap();
let operation_data = operation.get_data();
if !operation_data.contains("\n") {
if !operation_data.contains('\n') {
handle_inline(
&mut current_inline_style,
&mut line_buffer,
@ -137,23 +139,20 @@ pub fn markdown_encoder(delta: &Operations<TextAttributes>) -> String {
markdown_buffer
}
fn handle_inline(
current_inline_style: &mut TextAttributes,
buffer: &mut String,
mut text: String,
attributes: TextAttributes,
) {
let mut marked_for_removal: HashMap<TextAttributeKey, TextAttributeValue> = HashMap::new();
fn handle_inline(current_inline_style: &mut Attributes, buffer: &mut String, mut text: String, attributes: Attributes) {
let mut marked_for_removal: HashMap<AttributeKey, AttributeValue> = HashMap::new();
for key in current_inline_style
.clone()
.keys()
.collect::<Vec<&TextAttributeKey>>()
.collect::<Vec<&AttributeKey>>()
.into_iter()
.rev()
{
if is_block(key) {
continue;
if let Some(attribute) = TextAttributeKey::from_str(key) {
if is_block(&attribute) {
continue;
}
}
if attributes.contains_key(key) {
@ -161,7 +160,7 @@ fn handle_inline(
}
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() {
buffer.push_str(&padding)
}
@ -175,8 +174,10 @@ fn handle_inline(
}
for (key, value) in attributes.iter() {
if is_block(key) {
continue;
if let Some(attribute) = TextAttributeKey::from_str(key) {
if is_block(&attribute) {
continue;
}
}
if current_inline_style.contains_key(key) {
continue;
@ -196,7 +197,7 @@ fn handle_inline(
fn trim_right(buffer: &mut String) -> String {
let text = buffer.clone();
if !text.ends_with(" ") {
if !text.ends_with(' ') {
return String::from("");
}
let result = text.trim_end();
@ -205,10 +206,11 @@ fn trim_right(buffer: &mut String) -> String {
" ".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 {
TextAttributeKey::Bold => buffer.push_str("**"),
TextAttributeKey::Italic => buffer.push_str("_"),
TextAttributeKey::Italic => buffer.push('_'),
TextAttributeKey::Underline => {
if close {
buffer.push_str("</u>")
@ -216,18 +218,12 @@ fn write_attribute(buffer: &mut String, key: &TextAttributeKey, value: &TextAttr
buffer.push_str("<u>")
}
}
TextAttributeKey::StrikeThrough => {
if close {
buffer.push_str("~~")
} else {
buffer.push_str("~~")
}
}
TextAttributeKey::StrikeThrough => buffer.push_str("~~"),
TextAttributeKey::Link => {
if close {
buffer.push_str(format!("]({})", value.0.as_ref().unwrap()).as_str())
} else {
buffer.push_str("[")
buffer.push('[')
}
}
TextAttributeKey::Background => {
@ -244,13 +240,7 @@ fn write_attribute(buffer: &mut String, key: &TextAttributeKey, value: &TextAttr
buffer.push_str("```\n")
}
}
TextAttributeKey::InlineCode => {
if close {
buffer.push_str("`")
} else {
buffer.push_str("`")
}
}
TextAttributeKey::InlineCode => buffer.push('`'),
_ => {}
}
}
@ -259,10 +249,10 @@ fn handle_line(
buffer: &mut String,
markdown_buffer: &mut String,
data: String,
attributes: TextAttributes,
attributes: Attributes,
current_block_style: &mut Option<Attribute>,
current_block_lines: &mut Vec<String>,
current_inline_style: &mut TextAttributes,
current_inline_style: &mut Attributes,
) {
let mut span = String::new();
for c in data.chars() {
@ -270,18 +260,13 @@ fn handle_line(
if !span.is_empty() {
handle_inline(current_inline_style, buffer, span.clone(), attributes.clone());
}
handle_inline(
current_inline_style,
buffer,
String::from(""),
TextAttributes::default(),
);
handle_inline(current_inline_style, buffer, String::from(""), Attributes::default());
let line_block_key = attributes.keys().find(|key| {
if is_block(*key) {
return true;
if let Some(attribute) = TextAttributeKey::from_str(key) {
is_block(&attribute)
} else {
return false;
false
}
});
@ -339,7 +324,7 @@ fn handle_block(
markdown_buffer.push_str(&current_block_lines.join("\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);
markdown_buffer.push_str(&current_block_lines.join("\n"));
write_attribute(markdown_buffer, &block_style.key, &block_style.value, true);
@ -347,7 +332,7 @@ fn handle_block(
}
Some(block_style) => {
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('\n');
}
@ -360,9 +345,9 @@ fn write_block_tag(buffer: &mut String, block: &Attribute, close: bool) {
return;
}
if block.key == TextAttributeKey::BlockQuote {
if block.key == AttributeKey::BlockQuote {
buffer.push_str("> ");
} else if block.key == TextAttributeKey::List {
} else if block.key == AttributeKey::List {
if block.value.0.as_ref().unwrap().eq("bullet") {
buffer.push_str("* ");
} 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 {
buffer.push_str("* ");
}
} else if block.key == TextAttributeKey::Header {
} else if block.key == AttributeKey::Header {
if block.value.0.as_ref().unwrap().eq("1") {
buffer.push_str("# ");
} else if block.value.0.as_ref().unwrap().eq("2") {
buffer.push_str("## ");
} else if block.value.0.as_ref().unwrap().eq("3") {
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,47 +1,48 @@
use std::fmt;
use crate::core::attributes::AttributeValue;
use serde::{
de::{self, MapAccess, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use super::AttributeValue;
use std::fmt;
impl Serialize for AttributeValue {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self.ty {
super::ValueType::IntType => {
//
if let Some(value) = self.int_value() {
serializer.serialize_i64(value)
} else {
serializer.serialize_none()
match &self.ty {
None => serializer.serialize_none(),
Some(ty) => match ty {
super::ValueType::IntType => {
if let Some(value) = self.int_value() {
serializer.serialize_i64(value)
} else {
serializer.serialize_none()
}
}
}
super::ValueType::FloatType => {
if let Some(value) = self.float_value() {
serializer.serialize_f64(value)
} else {
serializer.serialize_none()
super::ValueType::FloatType => {
if let Some(value) = self.float_value() {
serializer.serialize_f64(value)
} else {
serializer.serialize_none()
}
}
}
super::ValueType::StrType => {
if let Some(value) = self.str_value() {
serializer.serialize_str(&value)
} else {
serializer.serialize_none()
super::ValueType::StrType => {
if let Some(value) = self.str_value() {
serializer.serialize_str(&value)
} else {
serializer.serialize_none()
}
}
}
super::ValueType::BoolType => {
if let Some(value) = self.bool_value() {
serializer.serialize_bool(value)
} else {
serializer.serialize_none()
super::ValueType::BoolType => {
if let Some(value) = self.bool_value() {
serializer.serialize_bool(value)
} else {
serializer.serialize_none()
}
}
}
},
}
}
}
@ -125,14 +126,14 @@ impl<'de> Deserialize<'de> for AttributeValue {
where
E: de::Error,
{
Ok(AttributeValue::from_str(s))
Ok(AttributeValue::from_string(s))
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(AttributeValue::empty())
Ok(AttributeValue::none())
}
fn visit_unit<E>(self) -> Result<Self::Value, E>
@ -140,7 +141,7 @@ impl<'de> Deserialize<'de> for AttributeValue {
E: de::Error,
{
// 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>

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::Operation;
@ -16,13 +16,13 @@ use crate::core::Operation;
/// .build();
/// assert_eq!(delta.content().unwrap(), "AppFlowy");
/// ```
pub struct OperationBuilder<T: Attributes> {
pub struct OperationBuilder<T: OperationAttributes> {
delta: Operations<T>,
}
impl<T> std::default::Default for OperationBuilder<T>
where
T: Attributes,
T: OperationAttributes,
{
fn default() -> Self {
Self {
@ -33,7 +33,7 @@ where
impl<T> OperationBuilder<T>
where
T: Attributes,
T: OperationAttributes,
{
pub fn new() -> Self {
OperationBuilder::default()
@ -52,9 +52,9 @@ where
/// # 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();
///
/// 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::text_delta::{TextAttribute, TextDeltaBuilder};
/// use lib_ot::text_delta::{BuildInTextAttribute, TextDeltaBuilder};
/// let delta = DeltaBuilder::new()
/// .retain(3)
/// .trim()
@ -119,7 +119,7 @@ where
/// assert_eq!(delta.ops.len(), 0);
///
/// let delta = TextDeltaBuilder::new()
/// .retain_with_attributes(3, TextAttribute::Bold(true).into())
/// .retain_with_attributes(3, BuildInTextAttribute::Bold(true).into())
/// .trim()
/// .build();
/// assert_eq!(delta.ops.len(), 1);

View file

@ -1,5 +1,5 @@
#![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::interval::Interval;
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.
#[derive(Debug)]
pub struct OperationsCursor<'a, T: Attributes> {
pub struct OperationsCursor<'a, T: OperationAttributes> {
pub(crate) delta: &'a Operations<T>,
pub(crate) origin_iv: Interval,
pub(crate) consume_iv: Interval,
@ -19,7 +19,7 @@ pub struct OperationsCursor<'a, T: Attributes> {
impl<'a, T> OperationsCursor<'a, T>
where
T: Attributes,
T: OperationAttributes,
{
/// # Arguments
///
@ -184,7 +184,7 @@ where
fn find_next<'a, T>(cursor: &mut OperationsCursor<'a, T>) -> Option<&'a Operation<T>>
where
T: Attributes,
T: OperationAttributes,
{
match cursor.iter.next() {
None => None,
@ -197,7 +197,7 @@ where
type SeekResult = Result<(), OTError>;
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
@ -205,7 +205,7 @@ pub trait Metric {
pub struct 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 mut seek_cursor = OperationsCursor::new(cursor.delta, cursor.origin_iv);
@ -224,7 +224,7 @@ impl Metric for OpMetric {
pub struct 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 {
let _ = check_bound(cursor.consume_count, offset)?;
let _ = cursor.next_with_len(Some(offset));

View file

@ -1,8 +1,9 @@
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::interval::Interval;
use crate::text_delta::TextAttributes;
use crate::core::Attributes;
use std::ops::{Deref, DerefMut};
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")]
/// );
/// ```
pub struct OperationIterator<'a, T: Attributes> {
pub struct OperationIterator<'a, T: OperationAttributes> {
cursor: OperationsCursor<'a, T>,
}
impl<'a, T> OperationIterator<'a, T>
where
T: Attributes,
T: OperationAttributes,
{
pub fn new(delta: &'a Operations<T>) -> Self {
let interval = Interval::new(0, MAX_IV_LEN);
@ -124,7 +125,7 @@ where
impl<'a, T> Iterator for OperationIterator<'a, T>
where
T: Attributes,
T: OperationAttributes,
{
type Item = Operation<T>;
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 (prev, next) = (iter.next_op_with_len(index), iter.next_op());
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()
}
pub struct AttributesIter<'a, T: Attributes> {
pub struct AttributesIter<'a, T: OperationAttributes> {
delta_iter: OperationIterator<'a, T>,
}
impl<'a, T> AttributesIter<'a, T>
where
T: Attributes,
T: OperationAttributes,
{
pub fn new(delta: &'a Operations<T>) -> Self {
let interval = Interval::new(0, usize::MAX);
@ -176,7 +177,7 @@ where
impl<'a, T> Deref for AttributesIter<'a, T>
where
T: Attributes,
T: OperationAttributes,
{
type Target = OperationIterator<'a, T>;
@ -187,7 +188,7 @@ where
impl<'a, T> DerefMut for AttributesIter<'a, T>
where
T: Attributes,
T: OperationAttributes,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.delta_iter
@ -196,7 +197,7 @@ where
impl<'a, T> Iterator for AttributesIter<'a, T>
where
T: Attributes,
T: OperationAttributes,
{
type Item = (usize, T);
fn next(&mut self) -> Option<Self::Item> {
@ -234,7 +235,7 @@ pub enum 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();
if s == NEW_LINE {

View file

@ -1,17 +1,16 @@
use crate::core::delta::operation::{Attributes, EmptyAttributes, Operation};
use crate::text_delta::TextAttributes;
use crate::core::delta::operation::{EmptyAttributes, Operation, OperationAttributes};
pub type RichTextOpBuilder = OperationsBuilder<TextAttributes>;
// pub type RichTextOpBuilder = OperationsBuilder<TextAttributes>;
pub type PlainTextOpBuilder = OperationsBuilder<EmptyAttributes>;
#[derive(Default)]
pub struct OperationsBuilder<T: Attributes> {
pub struct OperationsBuilder<T: OperationAttributes> {
operations: Vec<Operation<T>>,
}
impl<T> OperationsBuilder<T>
where
T: Attributes,
T: OperationAttributes,
{
pub fn new() -> OperationsBuilder<T> {
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
///[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 {
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.
///
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Operation<T: Attributes> {
pub enum Operation<T: OperationAttributes> {
Delete(usize),
Retain(Retain<T>),
Insert(Insert<T>),
@ -104,7 +104,7 @@ pub enum Operation<T: Attributes> {
impl<T> Operation<T>
where
T: Attributes,
T: OperationAttributes,
{
pub fn delete(n: usize) -> Self {
Self::Delete(n)
@ -281,7 +281,7 @@ where
impl<T> fmt::Display for Operation<T>
where
T: Attributes,
T: OperationAttributes,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("{")?;
@ -302,14 +302,14 @@ where
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Retain<T: Attributes> {
pub struct Retain<T: OperationAttributes> {
pub n: usize,
pub attributes: T,
}
impl<T> fmt::Display for Retain<T>
where
T: Attributes,
T: OperationAttributes,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.attributes.is_empty() {
@ -322,7 +322,7 @@ where
impl<T> Retain<T>
where
T: Attributes,
T: OperationAttributes,
{
pub fn merge_or_new(&mut self, n: usize, attributes: T) -> Option<Operation<T>> {
// tracing::trace!(
@ -346,7 +346,7 @@ where
impl<T> std::convert::From<usize> for Retain<T>
where
T: Attributes,
T: OperationAttributes,
{
fn from(n: usize) -> Self {
Retain {
@ -358,7 +358,7 @@ where
impl<T> Deref for Retain<T>
where
T: Attributes,
T: OperationAttributes,
{
type Target = usize;
@ -369,7 +369,7 @@ where
impl<T> DerefMut for Retain<T>
where
T: Attributes,
T: OperationAttributes,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.n
@ -377,14 +377,14 @@ where
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Insert<T: Attributes> {
pub struct Insert<T: OperationAttributes> {
pub s: OTString,
pub attributes: T,
}
impl<T> fmt::Display for Insert<T>
where
T: Attributes,
T: OperationAttributes,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut s = self.s.clone();
@ -405,7 +405,7 @@ where
impl<T> Insert<T>
where
T: Attributes,
T: OperationAttributes,
{
pub fn utf16_size(&self) -> usize {
self.s.utf16_len()
@ -427,7 +427,7 @@ where
impl<T> std::convert::From<String> for Insert<T>
where
T: Attributes,
T: OperationAttributes,
{
fn from(s: String) -> Self {
Insert {
@ -439,7 +439,7 @@ where
impl<T> std::convert::From<&str> for Insert<T>
where
T: Attributes,
T: OperationAttributes,
{
fn from(s: &str) -> Self {
Insert::from(s.to_owned())
@ -448,7 +448,7 @@ where
impl<T> std::convert::From<OTString> for Insert<T>
where
T: Attributes,
T: OperationAttributes,
{
fn from(s: OTString) -> Self {
Insert {
@ -466,7 +466,7 @@ impl fmt::Display for EmptyAttributes {
}
}
impl Attributes for EmptyAttributes {}
impl OperationAttributes for EmptyAttributes {}
impl OperationTransform for EmptyAttributes {
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 serde::{
de,
@ -10,7 +10,7 @@ use std::{fmt, marker::PhantomData};
impl<T> Serialize for Operation<T>
where
T: Attributes + Serialize,
T: OperationAttributes + Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@ -30,7 +30,7 @@ where
impl<'de, T> Deserialize<'de> for Operation<T>
where
T: Attributes + Deserialize<'de>,
T: OperationAttributes + Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Operation<T>, D::Error>
where
@ -40,7 +40,7 @@ where
impl<'de, T> Visitor<'de> for OperationVisitor<T>
where
T: Attributes + Deserialize<'de>,
T: OperationAttributes + Deserialize<'de>,
{
type Value = Operation<T>;
@ -105,7 +105,7 @@ where
impl<T> Serialize for Retain<T>
where
T: Attributes + Serialize,
T: OperationAttributes + Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@ -123,7 +123,7 @@ where
impl<'de, T> Deserialize<'de> for Retain<T>
where
T: Attributes + Deserialize<'de>,
T: OperationAttributes + Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
where
@ -133,7 +133,7 @@ where
impl<'de, T> Visitor<'de> for RetainVisitor<T>
where
T: Attributes + Deserialize<'de>,
T: OperationAttributes + Deserialize<'de>,
{
type Value = Retain<T>;
@ -208,7 +208,7 @@ where
impl<T> Serialize for Insert<T>
where
T: Attributes + Serialize,
T: OperationAttributes + Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@ -226,7 +226,7 @@ where
impl<'de, T> Deserialize<'de> for Insert<T>
where
T: Attributes + Deserialize<'de>,
T: OperationAttributes + Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
where
@ -236,7 +236,7 @@ where
impl<'de, T> Visitor<'de> for InsertVisitor<T>
where
T: Attributes + Deserialize<'de>,
T: OperationAttributes + Deserialize<'de>,
{
type Value = Insert<T>;

View file

@ -1,6 +1,6 @@
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::interval::Interval;
use crate::core::ot_str::OTString;
@ -28,7 +28,7 @@ pub type DeltaBuilder = OperationBuilder<EmptyAttributes>;
/// a JSON string.
///
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Operations<T: Attributes> {
pub struct Operations<T: OperationAttributes> {
pub ops: Vec<Operation<T>>,
/// '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>
where
T: Attributes,
T: OperationAttributes,
{
fn default() -> Self {
Self {
@ -55,7 +55,7 @@ where
impl<T> fmt::Display for Operations<T>
where
T: Attributes,
T: OperationAttributes,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// 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>
where
T: Attributes,
T: OperationAttributes,
{
fn from_iter<I: IntoIterator<Item = Operation<T>>>(ops: I) -> Self {
let mut operations = Operations::default();
@ -83,7 +83,7 @@ where
impl<T> Operations<T>
where
T: Attributes,
T: OperationAttributes,
{
pub fn new() -> Self {
Self::default()
@ -294,7 +294,7 @@ where
impl<T> OperationTransform for Operations<T>
where
T: Attributes,
T: OperationAttributes,
{
fn compose(&self, other: &Self) -> Result<Self, OTError>
where
@ -509,7 +509,7 @@ where
/// Removes trailing retain operation with empty attributes, if present.
pub fn trim<T>(delta: &mut Operations<T>)
where
T: Attributes,
T: OperationAttributes,
{
if let Some(last) = delta.ops.last() {
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>,
other: &Operations<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>>,
right: &Option<Operation<T>>,
) -> Result<T, OTError> {
@ -565,7 +565,7 @@ fn transform_op_attribute<T: Attributes>(
impl<T> Operations<T>
where
T: Attributes + DeserializeOwned,
T: OperationAttributes + DeserializeOwned,
{
/// # Examples
///
@ -576,7 +576,7 @@ where
/// {"retain":7,"attributes":{"bold":null}}
/// ]"#;
/// 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> {
let delta = serde_json::from_str(json).map_err(|e| {
@ -597,7 +597,7 @@ where
impl<T> Operations<T>
where
T: Attributes + serde::Serialize,
T: OperationAttributes + serde::Serialize,
{
/// Serialize the [Delta] into a String in JSON format
pub fn json_str(&self) -> String {
@ -618,7 +618,7 @@ where
impl<T> FromStr for Operations<T>
where
T: Attributes,
T: OperationAttributes,
{
type Err = ();
@ -631,7 +631,7 @@ where
impl<T> std::convert::TryFrom<Vec<u8>> for Operations<T>
where
T: Attributes + DeserializeOwned,
T: OperationAttributes + DeserializeOwned,
{
type Error = OTError;
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
@ -641,7 +641,7 @@ where
impl<T> std::convert::TryFrom<Bytes> for Operations<T>
where
T: Attributes + DeserializeOwned,
T: OperationAttributes + DeserializeOwned,
{
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 serde::{
de::{SeqAccess, Visitor},
@ -9,7 +9,7 @@ use std::{fmt, marker::PhantomData};
impl<T> Serialize for Operations<T>
where
T: Attributes + Serialize,
T: OperationAttributes + Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@ -25,7 +25,7 @@ where
impl<'de, T> Deserialize<'de> for Operations<T>
where
T: Attributes + Deserialize<'de>,
T: OperationAttributes + Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Operations<T>, D::Error>
where
@ -35,7 +35,7 @@ where
impl<'de, T> Visitor<'de> for OperationSeqVisitor<T>
where
T: Attributes + Deserialize<'de>,
T: OperationAttributes + Deserialize<'de>,
{
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)]
mod attributes;
mod attributes_serde;
mod node;
mod node_serde;
mod node_tree;
@ -9,7 +8,6 @@ mod operation_serde;
mod path;
mod transaction;
pub use attributes::*;
pub use node::*;
pub use node_tree::*;
pub use operation::*;

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,6 @@
use crate::core::attributes::Attributes;
use crate::core::document::path::Path;
use crate::core::{NodeAttributes, NodeData, NodeOperation, NodeTree};
use crate::core::{NodeData, NodeOperation, NodeTree};
use indextree::NodeId;
use super::{NodeBodyChangeset, NodeOperationList};
@ -100,10 +101,10 @@ impl<'a> TransactionBuilder<'a> {
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) {
Some(node) => {
let mut old_attributes = NodeAttributes::new();
let mut old_attributes = Attributes::new();
for key in attributes.keys() {
let old_attrs = &node.attributes;
if let Some(value) = old_attrs.get(key.as_str()) {

View file

@ -1,8 +1,10 @@
pub mod attributes;
mod delta;
mod document;
mod interval;
mod ot_str;
pub use attributes::*;
pub use delta::operation::*;
pub use delta::*;
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)]
use crate::core::{Attributes, Operation, OperationTransform};
use crate::{block_attribute, errors::OTError, ignore_attribute, inline_attribute, list_attribute};
use crate::core::{AttributeEntry, AttributeKey, Attributes};
use crate::text_delta::TextOperation;
use crate::{inline_attribute_entry, inline_list_attribute_entry};
use lazy_static::lazy_static;
use std::{
collections::{HashMap, HashSet},
fmt,
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))
}
}
use std::str::FromStr;
use std::{collections::HashSet, iter::FromIterator};
use strum_macros::{AsRefStr, Display, EnumString};
#[inline(always)]
pub fn plain_attributes() -> TextAttributes {
TextAttributes::default()
pub fn empty_attributes() -> Attributes {
Attributes::default()
}
impl TextAttributes {
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 {
pub fn attributes_except_header(op: &TextOperation) -> Attributes {
let mut attributes = op.get_attributes();
attributes.remove(TextAttributeKey::Header);
attributes.remove_key(BuildInTextAttributeKey::Header);
attributes
}
#[derive(Debug, Clone)]
pub struct TextAttribute {
pub key: TextAttributeKey,
pub value: TextAttributeValue,
pub scope: AttributeScope,
}
pub struct BuildInTextAttribute();
impl TextAttribute {
// inline
inline_attribute!(Bold, bool);
inline_attribute!(Italic, bool);
inline_attribute!(Underline, bool);
inline_attribute!(StrikeThrough, bool);
inline_attribute!(Link, &str);
inline_attribute!(Color, String);
inline_attribute!(Font, usize);
inline_attribute!(Size, usize);
inline_attribute!(Background, String);
inline_attribute!(InlineCode, bool);
impl BuildInTextAttribute {
inline_attribute_entry!(Bold, bool);
inline_attribute_entry!(Italic, bool);
inline_attribute_entry!(Underline, bool);
inline_attribute_entry!(StrikeThrough, bool);
inline_attribute_entry!(Link, &str);
inline_attribute_entry!(Color, String);
inline_attribute_entry!(Font, usize);
inline_attribute_entry!(Size, usize);
inline_attribute_entry!(Background, String);
inline_attribute_entry!(InlineCode, bool);
// block
block_attribute!(Header, usize);
block_attribute!(Indent, usize);
block_attribute!(Align, String);
block_attribute!(List, &str);
block_attribute!(CodeBlock, bool);
block_attribute!(BlockQuote, bool);
inline_attribute_entry!(Header, usize);
inline_attribute_entry!(Indent, usize);
inline_attribute_entry!(Align, String);
inline_attribute_entry!(List, &str);
inline_attribute_entry!(CodeBlock, bool);
inline_attribute_entry!(BlockQuote, bool);
// ignore
ignore_attribute!(Width, usize);
ignore_attribute!(Height, usize);
inline_attribute_entry!(Width, usize);
inline_attribute_entry!(Height, usize);
// List extension
list_attribute!(Bullet, "bullet");
list_attribute!(Ordered, "ordered");
list_attribute!(Checked, "checked");
list_attribute!(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()
}
}
}
inline_list_attribute_entry!(Bullet, "bullet");
inline_list_attribute_entry!(Ordered, "ordered");
inline_list_attribute_entry!(Checked, "checked");
inline_list_attribute_entry!(UnChecked, "unchecked");
}
impl fmt::Display for TextAttribute {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let s = format!("{:?}:{:?} {:?}", self.key, self.value.0, self.scope);
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 {
#[derive(Clone, Debug, Display, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize, AsRefStr, EnumString)]
#[strum(serialize_all = "snake_case")]
pub enum BuildInTextAttributeKey {
#[serde(rename = "bold")]
Bold,
#[serde(rename = "italic")]
@ -286,91 +91,45 @@ pub enum TextAttributeKey {
Header,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TextAttributeValue(pub Option<String>);
impl std::convert::From<&usize> for TextAttributeValue {
fn from(val: &usize) -> Self {
TextAttributeValue::from(*val)
pub fn is_block(k: &AttributeKey) -> bool {
if let Ok(key) = BuildInTextAttributeKey::from_str(k) {
BLOCK_KEYS.contains(&key)
} else {
false
}
}
impl std::convert::From<usize> for TextAttributeValue {
fn from(val: usize) -> Self {
if val > 0_usize {
TextAttributeValue(Some(format!("{}", val)))
} else {
TextAttributeValue(None)
}
pub fn is_inline(k: &AttributeKey) -> bool {
if let Ok(key) = BuildInTextAttributeKey::from_str(k) {
INLINE_KEYS.contains(&key)
} else {
false
}
}
impl std::convert::From<&str> for TextAttributeValue {
fn from(val: &str) -> Self {
val.to_owned().into()
}
}
impl std::convert::From<String> for TextAttributeValue {
fn from(val: String) -> Self {
if val.is_empty() {
TextAttributeValue(None)
} else {
TextAttributeValue(Some(val))
}
}
}
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! {
static ref BLOCK_KEYS: HashSet<TextAttributeKey> = HashSet::from_iter(vec![
TextAttributeKey::Header,
TextAttributeKey::Indent,
TextAttributeKey::Align,
TextAttributeKey::CodeBlock,
TextAttributeKey::List,
TextAttributeKey::BlockQuote,
static ref BLOCK_KEYS: HashSet<BuildInTextAttributeKey> = HashSet::from_iter(vec![
BuildInTextAttributeKey::Header,
BuildInTextAttributeKey::Indent,
BuildInTextAttributeKey::Align,
BuildInTextAttributeKey::CodeBlock,
BuildInTextAttributeKey::List,
BuildInTextAttributeKey::BlockQuote,
]);
static ref INLINE_KEYS: HashSet<TextAttributeKey> = HashSet::from_iter(vec![
TextAttributeKey::Bold,
TextAttributeKey::Italic,
TextAttributeKey::Underline,
TextAttributeKey::StrikeThrough,
TextAttributeKey::Link,
TextAttributeKey::Color,
TextAttributeKey::Font,
TextAttributeKey::Size,
TextAttributeKey::Background,
TextAttributeKey::InlineCode,
static ref INLINE_KEYS: HashSet<BuildInTextAttributeKey> = HashSet::from_iter(vec![
BuildInTextAttributeKey::Bold,
BuildInTextAttributeKey::Italic,
BuildInTextAttributeKey::Underline,
BuildInTextAttributeKey::StrikeThrough,
BuildInTextAttributeKey::Link,
BuildInTextAttributeKey::Color,
BuildInTextAttributeKey::Font,
BuildInTextAttributeKey::Size,
BuildInTextAttributeKey::Background,
BuildInTextAttributeKey::InlineCode,
]);
static ref INGORE_KEYS: HashSet<TextAttributeKey> =
HashSet::from_iter(vec![TextAttributeKey::Width, TextAttributeKey::Height,]);
static ref INGORE_KEYS: HashSet<BuildInTextAttributeKey> =
HashSet::from_iter(vec![BuildInTextAttributeKey::Width, BuildInTextAttributeKey::Height,]);
}
#[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_rules! inline_attribute {
macro_rules! inline_attribute_entry {
(
$key: ident,
$value: ty
) => {
pub fn $key(value: $value) -> Self {
Self {
key: TextAttributeKey::$key,
pub fn $key(value: $value) -> crate::core::AttributeEntry {
AttributeEntry {
key: BuildInTextAttributeKey::$key.as_ref().to_string(),
value: value.into(),
scope: AttributeScope::Inline,
}
}
};
}
#[macro_export]
macro_rules! block_attribute {
(
$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 {
macro_rules! inline_list_attribute_entry {
(
$key: ident,
$value: expr
) => {
pub fn $key(b: bool) -> Self {
pub fn $key(b: bool) -> crate::core::AttributeEntry {
let value = match b {
true => $value,
false => "",
};
TextAttribute::List(value)
}
};
}
#[macro_export]
macro_rules! ignore_attribute {
(
$key: ident,
$value: ident
) => {
pub fn $key(value: $value) -> Self {
Self {
key: TextAttributeKey::$key,
AttributeEntry {
key: BuildInTextAttributeKey::List.as_ref().to_string(),
value: value.into(),
scope: AttributeScope::Ignore,
}
}
};

View file

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

View file

@ -1,5 +1,6 @@
use lib_ot::core::AttributeBuilder;
use lib_ot::{
core::{NodeAttributeBuilder, NodeBodyChangeset, NodeData, NodeDataBuilder, NodeOperation, Path},
core::{NodeBodyChangeset, NodeData, NodeDataBuilder, NodeOperation, Path},
text_delta::TextDeltaBuilder,
};
@ -32,15 +33,15 @@ fn operation_insert_node_with_children_serde_test() {
fn operation_update_node_attributes_serde_test() {
let operation = NodeOperation::UpdateAttributes {
path: Path(vec![0, 1]),
attributes: NodeAttributeBuilder::new().insert("bold", true).build(),
old_attributes: NodeAttributeBuilder::new().insert("bold", false).build(),
attributes: AttributeBuilder::new().insert("bold", true).build(),
old_attributes: AttributeBuilder::new().insert("bold", false).build(),
};
let result = serde_json::to_string(&operation).unwrap();
assert_eq!(
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::{
core::{NodeAttributes, NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
core::attributes::Attributes,
core::{NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
text_delta::TextDelta,
};
pub enum NodeScript {
InsertNode { path: Path, node: NodeData },
UpdateAttributes { path: Path, attributes: NodeAttributes },
UpdateAttributes { path: Path, attributes: Attributes },
UpdateBody { path: Path, changeset: NodeBodyChangeset },
DeleteNode { path: Path },
AssertNumberOfNodesAtPath { path: Option<Path>, len: usize },
@ -65,7 +66,7 @@ impl NodeTest {
None => assert!(node_id.is_none()),
Some(node_id) => {
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![
InsertNode {
path: path.clone(),
node: inserted_node.clone(),
node: inserted_node,
},
DeleteNode { path: path.clone() },
AssertNode { path, expected: None },
@ -198,7 +198,7 @@ fn node_update_body_test() {
let scripts = vec![
InsertNode {
path: path.clone(),
node: node.clone(),
node,
},
UpdateBody {
path: path.clone(),