From c68c2bd030f6563ebcd39897d2934a13ebffacc5 Mon Sep 17 00:00:00 2001 From: sourabhguptazeil <108905944+sourabhguptazeil@users.noreply.github.com> Date: Thu, 16 Feb 2023 21:38:49 +1300 Subject: [PATCH] Added a property to control the detect word boundary behaviour (#1102) --- lib/src/widgets/delegate.dart | 9 +++++++-- lib/src/widgets/editor.dart | 23 +++++++++++++++++------ lib/src/widgets/text_selection.dart | 3 +++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/src/widgets/delegate.dart b/lib/src/widgets/delegate.dart index a75b64e7..a7ad9866 100644 --- a/lib/src/widgets/delegate.dart +++ b/lib/src/widgets/delegate.dart @@ -66,7 +66,9 @@ class EditorTextSelectionGestureDetectorBuilder { /// Creates a [EditorTextSelectionGestureDetectorBuilder]. /// /// The [delegate] must not be null. - EditorTextSelectionGestureDetectorBuilder({required this.delegate}); + EditorTextSelectionGestureDetectorBuilder({ + required this.delegate, + this.detectWordBoundary = true}); /// The delegate for this [EditorTextSelectionGestureDetectorBuilder]. /// @@ -83,6 +85,8 @@ class EditorTextSelectionGestureDetectorBuilder { /// a stylus. bool shouldShowSelectionToolbar = true; + bool detectWordBoundary = true; + /// The [State] of the [EditableText] for which the builder will provide a /// [EditorTextSelectionGestureDetector]. @protected @@ -354,7 +358,8 @@ class EditorTextSelectionGestureDetectorBuilder { onDragSelectionUpdate: onDragSelectionUpdate, onDragSelectionEnd: onDragSelectionEnd, behavior: behavior, - child: child, + detectWordBoundary: detectWordBoundary, + child: child ); } } diff --git a/lib/src/widgets/editor.dart b/lib/src/widgets/editor.dart index 13f5ee78..b8d007d2 100644 --- a/lib/src/widgets/editor.dart +++ b/lib/src/widgets/editor.dart @@ -182,6 +182,7 @@ class QuillEditor extends StatefulWidget { this.onImagePaste, this.customShortcuts, this.customActions, + this.detectWordBoundary = true, Key? key}) : super(key: key); @@ -399,6 +400,8 @@ class QuillEditor extends StatefulWidget { final Map? customShortcuts; final Map>? customActions; + final bool detectWordBoundary; + @override QuillEditorState createState() => QuillEditorState(); } @@ -413,7 +416,8 @@ class QuillEditorState extends State void initState() { super.initState(); _selectionGestureDetectorBuilder = - _QuillEditorSelectionGestureDetectorBuilder(this); + _QuillEditorSelectionGestureDetectorBuilder(this, + widget.detectWordBoundary); } @override @@ -591,10 +595,11 @@ class QuillEditorState extends State class _QuillEditorSelectionGestureDetectorBuilder extends EditorTextSelectionGestureDetectorBuilder { - _QuillEditorSelectionGestureDetectorBuilder(this._state) - : super(delegate: _state); + _QuillEditorSelectionGestureDetectorBuilder(this._state, this._detectWordBoundary) + : super(delegate: _state, detectWordBoundary: _detectWordBoundary); final QuillEditorState _state; + final bool _detectWordBoundary; @override void onForcePressStart(ForcePressDetails details) { @@ -712,9 +717,15 @@ class _QuillEditorSelectionGestureDetectorBuilder case PointerDeviceKind.unknown: // On macOS/iOS/iPadOS a touch tap places the cursor at the edge // of the word. - renderEditor! - ..selectWordEdge(SelectionChangedCause.tap) - ..onSelectionCompleted(); + if (_detectWordBoundary) { + renderEditor! + ..selectWordEdge(SelectionChangedCause.tap) + ..onSelectionCompleted(); + } else { + renderEditor! + ..selectPosition(cause: SelectionChangedCause.tap) + ..onSelectionCompleted(); + } break; case PointerDeviceKind.trackpad: // TODO: Handle this case. diff --git a/lib/src/widgets/text_selection.dart b/lib/src/widgets/text_selection.dart index 811d3fe6..7930d468 100644 --- a/lib/src/widgets/text_selection.dart +++ b/lib/src/widgets/text_selection.dart @@ -714,6 +714,7 @@ class EditorTextSelectionGestureDetector extends StatefulWidget { this.onDragSelectionUpdate, this.onDragSelectionEnd, this.behavior, + this.detectWordBoundary = true, Key? key, }) : super(key: key); @@ -789,6 +790,8 @@ class EditorTextSelectionGestureDetector extends StatefulWidget { /// Child below this widget. final Widget child; + final bool detectWordBoundary; + @override State createState() => _EditorTextSelectionGestureDetectorState();