Skip to content

Commit

Permalink
Added textChanges SharedFlow to follow text changes
Browse files Browse the repository at this point in the history
cleanup
  • Loading branch information
Wavesonics committed Nov 17, 2024
1 parent bd12754 commit 2951cc0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -54,6 +55,16 @@ public class RichTextState internal constructor(
internal val inlineContentMap = mutableStateMapOf<String, InlineTextContent>()
internal val usedInlineContentMapKeys = mutableSetOf<String>()

private val _textChanges = MutableSharedFlow<RichTextState>(
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)
public val textChanges: SharedFlow<RichTextState> = _textChanges

private fun emitTextChange() {
_textChanges.tryEmit(this)
}

/**
* The annotated string representing the rich text.
*/
Expand Down Expand Up @@ -1137,6 +1148,8 @@ public class RichTextState internal constructor(

// Update text field value
updateTextFieldValue()

emitTextChange()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 2951cc0

Please sign in to comment.