feat: add code, quote to format bar; improve cursor position for empty selection
This commit is contained in:
parent
1a25a1bf44
commit
865eb2816d
@ -3,14 +3,17 @@ package com.github.diegoberaldin.raccoonforlemmy.core.commonui.components
|
|||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.material.icons.Icons
|
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.FormatBold
|
||||||
import androidx.compose.material.icons.filled.FormatItalic
|
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.FormatStrikethrough
|
||||||
import androidx.compose.material.icons.filled.Image
|
import androidx.compose.material.icons.filled.Image
|
||||||
import androidx.compose.material.icons.filled.InsertLink
|
import androidx.compose.material.icons.filled.InsertLink
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.text.TextRange
|
||||||
import androidx.compose.ui.text.input.TextFieldValue
|
import androidx.compose.ui.text.input.TextFieldValue
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme.Spacing
|
import com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme.Spacing
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.core.utils.onClick
|
import com.github.diegoberaldin.raccoonforlemmy.core.utils.onClick
|
||||||
@ -31,7 +34,10 @@ fun TextFormattingBar(
|
|||||||
val selection = textFieldValue.selection
|
val selection = textFieldValue.selection
|
||||||
if (selection.length == 0) {
|
if (selection.length == 0) {
|
||||||
val newValue = textFieldValue.let {
|
val newValue = textFieldValue.let {
|
||||||
it.copy(text = it.text + "****")
|
it.copy(
|
||||||
|
text = it.text + "****",
|
||||||
|
selection = TextRange(it.text.length + 2),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
onTextFieldValueChanged(newValue)
|
onTextFieldValueChanged(newValue)
|
||||||
} else {
|
} else {
|
||||||
@ -66,7 +72,10 @@ fun TextFormattingBar(
|
|||||||
val selection = textFieldValue.selection
|
val selection = textFieldValue.selection
|
||||||
if (selection.length == 0) {
|
if (selection.length == 0) {
|
||||||
val newValue = textFieldValue.let {
|
val newValue = textFieldValue.let {
|
||||||
it.copy(text = it.text + "**")
|
it.copy(
|
||||||
|
text = it.text + "**",
|
||||||
|
selection = TextRange(it.text.length + 1),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
onTextFieldValueChanged(newValue)
|
onTextFieldValueChanged(newValue)
|
||||||
} else {
|
} else {
|
||||||
@ -101,7 +110,10 @@ fun TextFormattingBar(
|
|||||||
val selection = textFieldValue.selection
|
val selection = textFieldValue.selection
|
||||||
if (selection.length == 0) {
|
if (selection.length == 0) {
|
||||||
val newValue = textFieldValue.let {
|
val newValue = textFieldValue.let {
|
||||||
it.copy(text = it.text + "~~~~")
|
it.copy(
|
||||||
|
text = it.text + "~~~~",
|
||||||
|
selection = TextRange(it.text.length + 2),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
onTextFieldValueChanged(newValue)
|
onTextFieldValueChanged(newValue)
|
||||||
} else {
|
} else {
|
||||||
@ -140,11 +152,42 @@ fun TextFormattingBar(
|
|||||||
)
|
)
|
||||||
Icon(
|
Icon(
|
||||||
modifier = Modifier.onClick {
|
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)
|
onTextFieldValueChanged(newValue)
|
||||||
},
|
},
|
||||||
imageVector = Icons.Default.InsertLink,
|
imageVector = Icons.Default.InsertLink,
|
||||||
contentDescription = null,
|
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,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user