Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added textChanges SharedFlow to follow text changes #432

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions richeditor-compose/api/android/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions richeditor-compose/api/desktop/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions richeditor-compose/api/richeditor-compose.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ final class com.mohamedrejeb.richeditor.model/RichTextState { // com.mohamedreje
final fun <get-selectedLinkText>(): kotlin/String? // com.mohamedrejeb.richeditor.model/RichTextState.selectedLinkText.<get-selectedLinkText>|<get-selectedLinkText>(){}[0]
final val selectedLinkUrl // com.mohamedrejeb.richeditor.model/RichTextState.selectedLinkUrl|{}selectedLinkUrl[0]
final fun <get-selectedLinkUrl>(): kotlin/String? // com.mohamedrejeb.richeditor.model/RichTextState.selectedLinkUrl.<get-selectedLinkUrl>|<get-selectedLinkUrl>(){}[0]
final val textChanges // com.mohamedrejeb.richeditor.model/RichTextState.textChanges|{}textChanges[0]
final fun <get-textChanges>(): kotlinx.coroutines.flow/SharedFlow<com.mohamedrejeb.richeditor.model/RichTextState> // com.mohamedrejeb.richeditor.model/RichTextState.textChanges.<get-textChanges>|<get-textChanges>(){}[0]

final var annotatedString // com.mohamedrejeb.richeditor.model/RichTextState.annotatedString|{}annotatedString[0]
final fun <get-annotatedString>(): androidx.compose.ui.text/AnnotatedString // com.mohamedrejeb.richeditor.model/RichTextState.annotatedString.<get-annotatedString>|<get-annotatedString>(){}[0]
Expand Down
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,16 @@ fun RichEditorContent() {
val basicRichTextState = rememberRichTextState()
val richTextState = rememberRichTextState()
val outlinedRichTextState = rememberRichTextState()
val scope = rememberCoroutineScope()

LaunchedEffect(Unit) {
scope.launch {
outlinedRichTextState.textChanges.collect { state ->
println("Text changed!")
println(state.toText())
}
}
}

LaunchedEffect(Unit) {
richTextState.setHtml(
Expand Down
Loading