Sfoglia il codice sorgente

feat: implement editor state operation

Vincent Chan 2 anni fa
parent
commit
1b0c29ea09

+ 32 - 0
frontend/app_flowy/packages/flowy_editor/lib/editor_state.dart

@@ -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);
+    }
+  }
+
+}

+ 58 - 0
frontend/app_flowy/packages/flowy_editor/lib/operation/operation.dart

@@ -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);
+  }
+
+}

+ 6 - 0
frontend/app_flowy/packages/flowy_editor/lib/operation/transaction.dart

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