From 865eb2816dd13b6f31b3df710d8077dd93365203 Mon Sep 17 00:00:00 2001 From: Diego Beraldin Date: Sat, 28 Oct 2023 19:29:23 +0200 Subject: [PATCH] feat: add code, quote to format bar; improve cursor position for empty selection --- .../commonui/components/TextFormattingBar.kt | 51 +++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/TextFormattingBar.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/TextFormattingBar.kt index 0597cea58..7da7330b8 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/TextFormattingBar.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/TextFormattingBar.kt @@ -3,14 +3,17 @@ package com.github.diegoberaldin.raccoonforlemmy.core.commonui.components import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Row import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Code import androidx.compose.material.icons.filled.FormatBold import androidx.compose.material.icons.filled.FormatItalic +import androidx.compose.material.icons.filled.FormatQuote import androidx.compose.material.icons.filled.FormatStrikethrough import androidx.compose.material.icons.filled.Image import androidx.compose.material.icons.filled.InsertLink import androidx.compose.material3.Icon import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.input.TextFieldValue import com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme.Spacing import com.github.diegoberaldin.raccoonforlemmy.core.utils.onClick @@ -31,7 +34,10 @@ fun TextFormattingBar( val selection = textFieldValue.selection if (selection.length == 0) { val newValue = textFieldValue.let { - it.copy(text = it.text + "****") + it.copy( + text = it.text + "****", + selection = TextRange(it.text.length + 2), + ) } onTextFieldValueChanged(newValue) } else { @@ -66,7 +72,10 @@ fun TextFormattingBar( val selection = textFieldValue.selection if (selection.length == 0) { val newValue = textFieldValue.let { - it.copy(text = it.text + "**") + it.copy( + text = it.text + "**", + selection = TextRange(it.text.length + 1), + ) } onTextFieldValueChanged(newValue) } else { @@ -101,7 +110,10 @@ fun TextFormattingBar( val selection = textFieldValue.selection if (selection.length == 0) { val newValue = textFieldValue.let { - it.copy(text = it.text + "~~~~") + it.copy( + text = it.text + "~~~~", + selection = TextRange(it.text.length + 2), + ) } onTextFieldValueChanged(newValue) } else { @@ -140,11 +152,42 @@ fun TextFormattingBar( ) Icon( modifier = Modifier.onClick { - val newValue = textFieldValue.let { it.copy(text = it.text + " [text](url)") } + val newValue = textFieldValue.let { + it.copy( + text = it.text + "[](url)", + selection = TextRange(it.text.length + 1), + ) + } onTextFieldValueChanged(newValue) }, imageVector = Icons.Default.InsertLink, contentDescription = null, ) + Icon( + modifier = Modifier.onClick { + val newValue = textFieldValue.let { + it.copy( + text = it.text + "``", + selection = TextRange(it.text.length + 1), + ) + } + onTextFieldValueChanged(newValue) + }, + imageVector = Icons.Default.Code, + contentDescription = null, + ) + Icon( + modifier = Modifier.onClick { + val newValue = textFieldValue.let { + it.copy( + text = it.text + "\n> ", + selection = TextRange(it.text.length + 3), + ) + } + onTextFieldValueChanged(newValue) + }, + imageVector = Icons.Default.FormatQuote, + contentDescription = null, + ) } } \ No newline at end of file