transaction.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import 'dart:collection';
  2. import 'package:flutter/material.dart';
  3. import 'package:flowy_editor/document/selection.dart';
  4. import './operation.dart';
  5. /// A [Transaction] has a list of [Operation] objects that will be applied
  6. /// to the editor. It is an immutable class and used to store and transmit.
  7. ///
  8. /// If you want to build a new [Transaction], use [TransactionBuilder] directly.
  9. ///
  10. /// There will be several ways to consume the transaction:
  11. /// 1. Apply to the state to update the UI.
  12. /// 2. Send to the backend to store and do operation transforming.
  13. /// 3. Used by the UndoManager to implement redo/undo.
  14. @immutable
  15. class Transaction {
  16. final UnmodifiableListView<Operation> operations;
  17. final Selection? beforeSelection;
  18. final Selection? afterSelection;
  19. const Transaction({
  20. required this.operations,
  21. this.beforeSelection,
  22. this.afterSelection,
  23. });
  24. Map<String, dynamic> toJson() {
  25. final Map<String, dynamic> result = {
  26. "operations": operations.map((e) => e.toJson()),
  27. };
  28. if (beforeSelection != null) {
  29. result["beforeSelection"] = beforeSelection!.toJson();
  30. }
  31. if (afterSelection != null) {
  32. result["afterSelection"] = afterSelection!.toJson();
  33. }
  34. return result;
  35. }
  36. }