From 2e2de297899a7332db607447fbd78a71323fce8c Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Mon, 1 Aug 2022 11:34:35 +0800 Subject: [PATCH] feat: transaction to json --- .../flowy_editor/lib/document/position.dart | 7 +++ .../flowy_editor/lib/document/selection.dart | 7 +++ .../flowy_editor/lib/operation/operation.dart | 39 ++++++++++++++++ .../lib/operation/transaction.dart | 13 ++++++ .../flowy_editor/test/operation_test.dart | 44 +++++++++++++++++++ 5 files changed, 110 insertions(+) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/document/position.dart b/frontend/app_flowy/packages/flowy_editor/lib/document/position.dart index a60f04e89b..a87064d85a 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/document/position.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/document/position.dart @@ -34,4 +34,11 @@ class Position { @override String toString() => 'path = $path, offset = $offset'; + + Map toJson() { + return { + "path": path.toList(), + "offset": offset, + }; + } } diff --git a/frontend/app_flowy/packages/flowy_editor/lib/document/selection.dart b/frontend/app_flowy/packages/flowy_editor/lib/document/selection.dart index a3919a21f6..f1fa0682f6 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/document/selection.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/document/selection.dart @@ -48,4 +48,11 @@ class Selection { @override String toString() => '[Selection] start = $start, end = $end'; + + Map toJson() { + return { + "start": start.toJson(), + "end": end.toJson(), + }; + } } diff --git a/frontend/app_flowy/packages/flowy_editor/lib/operation/operation.dart b/frontend/app_flowy/packages/flowy_editor/lib/operation/operation.dart index eafa4a31da..e4d2c1d8ed 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/operation/operation.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/operation/operation.dart @@ -6,6 +6,7 @@ abstract class Operation { Operation({required this.path}); Operation copyWithPath(Path path); Operation invert(); + Map toJson(); } class InsertOperation extends Operation { @@ -29,6 +30,15 @@ class InsertOperation extends Operation { removedValue: value, ); } + + @override + Map toJson() { + return { + "type": "insert-operation", + "path": path.toList(), + "value": value.toJson(), + }; + } } class UpdateOperation extends Operation { @@ -59,6 +69,16 @@ class UpdateOperation extends Operation { oldAttributes: attributes, ); } + + @override + Map toJson() { + return { + "type": "update-operation", + "path": path.toList(), + "attributes": {...attributes}, + "oldAttributes": {...oldAttributes}, + }; + } } class DeleteOperation extends Operation { @@ -82,6 +102,15 @@ class DeleteOperation extends Operation { value: removedValue, ); } + + @override + Map toJson() { + return { + "type": "delete-operation", + "path": path.toList(), + "removedValue": removedValue.toJson(), + }; + } } class TextEditOperation extends Operation { @@ -107,6 +136,16 @@ class TextEditOperation extends Operation { Operation invert() { return TextEditOperation(path: path, delta: inverted, inverted: delta); } + + @override + Map toJson() { + return { + "type": "text-edit-operation", + "path": path.toList(), + "delta": delta.toJson(), + "invert": inverted.toJson(), + }; + } } Path transformPath(Path preInsertPath, Path b, [int delta = 1]) { diff --git a/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction.dart b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction.dart index 85bc43f537..5dcf167628 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction.dart @@ -23,4 +23,17 @@ class Transaction { this.beforeSelection, this.afterSelection, }); + + Map toJson() { + final Map result = { + "operations": operations.map((e) => e.toJson()), + }; + if (beforeSelection != null) { + result["beforeSelection"] = beforeSelection!.toJson(); + } + if (afterSelection != null) { + result["afterSelection"] = afterSelection!.toJson(); + } + return result; + } } diff --git a/frontend/app_flowy/packages/flowy_editor/test/operation_test.dart b/frontend/app_flowy/packages/flowy_editor/test/operation_test.dart index b0b6cec141..a027fa8027 100644 --- a/frontend/app_flowy/packages/flowy_editor/test/operation_test.dart +++ b/frontend/app_flowy/packages/flowy_editor/test/operation_test.dart @@ -78,4 +78,48 @@ void main() { expect(transaction.operations[1].path, [0]); expect(transaction.operations[2].path, [0]); }); + group("toJson", () { + test("insert", () { + final root = Node(type: "root", attributes: {}, children: LinkedList()); + final state = EditorState(document: StateTree(root: root)); + + final item1 = Node(type: "node", attributes: {}, children: LinkedList()); + final tb = TransactionBuilder(state); + tb.insertNode([0], item1); + + final transaction = tb.finish(); + expect(transaction.toJson(), { + "operations": [ + { + "type": "insert-operation", + "path": [0], + "value": item1.toJson(), + } + ], + }); + }); + test("delete", () { + final item1 = Node(type: "node", attributes: {}, children: LinkedList()); + final root = Node( + type: "root", + attributes: {}, + children: LinkedList() + ..addAll([ + item1, + ])); + final state = EditorState(document: StateTree(root: root)); + final tb = TransactionBuilder(state); + tb.deleteNode(item1); + final transaction = tb.finish(); + expect(transaction.toJson(), { + "operations": [ + { + "type": "delete-operation", + "path": [0], + "removedValue": item1.toJson(), + } + ], + }); + }); + }); }