-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore!: move spell checker to example (#2145)
- Loading branch information
Showing
9 changed files
with
230 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# 📝 Spelling checker | ||
|
||
A spell checker is a software tool or feature integrated into various text processing applications that automatically identifies and corrects spelling errors in a written document. It works by comparing the words in the text against a built-in dictionary. If a word isn't found in the dictionary or doesn't match any known word patterns, the spell checker highlights it as a potential error. | ||
|
||
While spell-checking is not a feature that's implemented into the project, it can be used using external dependencies. | ||
|
||
It's implemented using the package `simple_spell_checker` in the [Example](../example/). | ||
|
||
> [!NOTE] | ||
> [`simple_spell_checker`](https://pub.dev/packages/simple_spell_checker) is a client-side dependency that works without an internet connection, so, it could weigh more than expected due to each of the dictionaries. As mentioned below, it supports a very wide variety of languages which can have a file of up to 300.000 words (this being just one language). | ||
### Benefits of a spell checker include: | ||
|
||
* Improved Accuracy: It helps writers avoid common spelling mistakes, ensuring that the text is free of errors. | ||
* Time-Saving: Automatically detecting errors reduces the time needed for manual proofreading. | ||
* Enhanced Professionalism: Correctly spelled words contribute to the overall professionalism of documents, which is crucial in academic, business, and formal writing. | ||
* Multilingual Support: Many spell checkers support multiple languages, making it easier for users to write accurately in different languages. | ||
|
||
> [!IMPORTANT] | ||
> The spell checker usually does not work as expected in most cases. For now it is a purely **experimental** feature that may have **code that will be modified** in future versions. | ||
### The translations supported so far are: | ||
|
||
* German - `de`, `de-ch` | ||
* English - `en`, `en-gb` | ||
* Spanish - `es` | ||
* Catalan - `ca` | ||
* Arabic - `ar` | ||
* Danish - `da` | ||
* French - `fr` | ||
* Bulgarian - `bg` | ||
* Dutch - `nl` | ||
* Korean - `ko` | ||
* Estonian - `et` | ||
* Hebrew - `he` | ||
* Slovak - `sk` | ||
* Italian - `it` | ||
* Norwegian - `no` | ||
* Portuguese - `pt` | ||
* Swedish - `sv` | ||
* Russian - `ru` | ||
|
||
_**Note**: If you have knowledge about any of these available languages or the unsupported ones, you can make a pull request to add support or add words that are not currently in [simple_spell_checker](https://github.com/CatHood0/simple_spell_checker)_. | ||
|
||
In order to activate this functionality you can use the following code: | ||
|
||
```dart | ||
// you can use the language of your preference or directly select the language of the operating system | ||
final language = 'en'; // or Localizations.localeOf(context).languageCode | ||
SpellChecker.useSpellCheckerService(language); | ||
``` | ||
|
||
> [!NOTE] | ||
> The class `SpellChecker` is not available as part of the project API. Instead, you will have to implement it manually. Take a look at the example [Spell Checker](../example/lib/spell_checker/spell_checker.dart) class. | ||
When you no longer need to have the Spell checker activated you can simply use `dispose()` of the `SpellCheckerServiceProvider` class: | ||
|
||
```dart | ||
// dispose all service and it cannot be used after this | ||
SpellCheckerServiceProvider.dispose(); | ||
``` | ||
|
||
If what we want is to **close the StreamControllers** without deleting the values that are already stored in it, we can set `onlyPartial` to `true`. | ||
|
||
```dart | ||
// it can be still used by the editor | ||
SpellCheckerServiceProvider.dispose(onlyPartial: true); | ||
``` | ||
|
||
One use of this would be having the opportunity to **activate and deactivate** the service when we want, we can see this in the example that we have in this package, in which you can see that on each screen, we have a button that dynamically activates and deactivates the service. To do this is pretty simple: | ||
|
||
```dart | ||
SpellCheckerServiceProvider.toggleState(); | ||
// use isServiceActive to get the state of the service | ||
SpellCheckerServiceProvider.isServiceActive(); | ||
setState(() {}); | ||
``` | ||
|
||
Open this [page](https://pub.dev/packages/simple_spell_checker) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
example/lib/spell_checker/simple_spell_checker_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_quill/flutter_quill.dart'; | ||
import 'package:simple_spell_checker/simple_spell_checker.dart'; | ||
|
||
/// SimpleSpellChecker is a simple spell checker for get | ||
/// all words divide on different objects if them are wrong or not | ||
class SimpleSpellCheckerService | ||
extends SpellCheckerService<LanguageIdentifier> { | ||
SimpleSpellCheckerService({required super.language}) | ||
: checker = SimpleSpellChecker( | ||
language: language, | ||
safeDictionaryLoad: true, | ||
); | ||
|
||
/// [SimpleSpellChecker] comes from the package [simple_spell_checker] | ||
/// that give us all necessary methods for get our spans with highlighting | ||
/// where needed | ||
final SimpleSpellChecker checker; | ||
|
||
@override | ||
List<TextSpan>? checkSpelling( | ||
String text, { | ||
LongPressGestureRecognizer Function(String word)? | ||
customLongPressRecognizerOnWrongSpan, | ||
}) { | ||
return checker.check( | ||
text, | ||
customLongPressRecognizerOnWrongSpan: | ||
customLongPressRecognizerOnWrongSpan, | ||
); | ||
} | ||
|
||
@override | ||
void toggleChecker() => checker.toggleChecker(); | ||
|
||
@override | ||
bool isServiceActive() => checker.isCheckerActive(); | ||
|
||
@override | ||
void dispose({bool onlyPartial = false}) { | ||
if (onlyPartial) { | ||
checker.disposeControllers(); | ||
return; | ||
} | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:flutter_quill/flutter_quill.dart'; | ||
|
||
import 'simple_spell_checker_service.dart'; | ||
|
||
class SpellChecker { | ||
SpellChecker._(); | ||
|
||
/// 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), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.