Skip to content

Commit

Permalink
chore: dart format
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Apr 11, 2023
1 parent 1293599 commit 42ffa09
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
10 changes: 7 additions & 3 deletions lib/src/core/transform/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ class Transaction {
void add(Operation op, {bool transform = true}) {
final Operation? last = operations.isEmpty ? null : operations.last;
if (last != null) {
if (op is UpdateTextOperation && last is UpdateTextOperation && op.path.equals(last.path)) {
if (op is UpdateTextOperation &&
last is UpdateTextOperation &&
op.path.equals(last.path)) {
final newOp = UpdateTextOperation(
op.path,
last.delta.compose(op.delta),
Expand Down Expand Up @@ -204,7 +206,8 @@ extension TextTransaction on Transaction {
}) {
var newAttributes = attributes;
if (index != 0 && attributes == null) {
newAttributes = textNode.delta.slice(max(index - 1, 0), index).first.attributes;
newAttributes =
textNode.delta.slice(max(index - 1, 0), index).first.attributes;
if (newAttributes != null) {
newAttributes = {...newAttributes}; // make a copy
}
Expand Down Expand Up @@ -266,7 +269,8 @@ extension TextTransaction on Transaction {
}) {
var newAttributes = attributes;
if (index != 0 && attributes == null) {
newAttributes = textNode.delta.slice(max(index - 1, 0), index).first.attributes;
newAttributes =
textNode.delta.slice(max(index - 1, 0), index).first.attributes;
if (newAttributes == null) {
final slicedDelta = textNode.delta.slice(index, index + length);
if (slicedDelta.isNotEmpty) {
Expand Down
71 changes: 54 additions & 17 deletions test/core/transform/transaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Document createEmptyDocument() {

void main() async {
group('transaction.dart', () {
testWidgets('test replaceTexts, textNodes.length == texts.length', (tester) async {
testWidgets('test replaceTexts, textNodes.length == texts.length',
(tester) async {
TestWidgetsFlutterBinding.ensureInitialized();

final editor = tester.editor
Expand All @@ -30,21 +31,28 @@ void main() async {
end: Position(path: [3], offset: 4),
);
final transaction = editor.editorState.transaction;
var textNodes = [0, 1, 2, 3].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
var textNodes = [0, 1, 2, 3]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
final texts = ['ABC', 'ABC', 'ABC', 'ABC'];
transaction.replaceTexts(textNodes, selection, texts);
editor.editorState.apply(transaction);
await tester.pumpAndSettle();

expect(editor.documentLength, 4);
textNodes = [0, 1, 2, 3].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
textNodes = [0, 1, 2, 3]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
expect(textNodes[0].toPlainText(), '0123ABC');
expect(textNodes[1].toPlainText(), 'ABC');
expect(textNodes[2].toPlainText(), 'ABC');
expect(textNodes[3].toPlainText(), 'ABC456789');
});

testWidgets('test replaceTexts, textNodes.length > texts.length', (tester) async {
testWidgets('test replaceTexts, textNodes.length > texts.length',
(tester) async {
TestWidgetsFlutterBinding.ensureInitialized();

final editor = tester.editor
Expand All @@ -63,21 +71,28 @@ void main() async {
end: Position(path: [4], offset: 4),
);
final transaction = editor.editorState.transaction;
var textNodes = [0, 1, 2, 3, 4].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
var textNodes = [0, 1, 2, 3, 4]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
final texts = ['ABC', 'ABC', 'ABC', 'ABC'];
transaction.replaceTexts(textNodes, selection, texts);
editor.editorState.apply(transaction);
await tester.pumpAndSettle();

expect(editor.documentLength, 4);
textNodes = [0, 1, 2, 3].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
textNodes = [0, 1, 2, 3]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
expect(textNodes[0].toPlainText(), '0123ABC');
expect(textNodes[1].toPlainText(), 'ABC');
expect(textNodes[2].toPlainText(), 'ABC');
expect(textNodes[3].toPlainText(), 'ABC456789');
});

testWidgets('test replaceTexts, textNodes.length >> texts.length', (tester) async {
testWidgets('test replaceTexts, textNodes.length >> texts.length',
(tester) async {
TestWidgetsFlutterBinding.ensureInitialized();

final editor = tester.editor
Expand All @@ -96,18 +111,25 @@ void main() async {
end: Position(path: [4], offset: 4),
);
final transaction = editor.editorState.transaction;
var textNodes = [0, 1, 2, 3, 4].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
var textNodes = [0, 1, 2, 3, 4]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
final texts = ['ABC'];
transaction.replaceTexts(textNodes, selection, texts);
editor.editorState.apply(transaction);
await tester.pumpAndSettle();

expect(editor.documentLength, 1);
textNodes = [0].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
textNodes = [0]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
expect(textNodes[0].toPlainText(), '0123ABC456789');
});

testWidgets('test replaceTexts, textNodes.length < texts.length', (tester) async {
testWidgets('test replaceTexts, textNodes.length < texts.length',
(tester) async {
TestWidgetsFlutterBinding.ensureInitialized();

final editor = tester.editor
Expand All @@ -124,21 +146,28 @@ void main() async {
end: Position(path: [2], offset: 4),
);
final transaction = editor.editorState.transaction;
var textNodes = [0, 1, 2].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
var textNodes = [0, 1, 2]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
final texts = ['ABC', 'ABC', 'ABC', 'ABC'];
transaction.replaceTexts(textNodes, selection, texts);
editor.editorState.apply(transaction);
await tester.pumpAndSettle();

expect(editor.documentLength, 4);
textNodes = [0, 1, 2, 3].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
textNodes = [0, 1, 2, 3]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
expect(textNodes[0].toPlainText(), '0123ABC');
expect(textNodes[1].toPlainText(), 'ABC');
expect(textNodes[2].toPlainText(), 'ABC');
expect(textNodes[3].toPlainText(), 'ABC456789');
});

testWidgets('test replaceTexts, textNodes.length << texts.length', (tester) async {
testWidgets('test replaceTexts, textNodes.length << texts.length',
(tester) async {
TestWidgetsFlutterBinding.ensureInitialized();

final editor = tester.editor..insertTextNode('Welcome to AppFlowy!');
Expand All @@ -153,22 +182,29 @@ void main() async {
end: Position(path: [0], offset: 10),
);
final transaction = editor.editorState.transaction;
var textNodes = [0].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
var textNodes = [0]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
final texts = ['ABC1', 'ABC2', 'ABC3', 'ABC4', 'ABC5'];
transaction.replaceTexts(textNodes, selection, texts);
editor.editorState.apply(transaction);
await tester.pumpAndSettle();

expect(editor.documentLength, 5);
textNodes = [0, 1, 2, 3, 4].map((e) => editor.nodeAtPath([e])!).whereType<TextNode>().toList(growable: false);
textNodes = [0, 1, 2, 3, 4]
.map((e) => editor.nodeAtPath([e])!)
.whereType<TextNode>()
.toList(growable: false);
expect(textNodes[0].toPlainText(), 'Welcome ABC1');
expect(textNodes[1].toPlainText(), 'ABC2');
expect(textNodes[2].toPlainText(), 'ABC3');
expect(textNodes[3].toPlainText(), 'ABC4');
expect(textNodes[4].toPlainText(), 'ABC5 AppFlowy!');
});

testWidgets('test selection propagates if non-selected node is deleted', (tester) async {
testWidgets('test selection propagates if non-selected node is deleted',
(tester) async {
TestWidgetsFlutterBinding.ensureInitialized();

final editor = tester.editor
Expand Down Expand Up @@ -205,7 +241,8 @@ void main() async {
);
});

testWidgets('test selection does not propagate if selected node is deleted', (tester) async {
testWidgets('test selection does not propagate if selected node is deleted',
(tester) async {
TestWidgetsFlutterBinding.ensureInitialized();

final editor = tester.editor
Expand Down

0 comments on commit 42ffa09

Please sign in to comment.