Skip to content

Commit

Permalink
Fix: Undo/redo cursor position fixed (#1885)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alspb authored May 21, 2024
1 parent 6a0c101 commit ef06a44
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 26 deletions.
9 changes: 4 additions & 5 deletions lib/src/models/documents/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@ class History {
}
final delta = source.removeLast();
// look for insert or delete
int? len = 0;
var len = 0;
final 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;
if ((ops[i].key == Operation.insertKey) ||
(ops[i].key == Operation.retainKey)) {
len += ops[i].length ?? 0;
}
}
final base = Delta.from(doc.toDelta());
Expand Down
2 changes: 1 addition & 1 deletion lib/src/models/structs/history_changed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class HistoryChanged {
);

final bool changed;
final int? len;
final int len;
}
27 changes: 7 additions & 20 deletions lib/src/widgets/quill/quill_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,26 +215,13 @@ class QuillController extends ChangeNotifier {
}
}

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(
(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 _handleHistoryChange(int len) {
updateSelection(
TextSelection.collapsed(
offset: len,
),
ChangeSource.local,
);
}

void redo() {
Expand Down

0 comments on commit ef06a44

Please sign in to comment.