Skip to content

Commit

Permalink
Merge pull request #366 from MohamedRejeb/1.x
Browse files Browse the repository at this point in the history
Fix list items loaded from markdown jump to the left when starting editing
  • Loading branch information
MohamedRejeb authored Sep 29, 2024
2 parents e71ab2d + a54e25a commit cf399d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2591,12 +2591,16 @@ public class RichTextState internal constructor(
) {
var isParagraphUpdated = false

richParagraphList.forEachIndexed { index, richParagraph ->
val paragraphType = richParagraph.type
if (index + 1 > maxLines || paragraphType !is OrderedList) return@forEachIndexed
textLayoutResult?.let { textLayoutResult ->
richParagraphList.forEachIndexed { index, richParagraph ->
val paragraphType = richParagraph.type
if (index + 1 > maxLines || paragraphType !is OrderedList)
return@forEachIndexed

if (!paragraphType.startRichSpan.textRange.collapsed) {
textLayoutResult?.let { textLayoutResult ->
if (
paragraphType.startText.isNotEmpty() &&
paragraphType.startRichSpan.textRange.max < textLayoutResult.layoutInput.text.text.length
) {
val start =
textLayoutResult.getHorizontalPosition(
offset = paragraphType.startRichSpan.textRange.min,
Expand Down Expand Up @@ -3000,8 +3004,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mohamedrejeb.richeditor.paragraph.type

import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.style.TextIndent
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
Expand All @@ -21,7 +22,7 @@ internal class OrderedList(
var number = number
set(value) {
field = value
startRichSpan = getNewStartRichSpan()
startRichSpan = getNewStartRichSpan(startRichSpan.textRange)
}

var startTextSpanStyle = startTextSpanStyle
Expand Down Expand Up @@ -62,12 +63,19 @@ internal class OrderedList(
getNewStartRichSpan()

@OptIn(ExperimentalRichTextApi::class)
private fun getNewStartRichSpan() =
RichSpan(
private fun getNewStartRichSpan(textRange: TextRange = TextRange(0)): RichSpan {
val text = "$number. "

return RichSpan(
paragraph = RichParagraph(type = this),
text = "$number. ",
spanStyle = startTextSpanStyle
text = text,
spanStyle = startTextSpanStyle,
textRange = TextRange(
textRange.min,
textRange.min + text.length
)
)
}

override fun getNextParagraphType(): ParagraphType =
OrderedList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fun RichTextToMarkdown(
richTextState: RichTextState,
modifier: Modifier = Modifier,
) {
val html by remember(richTextState.annotatedString) {
val markdown by remember(richTextState.annotatedString) {
mutableStateOf(richTextState.toMarkdown())
}

Expand Down Expand Up @@ -85,7 +85,7 @@ fun RichTextToMarkdown(
) {
item {
Text(
text = html,
text = markdown,
modifier = Modifier
.fillMaxWidth()
.weight(1f)
Expand Down

0 comments on commit cf399d3

Please sign in to comment.