瀏覽代碼

fix undo && redo selection

lucky1213 4 年之前
父節點
當前提交
4ea7970715

+ 1
- 0
packages/notus/lib/notus.dart 查看文件

@@ -6,6 +6,7 @@
6 6
 library notus;
7 7
 
8 8
 export 'src/document.dart';
9
+export 'src/history.dart';
9 10
 export 'src/document/attributes.dart';
10 11
 export 'src/document/block.dart';
11 12
 export 'src/document/leaf.dart';

+ 9
- 7
packages/notus/lib/src/document.dart 查看文件

@@ -57,6 +57,7 @@ class NotusDocument {
57 57
   NotusDocument.fromDelta(Delta delta)
58 58
       : assert(delta != null),
59 59
         _heuristics = NotusHeuristics.fallback,
60
+        _history = NotusHistory(),
60 61
         _delta = delta {
61 62
     _loadDocument(_delta);
62 63
   }
@@ -254,7 +255,10 @@ class NotusDocument {
254 255
       throw StateError('Compose produced inconsistent results. '
255 256
           'This is likely due to a bug in the library. Tried to compose change $change from $source.');
256 257
     }
257
-    _controller.add(NotusChange(before, change, source));
258
+    // _controller.add(NotusChange(before, change, source));
259
+    final notusChange = NotusChange(before, change, source);
260
+    _controller.add(notusChange);
261
+    if (history != true) _history?.handleDocChange(notusChange);
258 262
   }
259 263
 
260 264
   //
@@ -307,13 +311,11 @@ class NotusDocument {
307 311
     }
308 312
   }
309 313
 
310
-  void undo() {
311
-    assert(_history != null);
312
-    _history?.undo(this);
314
+  Delta undo() {
315
+    return _history?.undo(this);
313 316
   }
314 317
 
315
-  void redo() {
316
-    assert(_history != null);
317
-    _history?.redo(this);
318
+  Delta redo() {
319
+    return _history?.redo(this);
318 320
   }
319 321
 }

+ 8
- 7
packages/notus/lib/src/history.dart 查看文件

@@ -25,7 +25,7 @@ class NotusHistory {
25 25
 
26 26
   NotusHistory(
27 27
       {this.ignoreChange = false,
28
-      this.interval = 400,
28
+      this.interval = 800,
29 29
       this.maxStack = 100,
30 30
       this.userOnly = false,
31 31
       this.lastRecorded = 0});
@@ -83,8 +83,8 @@ class NotusHistory {
83 83
     }
84 84
   }
85 85
 
86
-  void _change(NotusDocument doc, List<Delta> source, List<Delta> dest) {
87
-    if (source.length == 0) return;
86
+  Delta _change(NotusDocument doc, List<Delta> source, List<Delta> dest) {
87
+    if (source.length == 0) return Delta();
88 88
     Delta delta = source.removeLast();
89 89
     Delta base = doc.toDelta();
90 90
     Delta inverseDelta = delta.invert(base);
@@ -93,14 +93,15 @@ class NotusHistory {
93 93
     this.ignoreChange = true;
94 94
     doc.compose(delta, ChangeSource.local, history: true);
95 95
     this.ignoreChange = false;
96
+    return delta;
96 97
   }
97 98
 
98
-  void undo(NotusDocument doc) {
99
-    _change(doc, stack.undo, stack.redo);
99
+  Delta undo(NotusDocument doc) {
100
+    return _change(doc, stack.undo, stack.redo);
100 101
   }
101 102
 
102
-  void redo(NotusDocument doc) {
103
-    _change(doc, stack.redo, stack.undo);
103
+  Delta redo(NotusDocument doc) {
104
+    return _change(doc, stack.redo, stack.undo);
104 105
   }
105 106
 }
106 107
 

+ 4
- 0
packages/zefyr/lib/src/widgets/buttons.dart 查看文件

@@ -163,6 +163,10 @@ class ZefyrButton extends StatelessWidget {
163 163
       return () => editor.closeKeyboard();
164 164
     } else if (action == ZefyrToolbarAction.showKeyboard) {
165 165
       return () => toolbar.closeOverlay();
166
+    } else if (action == ZefyrToolbarAction.redo && editor.controller.document.history.stack.redo.isNotEmpty) {
167
+      return () => editor.redo();
168
+    } else if (action == ZefyrToolbarAction.undo && editor.controller.document.history.stack.undo.isNotEmpty) {
169
+      return () => editor.undo();
166 170
     }
167 171
 
168 172
     return null;

+ 4
- 4
packages/zefyr/lib/src/widgets/controller.dart 查看文件

@@ -208,13 +208,13 @@ class ZefyrController extends ChangeNotifier {
208 208
   }
209 209
 
210 210
   void undo() {
211
-    document.undo();
212
-    updateSelection(TextSelection.collapsed(offset: document.length));
211
+    Delta change = document.undo();
212
+    updateSelection(TextSelection.collapsed(offset: change.transformPosition(_selection.extentOffset, force: false)));
213 213
   }
214 214
 
215 215
   void redo() {
216
-    document.redo();
217
-    updateSelection(TextSelection.collapsed(offset: document.length));
216
+    Delta change = document.redo();
217
+    updateSelection(TextSelection.collapsed(offset: change.transformPosition(_selection.extentOffset, force: true)));
218 218
   }
219 219
   
220 220
   /// Returns style of specified text range.