operation.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import 'package:flowy_editor/document/attributes.dart';
  2. import 'package:flowy_editor/flowy_editor.dart';
  3. abstract class Operation {
  4. factory Operation.fromJson(Map<String, dynamic> map) {
  5. String t = map["type"] as String;
  6. if (t == "insert-operation") {
  7. return InsertOperation.fromJson(map);
  8. } else if (t == "update-operation") {
  9. return UpdateOperation.fromJson(map);
  10. } else if (t == "delete-operation") {
  11. return DeleteOperation.fromJson(map);
  12. } else if (t == "text-edit-operation") {
  13. return TextEditOperation.fromJson(map);
  14. }
  15. throw ArgumentError('unexpected type $t');
  16. }
  17. final Path path;
  18. Operation(this.path);
  19. Operation copyWithPath(Path path);
  20. Operation invert();
  21. Map<String, dynamic> toJson();
  22. }
  23. class InsertOperation extends Operation {
  24. final List<Node> nodes;
  25. factory InsertOperation.fromJson(Map<String, dynamic> map) {
  26. final path = map["path"] as List<int>;
  27. final value =
  28. (map["nodes"] as List<dynamic>).map((n) => Node.fromJson(n)).toList();
  29. return InsertOperation(path, value);
  30. }
  31. InsertOperation(Path path, this.nodes) : super(path);
  32. InsertOperation copyWith({Path? path, List<Node>? nodes}) =>
  33. InsertOperation(path ?? this.path, nodes ?? this.nodes);
  34. @override
  35. Operation copyWithPath(Path path) => copyWith(path: path);
  36. @override
  37. Operation invert() {
  38. return DeleteOperation(
  39. path,
  40. nodes,
  41. );
  42. }
  43. @override
  44. Map<String, dynamic> toJson() {
  45. return {
  46. "type": "insert-operation",
  47. "path": path.toList(),
  48. "nodes": nodes.map((n) => n.toJson()),
  49. };
  50. }
  51. }
  52. class UpdateOperation extends Operation {
  53. final Attributes attributes;
  54. final Attributes oldAttributes;
  55. factory UpdateOperation.fromJson(Map<String, dynamic> map) {
  56. final path = map["path"] as List<int>;
  57. final attributes = map["attributes"] as Map<String, dynamic>;
  58. final oldAttributes = map["oldAttributes"] as Map<String, dynamic>;
  59. return UpdateOperation(path, attributes, oldAttributes);
  60. }
  61. UpdateOperation(
  62. Path path,
  63. this.attributes,
  64. this.oldAttributes,
  65. ) : super(path);
  66. UpdateOperation copyWith(
  67. {Path? path, Attributes? attributes, Attributes? oldAttributes}) =>
  68. UpdateOperation(path ?? this.path, attributes ?? this.attributes,
  69. oldAttributes ?? this.oldAttributes);
  70. @override
  71. Operation copyWithPath(Path path) => copyWith(path: path);
  72. @override
  73. Operation invert() {
  74. return UpdateOperation(
  75. path,
  76. oldAttributes,
  77. attributes,
  78. );
  79. }
  80. @override
  81. Map<String, dynamic> toJson() {
  82. return {
  83. "type": "update-operation",
  84. "path": path.toList(),
  85. "attributes": {...attributes},
  86. "oldAttributes": {...oldAttributes},
  87. };
  88. }
  89. }
  90. class DeleteOperation extends Operation {
  91. final List<Node> nodes;
  92. factory DeleteOperation.fromJson(Map<String, dynamic> map) {
  93. final path = map["path"] as List<int>;
  94. final List<Node> nodes =
  95. (map["nodes"] as List<dynamic>).map((e) => Node.fromJson(e)).toList();
  96. return DeleteOperation(path, nodes);
  97. }
  98. DeleteOperation(
  99. Path path,
  100. this.nodes,
  101. ) : super(path);
  102. DeleteOperation copyWith({Path? path, List<Node>? nodes}) =>
  103. DeleteOperation(path ?? this.path, nodes ?? this.nodes);
  104. @override
  105. Operation copyWithPath(Path path) => copyWith(path: path);
  106. @override
  107. Operation invert() {
  108. return InsertOperation(path, nodes);
  109. }
  110. @override
  111. Map<String, dynamic> toJson() {
  112. return {
  113. "type": "delete-operation",
  114. "path": path.toList(),
  115. "nodes": nodes.map((n) => n.toJson()),
  116. };
  117. }
  118. }
  119. class TextEditOperation extends Operation {
  120. final Delta delta;
  121. final Delta inverted;
  122. factory TextEditOperation.fromJson(Map<String, dynamic> map) {
  123. final path = map["path"] as List<int>;
  124. final delta = Delta.fromJson(map["delta"]);
  125. final invert = Delta.fromJson(map["invert"]);
  126. return TextEditOperation(path, delta, invert);
  127. }
  128. TextEditOperation(
  129. Path path,
  130. this.delta,
  131. this.inverted,
  132. ) : super(path);
  133. TextEditOperation copyWith({Path? path, Delta? delta, Delta? inverted}) =>
  134. TextEditOperation(
  135. path ?? this.path, delta ?? this.delta, inverted ?? this.inverted);
  136. @override
  137. Operation copyWithPath(Path path) => copyWith(path: path);
  138. @override
  139. Operation invert() {
  140. return TextEditOperation(path, inverted, delta);
  141. }
  142. @override
  143. Map<String, dynamic> toJson() {
  144. return {
  145. "type": "text-edit-operation",
  146. "path": path.toList(),
  147. "delta": delta.toJson(),
  148. "invert": inverted.toJson(),
  149. };
  150. }
  151. }
  152. Path transformPath(Path preInsertPath, Path b, [int delta = 1]) {
  153. if (preInsertPath.length > b.length) {
  154. return b;
  155. }
  156. if (preInsertPath.isEmpty || b.isEmpty) {
  157. return b;
  158. }
  159. // check the prefix
  160. for (var i = 0; i < preInsertPath.length - 1; i++) {
  161. if (preInsertPath[i] != b[i]) {
  162. return b;
  163. }
  164. }
  165. final prefix = preInsertPath.sublist(0, preInsertPath.length - 1);
  166. final suffix = b.sublist(preInsertPath.length);
  167. final preInsertLast = preInsertPath.last;
  168. final bAtIndex = b[preInsertPath.length - 1];
  169. if (preInsertLast <= bAtIndex) {
  170. prefix.add(bAtIndex + delta);
  171. } else {
  172. prefix.add(bAtIndex);
  173. }
  174. prefix.addAll(suffix);
  175. return prefix;
  176. }
  177. Operation transformOperation(Operation a, Operation b) {
  178. if (a is InsertOperation) {
  179. final newPath = transformPath(a.path, b.path);
  180. return b.copyWithPath(newPath);
  181. } else if (a is DeleteOperation) {
  182. final newPath = transformPath(a.path, b.path, -1);
  183. return b.copyWithPath(newPath);
  184. }
  185. // TODO: transform update and textedit
  186. return b;
  187. }