Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Wavesonics committed Nov 17, 2024
1 parent 62a0428 commit bebdd28
Showing 1 changed file with 24 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<out RichSpanStyle> by mutableStateOf(
RichSpanStyle.Default::class
)
private var toRemoveRichSpanStyleKClass: KClass<out RichSpanStyle> by mutableStateOf(RichSpanStyle.Default::class)

@Deprecated(
message = "Use isRichSpan with T or KClass instead",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 ->
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bebdd28

Please sign in to comment.