From 2951cc0eb75554b24133bb7f9732c47cc98bb962 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sat, 16 Nov 2024 16:02:16 -0800 Subject: [PATCH] Added textChanges SharedFlow to follow text changes cleanup --- .../richeditor/model/RichTextState.kt | 13 +++++++++++++ .../common/richeditor/RichEditorContent.kt | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt index 5dc5e0f4..e43fecc0 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt @@ -26,6 +26,7 @@ import com.mohamedrejeb.richeditor.parser.html.RichTextStateHtmlParser import com.mohamedrejeb.richeditor.parser.markdown.RichTextStateMarkdownParser import com.mohamedrejeb.richeditor.utils.* import kotlinx.coroutines.Job +import kotlinx.coroutines.channels.* import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.flow.* @@ -54,6 +55,16 @@ public class RichTextState internal constructor( internal val inlineContentMap = mutableStateMapOf() internal val usedInlineContentMapKeys = mutableSetOf() + private val _textChanges = MutableSharedFlow( + extraBufferCapacity = 1, + onBufferOverflow = BufferOverflow.DROP_OLDEST + ) + public val textChanges: SharedFlow = _textChanges + + private fun emitTextChange() { + _textChanges.tryEmit(this) + } + /** * The annotated string representing the rich text. */ @@ -1137,6 +1148,8 @@ public class RichTextState internal constructor( // Update text field value updateTextFieldValue() + + emitTextChange() } /** diff --git a/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt b/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt index 4bf028eb..49442d0d 100644 --- a/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt +++ b/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt @@ -5,8 +5,7 @@ import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material3.* -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import cafe.adriel.voyager.navigator.LocalNavigator @@ -18,6 +17,7 @@ import com.mohamedrejeb.richeditor.ui.BasicRichTextEditor import com.mohamedrejeb.richeditor.ui.material3.OutlinedRichTextEditor import com.mohamedrejeb.richeditor.ui.material3.RichText import com.mohamedrejeb.richeditor.ui.material3.RichTextEditor +import kotlinx.coroutines.* @OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class) @Composable @@ -27,6 +27,18 @@ fun RichEditorContent() { val basicRichTextState = rememberRichTextState() val richTextState = rememberRichTextState() val outlinedRichTextState = rememberRichTextState() + val scope = rememberCoroutineScope() + + LaunchedEffect(Unit) { + scope.launch { + println("Start collecting...") + outlinedRichTextState.textChanges.collect { state -> + println("Text changed!") + println(state.toText()) + } + println("Collection finished!") + } + } LaunchedEffect(Unit) { richTextState.setHtml(