Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inserting html and markdown after selection #546

Merged
merged 1 commit into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions richeditor-compose/api/android/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ public final class com/mohamedrejeb/richeditor/model/RichTextState {
public final fun getSpanStyle-5zc-tL8 (J)Landroidx/compose/ui/text/SpanStyle;
public final fun increaseListLevel ()V
public final fun insertHtml (Ljava/lang/String;I)V
public final fun insertHtmlAfterSelection (Ljava/lang/String;)V
public final fun insertMarkdown (Ljava/lang/String;I)V
public final fun insertMarkdownAfterSelection (Ljava/lang/String;)V
public final fun isCode ()Z
public final fun isCodeSpan ()Z
public final fun isLink ()Z
Expand Down
2 changes: 2 additions & 0 deletions richeditor-compose/api/desktop/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ public final class com/mohamedrejeb/richeditor/model/RichTextState {
public final fun getSpanStyle-5zc-tL8 (J)Landroidx/compose/ui/text/SpanStyle;
public final fun increaseListLevel ()V
public final fun insertHtml (Ljava/lang/String;I)V
public final fun insertHtmlAfterSelection (Ljava/lang/String;)V
public final fun insertMarkdown (Ljava/lang/String;I)V
public final fun insertMarkdownAfterSelection (Ljava/lang/String;)V
public final fun isCode ()Z
public final fun isCodeSpan ()Z
public final fun isLink ()Z
Expand Down
20 changes: 2 additions & 18 deletions richeditor-compose/api/richeditor-compose.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ final class com.mohamedrejeb.richeditor.model/RichTextState { // com.mohamedreje
final fun getSpanStyle(androidx.compose.ui.text/TextRange): androidx.compose.ui.text/SpanStyle // com.mohamedrejeb.richeditor.model/RichTextState.getSpanStyle|getSpanStyle(androidx.compose.ui.text.TextRange){}[0]
final fun increaseListLevel() // com.mohamedrejeb.richeditor.model/RichTextState.increaseListLevel|increaseListLevel(){}[0]
final fun insertHtml(kotlin/String, kotlin/Int) // com.mohamedrejeb.richeditor.model/RichTextState.insertHtml|insertHtml(kotlin.String;kotlin.Int){}[0]
final fun insertHtmlAfterSelection(kotlin/String) // com.mohamedrejeb.richeditor.model/RichTextState.insertHtmlAfterSelection|insertHtmlAfterSelection(kotlin.String){}[0]
final fun insertMarkdown(kotlin/String, kotlin/Int) // com.mohamedrejeb.richeditor.model/RichTextState.insertMarkdown|insertMarkdown(kotlin.String;kotlin.Int){}[0]
final fun insertMarkdownAfterSelection(kotlin/String) // com.mohamedrejeb.richeditor.model/RichTextState.insertMarkdownAfterSelection|insertMarkdownAfterSelection(kotlin.String){}[0]
final fun isRichSpan(com.mohamedrejeb.richeditor.model/RichSpanStyle): kotlin/Boolean // com.mohamedrejeb.richeditor.model/RichTextState.isRichSpan|isRichSpan(com.mohamedrejeb.richeditor.model.RichSpanStyle){}[0]
final fun isRichSpan(kotlin.reflect/KClass<out com.mohamedrejeb.richeditor.model/RichSpanStyle>): kotlin/Boolean // com.mohamedrejeb.richeditor.model/RichTextState.isRichSpan|isRichSpan(kotlin.reflect.KClass<out|com.mohamedrejeb.richeditor.model.RichSpanStyle>){}[0]
final fun removeCode() // com.mohamedrejeb.richeditor.model/RichTextState.removeCode|removeCode(){}[0]
Expand Down Expand Up @@ -448,21 +450,3 @@ final fun com.mohamedrejeb.richeditor.ui/BasicRichText(com.mohamedrejeb.richedit
final fun com.mohamedrejeb.richeditor.ui/BasicRichTextEditor(com.mohamedrejeb.richeditor.model/RichTextState, androidx.compose.ui/Modifier?, kotlin/Boolean, kotlin/Boolean, androidx.compose.ui.text/TextStyle?, androidx.compose.foundation.text/KeyboardOptions?, androidx.compose.foundation.text/KeyboardActions?, kotlin/Boolean, kotlin/Boolean, kotlin/Int, kotlin/Int, kotlin/Int, kotlin/Function1<androidx.compose.ui.text/TextLayoutResult, kotlin/Unit>?, androidx.compose.foundation.interaction/MutableInteractionSource?, androidx.compose.ui.graphics/Brush?, kotlin/Function3<kotlin/Function2<androidx.compose.runtime/Composer, kotlin/Int, kotlin/Unit>, androidx.compose.runtime/Composer, kotlin/Int, kotlin/Unit>?, androidx.compose.foundation.layout/PaddingValues, androidx.compose.runtime/Composer?, kotlin/Int, kotlin/Int, kotlin/Int) // com.mohamedrejeb.richeditor.ui/BasicRichTextEditor|BasicRichTextEditor(com.mohamedrejeb.richeditor.model.RichTextState;androidx.compose.ui.Modifier?;kotlin.Boolean;kotlin.Boolean;androidx.compose.ui.text.TextStyle?;androidx.compose.foundation.text.KeyboardOptions?;androidx.compose.foundation.text.KeyboardActions?;kotlin.Boolean;kotlin.Boolean;kotlin.Int;kotlin.Int;kotlin.Int;kotlin.Function1<androidx.compose.ui.text.TextLayoutResult,kotlin.Unit>?;androidx.compose.foundation.interaction.MutableInteractionSource?;androidx.compose.ui.graphics.Brush?;kotlin.Function3<kotlin.Function2<androidx.compose.runtime.Composer,kotlin.Int,kotlin.Unit>,androidx.compose.runtime.Composer,kotlin.Int,kotlin.Unit>?;androidx.compose.foundation.layout.PaddingValues;androidx.compose.runtime.Composer?;kotlin.Int;kotlin.Int;kotlin.Int){}[0]
final fun com.mohamedrejeb.richeditor.ui/BasicRichTextEditor(com.mohamedrejeb.richeditor.model/RichTextState, androidx.compose.ui/Modifier?, kotlin/Boolean, kotlin/Boolean, androidx.compose.ui.text/TextStyle?, androidx.compose.foundation.text/KeyboardOptions?, androidx.compose.foundation.text/KeyboardActions?, kotlin/Boolean, kotlin/Int, kotlin/Int, kotlin/Int, kotlin/Function1<androidx.compose.ui.text/TextLayoutResult, kotlin/Unit>?, androidx.compose.foundation.interaction/MutableInteractionSource?, androidx.compose.ui.graphics/Brush?, kotlin/Function3<kotlin/Function2<androidx.compose.runtime/Composer, kotlin/Int, kotlin/Unit>, androidx.compose.runtime/Composer, kotlin/Int, kotlin/Unit>?, androidx.compose.runtime/Composer?, kotlin/Int, kotlin/Int, kotlin/Int) // com.mohamedrejeb.richeditor.ui/BasicRichTextEditor|BasicRichTextEditor(com.mohamedrejeb.richeditor.model.RichTextState;androidx.compose.ui.Modifier?;kotlin.Boolean;kotlin.Boolean;androidx.compose.ui.text.TextStyle?;androidx.compose.foundation.text.KeyboardOptions?;androidx.compose.foundation.text.KeyboardActions?;kotlin.Boolean;kotlin.Int;kotlin.Int;kotlin.Int;kotlin.Function1<androidx.compose.ui.text.TextLayoutResult,kotlin.Unit>?;androidx.compose.foundation.interaction.MutableInteractionSource?;androidx.compose.ui.graphics.Brush?;kotlin.Function3<kotlin.Function2<androidx.compose.runtime.Composer,kotlin.Int,kotlin.Unit>,androidx.compose.runtime.Composer,kotlin.Int,kotlin.Unit>?;androidx.compose.runtime.Composer?;kotlin.Int;kotlin.Int;kotlin.Int){}[0]
final fun com.mohamedrejeb.richeditor.ui/com_mohamedrejeb_richeditor_ui_RichTextClipboardManager$stableprop_getter(): kotlin/Int // com.mohamedrejeb.richeditor.ui/com_mohamedrejeb_richeditor_ui_RichTextClipboardManager$stableprop_getter|com_mohamedrejeb_richeditor_ui_RichTextClipboardManager$stableprop_getter(){}[0]

// Targets: [ios]
final val com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_IosRichTextClipboardManager$stableprop // com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_IosRichTextClipboardManager$stableprop|#static{}com_mohamedrejeb_richeditor_clipboard_IosRichTextClipboardManager$stableprop[0]

// Targets: [ios]
final fun com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_IosRichTextClipboardManager$stableprop_getter(): kotlin/Int // com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_IosRichTextClipboardManager$stableprop_getter|com_mohamedrejeb_richeditor_clipboard_IosRichTextClipboardManager$stableprop_getter(){}[0]

// Targets: [js]
final val com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_JsRichTextClipboardManager$stableprop // com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_JsRichTextClipboardManager$stableprop|#static{}com_mohamedrejeb_richeditor_clipboard_JsRichTextClipboardManager$stableprop[0]

// Targets: [js]
final fun com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_JsRichTextClipboardManager$stableprop_getter(): kotlin/Int // com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_JsRichTextClipboardManager$stableprop_getter|com_mohamedrejeb_richeditor_clipboard_JsRichTextClipboardManager$stableprop_getter(){}[0]

// Targets: [wasmJs]
final val com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_WasmJsRichTextClipboardManager$stableprop // com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_WasmJsRichTextClipboardManager$stableprop|#static{}com_mohamedrejeb_richeditor_clipboard_WasmJsRichTextClipboardManager$stableprop[0]

// Targets: [wasmJs]
final fun com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_WasmJsRichTextClipboardManager$stableprop_getter(): kotlin/Int // com.mohamedrejeb.richeditor.clipboard/com_mohamedrejeb_richeditor_clipboard_WasmJsRichTextClipboardManager$stableprop_getter|com_mohamedrejeb_richeditor_clipboard_WasmJsRichTextClipboardManager$stableprop_getter(){}[0]
Original file line number Diff line number Diff line change
Expand Up @@ -3660,6 +3660,23 @@ public class RichTextState internal constructor(
return this
}

/**
* Inserts the given [html] content after selection in the [RichTextState].
*
* @param html The html content to insert.
*/
public fun insertHtmlAfterSelection(html: String) {
val newParagraphs = RichTextStateHtmlParser.encode(html).richParagraphList
val position = selection.max

selection = TextRange(selection.max)

insertParagraphs(
newParagraphs = newParagraphs,
position = position,
)
}

/**
* Inserts the given [html] content at the specified [position] in the [RichTextState].
*
Expand Down Expand Up @@ -3700,6 +3717,23 @@ public class RichTextState internal constructor(
return this
}

/**
* Inserts the given [markdown] content after selection in the [RichTextState].
*
* @param markdown The markdown content to insert.
*/
public fun insertMarkdownAfterSelection(markdown: String) {
val newParagraphs = RichTextStateMarkdownParser.encode(markdown).richParagraphList
val position = selection.max

selection = TextRange(selection.max)

insertParagraphs(
newParagraphs = newParagraphs,
position = position,
)
}

/**
* Inserts the given [markdown] content at the specified [position] in the [RichTextState].
*
Expand Down Expand Up @@ -3845,6 +3879,8 @@ public class RichTextState internal constructor(
if (richParagraphList.isEmpty())
richParagraphList.add(RichParagraph())

val beforeTextLength = annotatedString.text.length

val newStyledRichSpanList = mutableListOf<RichSpan>()

usedInlineContentMapKeys.clear()
Expand Down Expand Up @@ -3888,10 +3924,14 @@ public class RichTextState internal constructor(
}
}

val selectionIndex =
(textFieldValue.selection.min + (annotatedString.text.length - beforeTextLength))
.coerceIn(0, annotatedString.text.length)

styledRichSpanList.clear()
textFieldValue = TextFieldValue(
text = annotatedString.text,
selection = TextRange(annotatedString.text.length),
selection = TextRange(selectionIndex),
)
visualTransformation = VisualTransformation { _ ->
TransformedText(
Expand Down
Loading