Loading repository data…
Loading repository data…
Infomaniak / repository
A WYSIWYG text editor written in Kotlin for Android. Based on HTML contenteditable. Used in the Infomaniak Mail application.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
The Infomaniak Rich HTML Editor is an Android library designed to display HTML and easily modify it on the fly. It relies on
the power of the contenteditable HTML attribute inside a WebView.
Looking for an iOS or macOS equivalent? Check out the Swift version of this editor here: Infomaniak/swift-rich-html-editor
To modify the library, don't forget to update the api.txt file with metalava's gradle task ./gradlew metalavaGenerateSignatureRelease
Add this dependency to your project:
Using version catalog:
rich-html-editor = { module = "com.github.infomaniak:android-rich-html-editor", version.ref = "richHtmlEditorVersion" }
Directly inside gradle dependencies:
dependencies {
implementation "com.github.infomaniak:android-rich-html-editor:$richHtmlEditorVersion"
}
The latest version is:
In your layout:
<com.infomaniak.lib.richhtmleditor.RichHtmlEditorWebView
android:id="@+id/editor"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
In your fragment:
val html = """
<html>
<body>
<h1>Hello World</h1>
</body>
</html>
""".trimMargin()
editor.setHtml(html)
Add a button to your xml:
<Button
android:id="@+id/boldButton"
android:text="Bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Set a click listener on the button to apply the associated format and modify the appearance of the button to report the status where the cursor is placed:
boldButton.setOnClickListener { editor.toggleBold() }
viewLifecycleOwner.lifecycleScope.launch {
editor.editorStatusesFlow.collect {
// Update your button's activation status how you prefer. If you have provided
// an activated state color for your button, you can do the following:
boldButton.isActivated = it.isBold
}
}
[!TIP] If you want to listen to the status changes of a format, do not forget to subscribe to it as described in the following Subscribe to format states section.
You can subscribe only to the states that are relevant to you. This way, the flow will update its value only when at least one of your subscribed states changes.
editor.subscribeToStates(setOf(StatusCommand.BOLD, StatusCommand.ITALIC))
[!NOTE] By default, when
subscribeToStates()is never called, all available StatusCommand will be subscribed to.You can subscribe to all possible states by passing in
nulleditor.subscribeToStates(null)
You can add your own css to stylize your editor to your liking or you can add scripts to develop your own advanced functionalities. To do so, the editor exposes two easy to use methods.
editor.apply {
addCss("div { padding: 8px }")
addScript("document.body.style['background'] = '#00FFFF'")
}
You might want to define some custom scripts that exposes functions for you to call later on in your logic. If you need to make
sure the script is loaded before calling its methods, use executeJsMethodWhenEditorIsSetup()
// When setting up the editor add a script that will be loaded inside the <head>
editor.addScript(
"""
function setBackground(color) {
document.body.style['background'] = color
}
""".trimIndent()
)
// When you need to execute the method from the above script inside some logic or button click
editor.executeJsMethodWhenEditorIsSetup(JsExecutableMethod("setBackground", "#00FFFF"))
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.
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.
If you need to access the editor inside your custom javascript code, you can call the globally available getEditor() method.
This is the prefered way over trying to access the div of the editor by its ID directly.
If you need to retrieve the user selection inside the editor, you can call the getSelectedText method.
This returns the selected text in a String. If the user didn't select anything, an empty string will be returned.
For more advanced features, take a look at the sample project or the Infomaniak Mail app that uses this rich html editor.
Useful methods and classes inside the project are documented with KDoc.
You can find a sample project in the sample folder.
Infomaniak Mail allows you to manage your Infomaniak addresses in a completely secure environment.