From 527830f94b134b569b1c171e4c3e5f63c83785e6 Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Mon, 30 Sep 2024 10:58:07 +0100 Subject: [PATCH 1/5] Add orderedListIndent and unorderedListIndent --- .../richeditor/model/RichTextConfig.kt | 32 ++++++++++++++++++- .../richeditor/model/RichTextState.kt | 14 ++++---- .../richeditor/paragraph/type/OrderedList.kt | 4 +-- .../paragraph/type/UnorderedList.kt | 4 +-- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextConfig.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextConfig.kt index 67948e41..090d764c 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextConfig.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextConfig.kt @@ -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 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 ad5b083d..e696af75 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 @@ -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( @@ -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( @@ -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 = "" } @@ -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 ), @@ -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 ), @@ -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 diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/paragraph/type/OrderedList.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/paragraph/type/OrderedList.kt index c7fc423c..cbd996ef 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/paragraph/type/OrderedList.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/paragraph/type/OrderedList.kt @@ -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() } diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/paragraph/type/UnorderedList.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/paragraph/type/UnorderedList.kt index e2024acb..eebcbdeb 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/paragraph/type/UnorderedList.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/paragraph/type/UnorderedList.kt @@ -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() } From 23e78c5fee2e9c37b165909fd02fb54b2d34943b Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Mon, 30 Sep 2024 10:58:13 +0100 Subject: [PATCH 2/5] Add orderedListIndent and unorderedListIndent to sample --- .../richeditor/sample/common/slack/SlackDemoContent.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/slack/SlackDemoContent.kt b/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/slack/SlackDemoContent.kt index 3b154843..dddec860 100644 --- a/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/slack/SlackDemoContent.kt +++ b/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/slack/SlackDemoContent.kt @@ -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( From bfc7e067520e310f97f15055f1a93d09c391c268 Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Mon, 30 Sep 2024 11:21:02 +0100 Subject: [PATCH 3/5] Add orderedListIndent and unorderedListIndent to docs --- docs/ordered_unordered_lists.md | 13 +++++++++++++ docs/rich_text_state.md | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/docs/ordered_unordered_lists.md b/docs/ordered_unordered_lists.md index 6d1e8121..8d2046ba 100644 --- a/docs/ordered_unordered_lists.md +++ b/docs/ordered_unordered_lists.md @@ -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 +``` \ No newline at end of file diff --git a/docs/rich_text_state.md b/docs/rich_text_state.md index 5d64f3b8..3b422c72 100644 --- a/docs/rich_text_state.md +++ b/docs/rich_text_state.md @@ -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 From 17df87b4a2f1ea09d244b0ea7d51fab30ba808f5 Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Mon, 30 Sep 2024 11:33:25 +0100 Subject: [PATCH 4/5] Improve list indent responsiveness --- .../kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e696af75..e244ab8e 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 @@ -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( From 13b49ed94dda84032520a16c793031c144bb7f24 Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Mon, 30 Sep 2024 11:37:53 +0100 Subject: [PATCH 5/5] Run apiDump --- richeditor-compose/api/android/richeditor-compose.api | 4 ++++ richeditor-compose/api/desktop/richeditor-compose.api | 4 ++++ richeditor-compose/api/richeditor-compose.klib.api | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/richeditor-compose/api/android/richeditor-compose.api b/richeditor-compose/api/android/richeditor-compose.api index 3f25fcb5..278f1b7f 100644 --- a/richeditor-compose/api/android/richeditor-compose.api +++ b/richeditor-compose/api/android/richeditor-compose.api @@ -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 { diff --git a/richeditor-compose/api/desktop/richeditor-compose.api b/richeditor-compose/api/desktop/richeditor-compose.api index c8872c9c..9c5fd9cd 100644 --- a/richeditor-compose/api/desktop/richeditor-compose.api +++ b/richeditor-compose/api/desktop/richeditor-compose.api @@ -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 { diff --git a/richeditor-compose/api/richeditor-compose.klib.api b/richeditor-compose/api/richeditor-compose.klib.api index a8764c1c..b281513c 100644 --- a/richeditor-compose/api/richeditor-compose.klib.api +++ b/richeditor-compose/api/richeditor-compose.klib.api @@ -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 (): kotlin/Int // com.mohamedrejeb.richeditor.model/RichTextConfig.listIndent.|(){}[0] final fun (kotlin/Int) // com.mohamedrejeb.richeditor.model/RichTextConfig.listIndent.|(kotlin.Int){}[0] + final var orderedListIndent // com.mohamedrejeb.richeditor.model/RichTextConfig.orderedListIndent|{}orderedListIndent[0] + final fun (): kotlin/Int // com.mohamedrejeb.richeditor.model/RichTextConfig.orderedListIndent.|(){}[0] + final fun (kotlin/Int) // com.mohamedrejeb.richeditor.model/RichTextConfig.orderedListIndent.|(kotlin.Int){}[0] final var preserveStyleOnEmptyLine // com.mohamedrejeb.richeditor.model/RichTextConfig.preserveStyleOnEmptyLine|{}preserveStyleOnEmptyLine[0] final fun (): kotlin/Boolean // com.mohamedrejeb.richeditor.model/RichTextConfig.preserveStyleOnEmptyLine.|(){}[0] final fun (kotlin/Boolean) // com.mohamedrejeb.richeditor.model/RichTextConfig.preserveStyleOnEmptyLine.|(kotlin.Boolean){}[0] + final var unorderedListIndent // com.mohamedrejeb.richeditor.model/RichTextConfig.unorderedListIndent|{}unorderedListIndent[0] + final fun (): kotlin/Int // com.mohamedrejeb.richeditor.model/RichTextConfig.unorderedListIndent.|(){}[0] + final fun (kotlin/Int) // com.mohamedrejeb.richeditor.model/RichTextConfig.unorderedListIndent.|(kotlin.Int){}[0] } final class com.mohamedrejeb.richeditor.model/RichTextState { // com.mohamedrejeb.richeditor.model/RichTextState|null[0]