refactor: remove named parameters

This commit is contained in:
Vincent Chan 2022-08-01 12:58:32 +08:00
parent 5e86b83eee
commit 46dba122bd
3 changed files with 79 additions and 68 deletions

View File

@ -5,15 +5,19 @@ abstract class Operation {
factory Operation.fromJson(Map<String, dynamic> map) { factory Operation.fromJson(Map<String, dynamic> map) {
String t = map["type"] as String; String t = map["type"] as String;
if (t == "insert-operation") { if (t == "insert-operation") {
final path = map["path"] as List<int>; return InsertOperation.fromJson(map);
final value = Node.fromJson(map["value"]); } else if (t == "update-operation") {
return InsertOperation(path: path, value: value); return UpdateOperation.fromJson(map);
} else if (t == "delete-operation") {
return DeleteOperation.fromJson(map);
} else if (t == "text-edit-operation") {
return TextEditOperation.fromJson(map);
} }
throw ArgumentError('unexpected type $t'); throw ArgumentError('unexpected type $t');
} }
final Path path; final Path path;
Operation({required this.path}); Operation(this.path);
Operation copyWithPath(Path path); Operation copyWithPath(Path path);
Operation invert(); Operation invert();
Map<String, dynamic> toJson(); Map<String, dynamic> toJson();
@ -22,13 +26,16 @@ abstract class Operation {
class InsertOperation extends Operation { class InsertOperation extends Operation {
final Node value; final Node value;
InsertOperation({ factory InsertOperation.fromJson(Map<String, dynamic> map) {
required super.path, final path = map["path"] as List<int>;
required this.value, final value = Node.fromJson(map["value"]);
}); return InsertOperation(path, value);
}
InsertOperation(Path path, this.value) : super(path);
InsertOperation copyWith({Path? path, Node? value}) => InsertOperation copyWith({Path? path, Node? value}) =>
InsertOperation(path: path ?? this.path, value: value ?? this.value); InsertOperation(path ?? this.path, value ?? this.value);
@override @override
Operation copyWithPath(Path path) => copyWith(path: path); Operation copyWithPath(Path path) => copyWith(path: path);
@ -36,8 +43,8 @@ class InsertOperation extends Operation {
@override @override
Operation invert() { Operation invert() {
return DeleteOperation( return DeleteOperation(
path: path, path,
removedValue: value, value,
); );
} }
@ -55,18 +62,23 @@ class UpdateOperation extends Operation {
final Attributes attributes; final Attributes attributes;
final Attributes oldAttributes; final Attributes oldAttributes;
UpdateOperation({ factory UpdateOperation.fromJson(Map<String, dynamic> map) {
required super.path, final path = map["path"] as List<int>;
required this.attributes, final attributes = map["attributes"] as Map<String, dynamic>;
required this.oldAttributes, final oldAttributes = map["oldAttributes"] as Map<String, dynamic>;
}); return UpdateOperation(path, attributes, oldAttributes);
}
UpdateOperation(
Path path,
this.attributes,
this.oldAttributes,
) : super(path);
UpdateOperation copyWith( UpdateOperation copyWith(
{Path? path, Attributes? attributes, Attributes? oldAttributes}) => {Path? path, Attributes? attributes, Attributes? oldAttributes}) =>
UpdateOperation( UpdateOperation(path ?? this.path, attributes ?? this.attributes,
path: path ?? this.path, oldAttributes ?? this.oldAttributes);
attributes: attributes ?? this.attributes,
oldAttributes: oldAttributes ?? this.oldAttributes);
@override @override
Operation copyWithPath(Path path) => copyWith(path: path); Operation copyWithPath(Path path) => copyWith(path: path);
@ -74,9 +86,9 @@ class UpdateOperation extends Operation {
@override @override
Operation invert() { Operation invert() {
return UpdateOperation( return UpdateOperation(
path: path, path,
attributes: oldAttributes, oldAttributes,
oldAttributes: attributes, attributes,
); );
} }
@ -94,23 +106,26 @@ class UpdateOperation extends Operation {
class DeleteOperation extends Operation { class DeleteOperation extends Operation {
final Node removedValue; final Node removedValue;
DeleteOperation({ factory DeleteOperation.fromJson(Map<String, dynamic> map) {
required super.path, final path = map["path"] as List<int>;
required this.removedValue, final removedValue = Node.fromJson(map["removedValue"]);
}); return DeleteOperation(path, removedValue);
}
DeleteOperation copyWith({Path? path, Node? removedValue}) => DeleteOperation( DeleteOperation(
path: path ?? this.path, removedValue: removedValue ?? this.removedValue); Path path,
this.removedValue,
) : super(path);
DeleteOperation copyWith({Path? path, Node? removedValue}) =>
DeleteOperation(path ?? this.path, removedValue ?? this.removedValue);
@override @override
Operation copyWithPath(Path path) => copyWith(path: path); Operation copyWithPath(Path path) => copyWith(path: path);
@override @override
Operation invert() { Operation invert() {
return InsertOperation( return InsertOperation(path, removedValue);
path: path,
value: removedValue,
);
} }
@override @override
@ -127,24 +142,29 @@ class TextEditOperation extends Operation {
final Delta delta; final Delta delta;
final Delta inverted; final Delta inverted;
TextEditOperation({ factory TextEditOperation.fromJson(Map<String, dynamic> map) {
required super.path, final path = map["path"] as List<int>;
required this.delta, final delta = Delta.fromJson(map["delta"]);
required this.inverted, final invert = Delta.fromJson(map["invert"]);
}); return TextEditOperation(path, delta, invert);
}
TextEditOperation(
Path path,
this.delta,
this.inverted,
) : super(path);
TextEditOperation copyWith({Path? path, Delta? delta, Delta? inverted}) => TextEditOperation copyWith({Path? path, Delta? delta, Delta? inverted}) =>
TextEditOperation( TextEditOperation(
path: path ?? this.path, path ?? this.path, delta ?? this.delta, inverted ?? this.inverted);
delta: delta ?? this.delta,
inverted: inverted ?? this.inverted);
@override @override
Operation copyWithPath(Path path) => copyWith(path: path); Operation copyWithPath(Path path) => copyWith(path: path);
@override @override
Operation invert() { Operation invert() {
return TextEditOperation(path: path, delta: inverted, inverted: delta); return TextEditOperation(path, inverted, delta);
} }
@override @override

View File

@ -1,5 +1,4 @@
import 'dart:collection'; import 'dart:collection';
import 'dart:math';
import 'package:flowy_editor/editor_state.dart'; import 'package:flowy_editor/editor_state.dart';
import 'package:flowy_editor/document/node.dart'; import 'package:flowy_editor/document/node.dart';
import 'package:flowy_editor/document/path.dart'; import 'package:flowy_editor/document/path.dart';
@ -31,21 +30,21 @@ class TransactionBuilder {
insertNode(Path path, Node node) { insertNode(Path path, Node node) {
beforeSelection = state.cursorSelection; beforeSelection = state.cursorSelection;
add(InsertOperation(path: path, value: node)); add(InsertOperation(path, node));
} }
updateNode(Node node, Attributes attributes) { updateNode(Node node, Attributes attributes) {
beforeSelection = state.cursorSelection; beforeSelection = state.cursorSelection;
add(UpdateOperation( add(UpdateOperation(
path: node.path, node.path,
attributes: Attributes.from(node.attributes)..addAll(attributes), Attributes.from(node.attributes)..addAll(attributes),
oldAttributes: node.attributes, node.attributes,
)); ));
} }
deleteNode(Node node) { deleteNode(Node node) {
beforeSelection = state.cursorSelection; beforeSelection = state.cursorSelection;
add(DeleteOperation(path: node.path, removedValue: node)); add(DeleteOperation(node.path, node));
} }
textEdit(TextNode node, Delta Function() f) { textEdit(TextNode node, Delta Function() f) {
@ -56,7 +55,7 @@ class TransactionBuilder {
final inverted = delta.invert(node.delta); final inverted = delta.invert(node.delta);
add(TextEditOperation(path: path, delta: delta, inverted: inverted)); add(TextEditOperation(path, delta, inverted));
} }
insertText(TextNode node, int index, String content) { insertText(TextNode node, int index, String content) {
@ -82,9 +81,9 @@ class TransactionBuilder {
last is TextEditOperation && last is TextEditOperation &&
pathEquals(op.path, last.path)) { pathEquals(op.path, last.path)) {
final newOp = TextEditOperation( final newOp = TextEditOperation(
path: op.path, op.path,
delta: last.delta.compose(op.delta), last.delta.compose(op.delta),
inverted: op.inverted.compose(last.inverted), op.inverted.compose(last.inverted),
); );
operations[operations.length - 1] = newOp; operations[operations.length - 1] = newOp;
return; return;

View File

@ -27,26 +27,18 @@ void main() {
group('transform operation', () { group('transform operation', () {
test('insert + insert', () { test('insert + insert', () {
final t = transformOperation( final t = transformOperation(
InsertOperation(path: [ InsertOperation([0, 1],
0, Node(type: "node", attributes: {}, children: LinkedList())),
1 InsertOperation([0, 1],
], value: Node(type: "node", attributes: {}, children: LinkedList())), Node(type: "node", attributes: {}, children: LinkedList())));
InsertOperation(
path: [0, 1],
value:
Node(type: "node", attributes: {}, children: LinkedList())));
expect(t.path, [0, 2]); expect(t.path, [0, 2]);
}); });
test('delete + delete', () { test('delete + delete', () {
final t = transformOperation( final t = transformOperation(
DeleteOperation( DeleteOperation([0, 1],
path: [0, 1], Node(type: "node", attributes: {}, children: LinkedList())),
removedValue: DeleteOperation([0, 2],
Node(type: "node", attributes: {}, children: LinkedList())), Node(type: "node", attributes: {}, children: LinkedList())));
DeleteOperation(
path: [0, 2],
removedValue:
Node(type: "node", attributes: {}, children: LinkedList())));
expect(t.path, [0, 1]); expect(t.path, [0, 1]);
}); });
}); });