zefyr

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import 'package:notus/notus.dart';
  2. import 'package:notus/src/document.dart';
  3. import 'package:quill_delta/quill_delta.dart';
  4. ///
  5. /// record users operation or api change(Collaborative editing)
  6. /// used for redo or undo function
  7. ///
  8. class NotusHistory {
  9. final NotusHistoryStack stack = NotusHistoryStack.empty();
  10. /// used for disable redo or undo function
  11. bool ignoreChange;
  12. int lastRecorded;
  13. ///Collaborative editing's conditions should be true
  14. final bool userOnly;
  15. ///max operation count for undo
  16. final int maxStack;
  17. ///record delay
  18. final int interval;
  19. NotusHistory(
  20. {this.ignoreChange = false,
  21. this.interval = 800,
  22. this.maxStack = 100,
  23. this.userOnly = false,
  24. this.lastRecorded = 0});
  25. void handleDocChange(NotusChange event) {
  26. if (ignoreChange) return;
  27. if (!userOnly || event.source == ChangeSource.local) {
  28. record(event.change, event.before);
  29. } else {
  30. transform(event.change);
  31. }
  32. }
  33. void clear() {
  34. stack.clear();
  35. }
  36. void record(Delta change, Delta before) {
  37. if (change.isEmpty) return;
  38. stack.redo.clear();
  39. Delta undoDelta = change.invert(before);
  40. final timeStamp = DateTime.now().millisecondsSinceEpoch;
  41. if (lastRecorded + interval > timeStamp && stack.undo.isNotEmpty) {
  42. final lastDelta = stack.undo.removeLast();
  43. undoDelta = undoDelta.compose(lastDelta);
  44. } else {
  45. lastRecorded = timeStamp;
  46. }
  47. if (undoDelta.isEmpty) return;
  48. stack.undo.add(undoDelta);
  49. if (stack.undo.length > maxStack) {
  50. stack.undo.removeAt(0);
  51. }
  52. }
  53. ///
  54. ///It will override pre local undo delta,replaced by remote change
  55. ///
  56. void transform(Delta delta) {
  57. transformStack(this.stack.undo, delta);
  58. transformStack(this.stack.redo, delta);
  59. }
  60. void transformStack(List<Delta> stack, Delta delta) {
  61. for (int i = stack.length - 1; i >= 0; i -= 1) {
  62. final oldDelta = stack[i];
  63. stack[i] = delta.transform(oldDelta, true);
  64. delta = oldDelta.transform(delta, false);
  65. if (stack[i].length == 0) {
  66. stack.removeAt(i);
  67. }
  68. }
  69. }
  70. Delta _change(NotusDocument doc, List<Delta> source, List<Delta> dest) {
  71. if (source.length == 0) return Delta();
  72. Delta delta = source.removeLast();
  73. Delta base = doc.toDelta();
  74. Delta inverseDelta = delta.invert(base);
  75. dest.add(inverseDelta);
  76. this.lastRecorded = 0;
  77. this.ignoreChange = true;
  78. doc.compose(delta, ChangeSource.local, history: true);
  79. this.ignoreChange = false;
  80. return delta;
  81. }
  82. Delta undo(NotusDocument doc) {
  83. return _change(doc, stack.undo, stack.redo);
  84. }
  85. Delta redo(NotusDocument doc) {
  86. return _change(doc, stack.redo, stack.undo);
  87. }
  88. }
  89. class NotusHistoryStack {
  90. final List<Delta> undo;
  91. final List<Delta> redo;
  92. NotusHistoryStack.empty()
  93. : undo = [],
  94. redo = [];
  95. void clear() {
  96. undo.clear();
  97. redo.clear();
  98. }
  99. }