Undo/redo cursor position fixed

pull/1885/head
Alspb 11 months ago
parent 9d84ef6543
commit 891758705a
  1. 9
      lib/src/models/documents/history.dart
  2. 2
      lib/src/models/structs/history_changed.dart
  3. 21
      lib/src/widgets/quill/quill_controller.dart

@ -91,13 +91,12 @@ class History {
} }
final delta = source.removeLast(); final delta = source.removeLast();
// look for insert or delete // look for insert or delete
int? len = 0; int len = 0;
final ops = delta.toList(); final ops = delta.toList();
for (var i = 0; i < ops.length; i++) { for (var i = 0; i < ops.length; i++) {
if (ops[i].key == Operation.insertKey) { if ((ops[i].key == Operation.insertKey) ||
len = ops[i].length; (ops[i].key == Operation.retainKey)) {
} else if (ops[i].key == Operation.deleteKey) { len += ops[i].length ?? 0;
len = ops[i].length! * -1;
} }
} }
final base = Delta.from(doc.toDelta()); final base = Delta.from(doc.toDelta());

@ -8,5 +8,5 @@ class HistoryChanged {
); );
final bool changed; final bool changed;
final int? len; final int len;
} }

@ -212,26 +212,13 @@ class QuillController extends ChangeNotifier {
} }
} }
void _handleHistoryChange(int? len) { void _handleHistoryChange(int len) {
// move cursor according to the length inserted or deleted from redo or undo
// operation. len is the length inserted or deleted.
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( updateSelection(
(selection.baseOffset + len) > 0 TextSelection.collapsed(
? TextSelection.collapsed( offset: len,
offset: selection.baseOffset + len, ),
)
: TextSelection.collapsed(offset: document.length),
ChangeSource.local, ChangeSource.local,
); );
} else {
// no need to move cursor
notifyListeners();
}
} }
void redo() { void redo() {

Loading…
Cancel
Save