fix(@desktop/chat): adjust mentions handing logic

Fixes #7099
This commit is contained in:
PavelS 2022-09-06 12:45:16 +03:00 committed by PavelS
parent bdc072e337
commit cdfbb4ac87
1 changed files with 77 additions and 11 deletions

View File

@ -133,6 +133,9 @@ Rectangle {
// set to true when pasted text comes from this component (was copied within this component) // set to true when pasted text comes from this component (was copied within this component)
property bool internalPaste: false property bool internalPaste: false
property int leftOfMentionIndex: -1
property int rightOfMentionIndex: -1
readonly property StateGroup emojiPopupTakeover: StateGroup { readonly property StateGroup emojiPopupTakeover: StateGroup {
states: State { states: State {
when: control.emojiPopupOpened when: control.emojiPopupOpened
@ -147,6 +150,32 @@ Rectangle {
} }
} }
} }
function updateMentionsPositions() {
if (mentionsPos.length == 0) {
return
}
const unformattedText = messageInputField.getText(0, messageInputField.length)
if (!unformattedText.includes("@")) {
return
}
const keyEvent = messageInputField.keyEvent
if ((keyEvent.key === Qt.Key_Right) || (keyEvent.key === Qt.Key_Left)
|| (keyEvent.key === Qt.Key_Up) || (keyEvent.key === Qt.Key_Down)) {
return
}
for (var k = 0; k < mentionsPos.length; k++) {
const aliasIndex = unformattedText.indexOf(mentionsPos[k].name)
if ((aliasIndex !== -1) && (aliasIndex - 1 !== mentionsPos[k].leftIndex)) {
mentionsPos[k].leftIndex = aliasIndex - 1
mentionsPos[k].rightIndex = aliasIndex + mentionsPos[k].name.length
}
}
}
} }
function insertInTextInput(start, text) { function insertInTextInput(start, text) {
@ -247,6 +276,24 @@ Rectangle {
} }
} }
const symbolPressed = event.text.length > 0 &&
event.key !== Qt.Key_Backspace &&
event.key !== Qt.Key_Delete &&
event.key !== Qt.Key_Escape
if (mentionsPos.length > 0 && symbolPressed) {
for (var i = 0; i < mentionsPos.length; i++) {
if (messageInputField.cursorPosition === mentionsPos[i].leftIndex) {
d.leftOfMentionIndex = i
event.accepted = true
return
} else if (messageInputField.cursorPosition === mentionsPos[i].rightIndex) {
d.rightOfMentionIndex = i
event.accepted = true
return
}
}
}
if (event.key === Qt.Key_Tab) { if (event.key === Qt.Key_Tab) {
if (checkTextInsert()) { if (checkTextInsert()) {
event.accepted = true; event.accepted = true;
@ -494,6 +541,23 @@ Rectangle {
// the text doesn't get registered to the textarea fast enough // the text doesn't get registered to the textarea fast enough
// we can only get it in the `released` event // we can only get it in the `released` event
let eventText = event.text
if(event.key === Qt.Key_Space) {
eventText = "&nbsp;"
}
if(d.rightOfMentionIndex !== -1) {
messageInputField.insert(mentionsPos[d.rightOfMentionIndex].rightIndex, eventText)
d.rightOfMentionIndex = -1
}
if(d.leftOfMentionIndex !== -1) {
messageInputField.insert(mentionsPos[d.leftOfMentionIndex].leftIndex, eventText)
d.leftOfMentionIndex = -1
}
messageInputField.readOnly = false messageInputField.readOnly = false
if (d.internalPaste) { if (d.internalPaste) {
@ -1019,6 +1083,16 @@ Rectangle {
leftPadding: 0 leftPadding: 0
padding: 0 padding: 0
Keys.onPressed: { Keys.onPressed: {
if (mentionsPos.length > 0) {
for (var i = 0; i < mentionsPos.length; i++) {
if ((messageInputField.cursorPosition === (mentionsPos[i].leftIndex))
&& (event.key === Qt.Key_Delete)) {
messageInputField.remove(mentionsPos[i].rightIndex, mentionsPos[i].leftIndex)
mentionsPos.pop(i)
}
}
}
keyEvent = event; keyEvent = event;
onKeyPress(event) onKeyPress(event)
cursorWhenPressed = cursorPosition; cursorWhenPressed = cursorPosition;
@ -1062,17 +1136,6 @@ Rectangle {
} }
} }
} }
if ((mentionsPos.length > 0) && (cursorPosition < length) && getText(cursorPosition, length).includes("@")
&& (keyEvent.key !== Qt.Key_Right) && (keyEvent.key !== Qt.Key_Left) && (keyEvent.key !== Qt.Key_Up)
&& (keyEvent.key !== Qt.Key_Down)) {
var unformattedText = getText(cursorPosition, length);
for (var k = 0; k < mentionsPos.length; k++) {
if ((unformattedText.indexOf(mentionsPos[k].name) !== -1) && (unformattedText.indexOf(mentionsPos[k].name) !== mentionsPos[k].leftIndex)) {
mentionsPos[k].leftIndex = (cursorPosition + unformattedText.indexOf(mentionsPos[k].name) - 1);
mentionsPos[k].rightIndex = (cursorPosition + unformattedText.indexOf(mentionsPos[k].name) + mentionsPos[k].name.length);
}
}
}
inputScrollView.ensureVisible(cursorRectangle) inputScrollView.ensureVisible(cursorRectangle)
} }
@ -1097,6 +1160,9 @@ Rectangle {
var removeFrom = (cursorPosition < messageLimit) ? cursorWhenPressed : messageLimit; var removeFrom = (cursorPosition < messageLimit) ? cursorWhenPressed : messageLimit;
remove(removeFrom, cursorPosition); remove(removeFrom, cursorPosition);
} }
d.updateMentionsPositions()
messageLengthLimit.remainingChars = (messageLimit - length); messageLengthLimit.remainingChars = (messageLimit - length);
} }