From 14ffc608881470c95d81a3dd914564ccced9f13d Mon Sep 17 00:00:00 2001
From: Ellet Hnewa <73608287+ellet0@users.noreply.github.com>
Date: Mon, 22 Jan 2024 15:30:36 +0300
Subject: [PATCH] An attemp to fix the text selection bug (#1643)

---
 lib/src/models/documents/document.dart      | 2 +-
 lib/src/widgets/quill/C                     | 0
 lib/src/widgets/quill/quill_controller.dart | 9 +++++++--
 3 files changed, 8 insertions(+), 3 deletions(-)
 create mode 100644 lib/src/widgets/quill/C

diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart
index d3c2e5eb..85d91dca 100644
--- a/lib/src/models/documents/document.dart
+++ b/lib/src/models/documents/document.dart
@@ -219,7 +219,7 @@ class Document {
     if (res.node is Line) {
       return res;
     }
-    final block = res.node as Block;
+    final block = res.node as Block; // TODO: Can be nullable, handle this case
     return block.queryChild(res.offset, true);
   }
 
diff --git a/lib/src/widgets/quill/C b/lib/src/widgets/quill/C
new file mode 100644
index 00000000..e69de29b
diff --git a/lib/src/widgets/quill/quill_controller.dart b/lib/src/widgets/quill/quill_controller.dart
index 7ea7b9fc..c60083fe 100644
--- a/lib/src/widgets/quill/quill_controller.dart
+++ b/lib/src/widgets/quill/quill_controller.dart
@@ -251,8 +251,13 @@ class QuillController extends ChangeNotifier {
       // updateSelection(
       // TextSelection.collapsed(offset: document.length), ChangeSource.LOCAL);
       updateSelection(
-          TextSelection.collapsed(offset: selection.baseOffset + len),
-          ChangeSource.local);
+        (selection.baseOffset + len) > 0
+            ? TextSelection.collapsed(
+                offset: selection.baseOffset + len,
+              )
+            : TextSelection.collapsed(offset: document.length),
+        ChangeSource.local,
+      );
     } else {
       // no need to move cursor
       notifyListeners();