Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spell Checking #428

Open
wants to merge 86 commits into
base: main
Choose a base branch
from
Open

Conversation

Wavesonics
Copy link
Contributor

@Wavesonics Wavesonics commented Nov 14, 2024

Update: I've been working on this one and it is actually pretty much ready now I think.

It creates a new module: richeditor-compose-spellcheck which contains all of the common spell checking code, imports my SymSpellKt library, and ultimately provides a new composable: SpellCheckedRichTextEditor which automatically provides spell checking as you type.

You still need to provide it with a SpellChecker which requires loading a dictionary, and this has to be up to the application as it probably shouldn't be done inside the UI as it is done in the sample. Loading the dictionary is expensive and slow and should be done at the application level.

It also adds a new screen to the sample that implements spell checking fully.

The only "but" here is that it uses a fairly naive algorithm (if you can call it that) to scan for new misspellings. Because BasicTextField does not give us any info when it updates on WHAT changed, only that something changed, I haven't yet found a very efficient way to invalidate only the SpellCheck spans which contain the new edit.

As such, I invalidate all SpellCheck spans and fully rescan the text for new misspellings. To this end, I went to great lengths to make that scanning as efficient as possible. I find the individual words to spell check by walking the RichSpan tree rather than flattening the whole tree to a String and segmenting off of that. This reduces the required memory allocations be at least 1 full copy of the text.

SymSpell it's self is also incredibly efficient, so the cost of scanning each word is very minimal.

I think what is here is more than sufficient for a v1 of our spell checker, and I can continue to brain storm how we can be more efficient.

As mentioned below, this relies on #194 to be able to handle the clicks on the SpellCheck spans, and #432 to know when to scan for changes.


This is not production ready by any means yet, but I wanted to see how close I was to finally getting Spell Checking working.

The answer is: pretty darn close!

spell-check.mp4

This relies on #194 to be able to handle the clicks on the SpellCheck spans, and #432 to know when to scan for changes.

One problem currently is that when the spellcheck correction is applied, any existing rich text span is lost.

@Wavesonics Wavesonics marked this pull request as draft November 14, 2024 06:00
@Wavesonics Wavesonics force-pushed the spell-check-test branch 2 times, most recently from e9a772d to 4d3dc84 Compare November 18, 2024 02:44
@Wavesonics Wavesonics changed the title Spell check Spell Checking Nov 19, 2024
This update introduces a callback function `onRichSpanClick` to allow apps to handle clicks on RichSpans in the RichTextEditor. The updates also modify the visibility of some classes and properties to support this- `RichSpan`, `ParagraphType` and `getRichSpanByOffset` method in `RichTextState` are now publicly visible.
cleanup

# Conflicts:
#	sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt
voyager = "1.1.0-beta03"
richeditor = "1.0.0-rc10"
coroutines = "1.9.0"
ktor = "3.0.1"
android-minSdk = "21"
android-minSdk = "26"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't install APKs that have min SDK set below 26 on devices running SDK 35+

@Wavesonics Wavesonics marked this pull request as ready for review November 29, 2024 10:27
It will be required by anyone who uses this module
This is smaller and faster to load and parse
# Conflicts:
#	richeditor-compose/api/android/richeditor-compose.api
#	richeditor-compose/api/desktop/richeditor-compose.api
#	richeditor-compose/api/richeditor-compose.klib.api
#	richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/BasicRichTextEditor.kt
#	richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/material/OutlinedRichTextEditor.kt
#	richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/material/RichTextEditor.kt
#	richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/material3/OutlinedRichTextEditor.kt
#	sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt
@Wavesonics Wavesonics force-pushed the spell-check-test branch 2 times, most recently from c462fb6 to c057947 Compare December 6, 2024 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants