fix(Input): fix mention formation on rewriting a mention

Fixes #2325
This commit is contained in:
Jonathan Rainville 2021-05-05 13:54:35 -04:00 committed by Iuri Matias
parent 521d2372f3
commit d2b6bf9310
1 changed files with 14 additions and 5 deletions

View File

@ -258,9 +258,9 @@ Rectangle {
interrogateMessage(); interrogateMessage();
} else { } else {
if (event.key === Qt.Key_Asterisk || if (event.key === Qt.Key_Asterisk ||
event.key === Qt.Key_QuoteLeft || event.key === Qt.Key_QuoteLeft ||
event.key === Qt.Key_Space || event.key === Qt.Key_Space ||
event.key === Qt.Key_AsciiTilde) { event.key === Qt.Key_AsciiTilde) {
formatInputMessage() formatInputMessage()
} }
} }
@ -424,7 +424,7 @@ Rectangle {
const index = message.data.lastIndexOf(':', message.cursor); const index = message.data.lastIndexOf(':', message.cursor);
if (index >= 0) { if (index >= 0) {
emojiEvent = validSubstr(message.data.substr(index, message.cursor - index)); emojiEvent = validSubstr(message.data.substr(index, message.cursor - index));
} }
} }
function validSubstr(substr) { function validSubstr(substr) {
@ -564,18 +564,27 @@ Rectangle {
let nameLen = aliasName.length + 2 // We're doing a +2 here because of the `@` and the trailing whitespace let nameLen = aliasName.length + 2 // We're doing a +2 here because of the `@` and the trailing whitespace
let position = 0; let position = 0;
let text = "" let text = ""
let cursorPositionMention = 0
const spanPlusAlias = `${Constants.mentionSpanTag}@${aliasName}</span> ` const spanPlusAlias = `${Constants.mentionSpanTag}@${aliasName}</span> `
if (currentText === "@") { if (currentText === "@") {
position = nameLen position = nameLen
text = spanPlusAlias text = spanPlusAlias
} else { } else {
let left = currentText.substring(0, lastAtPosition) let left = currentText.substring(0, lastAtPosition)
// If there is an odd number of mentions, it means we are rewritting an old mention
let matches = left.match(/[[mention]]/g)
if (!!matches && matches.length % 2 === 1) {
let index = left.lastIndexOf('[[mention]]')
left = left.substring(0, index)
cursorPositionMention = lastAtPosition - index
}
let right = currentText.substring(hasEmoji ? lastCursorPosition + 2 : lastCursorPosition) let right = currentText.substring(hasEmoji ? lastCursorPosition + 2 : lastCursorPosition)
text = `${left} ${spanPlusAlias}${right}` text = `${left} ${spanPlusAlias}${right}`
} }
messageInputField.text = parseBackText(text) messageInputField.text = parseBackText(text)
messageInputField.cursorPosition = lastAtPosition + aliasName.length + 2 messageInputField.cursorPosition = lastAtPosition + aliasName.length + 2 - cursorPositionMention
if (messageInputField.cursorPosition === 0) { if (messageInputField.cursorPosition === 0) {
// It reset to 0 for some reason, go back to the end // It reset to 0 for some reason, go back to the end
messageInputField.cursorPosition = messageInputField.length messageInputField.cursorPosition = messageInputField.length