From 178d2bb3794f211710ebbd5340cfafd87622cd44 Mon Sep 17 00:00:00 2001 From: Simon Bengtsson Date: Tue, 4 Jun 2024 14:16:52 +0200 Subject: [PATCH] Enable auto correct (#359) --- packages/fleather/lib/src/widgets/editor.dart | 13 ++++++++++ .../widgets/editor_input_client_mixin.dart | 2 +- packages/fleather/lib/src/widgets/field.dart | 7 ++++++ ...editor_input_client_mixin_deltas_test.dart | 1 + .../editor_input_client_mixin_test.dart | 25 +++++++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/fleather/lib/src/widgets/editor.dart b/packages/fleather/lib/src/widgets/editor.dart index 0f9f9d27..3b2d36be 100644 --- a/packages/fleather/lib/src/widgets/editor.dart +++ b/packages/fleather/lib/src/widgets/editor.dart @@ -155,6 +155,11 @@ class FleatherEditor extends StatefulWidget { /// Defaults to `false`. Must not be `null`. final bool readOnly; + /// Whether to enable autocorrection. + /// + /// Defaults to `true`. + final bool autocorrect; + /// Whether to enable user interface affordances for changing the /// text selection. /// @@ -283,6 +288,7 @@ class FleatherEditor extends StatefulWidget { this.autofocus = false, this.showCursor = true, this.readOnly = false, + this.autocorrect = true, this.enableInteractiveSelection = true, this.minHeight, this.maxHeight, @@ -451,6 +457,7 @@ class _FleatherEditorState extends State scrollable: widget.scrollable, padding: widget.padding, autofocus: widget.autofocus, + autocorrect: widget.autocorrect, showCursor: widget.showCursor, readOnly: widget.readOnly, enableInteractiveSelection: widget.enableInteractiveSelection, @@ -551,6 +558,7 @@ class RawEditor extends StatefulWidget { this.autofocus = false, bool? showCursor, this.readOnly = false, + this.autocorrect = true, this.enableInteractiveSelection = true, this.minHeight, this.maxHeight, @@ -602,6 +610,11 @@ class RawEditor extends StatefulWidget { /// Defaults to false. Must not be null. final bool readOnly; + /// Whether to enable autocorrection. + /// + /// Defaults to `true`. + final bool autocorrect; + /// Callback which is triggered when the user wants to open a URL from /// a link in the document. final ValueChanged? onLaunchUrl; diff --git a/packages/fleather/lib/src/widgets/editor_input_client_mixin.dart b/packages/fleather/lib/src/widgets/editor_input_client_mixin.dart index 642400e2..17d38454 100644 --- a/packages/fleather/lib/src/widgets/editor_input_client_mixin.dart +++ b/packages/fleather/lib/src/widgets/editor_input_client_mixin.dart @@ -57,7 +57,7 @@ mixin RawEditorStateTextInputClientMixin on EditorState inputType: TextInputType.multiline, readOnly: widget.readOnly, obscureText: false, - autocorrect: false, + autocorrect: widget.autocorrect, enableDeltaModel: true, inputAction: TextInputAction.newline, keyboardAppearance: widget.keyboardAppearance, diff --git a/packages/fleather/lib/src/widgets/field.dart b/packages/fleather/lib/src/widgets/field.dart index c38a37a4..96debfe8 100644 --- a/packages/fleather/lib/src/widgets/field.dart +++ b/packages/fleather/lib/src/widgets/field.dart @@ -65,6 +65,11 @@ class FleatherField extends StatefulWidget { /// Defaults to `false`. Must not be `null`. final bool readOnly; + /// Whether to enable autocorrection. + /// + /// Defaults to `true`. + final bool autocorrect; + /// Whether to enable user interface affordances for changing the /// text selection. /// @@ -175,6 +180,7 @@ class FleatherField extends StatefulWidget { this.autofocus = false, this.showCursor = true, this.readOnly = false, + this.autocorrect = true, this.enableInteractiveSelection = true, this.minHeight, this.maxHeight, @@ -245,6 +251,7 @@ class _FleatherFieldState extends State { autofocus: widget.autofocus, showCursor: widget.showCursor, readOnly: widget.readOnly, + autocorrect: widget.autocorrect, enableInteractiveSelection: widget.enableInteractiveSelection, minHeight: widget.minHeight, maxHeight: widget.maxHeight, diff --git a/packages/fleather/test/widgets/editor_input_client_mixin_deltas_test.dart b/packages/fleather/test/widgets/editor_input_client_mixin_deltas_test.dart index 35ade2cf..80ec5af7 100644 --- a/packages/fleather/test/widgets/editor_input_client_mixin_deltas_test.dart +++ b/packages/fleather/test/widgets/editor_input_client_mixin_deltas_test.dart @@ -127,6 +127,7 @@ void main() { style: const TextStyle(), spacing: const VerticalSpacing()))); when(() => rawEditor.controller).thenReturn(controller); when(() => rawEditor.readOnly).thenReturn(false); + when(() => rawEditor.autocorrect).thenReturn(true); when(() => rawEditor.keyboardAppearance).thenReturn(Brightness.light); when(() => rawEditor.textCapitalization) .thenReturn(TextCapitalization.none); diff --git a/packages/fleather/test/widgets/editor_input_client_mixin_test.dart b/packages/fleather/test/widgets/editor_input_client_mixin_test.dart index f3e1234f..b07e94cb 100644 --- a/packages/fleather/test/widgets/editor_input_client_mixin_test.dart +++ b/packages/fleather/test/widgets/editor_input_client_mixin_test.dart @@ -298,4 +298,29 @@ void main() { } }); }); + + group('send editor options to TextInputConnection', () { + testWidgets('send autocorrect option', (tester) async { + Map? textInputClientProperties; + tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.textInput, (MethodCall methodCall) async { + if (methodCall.method == 'TextInput.setClient') { + textInputClientProperties = methodCall.arguments[1]; + } + return null; + }); + + final editor = + EditorSandBox(tester: tester, document: ParchmentDocument()); + await editor.pump(); + await editor.tap(); + tester.binding.scheduleWarmUpFrame(); + await tester.pumpAndSettle(); + + expect( + textInputClientProperties?['autocorrect'], + true, + ); + }); + }); }