Skip to content

Commit

Permalink
fix: use retain instead of insert/delete for same attr (AppFlowy-IO#937)
Browse files Browse the repository at this point in the history
* fix: use retain instead of insert/delete for same attr

* test: add delta diff test
  • Loading branch information
LucasXu0 authored Oct 17, 2024
1 parent 5cb871d commit 4509123
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
17 changes: 17 additions & 0 deletions lib/src/core/document/attributes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ Attributes invertAttributes(Attributes? from, Attributes? to) {
return attributes;
}

Attributes? diffAttributes(
Map<String, dynamic>? from,
Map<String, dynamic>? to,
) {
from ??= const {};
to ??= const {};
final attributes = Attributes.from({});

for (final key in (from.keys.toList()..addAll(to.keys))) {
if (from[key] != to[key]) {
attributes[key] = to.containsKey(key) ? to[key] : null;
}
}

return attributes;
}

int hashAttributes(Attributes base) => Object.hashAllUnordered(
base.entries.map((e) => Object.hash(e.key, e.value)),
);
11 changes: 9 additions & 2 deletions lib/src/core/document/text_delta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const int _maxInt = 9007199254740991;

sealed class TextOperation {
Attributes? get attributes;

// available for TextInsert, for TextDelete and TextRetain, it's null
Object? get data => null;

int get length;

bool get isEmpty => length == 0;
Expand All @@ -81,6 +85,9 @@ class TextInsert extends TextOperation {
@override
int get length => text.length;

@override
Object? get data => text;

@override
Attributes? get attributes => _attributes != null ? {..._attributes} : null;

Expand Down Expand Up @@ -402,10 +409,10 @@ class Delta extends Iterable<TextOperation> {
);
final thisOp = thisIter.next(opLength);
final otherOp = otherIter.next(opLength);
if (isAttributesEqual(thisOp.attributes, otherOp.attributes)) {
if (thisOp.data == otherOp.data) {
retDelta.retain(
opLength,
attributes: invertAttributes(
attributes: diffAttributes(
thisOp.attributes,
otherOp.attributes,
),
Expand Down
47 changes: 47 additions & 0 deletions test/core/document/text_delta_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,53 @@ void main() {
final diff = delta.diff(restored);
expect(delta.compose(diff), restored);
});

test('insert', () {
final a = Delta()..insert('Hello');
final b = Delta()..insert('Hello!');
final expected = Delta()
..retain(5)
..insert('!');
expect(a.diff(b), expected);
});

test('delete', () {
final a = Delta()..insert('Hello!');
final b = Delta()..insert('Hello');
final expected = Delta()
..retain(5)
..delete(1);
expect(a.diff(b), expected);
});

test('retain', () {
final a = Delta()..insert('A');
final b = Delta()..insert('A');
final expected = Delta();
expect(a.diff(b), expected);
});

test('retain with attributes - 1', () {
final a = Delta()..insert('Hello');
final b = Delta()..insert('Hello', attributes: {'bold': true});
final expected = Delta()
..retain(
5,
attributes: {'bold': true},
);
expect(a.diff(b), expected);
});

test('retain with attributes - 2', () {
final a = Delta()..insert('Hello', attributes: {'bold': true});
final b = Delta()..insert('Hello', attributes: {'italic': true});
final expected = Delta()
..retain(
5,
attributes: {'bold': null, 'italic': true},
);
expect(a.diff(b), expected);
});
});
});
}

0 comments on commit 4509123

Please sign in to comment.