Skip to content

Commit

Permalink
Support clearing spans
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Jul 29, 2024
1 parent 80545e1 commit 3c584bf
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
4 changes: 4 additions & 0 deletions richeditor-compose/api/android/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ public final class com/mohamedrejeb/richeditor/model/RichTextState {
public final fun addSpanStyle-FDrldGo (Landroidx/compose/ui/text/SpanStyle;J)V
public final fun addUnorderedList ()V
public final fun clear ()V
public final fun clearRichSpans ()V
public final fun clearRichSpans-5zc-tL8 (J)V
public final fun clearSpanStyles ()V
public final fun clearSpanStyles-5zc-tL8 (J)V
public final fun copy ()Lcom/mohamedrejeb/richeditor/model/RichTextState;
public final fun getAnnotatedString ()Landroidx/compose/ui/text/AnnotatedString;
public final fun getComposition-MzsxiRA ()Landroidx/compose/ui/text/TextRange;
Expand Down
4 changes: 4 additions & 0 deletions richeditor-compose/api/desktop/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ public final class com/mohamedrejeb/richeditor/model/RichTextState {
public final fun addSpanStyle-FDrldGo (Landroidx/compose/ui/text/SpanStyle;J)V
public final fun addUnorderedList ()V
public final fun clear ()V
public final fun clearRichSpans ()V
public final fun clearRichSpans-5zc-tL8 (J)V
public final fun clearSpanStyles ()V
public final fun clearSpanStyles-5zc-tL8 (J)V
public final fun copy ()Lcom/mohamedrejeb/richeditor/model/RichTextState;
public final fun getAnnotatedString ()Landroidx/compose/ui/text/AnnotatedString;
public final fun getComposition-MzsxiRA ()Landroidx/compose/ui/text/TextRange;
Expand Down
4 changes: 4 additions & 0 deletions richeditor-compose/api/richeditor-compose.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ final class com.mohamedrejeb.richeditor.model/RichTextState { // com.mohamedreje
final fun addSpanStyle(androidx.compose.ui.text/SpanStyle, androidx.compose.ui.text/TextRange) // com.mohamedrejeb.richeditor.model/RichTextState.addSpanStyle|addSpanStyle(androidx.compose.ui.text.SpanStyle;androidx.compose.ui.text.TextRange){}[0]
final fun addUnorderedList() // com.mohamedrejeb.richeditor.model/RichTextState.addUnorderedList|addUnorderedList(){}[0]
final fun clear() // com.mohamedrejeb.richeditor.model/RichTextState.clear|clear(){}[0]
final fun clearRichSpans() // com.mohamedrejeb.richeditor.model/RichTextState.clearRichSpans|clearRichSpans(){}[0]
final fun clearRichSpans(androidx.compose.ui.text/TextRange) // com.mohamedrejeb.richeditor.model/RichTextState.clearRichSpans|clearRichSpans(androidx.compose.ui.text.TextRange){}[0]
final fun clearSpanStyles() // com.mohamedrejeb.richeditor.model/RichTextState.clearSpanStyles|clearSpanStyles(){}[0]
final fun clearSpanStyles(androidx.compose.ui.text/TextRange) // com.mohamedrejeb.richeditor.model/RichTextState.clearSpanStyles|clearSpanStyles(androidx.compose.ui.text.TextRange){}[0]
final fun copy(): com.mohamedrejeb.richeditor.model/RichTextState // com.mohamedrejeb.richeditor.model/RichTextState.copy|copy(){}[0]
final fun getParagraphStyle(androidx.compose.ui.text/TextRange): androidx.compose.ui.text/ParagraphStyle // com.mohamedrejeb.richeditor.model/RichTextState.getParagraphStyle|getParagraphStyle(androidx.compose.ui.text.TextRange){}[0]
final fun getRichSpanStyle(androidx.compose.ui.text/TextRange): com.mohamedrejeb.richeditor.model/RichSpanStyle // com.mohamedrejeb.richeditor.model/RichTextState.getRichSpanStyle|getRichSpanStyle(androidx.compose.ui.text.TextRange){}[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,20 @@ public class RichTextState internal constructor(
applyRichSpanStyleToTextRange(textRange)
}

/**
* Clear all [SpanStyle]s.
*/
public fun clearSpanStyles() {
removeSpanStyle(currentSpanStyle)
}

/**
* Clear all [SpanStyle]s from a specific [TextRange].
*/
public fun clearSpanStyles(textRange: TextRange) {
removeSpanStyle(currentSpanStyle, textRange)
}

/**
* Add a link to the text field.
* The link is going to be added after the current selection.
Expand Down Expand Up @@ -714,6 +728,20 @@ public class RichTextState internal constructor(
applyRichSpanStyleToTextRange(textRange)
}

/**
* Clear all [RichSpanStyle]s.
*/
public fun clearRichSpans() {
removeRichSpan(currentRichSpanStyle)
}

/**
* Clear all [RichSpanStyle]s from a specific [TextRange].
*/
public fun clearRichSpans(textRange: TextRange) {
removeRichSpan(currentRichSpanStyle, textRange)
}

/**
* Toggle the [ParagraphStyle]
* If the passed paragraph style doesn't exist in the [currentParagraphStyle] it's going to be added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mohamedrejeb.richeditor.model
import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
Expand Down Expand Up @@ -177,6 +178,61 @@ class RichTextStateTest {
assertEquals(richTextState.currentSpanStyle, SpanStyle(fontWeight = FontWeight.Bold))
}

@Test
fun testClearSpanStyles() {
val richTextState = RichTextState(
initialRichParagraphList = listOf(
RichParagraph(
key = 1,
).also {
it.children.add(
RichSpan(
text = "Testing some text",
paragraph = it,
),
)
}
)
)

val boldSpan = SpanStyle(fontWeight = FontWeight.Bold)
val italicSpan = SpanStyle(fontStyle = FontStyle.Italic)
val defaultSpan = SpanStyle()

richTextState.addSpanStyle(
spanStyle = boldSpan,
// "Testing some" is bold.
textRange = TextRange(0, 12),
)
richTextState.addSpanStyle(
spanStyle = italicSpan,
// "some text" is italic.
textRange = TextRange(8, 17),
)

richTextState.selection = TextRange(8, 12)
// Clear spans of "some".
richTextState.clearSpanStyles()

assertEquals(defaultSpan, richTextState.currentSpanStyle)
richTextState.selection = TextRange(0, 8)
// "Testing" is bold.
assertEquals(boldSpan, richTextState.currentSpanStyle)
richTextState.selection = TextRange(8, 12)
// "some" is the default.
assertEquals(defaultSpan, richTextState.currentSpanStyle)
richTextState.selection = TextRange(12, 17)
// "text" is italic.
assertEquals(italicSpan, richTextState.currentSpanStyle)

// Clear all spans.
richTextState.clearRichSpans(TextRange(0, 17))

assertEquals(defaultSpan, richTextState.currentSpanStyle)
richTextState.selection = TextRange(0, 17)
assertEquals(defaultSpan, richTextState.currentSpanStyle)
}

@OptIn(ExperimentalRichTextApi::class)
@Test
fun testAddRichSpanStyleByTextRange() {
Expand Down Expand Up @@ -258,6 +314,62 @@ class RichTextStateTest {
assertEquals(richTextState.currentRichSpanStyle::class, RichSpanStyle.Code::class)
}

@OptIn(ExperimentalRichTextApi::class)
@Test
fun testClearRichSpanStyles() {
val richTextState = RichTextState(
initialRichParagraphList = listOf(
RichParagraph(
key = 1,
).also {
it.children.add(
RichSpan(
text = "Testing some text",
paragraph = it,
),
)
}
)
)

val codeSpan = RichSpanStyle.Code()
val linkSpan = RichSpanStyle.Link("https://example.com")
val defaultSpan = RichSpanStyle.Default

richTextState.addRichSpan(
spanStyle = codeSpan,
// "Testing some" is the code.
textRange = TextRange(0, 12),
)
richTextState.addRichSpan(
spanStyle = linkSpan,
// "some text" is the link.
textRange = TextRange(8, 17),
)

richTextState.selection = TextRange(8, 12)
// Clear spans of "some".
richTextState.clearRichSpans()

assertEquals(defaultSpan, richTextState.currentRichSpanStyle)
richTextState.selection = TextRange(0, 8)
// "Testing" is the code.
assertEquals(codeSpan, richTextState.currentRichSpanStyle)
richTextState.selection = TextRange(8, 12)
// "some" is the default.
assertEquals(defaultSpan, richTextState.currentRichSpanStyle)
richTextState.selection = TextRange(12, 17)
// "text" is the link.
assertEquals(linkSpan, richTextState.currentRichSpanStyle)

// Clear all spans.
richTextState.clearRichSpans(TextRange(0, 17))

assertEquals(defaultSpan, richTextState.currentRichSpanStyle)
richTextState.selection = TextRange(0, 17)
assertEquals(defaultSpan, richTextState.currentRichSpanStyle)
}

@Test
fun testGetSpanStyle() {
val richTextState = RichTextState(
Expand Down

0 comments on commit 3c584bf

Please sign in to comment.