fix(Chat): ensure cursor is set correctly in when selecting suggestion

This commit fixes a regression that was introduced in: https://github.com/status-im/nim-status-client/commit/459de8cd1
where the cursor position in the chat input was calculated incorrectly.
It also introduced a bug in the text processing which is described in #769

Partly fixes #769
This commit is contained in:
Pascal Precht 2020-08-27 19:43:06 +02:00 committed by Iuri Matias
parent 858a5b467d
commit de5b62030b
2 changed files with 13 additions and 6 deletions

View File

@ -207,8 +207,11 @@ StackLayout {
cursorPosition: chatInput.textInput.cursorPosition
property: "ensName, alias"
onItemSelected: function (item, lastAtPosition, lastCursorPosition) {
let currentText = chatsModel.plainText(Emoji.deparse(chatInput.textInput.text))
let lastAt = currentText.lastIndexOf("@")
let hasEmoji = Emoji.hasEmoji(chatInput.textInput.text)
let currentText = hasEmoji ?
chatsModel.plainText(Emoji.deparse(chatInput.textInput.text)) :
chatsModel.plainText(chatInput.textInput.text);
let aliasName = item[suggestionsBox.property.split(",").map(p => p.trim()).find(p => !!item[p])]
let nameLen = aliasName.length + 2 // We're doing a +2 here because of the `@` and the trailing whitespace
let position = 0;
@ -218,13 +221,13 @@ StackLayout {
position = nameLen
text = "@" + aliasName + " "
} else {
let left = currentText.substring(0, lastAt)
let right = currentText.substring(lastAt + 1)
let left = currentText.substring(0, lastAtPosition)
let right = currentText.substring(hasEmoji ? lastCursorPosition + 2 : lastCursorPosition)
text = `${left} @${aliasName} ${right}`
}
chatInput.textInput.text = Emoji.parse(text, "26x26")
chatInput.textInput.cursorPosition = lastCursorPosition + aliasName.length
chatInput.textInput.text = hasEmoji ? Emoji.parse(text, "26x26") : text
chatInput.textInput.cursorPosition = lastAtPosition + aliasName.length + 2
suggestionsBox.suggestionsModel.clear()
}
}

View File

@ -17,4 +17,8 @@ QtObject {
function deparse(value){
return value.replace(/<img src=\"qrc:\/imports\/twemoji\/.+?" alt=\"(.+?)\" \/>/g, "$1");
}
function hasEmoji(value) {
let match = value.match(/<img src=\"qrc:\/imports\/twemoji\/.+?" alt=\"(.+?)\" \/>/g)
return match && match.length > 0
}
}