feat: add code, quote to format bar; improve cursor position for empty selection

This commit is contained in:
Diego Beraldin 2023-10-28 19:29:23 +02:00
parent 1a25a1bf44
commit 865eb2816d

View File

@ -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,
)
}
}