From 0800c6cb34e265ce50dd59590f56757d250712df Mon Sep 17 00:00:00 2001 From: singerdmx Date: Mon, 21 Dec 2020 23:38:43 -0800 Subject: [PATCH] Fix getPositionDelta --- lib/utils/diff_delta.dart | 2 +- lib/widgets/controller.dart | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/utils/diff_delta.dart b/lib/utils/diff_delta.dart index 1b4440b4..6de3c0af 100644 --- a/lib/utils/diff_delta.dart +++ b/lib/utils/diff_delta.dart @@ -71,7 +71,7 @@ int getPositionDelta(Delta user, Delta actual) { DeltaIterator actualItr = DeltaIterator(actual); int diff = 0; while (userItr.hasNext || actualItr.hasNext) { - int length = math.min(userItr.peekLength(), actualItr.peekLength()); + final length = math.min(userItr.peekLength(), actualItr.peekLength()); Operation userOperation = userItr.next(length); Operation actualOperation = actualItr.next(length); if (userOperation.length != actualOperation.length) { diff --git a/lib/widgets/controller.dart b/lib/widgets/controller.dart index e11ffe82..07f081aa 100644 --- a/lib/widgets/controller.dart +++ b/lib/widgets/controller.dart @@ -57,18 +57,23 @@ class QuillController extends ChangeNotifier { if (delta == null) { _updateSelection(textSelection, ChangeSource.LOCAL); } else { - Delta user = Delta() - ..retain(index) - ..insert(data) - ..delete(len); - int positionDelta = getPositionDelta(user, delta); - _updateSelection( - textSelection.copyWith( - baseOffset: textSelection.baseOffset + positionDelta, - extentOffset: textSelection.extentOffset + positionDelta, - ), - ChangeSource.LOCAL, - ); + try { + Delta user = Delta() + ..retain(index) + ..insert(data) + ..delete(len); + int positionDelta = getPositionDelta(user, delta); + _updateSelection( + textSelection.copyWith( + baseOffset: textSelection.baseOffset + positionDelta, + extentOffset: textSelection.extentOffset + positionDelta, + ), + ChangeSource.LOCAL, + ); + } catch (e) { + print ('getPositionDelta or getPositionDelta error: $e'); + throw e; + } } } notifyListeners();