From ef06a447c4f527b7b49bdf4ae24a47657a18be23 Mon Sep 17 00:00:00 2001 From: Alspb <73047043+Alspb@users.noreply.github.com> Date: Tue, 21 May 2024 02:59:49 +0000 Subject: [PATCH] Fix: Undo/redo cursor position fixed (#1885) --- lib/src/models/documents/history.dart | 9 +++---- lib/src/models/structs/history_changed.dart | 2 +- lib/src/widgets/quill/quill_controller.dart | 27 ++++++--------------- 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/lib/src/models/documents/history.dart b/lib/src/models/documents/history.dart index 11082e4f..39173783 100644 --- a/lib/src/models/documents/history.dart +++ b/lib/src/models/documents/history.dart @@ -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()); diff --git a/lib/src/models/structs/history_changed.dart b/lib/src/models/structs/history_changed.dart index 1a8b2d60..cafaba60 100644 --- a/lib/src/models/structs/history_changed.dart +++ b/lib/src/models/structs/history_changed.dart @@ -8,5 +8,5 @@ class HistoryChanged { ); final bool changed; - final int? len; + final int len; } diff --git a/lib/src/widgets/quill/quill_controller.dart b/lib/src/widgets/quill/quill_controller.dart index 7e71f201..234d4ee3 100644 --- a/lib/src/widgets/quill/quill_controller.dart +++ b/lib/src/widgets/quill/quill_controller.dart @@ -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() {