mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
test: invert
This commit is contained in:
parent
3cbac6f3f9
commit
def03273b8
@ -1,6 +1,7 @@
|
|||||||
import 'package:flowy_editor/flowy_editor.dart';
|
import 'package:flowy_editor/flowy_editor.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:flowy_editor/document/attributes.dart';
|
||||||
|
|
||||||
class ImageNodeBuilder extends NodeWidgetBuilder {
|
class ImageNodeBuilder extends NodeWidgetBuilder {
|
||||||
ImageNodeBuilder.create({
|
ImageNodeBuilder.create({
|
||||||
|
@ -21,3 +21,22 @@ Attributes invertAttributes(Attributes? attr, Attributes? base) {
|
|||||||
return memo;
|
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;
|
||||||
|
}
|
||||||
|
@ -336,7 +336,8 @@ class Delta {
|
|||||||
final length = min(thisIter.peekLength(), otherIter.peekLength());
|
final length = min(thisIter.peekLength(), otherIter.peekLength());
|
||||||
final thisOp = thisIter.next(length);
|
final thisOp = thisIter.next(length);
|
||||||
final otherOp = otherIter.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) {
|
if (otherOp is TextRetain && otherOp.length > 0) {
|
||||||
TextOperation? newOp;
|
TextOperation? newOp;
|
||||||
if (thisOp is TextRetain) {
|
if (thisOp is TextRetain) {
|
||||||
@ -423,22 +424,3 @@ class Delta {
|
|||||||
return inverted.chop();
|
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;
|
|
||||||
}
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import 'package:flowy_editor/document/node.dart';
|
import 'package:flowy_editor/document/node.dart';
|
||||||
import 'package:flowy_editor/operation/operation.dart';
|
import 'package:flowy_editor/operation/operation.dart';
|
||||||
|
import 'package:flowy_editor/document/attributes.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import './document/state_tree.dart';
|
import './document/state_tree.dart';
|
||||||
|
@ -2,7 +2,7 @@ import 'package:flutter_test/flutter_test.dart';
|
|||||||
import 'package:flowy_editor/document/text_delta.dart';
|
import 'package:flowy_editor/document/text_delta.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
group('compose', () {
|
||||||
test('test delta', () {
|
test('test delta', () {
|
||||||
final delta = Delta(<TextOperation>[
|
final delta = Delta(<TextOperation>[
|
||||||
TextInsert('Gandalf', {
|
TextInsert('Gandalf', {
|
||||||
@ -170,4 +170,31 @@ void main() {
|
|||||||
.insert('F');
|
.insert('F');
|
||||||
expect(a.compose(b), expected);
|
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);
|
||||||
|
// });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user