Chore: Move spellchecker service to extensions (#2120)

* Removed dependency from pubspec.yaml

* Chore: moved simple_spell_checker_service to extensions

* Chore: dart fixes and formatting

* Fix: imports

---------

Co-authored-by: CatHood0 <santiagowmar@gmail.com>
pull/2095/head v10.3.1
Cat 8 months ago committed by GitHub
parent b62f413bf7
commit da4f0efeaf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 23
      flutter_quill_extensions/lib/flutter_quill_extensions.dart
  2. 31
      flutter_quill_extensions/lib/src/editor/spell_checker/simple_spell_checker_service.dart
  3. 3
      flutter_quill_extensions/pubspec.yaml
  4. 1
      lib/flutter_quill.dart
  5. 23
      lib/src/editor/spellchecker/default_spellchecker_service.dart
  6. 15
      lib/src/editor/spellchecker/spellchecker_service.dart
  7. 23
      lib/src/editor/spellchecker/spellchecker_service_provider.dart
  8. 4
      lib/src/editor/widgets/text/text_line.dart
  9. 3
      pubspec.yaml

@ -1,9 +1,12 @@
library flutter_quill_extensions; library flutter_quill_extensions;
// ignore: implementation_imports
import 'package:flutter_quill/src/editor/spellchecker/spellchecker_service_provider.dart';
// ignore: implementation_imports // ignore: implementation_imports
import 'package:flutter_quill/src/editor_toolbar_controller_shared/clipboard/clipboard_service_provider.dart'; import 'package:flutter_quill/src/editor_toolbar_controller_shared/clipboard/clipboard_service_provider.dart';
import 'package:meta/meta.dart' show immutable; import 'package:meta/meta.dart' show immutable;
import 'src/editor/spell_checker/simple_spell_checker_service.dart';
import 'src/editor_toolbar_controller_shared/clipboard/super_clipboard_service.dart'; import 'src/editor_toolbar_controller_shared/clipboard/super_clipboard_service.dart';
export 'src/common/extensions/controller_ext.dart'; export 'src/common/extensions/controller_ext.dart';
@ -13,6 +16,7 @@ export 'src/editor/image/image_embed_types.dart';
export 'src/editor/image/image_web_embed.dart'; export 'src/editor/image/image_web_embed.dart';
export 'src/editor/image/models/image_configurations.dart'; export 'src/editor/image/models/image_configurations.dart';
export 'src/editor/image/models/image_web_configurations.dart'; export 'src/editor/image/models/image_web_configurations.dart';
export 'src/editor/spell_checker/simple_spell_checker_service.dart';
export 'src/editor/table/table_cell_embed.dart'; export 'src/editor/table/table_cell_embed.dart';
export 'src/editor/table/table_embed.dart'; export 'src/editor/table/table_embed.dart';
export 'src/editor/table/table_models.dart'; export 'src/editor/table/table_models.dart';
@ -39,6 +43,25 @@ export 'src/toolbar/video/video_button.dart';
class FlutterQuillExtensions { class FlutterQuillExtensions {
const FlutterQuillExtensions._(); const FlutterQuillExtensions._();
/// override the default implementation of [SpellCheckerServiceProvider]
/// to allow a `flutter quill` support a better check spelling
///
/// # !WARNING
/// To avoid memory leaks, ensure to use [dispose()] method to
/// close stream controllers that used by this custom implementation
/// when them no longer needed
///
/// Example:
///
///```dart
///// set partial true if you only need to close the controllers
///SpellCheckerServiceProvider.dispose(onlyPartial: false);
///```
static void useSpellCheckerService(String language) {
SpellCheckerServiceProvider.setNewCheckerService(
SimpleSpellCheckerService(language: language));
}
/// Override default implementation of [ClipboardServiceProvider.instance] /// Override default implementation of [ClipboardServiceProvider.instance]
/// to allow `flutter_quill` package to use `super_clipboard` plugin /// to allow `flutter_quill` package to use `super_clipboard` plugin
/// to support rich text features, gif and images. /// to support rich text features, gif and images.

@ -1,13 +1,13 @@
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:simple_spell_checker/simple_spell_checker.dart'; import 'package:simple_spell_checker/simple_spell_checker.dart';
import 'spellchecker_service.dart'; /// SimpleSpellChecker is a simple spell checker for get
/// SimpleSpellCheckerImpl is a simple spell checker for get
/// all words divide on different objects if them are wrong or not /// all words divide on different objects if them are wrong or not
class SimpleSpellCheckerImpl extends SpellcheckerService { class SimpleSpellCheckerService
SimpleSpellCheckerImpl({required super.language}) extends SpellCheckerService<LanguageIdentifier> {
SimpleSpellCheckerService({required super.language})
: checker = SimpleSpellChecker( : checker = SimpleSpellChecker(
language: language, language: language,
safeDictionaryLoad: true, safeDictionaryLoad: true,
@ -19,7 +19,7 @@ class SimpleSpellCheckerImpl extends SpellcheckerService {
final SimpleSpellChecker checker; final SimpleSpellChecker checker;
@override @override
List<TextSpan>? fetchSpellchecker( List<TextSpan>? checkSpelling(
String text, { String text, {
LongPressGestureRecognizer Function(String word)? LongPressGestureRecognizer Function(String word)?
customLongPressRecognizerOnWrongSpan, customLongPressRecognizerOnWrongSpan,
@ -37,6 +37,23 @@ class SimpleSpellCheckerImpl extends SpellcheckerService {
checker.disposeControllers(); checker.disposeControllers();
return; return;
} }
checker.dispose(closeDirectionary: true); checker.dispose();
}
@override
void addCustomLanguage({required languageIdentifier}) {
checker
..registerLanguage(languageIdentifier.language)
..addCustomLanguage(languageIdentifier);
}
@override
void setNewLanguageState({required String language}) {
checker.setNewLanguageToState(language);
}
@override
void updateCustomLanguageIfExist({required languageIdentifier}) {
checker.updateCustomLanguageIfExist(languageIdentifier);
} }
} }

@ -35,12 +35,13 @@ dependencies:
universal_html: ^2.2.4 universal_html: ^2.2.4
cross_file: ^0.3.3+6 cross_file: ^0.3.3+6
flutter_quill: ^10.0.0 flutter_quill: ^10.3.0
photo_view: ^0.15.0 photo_view: ^0.15.0
youtube_explode_dart: ^2.2.1 youtube_explode_dart: ^2.2.1
# Plugins # Plugins
video_player: ^2.8.1 video_player: ^2.8.1
simple_spell_checker: ^1.0.9
youtube_player_flutter: ^9.0.1 youtube_player_flutter: ^9.0.1
url_launcher: ^6.2.1 url_launcher: ^6.2.1
super_clipboard: ^0.8.15 super_clipboard: ^0.8.15

@ -24,7 +24,6 @@ export 'src/editor/raw_editor/config/raw_editor_configurations.dart';
export 'src/editor/raw_editor/quill_single_child_scroll_view.dart'; export 'src/editor/raw_editor/quill_single_child_scroll_view.dart';
export 'src/editor/raw_editor/raw_editor.dart'; export 'src/editor/raw_editor/raw_editor.dart';
export 'src/editor/raw_editor/raw_editor_state.dart'; export 'src/editor/raw_editor/raw_editor_state.dart';
export 'src/editor/spellchecker/simple_spellchecker_impl.dart';
export 'src/editor/spellchecker/spellchecker_service.dart'; export 'src/editor/spellchecker/spellchecker_service.dart';
export 'src/editor/spellchecker/spellchecker_service_provider.dart'; export 'src/editor/spellchecker/spellchecker_service_provider.dart';
export 'src/editor/style_widgets/style_widgets.dart'; export 'src/editor/style_widgets/style_widgets.dart';

@ -1,20 +1,31 @@
import 'package:flutter/gestures.dart' show LongPressGestureRecognizer; import 'package:flutter/gestures.dart' show LongPressGestureRecognizer;
import 'package:flutter/material.dart' show TextSpan; import 'package:flutter/material.dart' show TextSpan;
import 'spellchecker_service.dart' show SpellcheckerService; import 'spellchecker_service.dart' show SpellCheckerService;
/// A default implementation of the [SpellcheckerService] /// A default implementation of the [SpellcheckerService]
/// that always will return null since Spell checking /// that always will return null since Spell checking
/// is not a standard feature /// is not a standard feature
class DefaultSpellcheckerService extends SpellcheckerService { class DefaultSpellCheckerService extends SpellCheckerService<Object?> {
DefaultSpellcheckerService() : super(language: 'en'); DefaultSpellCheckerService() : super(language: 'en');
@override @override
void dispose({bool onlyPartial = false}) {} void dispose({bool onlyPartial = false}) {}
@override @override
List<TextSpan>? fetchSpellchecker(String text, List<TextSpan>? checkSpelling(
{LongPressGestureRecognizer Function(String p1)? String text, {
customLongPressRecognizerOnWrongSpan}) { LongPressGestureRecognizer Function(String p1)?
customLongPressRecognizerOnWrongSpan,
}) {
return null; return null;
} }
@override
void addCustomLanguage({languageIdentifier}) {}
@override
void setNewLanguageState({required String language}) {}
@override
void updateCustomLanguageIfExist({languageIdentifier}) {}
} }

@ -2,8 +2,8 @@ import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
/// A representation a custom SpellCheckService. /// A representation a custom SpellCheckService.
abstract class SpellcheckerService { abstract class SpellCheckerService<T> {
SpellcheckerService({required this.language}); SpellCheckerService({required this.language});
final String language; final String language;
@ -15,10 +15,19 @@ abstract class SpellcheckerService {
/// if [onlyPartial] is false dispose all resources /// if [onlyPartial] is false dispose all resources
void dispose({bool onlyPartial = false}); void dispose({bool onlyPartial = false});
/// set a new language state used for SpellcheckerService
void setNewLanguageState({required String language});
/// set a new language state used for SpellcheckerService
void updateCustomLanguageIfExist({required T languageIdentifier});
/// set a new custom language for SpellcheckerService
void addCustomLanguage({required T languageIdentifier});
/// Facilitates a spell check request. /// Facilitates a spell check request.
/// ///
/// Returns a [List<TextSpan>] with all misspelled words divide from the right words. /// Returns a [List<TextSpan>] with all misspelled words divide from the right words.
List<TextSpan>? fetchSpellchecker(String text, List<TextSpan>? checkSpelling(String text,
{LongPressGestureRecognizer Function(String)? {LongPressGestureRecognizer Function(String)?
customLongPressRecognizerOnWrongSpan}); customLongPressRecognizerOnWrongSpan});
} }

@ -3,17 +3,26 @@ import 'default_spellchecker_service.dart';
import 'spellchecker_service.dart'; import 'spellchecker_service.dart';
@immutable @immutable
class SpellcheckerServiceProvider { class SpellCheckerServiceProvider {
const SpellcheckerServiceProvider._(); const SpellCheckerServiceProvider._();
static SpellcheckerService _instance = DefaultSpellcheckerService(); static SpellCheckerService _instance = DefaultSpellCheckerService();
static SpellcheckerService get instance => _instance; static SpellCheckerService get instance => _instance;
static void setInstance(SpellcheckerService service) { static void setNewCheckerService(SpellCheckerService service) {
_instance = service; _instance = service;
} }
static void setInstanceToDefault() { static void dispose({bool onlyPartial = false}) {
_instance = DefaultSpellcheckerService(); _instance.dispose(onlyPartial: onlyPartial);
}
static void setNewLanguageState({required String language}) {
assert(language.isNotEmpty);
_instance.setNewLanguageState(language: language);
}
static void turnOffService() {
_instance = DefaultSpellCheckerService();
} }
} }

@ -408,8 +408,8 @@ class _TextLineState extends State<TextLine> {
!widget.readOnly && !widget.readOnly &&
!widget.line.style.attributes.containsKey('code-block') && !widget.line.style.attributes.containsKey('code-block') &&
!kIsWeb) { !kIsWeb) {
final service = SpellcheckerServiceProvider.instance; final service = SpellCheckerServiceProvider.instance;
final spellcheckedSpans = service.fetchSpellchecker(textNode.value); final spellcheckedSpans = service.checkSpelling(textNode.value);
if (spellcheckedSpans != null && spellcheckedSpans.isNotEmpty) { if (spellcheckedSpans != null && spellcheckedSpans.isNotEmpty) {
return TextSpan( return TextSpan(
children: spellcheckedSpans, children: spellcheckedSpans,

@ -45,7 +45,6 @@ dependencies:
# Dart Packages # Dart Packages
intl: ^0.19.0 intl: ^0.19.0
dart_quill_delta: ^10.0.0 dart_quill_delta: ^10.0.0
simple_spell_checker: ^1.0.6
collection: ^1.17.0 collection: ^1.17.0
quiver: ^3.2.1 quiver: ^3.2.1
equatable: ^2.0.5 equatable: ^2.0.5
@ -55,7 +54,7 @@ dependencies:
flutter_colorpicker: ^1.1.0 flutter_colorpicker: ^1.1.0
# For converting HTML to Quill delta # For converting HTML to Quill delta
flutter_quill_delta_from_html: ^1.3.13 flutter_quill_delta_from_html: ^1.4.0
markdown: ^7.2.1 markdown: ^7.2.1
charcode: ^1.3.1 charcode: ^1.3.1

Loading…
Cancel
Save