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 1118c0aa..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 @@ -138,9 +138,7 @@ public class RichTextState internal constructor( private var toRemoveSpanStyle: SpanStyle by mutableStateOf(SpanStyle()) private var toAddRichSpanStyle: RichSpanStyle by mutableStateOf(RichSpanStyle.Default) - private var toRemoveRichSpanStyleKClass: KClass by mutableStateOf( - RichSpanStyle.Default::class - ) + private var toRemoveRichSpanStyleKClass: KClass by mutableStateOf(RichSpanStyle.Default::class) @Deprecated( message = "Use isRichSpan with T or KClass instead", @@ -200,8 +198,7 @@ public class RichTextState internal constructor( * If the selection is not collapsed, the paragraph style is the style of the selection. */ public val currentParagraphStyle: ParagraphStyle - get() = currentAppliedParagraphStyle.merge(toAddParagraphStyle) - .unmerge(toRemoveParagraphStyle) + get() = currentAppliedParagraphStyle.merge(toAddParagraphStyle).unmerge(toRemoveParagraphStyle) private var currentRichParagraphType: ParagraphType by mutableStateOf( getRichParagraphByTextIndex(textIndex = selection.min - 1)?.type @@ -285,8 +282,8 @@ public class RichTextState internal constructor( require(textRange.max <= textFieldValue.text.length) { "The end index must be within the text bounds. " + - "The text length is ${textFieldValue.text.length}, " + - "but the end index is ${textRange.max}." + "The text length is ${textFieldValue.text.length}, " + + "but the end index is ${textRange.max}." } onTextFieldValueChange( @@ -866,9 +863,7 @@ public class RichTextState internal constructor( else { val paragraphs = getRichParagraphListByTextRange(selection) if (paragraphs.isEmpty()) return - paragraphs.fastForEach { - it.paragraphStyle = it.paragraphStyle.merge(paragraphStyle) - } + paragraphs.fastForEach { it.paragraphStyle = it.paragraphStyle.merge(paragraphStyle) } } // We update the annotated string to reflect the changes updateAnnotatedString() @@ -902,9 +897,7 @@ public class RichTextState internal constructor( else { val paragraphs = getRichParagraphListByTextRange(selection) if (paragraphs.isEmpty()) return - paragraphs.fastForEach { - it.paragraphStyle = it.paragraphStyle.unmerge(paragraphStyle) - } + paragraphs.fastForEach { it.paragraphStyle = it.paragraphStyle.unmerge(paragraphStyle) } } // We update the annotated string to reflect the changes updateAnnotatedString() @@ -1100,10 +1093,7 @@ public class RichTextState internal constructor( // If the paragraph type start text length didn't change, we don't need to update the text field value if (paragraphOldStartTextLength == newType.startText.length) return textFieldValue - val beforeText = textFieldValue.text.substring( - 0, - paragraphFirstChildStartIndex - paragraphOldStartTextLength - ) + val beforeText = textFieldValue.text.substring(0, paragraphFirstChildStartIndex - paragraphOldStartTextLength) val afterText = textFieldValue.text.substring(paragraphFirstChildStartIndex) val newSelectionMin = @@ -1233,8 +1223,7 @@ public class RichTextState internal constructor( withStyle(richParagraph.paragraphStyle.merge(richParagraph.type.getStyle(config))) { append(richParagraph.type.startText) val richParagraphStartTextLength = richParagraph.type.startText.length - richParagraph.type.startRichSpan.textRange = - TextRange(index, index + richParagraphStartTextLength) + richParagraph.type.startRichSpan.textRange = TextRange(index, index + richParagraphStartTextLength) index += richParagraphStartTextLength withStyle(RichSpanStyle.DefaultSpanStyle) { index = append( @@ -1325,8 +1314,7 @@ public class RichTextState internal constructor( val afterText = activeRichSpan.text.substring(startIndex) val activeRichSpanFullSpanStyle = activeRichSpan.fullSpanStyle - val newSpanStyle = - activeRichSpanFullSpanStyle.customMerge(toAddSpanStyle).unmerge(toRemoveSpanStyle) + val newSpanStyle = activeRichSpanFullSpanStyle.customMerge(toAddSpanStyle).unmerge(toRemoveSpanStyle) val newRichSpanStyle = when { toAddRichSpanStyle !is RichSpanStyle.Default -> @@ -1412,14 +1400,12 @@ public class RichTextState internal constructor( // Get the first non-empty child of the min paragraph val minFirstNonEmptyChild = minRichSpan.paragraph.getFirstNonEmptyChild() val minParagraphStartTextLength = minRichSpan.paragraph.type.startRichSpan.text.length - val minParagraphFirstChildMinIndex = - minFirstNonEmptyChild?.textRange?.min ?: minParagraphStartTextLength + val minParagraphFirstChildMinIndex = minFirstNonEmptyChild?.textRange?.min ?: minParagraphStartTextLength // Get the first non-empty child of the max paragraph val maxFirstNonEmptyChild = maxRichSpan.paragraph.getFirstNonEmptyChild() val maxParagraphStartTextLength = maxRichSpan.paragraph.type.startRichSpan.text.length - val maxParagraphFirstChildMinIndex = - maxFirstNonEmptyChild?.textRange?.min ?: maxParagraphStartTextLength + val maxParagraphFirstChildMinIndex = maxFirstNonEmptyChild?.textRange?.min ?: maxParagraphStartTextLength // TODO: // Check if we can remove this condition since we are already checking below @@ -1617,8 +1603,7 @@ public class RichTextState internal constructor( endIndex = minRemoveIndex, ) - val afterTextStartIndex = - minRemoveIndex + (paragraphFirstChildMinIndex - maxRemoveIndex) + val afterTextStartIndex = minRemoveIndex + (paragraphFirstChildMinIndex - maxRemoveIndex) val afterText = if (tempTextFieldValue.text.length <= afterTextStartIndex) @@ -1860,8 +1845,7 @@ public class RichTextState internal constructor( "" val richSpanFullSpanStyle = richSpan.fullSpanStyle - val newSpanStyle = - richSpanFullSpanStyle.customMerge(toAddSpanStyle).unmerge(toRemoveSpanStyle) + val newSpanStyle = richSpanFullSpanStyle.customMerge(toAddSpanStyle).unmerge(toRemoveSpanStyle) val startApplyStyleIndex = maxOf(startSelectionIndex, richSpan.textRange.start) @@ -1896,8 +1880,7 @@ public class RichTextState internal constructor( afterText: String, startIndex: Int, richSpanFullSpanStyle: SpanStyle = richSpan.fullSpanStyle, - newSpanStyle: SpanStyle = richSpanFullSpanStyle.customMerge(toAddSpanStyle) - .unmerge(toRemoveSpanStyle), + newSpanStyle: SpanStyle = richSpanFullSpanStyle.customMerge(toAddSpanStyle).unmerge(toRemoveSpanStyle), newRichSpanStyle: RichSpanStyle = when { toAddRichSpanStyle !is RichSpanStyle.Default -> @@ -1913,9 +1896,7 @@ public class RichTextState internal constructor( if (richSpanFullSpanStyle == newSpanStyle && newRichSpanStyle::class == richSpan.richSpanStyle::class) return if ( - (toRemoveSpanStyle == SpanStyle() || !richSpanFullSpanStyle.isSpecifiedFieldsEquals( - toRemoveSpanStyle - )) && + (toRemoveSpanStyle == SpanStyle() || !richSpanFullSpanStyle.isSpecifiedFieldsEquals(toRemoveSpanStyle)) && (toRemoveRichSpanStyleKClass == RichSpanStyle.Default::class || newRichSpanStyle::class == richSpan.richSpanStyle::class) ) { applyStyleToRichSpan( @@ -1986,9 +1967,7 @@ public class RichTextState internal constructor( startIndex, startIndex + middleText.length ), - spanStyle = SpanStyle(textDecoration = fullSpanStyle.textDecoration).customMerge( - toAddSpanStyle - ), + spanStyle = SpanStyle(textDecoration = fullSpanStyle.textDecoration).customMerge(toAddSpanStyle), richSpanStyle = if (toAddRichSpanStyle !is RichSpanStyle.Default) toAddRichSpanStyle @@ -2059,8 +2038,7 @@ public class RichTextState internal constructor( afterRichSpan.parent = richSpan.parent if (!isRichSpanStylingEmpty) { - afterRichSpan.spanStyle = - richSpan.spanStyle.customMerge(afterRichSpan.spanStyle) + afterRichSpan.spanStyle = richSpan.spanStyle.customMerge(afterRichSpan.spanStyle) if (richSpan.richSpanStyle !is RichSpanStyle.Default && afterRichSpan.richSpanStyle is RichSpanStyle.Default) afterRichSpan.richSpanStyle = richSpan.richSpanStyle } @@ -2188,8 +2166,7 @@ public class RichTextState internal constructor( } // Merge RichSpan span style with parent RichSpan span style. - childRichSpan.spanStyle = - currentRichSpanFullSpanStyle.merge(childRichSpan.spanStyle) + childRichSpan.spanStyle = currentRichSpanFullSpanStyle.merge(childRichSpan.spanStyle) // Lookup for RichSpan with the same span style and merge them to optimize the RichSpan tree. val lastChild = toShiftRichSpanList.lastOrNull() @@ -2340,8 +2317,7 @@ public class RichTextState internal constructor( richSpan.size == 1 ) { activeRichSpan.text = firstRichSpan.text - activeRichSpan.spanStyle = - richSpan.first().spanStyle.customMerge(firstRichSpan.spanStyle) + activeRichSpan.spanStyle = richSpan.first().spanStyle.customMerge(firstRichSpan.spanStyle) activeRichSpan.children.clear() activeRichSpan.children.addAll(firstRichSpan.children) } @@ -2505,8 +2481,7 @@ public class RichTextState internal constructor( var currentRichSpan: RichSpan = richSpan val textStartIndex = startIndex - richSpan.textRange.min - val beforeText = - if (textStartIndex > 0) richSpan.text.substring(0, textStartIndex) else "" // + ' ' + val beforeText = if (textStartIndex > 0) richSpan.text.substring(0, textStartIndex) else "" // + ' ' val afterText = richSpan.text.substring(textStartIndex + 1) richSpan.text = beforeText @@ -2868,10 +2843,7 @@ public class RichTextState internal constructor( ) { updateTextFieldValue( textFieldValue.copy( - selection = TextRange( - (selection.min - 1).coerceAtLeast(0), - (selection.min - 1).coerceAtLeast(0) - ) + selection = TextRange((selection.min - 1).coerceAtLeast(0), (selection.min - 1).coerceAtLeast(0)) ) ) } else if ( @@ -2882,10 +2854,7 @@ public class RichTextState internal constructor( ) { updateTextFieldValue( textFieldValue.copy( - selection = TextRange( - (selection.min + 1).coerceAtMost(textLength - 1), - (selection.min + 1).coerceAtMost(textLength - 1) - ) + selection = TextRange((selection.min + 1).coerceAtMost(textLength - 1), (selection.min + 1).coerceAtMost(textLength - 1)) ) ) } else if (newSelection != null) { @@ -3143,8 +3112,7 @@ public class RichTextState internal constructor( withStyle(richParagraph.paragraphStyle.merge(richParagraph.type.getStyle(config))) { append(richParagraph.type.startText) val richParagraphStartTextLength = richParagraph.type.startText.length - richParagraph.type.startRichSpan.textRange = - TextRange(index, index + richParagraphStartTextLength) + richParagraph.type.startRichSpan.textRange = TextRange(index, index + richParagraphStartTextLength) index += richParagraphStartTextLength withStyle(RichSpanStyle.DefaultSpanStyle) { index = append( @@ -3205,8 +3173,7 @@ public class RichTextState internal constructor( orderedListNumber++ if (orderedListNumber == 1) - orderedListStartTextSpanStyle = - richParagraph.getFirstNonEmptyChild()?.spanStyle ?: SpanStyle() + orderedListStartTextSpanStyle = richParagraph.getFirstNonEmptyChild()?.spanStyle ?: SpanStyle() type.number = orderedListNumber type.startTextSpanStyle = orderedListStartTextSpanStyle