mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: implement editor state operation
This commit is contained in:
parent
9c73a8cd9a
commit
1b0c29ea09
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
import './operation.dart';
|
||||
|
||||
class Transaction {
|
||||
final List<Operation> operations = [];
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user