Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

feat: implement editor state operation

Vincent Chan1b0c29ea09ac
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/editor_state.dart b/frontend/app_flowy/packages/flowy_editor/lib/editor_state.dart
new file mode 100644
index 0000000000..d0f8c39847
--- /dev/null
+++ b/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);
+ }
+ }
+
+}
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/operation/operation.dart b/frontend/app_flowy/packages/flowy_editor/lib/operation/operation.dart
new file mode 100644
index 0000000000..b5d71b57d4
--- /dev/null
+++ b/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);
+ }
+
+}
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction.dart b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction.dart
new file mode 100644
index 0000000000..c6fbed63aa
--- /dev/null
+++ b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction.dart
@@ -0,0 +1,6 @@
+import './operation.dart';
+
+class Transaction {
+ final List<Operation> operations = [];
+
+}