test: invert

This commit is contained in:
Vincent Chan 2022-07-13 18:46:00 +08:00
parent 3cbac6f3f9
commit def03273b8
5 changed files with 206 additions and 176 deletions

View File

@ -1,6 +1,7 @@
import 'package:flowy_editor/flowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flowy_editor/document/attributes.dart';
class ImageNodeBuilder extends NodeWidgetBuilder {
ImageNodeBuilder.create({

View File

@ -21,3 +21,22 @@ Attributes invertAttributes(Attributes? attr, Attributes? base) {
return memo;
});
}
Attributes? composeAttributes(Attributes? a, Attributes? b) {
a ??= {};
b ??= {};
final Attributes attributes = {};
attributes.addAll(b);
for (final entry in a.entries) {
if (!b.containsKey(entry.key)) {
attributes[entry.key] = entry.value;
}
}
if (attributes.isEmpty) {
return null;
}
return attributes;
}

View File

@ -336,7 +336,8 @@ class Delta {
final length = min(thisIter.peekLength(), otherIter.peekLength());
final thisOp = thisIter.next(length);
final otherOp = otherIter.next(length);
final attributes = _composeMap(thisOp.attributes, otherOp.attributes);
final attributes =
composeAttributes(thisOp.attributes, otherOp.attributes);
if (otherOp is TextRetain && otherOp.length > 0) {
TextOperation? newOp;
if (thisOp is TextRetain) {
@ -423,22 +424,3 @@ class Delta {
return inverted.chop();
}
}
Attributes? _composeMap(Attributes? a, Attributes? b) {
a ??= {};
b ??= {};
final Attributes attributes = {};
attributes.addAll(b);
for (final entry in a.entries) {
if (!b.containsKey(entry.key)) {
attributes[entry.key] = entry.value;
}
}
if (attributes.isEmpty) {
return null;
}
return attributes;
}

View File

@ -1,5 +1,6 @@
import 'package:flowy_editor/document/node.dart';
import 'package:flowy_editor/operation/operation.dart';
import 'package:flowy_editor/document/attributes.dart';
import 'package:flutter/material.dart';
import './document/state_tree.dart';

View File

@ -2,7 +2,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flowy_editor/document/text_delta.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('compose', () {
test('test delta', () {
final delta = Delta(<TextOperation>[
TextInsert('Gandalf', {
@ -170,4 +170,31 @@ void main() {
.insert('F');
expect(a.compose(b), expected);
});
});
group('invert', () {
test('insert', () {
final delta = Delta().retain(2).insert('A');
final base = Delta().insert('12346');
final expected = Delta().retain(2).delete(1);
final inverted = delta.invert(base);
expect(expected, inverted);
expect(base.compose(delta).compose(inverted), base);
});
test('delete', () {
final delta = Delta().retain(2).delete(3);
final base = Delta().insert('123456');
final expected = Delta().retain(2).insert('345');
final inverted = delta.invert(base);
expect(expected, inverted);
expect(base.compose(delta).compose(inverted), base);
});
// test('retain', () {
// final delta = Delta().retain(2).retain(3, {'bold': true});
// final base = Delta().insert('123456');
// final expected = Delta().retain(2).retain(3, {'bold': null});
// final inverted = delta.invert(base);
// expect(expected, inverted);
// expect(base.compose(delta).compose(inverted), base);
// });
});
}