Skip to content

Commit c68c2bd

Browse files
author
sourabhguptazeil
authored
Added a property to control the detect word boundary behaviour (singerdmx#1102)
1 parent fdbebef commit c68c2bd

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

lib/src/widgets/delegate.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ class EditorTextSelectionGestureDetectorBuilder {
6666
/// Creates a [EditorTextSelectionGestureDetectorBuilder].
6767
///
6868
/// The [delegate] must not be null.
69-
EditorTextSelectionGestureDetectorBuilder({required this.delegate});
69+
EditorTextSelectionGestureDetectorBuilder({
70+
required this.delegate,
71+
this.detectWordBoundary = true});
7072

7173
/// The delegate for this [EditorTextSelectionGestureDetectorBuilder].
7274
///
@@ -83,6 +85,8 @@ class EditorTextSelectionGestureDetectorBuilder {
8385
/// a stylus.
8486
bool shouldShowSelectionToolbar = true;
8587

88+
bool detectWordBoundary = true;
89+
8690
/// The [State] of the [EditableText] for which the builder will provide a
8791
/// [EditorTextSelectionGestureDetector].
8892
@protected
@@ -354,7 +358,8 @@ class EditorTextSelectionGestureDetectorBuilder {
354358
onDragSelectionUpdate: onDragSelectionUpdate,
355359
onDragSelectionEnd: onDragSelectionEnd,
356360
behavior: behavior,
357-
child: child,
361+
detectWordBoundary: detectWordBoundary,
362+
child: child
358363
);
359364
}
360365
}

lib/src/widgets/editor.dart

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class QuillEditor extends StatefulWidget {
182182
this.onImagePaste,
183183
this.customShortcuts,
184184
this.customActions,
185+
this.detectWordBoundary = true,
185186
Key? key})
186187
: super(key: key);
187188

@@ -399,6 +400,8 @@ class QuillEditor extends StatefulWidget {
399400
final Map<LogicalKeySet, Intent>? customShortcuts;
400401
final Map<Type, Action<Intent>>? customActions;
401402

403+
final bool detectWordBoundary;
404+
402405
@override
403406
QuillEditorState createState() => QuillEditorState();
404407
}
@@ -413,7 +416,8 @@ class QuillEditorState extends State<QuillEditor>
413416
void initState() {
414417
super.initState();
415418
_selectionGestureDetectorBuilder =
416-
_QuillEditorSelectionGestureDetectorBuilder(this);
419+
_QuillEditorSelectionGestureDetectorBuilder(this,
420+
widget.detectWordBoundary);
417421
}
418422

419423
@override
@@ -591,10 +595,11 @@ class QuillEditorState extends State<QuillEditor>
591595

592596
class _QuillEditorSelectionGestureDetectorBuilder
593597
extends EditorTextSelectionGestureDetectorBuilder {
594-
_QuillEditorSelectionGestureDetectorBuilder(this._state)
595-
: super(delegate: _state);
598+
_QuillEditorSelectionGestureDetectorBuilder(this._state, this._detectWordBoundary)
599+
: super(delegate: _state, detectWordBoundary: _detectWordBoundary);
596600

597601
final QuillEditorState _state;
602+
final bool _detectWordBoundary;
598603

599604
@override
600605
void onForcePressStart(ForcePressDetails details) {
@@ -712,9 +717,15 @@ class _QuillEditorSelectionGestureDetectorBuilder
712717
case PointerDeviceKind.unknown:
713718
// On macOS/iOS/iPadOS a touch tap places the cursor at the edge
714719
// of the word.
715-
renderEditor!
716-
..selectWordEdge(SelectionChangedCause.tap)
717-
..onSelectionCompleted();
720+
if (_detectWordBoundary) {
721+
renderEditor!
722+
..selectWordEdge(SelectionChangedCause.tap)
723+
..onSelectionCompleted();
724+
} else {
725+
renderEditor!
726+
..selectPosition(cause: SelectionChangedCause.tap)
727+
..onSelectionCompleted();
728+
}
718729
break;
719730
case PointerDeviceKind.trackpad:
720731
// TODO: Handle this case.

lib/src/widgets/text_selection.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ class EditorTextSelectionGestureDetector extends StatefulWidget {
714714
this.onDragSelectionUpdate,
715715
this.onDragSelectionEnd,
716716
this.behavior,
717+
this.detectWordBoundary = true,
717718
Key? key,
718719
}) : super(key: key);
719720

@@ -789,6 +790,8 @@ class EditorTextSelectionGestureDetector extends StatefulWidget {
789790
/// Child below this widget.
790791
final Widget child;
791792

793+
final bool detectWordBoundary;
794+
792795
@override
793796
State<StatefulWidget> createState() =>
794797
_EditorTextSelectionGestureDetectorState();

0 commit comments

Comments
 (0)