Skip to content

Commit

Permalink
adjust cursor after undo/redo
Browse files Browse the repository at this point in the history
  • Loading branch information
li3317 committed Jan 2, 2021
1 parent 52e08d4 commit e475d3b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/models/documents/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ class Document {
_history.handleDocChange(change);
}

bool undo() {
Tuple2 undo() {
return _history.undo(this);
}

bool redo() {
Tuple2 redo() {
return _history.redo(this);
}

Expand Down
21 changes: 16 additions & 5 deletions lib/models/documents/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,37 @@ class History {
}
}

bool _change(Document doc, List<Delta> source, List<Delta> dest) {
Tuple2 _change(Document doc, List<Delta> source, List<Delta> dest) {
if (source.length == 0) {
return false;
return new Tuple2(false, 0);
}
Delta delta = source.removeLast();
// look for insert or delete
int len = 0;
List<Operation> ops = delta.toList();
for(var i = 0; i < ops.length; i++){
if (ops[i].key == Operation.insertKey){
len = ops[i].length;
}
else if (ops[i].key == Operation.deleteKey){
len = ops[i].length * -1;
}
}
Delta base = Delta.from(doc.toDelta());
Delta inverseDelta = delta.invert(base);
dest.add(inverseDelta);
this.lastRecorded = 0;
this.ignoreChange = true;
doc.compose(delta, ChangeSource.LOCAL);
this.ignoreChange = false;
return true;
return new Tuple2(true, len);
}

bool undo(Document doc) {
Tuple2 undo(Document doc) {
return _change(doc, stack.undo, stack.redo);
}

bool redo(Document doc) {
Tuple2 redo(Document doc) {
return _change(doc, stack.redo, stack.undo);
}
}
Expand Down
21 changes: 13 additions & 8 deletions lib/widgets/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,30 @@ class QuillController extends ChangeNotifier {
}

void undo() {
if (document.undo()) {
_handleHistoryChange();
Tuple2 tup = document.undo();
if (tup.item1){
_handleHistoryChange(tup.item2);
}
}

void _handleHistoryChange() {
if (this.selection.extentOffset >= document.length) {
// cursor exceeds the length of document, position it in the end
void _handleHistoryChange(int len) {
if (len != 0) {
// if (this.selection.extentOffset >= document.length) {
// // cursor exceeds the length of document, position it in the end
// updateSelection(
// TextSelection.collapsed(offset: document.length), ChangeSource.LOCAL);
updateSelection(
TextSelection.collapsed(offset: document.length), ChangeSource.LOCAL);
TextSelection.collapsed(offset: this.selection.baseOffset + len), ChangeSource.LOCAL);
} else {
// no need to move cursor
notifyListeners();
}
}

void redo() {
if (document.redo()) {
_handleHistoryChange();
Tuple2 tup = document.redo();
if (tup.item1){
_handleHistoryChange(tup.item2);
}
}

Expand Down

0 comments on commit e475d3b

Please sign in to comment.