From d2b6bf9310df088c89abcca7c1df42abc3999b18 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 5 May 2021 13:54:35 -0400 Subject: [PATCH] fix(Input): fix mention formation on rewriting a mention Fixes #2325 --- ui/shared/status/StatusChatInput.qml | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ui/shared/status/StatusChatInput.qml b/ui/shared/status/StatusChatInput.qml index 6c1592b064..5f0e5c7399 100644 --- a/ui/shared/status/StatusChatInput.qml +++ b/ui/shared/status/StatusChatInput.qml @@ -258,9 +258,9 @@ Rectangle { interrogateMessage(); } else { if (event.key === Qt.Key_Asterisk || - event.key === Qt.Key_QuoteLeft || - event.key === Qt.Key_Space || - event.key === Qt.Key_AsciiTilde) { + event.key === Qt.Key_QuoteLeft || + event.key === Qt.Key_Space || + event.key === Qt.Key_AsciiTilde) { formatInputMessage() } } @@ -424,7 +424,7 @@ Rectangle { const index = message.data.lastIndexOf(':', message.cursor); if (index >= 0) { emojiEvent = validSubstr(message.data.substr(index, message.cursor - index)); - } + } } 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 position = 0; let text = "" + let cursorPositionMention = 0 const spanPlusAlias = `${Constants.mentionSpanTag}@${aliasName} ` if (currentText === "@") { position = nameLen text = spanPlusAlias } else { 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) text = `${left} ${spanPlusAlias}${right}` } messageInputField.text = parseBackText(text) - messageInputField.cursorPosition = lastAtPosition + aliasName.length + 2 + messageInputField.cursorPosition = lastAtPosition + aliasName.length + 2 - cursorPositionMention if (messageInputField.cursorPosition === 0) { // It reset to 0 for some reason, go back to the end messageInputField.cursorPosition = messageInputField.length