From 3de330031144ba04c20abf148c3fb8a20339d15c Mon Sep 17 00:00:00 2001 From: Braian Date: Mon, 6 Jan 2025 16:18:29 +0100 Subject: [PATCH 1/2] Add ability to set Unspecified color span over existing --- .../richeditor/model/RichTextState.kt | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 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 b0e8d038..68b11469 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 @@ -446,10 +446,16 @@ public class RichTextState internal constructor( * @see [removeSpanStyle] */ public fun toggleSpanStyle(spanStyle: SpanStyle) { - if (currentSpanStyle.isSpecifiedFieldsEquals(spanStyle)) - removeSpanStyle(spanStyle) - else - addSpanStyle(spanStyle) + if (spanStyle.isUnspecifiedColorSpan()) { + val modifiedSpan = currentSpanStyle.copy(color = Color.Unspecified) + removeSpanStyle(currentSpanStyle) + addSpanStyle(modifiedSpan) + } else { + if (currentSpanStyle.isSpecifiedFieldsEquals(spanStyle)) + removeSpanStyle(spanStyle) + else + addSpanStyle(spanStyle) + } } /** @@ -3277,3 +3283,7 @@ public class RichTextState internal constructor( ) } } + +private fun SpanStyle.isUnspecifiedColorSpan(): Boolean { + return this == SpanStyle(color = Color.Unspecified) +} From d3710edf0bb5917d5bac231bdd9ed97b30ed1f6b Mon Sep 17 00:00:00 2001 From: Braian Date: Mon, 6 Jan 2025 16:29:11 +0100 Subject: [PATCH 2/2] Added ability to clear color spans for specified range --- .../richeditor/model/RichTextState.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 68b11469..417101d7 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 @@ -458,6 +458,22 @@ public class RichTextState internal constructor( } } + /** + * Clear color [SpanStyle]s for current selection. + */ + public fun clearSelectionColorSpans() { + annotatedString + .subSequence(selection) + .spanStyles + .filter { it.item.color.isSpecified } + .forEach { + val spanStyle = SpanStyle(color = it.item.color) + toRemoveSpanStyle = toRemoveSpanStyle.customMerge(spanStyle) + toAddSpanStyle = toAddSpanStyle.unmerge(spanStyle) + if (!selection.collapsed) applyRichSpanStyleToSelectedText() + } + } + /** * Add new [SpanStyle] to the [currentSpanStyle] *