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