mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-10 14:09:40 +01:00
- Migrates NIP-17 DM new message model to its own model and disable some of the generic behavior from new posts.
- Fixes a bug on edits showing a previous edit. - Refactors user suggestions and lastword procedures - Adds a DM inline reply from other parts of the app, like on notification. - Hides Retweet button from DMs on Notifications - Fixes colors for drafts in chats. - Adds a nav function that computes the destination only after the user clicks on it and on a coroutine.
This commit is contained in:
@@ -22,6 +22,8 @@ package com.vitorpamplona.amethyst.commons.compose
|
||||
|
||||
import androidx.compose.ui.text.TextRange
|
||||
import androidx.compose.ui.text.input.TextFieldValue
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
fun TextFieldValue.insertUrlAtCursor(url: String): TextFieldValue {
|
||||
var toInsert = url.trim()
|
||||
@@ -41,3 +43,54 @@ fun TextFieldValue.insertUrlAtCursor(url: String): TextFieldValue {
|
||||
TextRange(endOfUrlIndex, endOfUrlIndex),
|
||||
)
|
||||
}
|
||||
|
||||
fun TextFieldValue.replaceCurrentWord(wordToInsert: String): TextFieldValue {
|
||||
val lastWordStart = currentWordStartIdx()
|
||||
val lastWordEnd = currentWordEndIdx()
|
||||
val cursor = lastWordStart + wordToInsert.length
|
||||
return TextFieldValue(
|
||||
text.replaceRange(lastWordStart, lastWordEnd, wordToInsert),
|
||||
TextRange(cursor, cursor),
|
||||
)
|
||||
}
|
||||
|
||||
fun TextFieldValue.currentWordStartIdx(): Int {
|
||||
val previousNewLine = text.lastIndexOf('\n', selection.start - 1)
|
||||
val previousSpace = text.lastIndexOf(' ', selection.start - 1)
|
||||
|
||||
return max(
|
||||
previousNewLine,
|
||||
previousSpace,
|
||||
) + 1
|
||||
}
|
||||
|
||||
fun TextFieldValue.currentWordEndIdx(): Int {
|
||||
val nextNewLine = text.indexOf('\n', selection.end)
|
||||
val nextSpace = text.indexOf(' ', selection.end)
|
||||
|
||||
if (nextSpace < 0 && nextNewLine < 0) return selection.end
|
||||
if (nextSpace > 0 && nextNewLine > 0) {
|
||||
return min(
|
||||
nextNewLine,
|
||||
nextSpace,
|
||||
)
|
||||
}
|
||||
if (nextSpace > 0) {
|
||||
return nextSpace
|
||||
}
|
||||
return nextNewLine
|
||||
}
|
||||
|
||||
fun TextFieldValue.currentWord(): String {
|
||||
if (selection.end != selection.start) return ""
|
||||
|
||||
val start = currentWordStartIdx()
|
||||
val end = currentWordEndIdx()
|
||||
|
||||
return if (start < end) {
|
||||
val word = text.substring(start, end)
|
||||
word
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user