operation.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'package:flowy_editor/document/path.dart';
  2. import 'package:flowy_editor/document/node.dart';
  3. import 'package:flowy_editor/document/text_delta.dart';
  4. import 'package:flowy_editor/document/attributes.dart';
  5. abstract class Operation {
  6. Operation invert();
  7. }
  8. class InsertOperation extends Operation {
  9. final Path path;
  10. final Node value;
  11. InsertOperation({
  12. required this.path,
  13. required this.value,
  14. });
  15. @override
  16. Operation invert() {
  17. return DeleteOperation(
  18. path: path,
  19. removedValue: value,
  20. );
  21. }
  22. }
  23. class UpdateOperation extends Operation {
  24. final Path path;
  25. final Attributes attributes;
  26. final Attributes oldAttributes;
  27. UpdateOperation({
  28. required this.path,
  29. required this.attributes,
  30. required this.oldAttributes,
  31. });
  32. @override
  33. Operation invert() {
  34. return UpdateOperation(
  35. path: path,
  36. attributes: oldAttributes,
  37. oldAttributes: attributes,
  38. );
  39. }
  40. }
  41. class DeleteOperation extends Operation {
  42. final Path path;
  43. final Node removedValue;
  44. DeleteOperation({
  45. required this.path,
  46. required this.removedValue,
  47. });
  48. @override
  49. Operation invert() {
  50. return InsertOperation(
  51. path: path,
  52. value: removedValue,
  53. );
  54. }
  55. }
  56. class TextEditOperation extends Operation {
  57. final Path path;
  58. final Delta delta;
  59. final Delta inverted;
  60. TextEditOperation({
  61. required this.path,
  62. required this.delta,
  63. required this.inverted,
  64. });
  65. @override
  66. Operation invert() {
  67. return TextEditOperation(path: path, delta: inverted, inverted: delta);
  68. }
  69. }