diff --git a/README.md b/README.md index cb0ee25..358147f 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,15 @@ editor.apply { To safely use your own WebViewClient instance with the editor, you have to call the RichHtmlEditorWebView's `notifyPageHasLoaded()` inside your custom WebViewClient's `onPageFinished()` callback. +### Activate/Deactivate spellcheck + +Spellcheck is activated by default but if you don't want to have any spellcheck done on the editor content, +you can do that by doing so. + +```kt +editor.withSpellCheck(false) +``` + ### More advanced features For more advanced features, take a look at the [sample project](sample) or diff --git a/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/RichHtmlEditorWebView.kt b/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/RichHtmlEditorWebView.kt index 11a056a..719028a 100644 --- a/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/RichHtmlEditorWebView.kt +++ b/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/RichHtmlEditorWebView.kt @@ -40,6 +40,7 @@ import com.infomaniak.lib.richhtmleditor.executor.KeyboardOpener import com.infomaniak.lib.richhtmleditor.executor.ScriptCssInjector import com.infomaniak.lib.richhtmleditor.executor.ScriptCssInjector.CodeInjection import com.infomaniak.lib.richhtmleditor.executor.ScriptCssInjector.CodeInjection.InjectionType +import com.infomaniak.lib.richhtmleditor.executor.SpellCheckHtmlSetter import com.infomaniak.lib.richhtmleditor.executor.StateSubscriber import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope @@ -83,6 +84,7 @@ class RichHtmlEditorWebView @JvmOverloads constructor( private val documentInitializer = DocumentInitializer() private val stateSubscriber = StateSubscriber(this) private val htmlSetter = HtmlSetter(this) + private val spellCheckHtmlSetter = SpellCheckHtmlSetter(this) private val jsExecutor = JsExecutor(this) private val scriptCssInjector = ScriptCssInjector(this) private val keyboardOpener = KeyboardOpener(this) @@ -134,6 +136,8 @@ class RichHtmlEditorWebView @JvmOverloads constructor( webViewClient = RichHtmlEditorWebViewClient(::notifyPageHasLoaded) + withSpellCheck(true) + addJavascriptInterface(jsBridge, "editor") stateSubscriber.executeWhenDomIsLoaded(null) @@ -171,6 +175,11 @@ class RichHtmlEditorWebView @JvmOverloads constructor( */ fun addCss(css: String) = scriptCssInjector.executeWhenDomIsLoaded(CodeInjection(InjectionType.CSS, css)) + /** + * Injects a custom attribute to the editor div to activate/deactivate spellchecking. By default, spellcheck is activated. + */ + fun withSpellCheck(enable: Boolean) = spellCheckHtmlSetter.executeWhenDomIsLoaded(enable) + /** * Injects a custom script tag into the `` of the editor template. * @@ -224,6 +233,7 @@ class RichHtmlEditorWebView @JvmOverloads constructor( stateSubscriber.notifyDomLoaded() htmlSetter.notifyDomLoaded() + spellCheckHtmlSetter.notifyDomLoaded() jsExecutor.notifyDomLoaded() scriptCssInjector.notifyDomLoaded() keyboardOpener.notifyDomLoaded() diff --git a/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/executor/SpellCheckHtmlSetter.kt b/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/executor/SpellCheckHtmlSetter.kt new file mode 100644 index 0000000..60471e1 --- /dev/null +++ b/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/executor/SpellCheckHtmlSetter.kt @@ -0,0 +1,35 @@ +/* + * Infomaniak Rich HTML Editor - Android + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.lib.richhtmleditor.executor + +import android.webkit.WebView + +internal class SpellCheckHtmlSetter(private val webView: WebView) : JsLifecycleAwareExecutor() { + + override fun executeImmediately(value: Boolean) = webView.insertSpellCheckValue(value) + + private fun WebView.insertSpellCheckValue(enable: Boolean) { + val spellCheckValue = if (enable) "true" else "false" + evaluateJavascript("""document.getElementById("$EDITOR_ID").setAttribute("spellcheck", $spellCheckValue)""", null) + } + + companion object { + // The id of this HTML tag is shared across multiple files and needs to remain the same + private const val EDITOR_ID = "editor" + } +} diff --git a/sample/src/main/java/com/infomaniak/lib/richhtmleditor/sample/EditorSampleFragment.kt b/sample/src/main/java/com/infomaniak/lib/richhtmleditor/sample/EditorSampleFragment.kt index a7da345..cd7b6cb 100644 --- a/sample/src/main/java/com/infomaniak/lib/richhtmleditor/sample/EditorSampleFragment.kt +++ b/sample/src/main/java/com/infomaniak/lib/richhtmleditor/sample/EditorSampleFragment.kt @@ -64,6 +64,7 @@ class EditorSampleFragment : Fragment() { // addCss(readAsset("editor_custom_css.css")) // addScript("document.body.style['background'] = '#00FFFF'") // subscribeToStates(setOf(StatusCommand.BOLD, StatusCommand.ITALIC)) + // withSpellCheck(false) isVisible = true setOnFocusChangeListener { _, hasFocus -> setToolbarEnabledStatus(hasFocus) }