Fix: Undo/redo cursor position fixed (#1885)

pull/1890/head v9.3.18
Alspb 11 months ago committed by GitHub
parent 6a0c101443
commit ef06a447c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 9
      lib/src/models/documents/history.dart
  2. 2
      lib/src/models/structs/history_changed.dart
  3. 27
      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; var 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;
} }

@ -215,26 +215,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 updateSelection(
// operation. len is the length inserted or deleted. TextSelection.collapsed(
if (len! != 0) { offset: len,
// if (this.selection.extentOffset >= document.length) { ),
// // cursor exceeds the length of document, position it in the end ChangeSource.local,
// updateSelection( );
// TextSelection.collapsed(offset: document.length), ChangeSource.LOCAL);
updateSelection(
(selection.baseOffset + len) > 0
? TextSelection.collapsed(
offset: selection.baseOffset + len,
)
: TextSelection.collapsed(offset: document.length),
ChangeSource.local,
);
} else {
// no need to move cursor
notifyListeners();
}
} }
void redo() { void redo() {

Loading…
Cancel
Save