mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
fix: inverted delta not equal to the old delta
This commit is contained in:
parent
80a94880fc
commit
b322f40ff7
@ -161,6 +161,7 @@ pub fn compose_attributes(left: &Option<Operation>, right: &Option<Operation>) -
|
|||||||
Some(_) => attr_l.unwrap().extend(attr_r.clone()),
|
Some(_) => attr_l.unwrap().extend(attr_r.clone()),
|
||||||
},
|
},
|
||||||
(Some(Attributes::Custom(_)), _) => attr_l.unwrap().extend(attr_r),
|
(Some(Attributes::Custom(_)), _) => attr_l.unwrap().extend(attr_r),
|
||||||
|
(Some(Attributes::Follow), Some(Attributes::Follow)) => Attributes::Follow,
|
||||||
_ => Attributes::Empty,
|
_ => Attributes::Empty,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -186,7 +187,7 @@ pub fn transform_attributes(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return match attr_r.as_ref().unwrap() {
|
return match attr_r.as_ref().unwrap() {
|
||||||
Attributes::Follow => Attributes::Empty,
|
Attributes::Follow => Attributes::Follow,
|
||||||
Attributes::Custom(_) => attr_r.unwrap(),
|
Attributes::Custom(_) => attr_r.unwrap(),
|
||||||
Attributes::Empty => Attributes::Empty,
|
Attributes::Empty => Attributes::Empty,
|
||||||
};
|
};
|
||||||
|
@ -161,10 +161,7 @@ impl Delta {
|
|||||||
next_op1 = ops1.next();
|
next_op1 = ops1.next();
|
||||||
},
|
},
|
||||||
(_, Some(Operation::Insert(o_insert))) => {
|
(_, Some(Operation::Insert(o_insert))) => {
|
||||||
new_delta.insert(
|
new_delta.insert(&o_insert.s, o_insert.attributes.clone());
|
||||||
&o_insert.s,
|
|
||||||
attributes_from(&next_op2).unwrap_or(Attributes::Empty),
|
|
||||||
);
|
|
||||||
next_op2 = ops2.next();
|
next_op2 = ops2.next();
|
||||||
},
|
},
|
||||||
(None, _) | (_, None) => {
|
(None, _) | (_, None) => {
|
||||||
@ -502,7 +499,7 @@ impl Delta {
|
|||||||
},
|
},
|
||||||
Operation::Retain(_) => {
|
Operation::Retain(_) => {
|
||||||
if op.is_plain() {
|
if op.is_plain() {
|
||||||
inverted.retain(op_len as u64, Attributes::Empty);
|
inverted.retain(op_len as u64, op.get_attributes());
|
||||||
} else {
|
} else {
|
||||||
a(&mut inverted, op, index, op_len as usize);
|
a(&mut inverted, op, index, op_len as usize);
|
||||||
}
|
}
|
||||||
|
@ -283,7 +283,8 @@ fn delta_compose_attr_delta_with_attr_delta_test2() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn delta_compose_attr_delta_with_no_attr_delta_test() {
|
fn delta_compose_attr_delta_with_no_attr_delta_test() {
|
||||||
let expected = r#"[{"insert":"123456","attributes":{"bold":"true"}},{"insert":"7"}]"#;
|
let expected = r#"[{"insert":"1234567","attributes":{"bold":"true"}}]"#;
|
||||||
|
|
||||||
let ops = vec![
|
let ops = vec![
|
||||||
InsertBold(0, "123456", Interval::new(0, 6)),
|
InsertBold(0, "123456", Interval::new(0, 6)),
|
||||||
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
|
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
|
||||||
|
@ -12,6 +12,8 @@ pub enum TestOp {
|
|||||||
Delete(usize, Interval),
|
Delete(usize, Interval),
|
||||||
Italic(usize, Interval, bool),
|
Italic(usize, Interval, bool),
|
||||||
Transform(usize, usize),
|
Transform(usize, usize),
|
||||||
|
// invert the delta_a base on the delta_b
|
||||||
|
Invert(usize, usize),
|
||||||
AssertStr(usize, &'static str),
|
AssertStr(usize, &'static str),
|
||||||
AssertOpsJson(usize, &'static str),
|
AssertOpsJson(usize, &'static str),
|
||||||
}
|
}
|
||||||
@ -70,6 +72,19 @@ impl OpTester {
|
|||||||
self.deltas[*delta_a_i] = new_delta_a;
|
self.deltas[*delta_a_i] = new_delta_a;
|
||||||
self.deltas[*delta_b_i] = new_delta_b;
|
self.deltas[*delta_b_i] = new_delta_b;
|
||||||
},
|
},
|
||||||
|
TestOp::Invert(delta_a_i, delta_b_i) => {
|
||||||
|
let delta_a = &self.deltas[*delta_a_i];
|
||||||
|
let delta_b = &self.deltas[*delta_b_i];
|
||||||
|
|
||||||
|
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
|
||||||
|
let undo = b_prime.invert_delta(&delta_a);
|
||||||
|
let new_delta = delta_a.compose(&b_prime).unwrap();
|
||||||
|
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(delta_a, &new_delta_after_undo);
|
||||||
|
|
||||||
|
self.deltas[*delta_a_i] = new_delta_after_undo;
|
||||||
|
},
|
||||||
TestOp::AssertStr(delta_i, expected) => {
|
TestOp::AssertStr(delta_i, expected) => {
|
||||||
let s = self.deltas[*delta_i].apply("").unwrap();
|
let s = self.deltas[*delta_i].apply("").unwrap();
|
||||||
assert_eq!(&s, expected);
|
assert_eq!(&s, expected);
|
||||||
|
@ -18,6 +18,17 @@ fn delta_invert_delta_test() {
|
|||||||
assert_eq!(delta_after_undo, delta);
|
assert_eq!(delta_after_undo, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn delta_invert_delta_test2() {
|
||||||
|
let ops = vec![
|
||||||
|
Insert(0, "1234", 0),
|
||||||
|
Insert(1, "4567", 0),
|
||||||
|
Invert(0, 1),
|
||||||
|
AssertOpsJson(0, r#"[{"insert":"1234"}]"#),
|
||||||
|
];
|
||||||
|
OpTester::new().run_script(ops);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn delta_get_ops_in_interval_1() {
|
fn delta_get_ops_in_interval_1() {
|
||||||
let mut delta = Delta::default();
|
let mut delta = Delta::default();
|
||||||
|
Loading…
Reference in New Issue
Block a user