Skip to content

Commit

Permalink
Merge pull request #370 from MohamedRejeb/1.x
Browse files Browse the repository at this point in the history
Add orderedListIndent and unorderedListIndent
  • Loading branch information
MohamedRejeb authored Sep 30, 2024
2 parents 5a5f552 + 13b49ed commit 1f48669
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 12 deletions.
13 changes: 13 additions & 0 deletions docs/ordered_unordered_lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ val isOrderedList = richTextState.isOrderedList
// Get if the current selection is an unordered list.
val isUnorderedList = richTextState.isUnorderedList
```

You can control the list indentation using `RichTextState`:

```kotlin
// Change list indentation (ordered and unordered).
richTextState.config.listIndent = 20

// Change only ordered list indentation.
richTextState.config.orderedListIndent = 20

// Change only unordered list indentation.
richTextState.config.unorderedListIndent = 20
```
12 changes: 12 additions & 0 deletions docs/rich_text_state.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@ RichTextEditor(
Some of the rich text editor's features can be customized, such as the color of the links and the code blocks.

```kotlin
// Change link color and text decoration.
richTextState.config.linkColor = Color.Blue
richTextState.config.linkTextDecoration = TextDecoration.Underline

// Change code block colors.
richTextState.config.codeSpanColor = Color.Yellow
richTextState.config.codeSpanBackgroundColor = Color.Transparent
richTextState.config.codeSpanStrokeColor = Color.LightGray

// Change list indentation (ordered and unordered).
richTextState.config.listIndent = 20

// Change only ordered list indentation.
richTextState.config.orderedListIndent = 20

