feat: implement editor state operation

This commit is contained in:
Vincent Chan 2022-07-12 13:34:40 +08:00
parent 9c73a8cd9a
commit 1b0c29ea09
3 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import 'package:flowy_editor/operation/operation.dart';
import './document/state_tree.dart';
import './document/selection.dart';
import './operation/operation.dart';
import './operation/transaction.dart';
class EditorState {
final StateTree document;
Selection? cursorSelection;
EditorState({
required this.document,
});
apply(Transaction transaction) {
for (final op in transaction.operations) {
_applyOperation(op);
}
}
_applyOperation(Operation op) {
if (op is InsertOperation) {
document.insert(op.path, op.value);
} else if (op is UpdateOperation) {
document.update(op.path, op.attributes);
} else if (op is DeleteOperation) {
document.delete(op.path);
}
}
}

View File

@ -0,0 +1,58 @@
import 'package:flowy_editor/document/path.dart';
import 'package:flowy_editor/document/node.dart';
abstract class Operation {
Operation invert();
}
class InsertOperation extends Operation {
final Path path;
final Node value;
InsertOperation({
required this.path,
required this.value,
});
@override
Operation invert() {
return DeleteOperation(path: path, removedValue: value);
}
}
class UpdateOperation extends Operation {
final Path path;
final Attributes attributes;
final Attributes oldAttributes;
UpdateOperation({
required this.path,
required this.attributes,
required this.oldAttributes,
});
@override
Operation invert() {
return UpdateOperation(path: path, attributes: oldAttributes, oldAttributes: attributes);
}
}
class DeleteOperation extends Operation {
final Path path;
final Node removedValue;
DeleteOperation({
required this.path,
required this.removedValue,
});
@override
Operation invert() {
return InsertOperation(path: path, value: removedValue);
}
}

View File

@ -0,0 +1,6 @@
import './operation.dart';
class Transaction {
final List<Operation> operations = [];
}