From e475d3b0c9c8987020ece3eeeb681db57681afab Mon Sep 17 00:00:00 2001 From: li3317 Date: Sat, 2 Jan 2021 13:47:47 -0500 Subject: [PATCH] adjust cursor after undo/redo --- lib/models/documents/document.dart | 4 ++-- lib/models/documents/history.dart | 21 ++++++++++++++++----- lib/widgets/controller.dart | 21 +++++++++++++-------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/models/documents/document.dart b/lib/models/documents/document.dart index 21bc269a..bcfd5152 100644 --- a/lib/models/documents/document.dart +++ b/lib/models/documents/document.dart @@ -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); } diff --git a/lib/models/documents/history.dart b/lib/models/documents/history.dart index 423245ff..8ac402d3 100644 --- a/lib/models/documents/history.dart +++ b/lib/models/documents/history.dart @@ -84,11 +84,22 @@ class History { } } - bool _change(Document doc, List source, List dest) { + Tuple2 _change(Document doc, List source, List 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 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); @@ -96,14 +107,14 @@ class History { 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); } } diff --git a/lib/widgets/controller.dart b/lib/widgets/controller.dart index 7c419d0c..650f6337 100644 --- a/lib/widgets/controller.dart +++ b/lib/widgets/controller.dart @@ -43,16 +43,20 @@ 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(); @@ -60,8 +64,9 @@ class QuillController extends ChangeNotifier { } void redo() { - if (document.redo()) { - _handleHistoryChange(); + Tuple2 tup = document.redo(); + if (tup.item1){ + _handleHistoryChange(tup.item2); } }