From 1b0c29ea09ac8b5a9e702bd332c18cba6feb7256 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Tue, 12 Jul 2022 13:34:40 +0800 Subject: [PATCH] feat: implement editor state operation --- .../flowy_editor/lib/editor_state.dart | 32 ++++++++++ .../flowy_editor/lib/operation/operation.dart | 58 +++++++++++++++++++ .../lib/operation/transaction.dart | 6 ++ 3 files changed, 96 insertions(+) create mode 100644 frontend/app_flowy/packages/flowy_editor/lib/editor_state.dart create mode 100644 frontend/app_flowy/packages/flowy_editor/lib/operation/operation.dart create mode 100644 frontend/app_flowy/packages/flowy_editor/lib/operation/transaction.dart 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 operations = []; + +}