Przeglądaj źródła

fix undo && redo selection

lucky1213 4 lat temu
rodzic
commit
4ea7970715

+ 1
- 0
packages/notus/lib/notus.dart Wyświetl plik

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

+ 9
- 7
packages/notus/lib/src/document.dart Wyświetl plik

57
   NotusDocument.fromDelta(Delta delta)
57
   NotusDocument.fromDelta(Delta delta)
58
       : assert(delta != null),
58
       : assert(delta != null),
59
         _heuristics = NotusHeuristics.fallback,
59
         _heuristics = NotusHeuristics.fallback,
60
+        _history = NotusHistory(),
60
         _delta = delta {
61
         _delta = delta {
61
     _loadDocument(_delta);
62
     _loadDocument(_delta);
62
   }
63
   }
254
       throw StateError('Compose produced inconsistent results. '
255
       throw StateError('Compose produced inconsistent results. '
255
           'This is likely due to a bug in the library. Tried to compose change $change from $source.');
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
     }
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 Wyświetl plik

25
 
25
 
26
   NotusHistory(
26
   NotusHistory(
27
       {this.ignoreChange = false,
27
       {this.ignoreChange = false,
28
-      this.interval = 400,
28
+      this.interval = 800,
29
       this.maxStack = 100,
29
       this.maxStack = 100,
30
       this.userOnly = false,
30
       this.userOnly = false,
31
       this.lastRecorded = 0});
31
       this.lastRecorded = 0});
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
     Delta delta = source.removeLast();
88
     Delta delta = source.removeLast();
89
     Delta base = doc.toDelta();
89
     Delta base = doc.toDelta();
90
     Delta inverseDelta = delta.invert(base);
90
     Delta inverseDelta = delta.invert(base);
93
     this.ignoreChange = true;
93
     this.ignoreChange = true;
94
     doc.compose(delta, ChangeSource.local, history: true);
94
     doc.compose(delta, ChangeSource.local, history: true);
95
     this.ignoreChange = false;
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 Wyświetl plik

163
       return () => editor.closeKeyboard();
163
       return () => editor.closeKeyboard();
164
     } else if (action == ZefyrToolbarAction.showKeyboard) {
164
     } else if (action == ZefyrToolbarAction.showKeyboard) {
165
       return () => toolbar.closeOverlay();
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
     return null;
172
     return null;

+ 4
- 4
packages/zefyr/lib/src/widgets/controller.dart Wyświetl plik

208
   }
208
   }
209
 
209
 
210
   void undo() {
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
   void redo() {
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
   /// Returns style of specified text range.
220
   /// Returns style of specified text range.