From 74727aa8d2911050aee56c3bc4487eb7000c1074 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sat, 16 Nov 2024 16:02:16 -0800 Subject: [PATCH 1/3] 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( From 2254b20ac8ef12eff9bd953cead5556e4eb0ff1e Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sat, 16 Nov 2024 21:57:12 -0800 Subject: [PATCH 2/3] API --- richeditor-compose/api/android/richeditor-compose.api | 1 + richeditor-compose/api/desktop/richeditor-compose.api | 1 + richeditor-compose/api/richeditor-compose.klib.api | 2 ++ 3 files changed, 4 insertions(+) diff --git a/richeditor-compose/api/android/richeditor-compose.api b/richeditor-compose/api/android/richeditor-compose.api index d22b06a1..cdc258dc 100644 --- a/richeditor-compose/api/android/richeditor-compose.api +++ b/richeditor-compose/api/android/richeditor-compose.api @@ -155,6 +155,7 @@ public final class com/mohamedrejeb/richeditor/model/RichTextState { public final fun getSelectedLinkUrl ()Ljava/lang/String; public final fun getSelection-d9O1mEE ()J public final fun getSpanStyle-5zc-tL8 (J)Landroidx/compose/ui/text/SpanStyle; + public final fun getTextChanges ()Lkotlinx/coroutines/flow/SharedFlow; public final fun isCode ()Z public final fun isCodeSpan ()Z public final fun isLink ()Z diff --git a/richeditor-compose/api/desktop/richeditor-compose.api b/richeditor-compose/api/desktop/richeditor-compose.api index 80975eaf..c6d52461 100644 --- a/richeditor-compose/api/desktop/richeditor-compose.api +++ b/richeditor-compose/api/desktop/richeditor-compose.api @@ -155,6 +155,7 @@ public final class com/mohamedrejeb/richeditor/model/RichTextState { public final fun getSelectedLinkUrl ()Ljava/lang/String; public final fun getSelection-d9O1mEE ()J public final fun getSpanStyle-5zc-tL8 (J)Landroidx/compose/ui/text/SpanStyle; + public final fun getTextChanges ()Lkotlinx/coroutines/flow/SharedFlow; public final fun isCode ()Z public final fun isCodeSpan ()Z public final fun isLink ()Z diff --git a/richeditor-compose/api/richeditor-compose.klib.api b/richeditor-compose/api/richeditor-compose.klib.api index ebc35522..d9faffe9 100644 --- a/richeditor-compose/api/richeditor-compose.klib.api +++ b/richeditor-compose/api/richeditor-compose.klib.api @@ -165,6 +165,8 @@ final class com.mohamedrejeb.richeditor.model/RichTextState { // com.mohamedreje final fun (): kotlin/String? // com.mohamedrejeb.richeditor.model/RichTextState.selectedLinkText.|(){}[0] final val selectedLinkUrl // com.mohamedrejeb.richeditor.model/RichTextState.selectedLinkUrl|{}selectedLinkUrl[0] final fun (): kotlin/String? // com.mohamedrejeb.richeditor.model/RichTextState.selectedLinkUrl.|(){}[0] + final val textChanges // com.mohamedrejeb.richeditor.model/RichTextState.textChanges|{}textChanges[0] + final fun (): kotlinx.coroutines.flow/SharedFlow // com.mohamedrejeb.richeditor.model/RichTextState.textChanges.|(){}[0] final var annotatedString // com.mohamedrejeb.richeditor.model/RichTextState.annotatedString|{}annotatedString[0] final fun (): androidx.compose.ui.text/AnnotatedString // com.mohamedrejeb.richeditor.model/RichTextState.annotatedString.|(){}[0] From ad23f9dabbc3360964d7efbb6100f095c4793862 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 18 Nov 2024 23:47:27 -0800 Subject: [PATCH 3/3] Clean up --- .../richeditor/sample/common/richeditor/RichEditorContent.kt | 2 -- 1 file changed, 2 deletions(-) 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 49442d0d..80197535 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 @@ -31,12 +31,10 @@ fun RichEditorContent() { LaunchedEffect(Unit) { scope.launch { - println("Start collecting...") outlinedRichTextState.textChanges.collect { state -> println("Text changed!") println(state.toText()) } - println("Collection finished!") } }