diff --git a/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/JsBridge.kt b/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/JsBridge.kt index 786a12a..c8545ae 100644 --- a/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/JsBridge.kt +++ b/rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/JsBridge.kt @@ -41,6 +41,10 @@ internal class JsBridge( fun removeFormat() = execCommand(OtherCommand.REMOVE_FORMAT) + fun setTextColor(@ColorInt color: Int) = execCommand(StatusCommand.TEXT_COLOR, colorToRgbHex(color)) + + fun setTextBackgroundColor(@ColorInt color: Int) = execCommand(StatusCommand.BACKGROUND_COLOR, colorToRgbHex(color)) + fun createLink(displayText: String?, url: String) { jsExecutor.executeImmediatelyAndRefreshToolbar(JsExecutableMethod("createLink", displayText, url)) } @@ -68,6 +72,9 @@ internal class JsBridge( return Color.argb(255, r.toInt(), g.toInt(), b.toInt()) } + @OptIn(ExperimentalStdlibApi::class) + private fun colorToRgbHex(color: Int) = color.toHexString(HexFormat.UpperCase).takeLast(6) + @JavascriptInterface fun reportCommandDataChange( isBold: Boolean, 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 67dad61..7057cf4 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 @@ -7,6 +7,7 @@ import android.util.AttributeSet import android.view.ViewGroup import android.webkit.WebView import android.webkit.WebViewClient +import androidx.annotation.ColorInt import androidx.core.view.updateLayoutParams import com.infomaniak.lib.richhtmleditor.executor.JsExecutableMethod import com.infomaniak.lib.richhtmleditor.executor.JsExecutor @@ -116,6 +117,8 @@ class RichHtmlEditorWebView @JvmOverloads constructor( fun toggleStrikeThrough() = jsBridge.toggleStrikeThrough() fun toggleUnderline() = jsBridge.toggleUnderline() fun removeFormat() = jsBridge.removeFormat() + fun setTextColor(@ColorInt color: Int) = jsBridge.setTextColor(color) + fun setTextBackgroundColor(@ColorInt color: Int) = jsBridge.setTextBackgroundColor(color) fun createLink(displayText: String?, url: String) = jsBridge.createLink(displayText, url) fun unlink() = jsBridge.unlink() 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 6cf28c5..3129284 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 @@ -1,6 +1,7 @@ package com.infomaniak.lib.richhtmleditor.sample import android.app.Activity +import android.graphics.Color import android.os.Bundle import android.util.Log import android.view.LayoutInflater @@ -74,6 +75,11 @@ class EditorSampleFragment : Fragment() { inputMethodManager.hideSoftInputFromWindow(editor.windowToken, 0) } focusEditorButton.setOnClickListener { editor.requestFocusAndOpenKeyboard() } + + textColorRed.setOnClickListener { editor.setTextColor(Color.parseColor("#FF0000")) } + textColorBlue.setOnClickListener { editor.setTextColor(Color.parseColor("#0000FF")) } + textBackgroundColorRed.setOnClickListener { editor.setTextBackgroundColor(Color.parseColor("#FF0000")) } + textBackgroundColorBlue.setOnClickListener { editor.setTextBackgroundColor(Color.parseColor("#0000FF")) } } private fun observeEditorStatusUpdates() = with(binding) { @@ -101,8 +107,9 @@ class EditorSampleFragment : Fragment() { .use(BufferedReader::readText) } - private fun setToolbarEnabledStatus(isEnabled: Boolean) { - binding.toolbarLayout.forEach { view -> view.isEnabled = isEnabled } + private fun setToolbarEnabledStatus(isEnabled: Boolean) = with(binding) { + toolbarLayout.forEach { view -> view.isEnabled = isEnabled } + colorLayout.forEach { view -> view.isEnabled = isEnabled } } inner class CreateLinkDialog { diff --git a/sample/src/main/res/layout/fragment_editor_sample.xml b/sample/src/main/res/layout/fragment_editor_sample.xml index ad7bfaf..ef526e4 100644 --- a/sample/src/main/res/layout/fragment_editor_sample.xml +++ b/sample/src/main/res/layout/fragment_editor_sample.xml @@ -105,6 +105,72 @@ + + + + + + + + + + + + + + + + + + + + + +