RichTextState

class RichTextState(initialText: <Error class: unknown class> = RichString(""), initialSelection: TextRange = TextRange(initialText.text.length))

A state object that can be hoisted to control and observe changes to RichTextEditor.

This state manages the underlying RichString, including the raw text and its associated formatting attributes (RichSpans). It acts as the single source of truth for the editor, integrating seamlessly with standard Compose text APIs.

Parameters

initialText

The initial RichString to display in the editor. Defaults to an empty RichString.

initialSelection

The initial TextRange selection or cursor position. Defaults to the end of initialText.

Constructors

Link copied to clipboard
constructor(initialText: <Error class: unknown class> = RichString(""), initialSelection: TextRange = TextRange(initialText.text.length))

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val currentAttributes: <Error class: unknown class>

The merged attributes at the current cursor position.

Link copied to clipboard
val richString: <Error class: unknown class>
Link copied to clipboard

The current selection range within the text field. Returns TextRange.Zero when no selection is active (cursor at position 0).

Link copied to clipboard
var typingAttributes: <Error class: unknown class>?

Attributes that will be applied to the next character typed via the keyboard.

Link copied to clipboard

Manages the undo and redo history for this state. Use this property to programmatically trigger undo/redo operations or observe their availability.

Functions

Link copied to clipboard
fun RichTextState.applyCompletion(match: AutocompleteMatch, replacement: <Error class: unknown class>)

Replaces the range matched by match with the given RichString, preserving its styling attributes and positioning the cursor immediately after the replacement.

Replaces the range matched by match with the given replacement plain text, and positions the cursor immediately after the replacement.

fun RichTextState.applyCompletion(match: AutocompleteMatch, replacement: String, attributes: <Error class: unknown class>)

Replaces the range matched by match with the given replacement text, applies attributes using the specified attributes to the replacement, and positions the cursor immediately after it.

Link copied to clipboard
fun <T : Any> RichTextState.applyFormat(key: <Error class: unknown class><T>, value: T)

Applies the given value for a ParagraphAttributeKey.

fun <T : Any> RichTextState.applyFormat(key: <Error class: unknown class><T>, value: T)

Applies the given value for a SpanAttributeKey.

Link copied to clipboard

Clears all currently active formats.

Link copied to clipboard

Clears all typing attributes, reverting to the cursor's inherited attributes.

Link copied to clipboard

Scans the current text for URLs and applies LinkKey attributes to them.

Link copied to clipboard
fun edit(block: RichTextBuffer.() -> Unit)

Edits the underlying RichString state using a builder DSL. This allows you to apply or remove multiple attributes within a RichTextBuffer, as well as insert, delete, or replace text.

Link copied to clipboard
fun RichTextState.removeFormat(key: <Error class: unknown class><out <Error class: unknown class>>)

Removes the format associated with the given key.

Link copied to clipboard
fun <T> removeTypingAttribute(key: <Error class: unknown class><T>)

Removes a single attribute from the current typing attributes. Has no effect when a text range is selected (non-collapsed selection).

Link copied to clipboard
fun setRichString(richString: <Error class: unknown class>)

Replaces the entire text and formatting attributes of this state with the given richString.

Link copied to clipboard
fun <T> setTypingAttribute(key: <Error class: unknown class><T>, value: T)

Adds or overwrites a single attribute in the current typing attributes. Has no effect when a text range is selected (non-collapsed selection).

Link copied to clipboard
fun RichTextState.toggleFormat(key: <Error class: unknown class><Unit>)

Toggles a ParagraphAttributeKey that does not require a value (i.e. its value is Unit).

fun RichTextState.toggleFormat(key: <Error class: unknown class><Unit>)

Toggles a SpanAttributeKey that does not require a value (i.e. its value is Unit).