// Change only unordered list indentation.
richTextState.config.unorderedListIndent = 20
```

### Changing the editor's selection
Expand Down
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 @@ -105,14 +105,18 @@ public final class com/mohamedrejeb/richeditor/model/RichTextConfig {
public final fun getLinkColor-0d7_KjU ()J
public final fun getLinkTextDecoration ()Landroidx/compose/ui/text/style/TextDecoration;
public final fun getListIndent ()I
public final fun getOrderedListIndent ()I
public final fun getPreserveStyleOnEmptyLine ()Z
public final fun getUnorderedListIndent ()I
public final fun setCodeSpanBackgroundColor-8_81llA (J)V
public final fun setCodeSpanColor-8_81llA (J)V
public final fun setCodeSpanStrokeColor-8_81llA (J)V
public final fun setLinkColor-8_81llA (J)V
public final fun setLinkTextDecoration (Landroidx/compose/ui/text/style/TextDecoration;)V
public final fun setListIndent (I)V
public final fun setOrderedListIndent (I)V
public final fun setPreserveStyleOnEmptyLine (Z)V
public final fun setUnorderedListIndent (I)V
}

public final class com/mohamedrejeb/richeditor/model/RichTextState {
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 @@ -105,14 +105,18 @@ public final class com/mohamedrejeb/richeditor/model/RichTextConfig {
public final fun getLinkColor-0d7_KjU ()J
public final fun getLinkTextDecoration ()Landroidx/compose/ui/text/style/TextDecoration;
public final fun getListIndent ()I
public final fun getOrderedListIndent ()I
public final fun getPreserveStyleOnEmptyLine ()Z
public final fun getUnorderedListIndent ()I
public final fun setCodeSpanBackgroundColor-8_81llA (J)V
public final fun setCodeSpanColor-8_81llA (J)V
public final fun setCodeSpanStrokeColor-8_81llA (J)V
public final fun setLinkColor-8_81llA (J)V
public final fun setLinkTextDecoration (Landroidx/compose/ui/text/style/TextDecoration;)V
public final fun setListIndent (I)V
public final fun setOrderedListIndent (I)V
public final fun setPreserveStyleOnEmptyLine (Z)V
public final fun setUnorderedListIndent (I)V
}

public final class com/mohamedrejeb/richeditor/model/RichTextState {
Expand Down
6 changes: 6 additions & 0 deletions richeditor-compose/api/richeditor-compose.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ final class com.mohamedrejeb.richeditor.model/RichTextConfig { // com.mohamedrej
final var listIndent // com.mohamedrejeb.richeditor.model/RichTextConfig.listIndent|{}listIndent[0]
final fun <get-listIndent>(): kotlin/Int // com.mohamedrejeb.richeditor.model/RichTextConfig.listIndent.<get-listIndent>|<get-listIndent>(){}[0]
final fun <set-listIndent>(kotlin/Int) // com.mohamedrejeb.richeditor.model/RichTextConfig.listIndent.<set-listIndent>|<set-listIndent>(kotlin.Int){}[0]
final var orderedListIndent // com.mohamedrejeb.richeditor.model/RichTextConfig.orderedListIndent|{}orderedListIndent[0]
final fun <get-orderedListIndent>(): kotlin/Int // com.mohamedrejeb.richeditor.model/RichTextConfig.orderedListIndent.<get-orderedListIndent>|<get-orderedListIndent>(){}[0]
final fun <set-orderedListIndent>(kotlin/Int) // com.mohamedrejeb.richeditor.model/RichTextConfig.orderedListIndent.<set-orderedListIndent>|<set-orderedListIndent>(kotlin.Int){}[0]
final var preserveStyleOnEmptyLine // com.mohamedrejeb.richeditor.model/RichTextConfig.preserveStyleOnEmptyLine|{}preserveStyleOnEmptyLine[0]
final fun <get-preserveStyleOnEmptyLine>(): kotlin/Boolean // com.mohamedrejeb.richeditor.model/RichTextConfig.preserveStyleOnEmptyLine.<get-preserveStyleOnEmptyLine>|<get-preserveStyleOnEmptyLine>(){}[0]
final fun <set-preserveStyleOnEmptyLine>(kotlin/Boolean) // com.mohamedrejeb.richeditor.model/RichTextConfig.preserveStyleOnEmptyLine.<set-preserveStyleOnEmptyLine>|<set-preserveStyleOnEmptyLine>(kotlin.Boolean){}[0]
final var unorderedListIndent // com.mohamedrejeb.richeditor.model/RichTextConfig.unorderedListIndent|{}unorderedListIndent[0]
final fun <get-unorderedListIndent>(): kotlin/Int // com.mohamedrejeb.richeditor.model/RichTextConfig.unorderedListIndent.<get-unorderedListIndent>|<get-unorderedListIndent>(){}[0]
final fun <set-unorderedListIndent>(kotlin/Int) // com.mohamedrejeb.richeditor.model/RichTextConfig.unorderedListIndent.<set-unorderedListIndent>|<set-unorderedListIndent>(kotlin.Int){}[0]
}

final class com.mohamedrejeb.richeditor.model/RichTextState { // com.mohamedrejeb.richeditor.model/RichTextState|null[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,42 @@ public class RichTextConfig internal constructor(
updateText()
}

public var listIndent: Int = DefaultListIndent
/**
* The indent for ordered lists.
*/
public var orderedListIndent: Int = DefaultListIndent
set(value) {
field = value
updateText()
}

/**
* The indent for unordered lists.
*/
public var unorderedListIndent: Int = DefaultListIndent
set(value) {
field = value
updateText()
}

/**
* The indent for both ordered and unordered lists.
*
* This property is a shortcut for setting both [orderedListIndent] and [unorderedListIndent].
*/
public var listIndent: Int = DefaultListIndent
get() {
if (orderedListIndent == unorderedListIndent)
field = orderedListIndent

return field
}
set(value) {
field = value
orderedListIndent = value
unorderedListIndent = value
}

/**
* Whether to preserve the style when the line is empty.
* The line can be empty when the user deletes all the characters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ public class RichTextState internal constructor(
if (paragraph.type is UnorderedList) return

val newType = UnorderedList(
initialIndent = config.listIndent
initialIndent = config.unorderedListIndent
)

updateParagraphType(
Expand Down Expand Up @@ -926,7 +926,7 @@ public class RichTextState internal constructor(

val newType = OrderedList(
number = orderedListNumber,
initialIndent = config.listIndent,
initialIndent = config.orderedListIndent,
startTextSpanStyle = firstRichSpan?.spanStyle ?: SpanStyle(),
)
updateTextFieldValue(
Expand Down Expand Up @@ -1520,14 +1520,14 @@ public class RichTextState internal constructor(

if (richSpan.text == "- " || richSpan.text == "* ") {
richSpan.paragraph.type = UnorderedList(
initialIndent = config.listIndent,
initialIndent = config.unorderedListIndent,
)
richSpan.text = ""
} else if (richSpan.text.matches(Regex("^\\d+\\. "))) {
val number = richSpan.text.first().digitToIntOrNull() ?: 1
richSpan.paragraph.type = OrderedList(
number = number,
initialIndent = config.listIndent,
initialIndent = config.orderedListIndent,
)
richSpan.text = ""
}
Expand All @@ -1549,7 +1549,7 @@ public class RichTextState internal constructor(
paragraph = currentParagraph,
newType = OrderedList(
number = number,
initialIndent = config.listIndent,
initialIndent = config.orderedListIndent,
startTextSpanStyle = currentParagraphType.startTextSpanStyle,
startTextWidth = currentParagraphType.startTextWidth
),
Expand Down Expand Up @@ -1580,7 +1580,7 @@ public class RichTextState internal constructor(
paragraph = currentParagraph,
newType = OrderedList(
number = number,
initialIndent = config.listIndent,
initialIndent = config.orderedListIndent,
startTextSpanStyle = currentParagraphType.startTextSpanStyle,
startTextWidth = currentParagraphType.startTextWidth
),
Expand Down Expand Up @@ -2598,7 +2598,7 @@ public class RichTextState internal constructor(

if (
paragraphType.startText.isNotEmpty() &&
paragraphType.startRichSpan.textRange.max < textLayoutResult.layoutInput.text.text.length
paragraphType.startRichSpan.textRange.max <= textLayoutResult.layoutInput.text.text.length
) {
val start =
textLayoutResult.getHorizontalPosition(
Expand Down Expand Up @@ -2935,6 +2935,8 @@ public class RichTextState internal constructor(
richTextState.config.codeSpanBackgroundColor = config.codeSpanBackgroundColor
richTextState.config.codeSpanStrokeColor = config.codeSpanStrokeColor
richTextState.config.listIndent = config.listIndent
richTextState.config.orderedListIndent = config.orderedListIndent
richTextState.config.unorderedListIndent = config.unorderedListIndent
richTextState.config.preserveStyleOnEmptyLine = config.preserveStyleOnEmptyLine

return richTextState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ internal class OrderedList(
getNewParagraphStyle()

override fun getStyle(config: RichTextConfig): ParagraphStyle {
if (config.listIndent != indent) {
indent = config.listIndent
if (config.orderedListIndent != indent) {
indent = config.orderedListIndent
style = getNewParagraphStyle()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ internal class UnorderedList(
getParagraphStyle()

override fun getStyle(config: RichTextConfig): ParagraphStyle {
if (config.listIndent != indent) {
indent = config.listIndent
if (config.unorderedListIndent != indent) {
indent = config.unorderedListIndent
style = getParagraphStyle()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ fun SlackDemoContent() {
richTextState.config.codeSpanColor = Color(0xFFd7882d)
richTextState.config.codeSpanBackgroundColor = Color.Transparent
richTextState.config.codeSpanStrokeColor = Color(0xFF494b4d)
richTextState.config.unorderedListIndent = 40
richTextState.config.orderedListIndent = 50
}

Box(
Expand Down

0 comments on commit 1f48669

Please sign in to comment.