fix(@desktop/chat): add cut mention handle

Add mentions copy logic with pasting mentions with pubKey information

Fixes #7110
This commit is contained in:
PavelS 2022-09-29 23:04:30 +03:00 committed by Lukáš Tinkl
parent 2da05c0025
commit 8bd194eb48
1 changed files with 35 additions and 26 deletions

View File

@ -126,6 +126,7 @@ Rectangle {
//mentions helper properties //mentions helper properties
property string copiedTextPlain: "" property string copiedTextPlain: ""
property string copiedTextFormatted: "" property string copiedTextFormatted: ""
property var copiedMentionsPos: []
property int copyTextStart: 0 property int copyTextStart: 0
// 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)
@ -149,6 +150,21 @@ Rectangle {
} }
} }
function copyMentions(start, end) {
copiedMentionsPos = []
for (let k = 0; k < mentionsPos.length; k++) {
if (mentionsPos[k].leftIndex >= start && mentionsPos[k].rightIndex <= end) {
const mention = {
name: mentionsPos[k].name,
pubKey: mentionsPos[k].pubKey,
leftIndex: mentionsPos[k].leftIndex - start,
rightIndex: mentionsPos[k].rightIndex - start
}
copiedMentionsPos.push(mention)
}
}
}
function sortMentions() { function sortMentions() {
if (mentionsPos.length < 2) { if (mentionsPos.length < 2) {
return return
@ -340,16 +356,17 @@ Rectangle {
} }
} }
if ((event.key === Qt.Key_C) && (event.modifiers & Qt.ControlModifier)) { if (event.matches(StandardKey.Copy) || event.matches(StandardKey.Cut)) {
if (messageInputField.selectedText !== "") { if (messageInputField.selectedText !== "") {
d.copiedTextPlain = messageInputField.getText( d.copiedTextPlain = messageInputField.getText(
messageInputField.selectionStart, messageInputField.selectionEnd) messageInputField.selectionStart, messageInputField.selectionEnd)
d.copiedTextFormatted = messageInputField.getFormattedText( d.copiedTextFormatted = messageInputField.getFormattedText(
messageInputField.selectionStart, messageInputField.selectionEnd) messageInputField.selectionStart, messageInputField.selectionEnd)
d.copyMentions(messageInputField.selectionStart, messageInputField.selectionEnd)
} }
} }
if ((event.key === Qt.Key_V) && (event.modifiers & Qt.ControlModifier)) { if (event.matches(StandardKey.Paste)) {
messageInputField.remove(messageInputField.selectionStart, messageInputField.selectionEnd) messageInputField.remove(messageInputField.selectionStart, messageInputField.selectionEnd)
// cursor position must be stored in a helper property because setting readonly to true causes change // cursor position must be stored in a helper property because setting readonly to true causes change
@ -362,6 +379,7 @@ Rectangle {
} else { } else {
d.copiedTextPlain = "" d.copiedTextPlain = ""
d.copiedTextFormatted = "" d.copiedTextFormatted = ""
d.copiedMentionsPos = []
} }
} }
@ -555,15 +573,6 @@ Rectangle {
} }
} }
Instantiator {
id: dummyContactList
model: control.usersStore ? control.usersStore.usersModel : []
delegate: QtObject {
property string contactName: model.displayName
}
}
function onRelease(event) { function onRelease(event) {
if (event.key === Qt.Key_Backspace && textFormatMenu.opened) { if (event.key === Qt.Key_Backspace && textFormatMenu.opened) {
textFormatMenu.close() textFormatMenu.close()
@ -593,31 +602,31 @@ Rectangle {
if (d.internalPaste) { if (d.internalPaste) {
if (d.copiedTextPlain.includes("@")) { if (d.copiedTextPlain.includes("@")) {
d.copiedTextFormatted = d.copiedTextFormatted.replace(/underline/g, "none").replace(/span style="/g, "span style=\" text-decoration:none;") d.copiedTextFormatted = d.copiedTextFormatted.replace(/underline/g, "none").replace(/span style="/g, "span style=\" text-decoration:none;")
for (let j = 0; j < dummyContactList.count; j++) {
const name = dummyContactList.objectAt(j).contactName
if (d.copiedTextPlain.indexOf(name) > -1) { let lastFoundIndex = -1
const subStr = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') for (let j = 0; j < d.copiedMentionsPos.length; j++) {
const regex = new RegExp(subStr, 'gi') const name = d.copiedMentionsPos[j].name
let result = null const indexOfName = d.copiedTextPlain.indexOf(name, lastFoundIndex)
while ((result = regex.exec(d.copiedTextPlain))) { lastFoundIndex += name.length
if (indexOfName === d.copiedMentionsPos[j].leftIndex + 1) {
const mention = { const mention = {
name: name, name: name,
leftIndex: (result.index + d.copyTextStart - 1), pubKey: d.copiedMentionsPos[j].pubKey,
rightIndex: (result.index + d.copyTextStart + name.length) leftIndex: (d.copiedMentionsPos[j].leftIndex + d.copyTextStart - 1),
rightIndex: (d.copiedMentionsPos[j].leftIndex + d.copyTextStart + name.length)
} }
mentionsPos.push(mention) mentionsPos.push(mention)
d.sortMentions() d.sortMentions()
} }
} }
} }
}
const prevLength = messageInputField.length const prevLength = messageInputField.length
insertInTextInput(d.copyTextStart, d.copiedTextFormatted) insertInTextInput(d.copyTextStart, d.copiedTextFormatted)
messageInputField.cursorPosition = d.copyTextStart + messageInputField.length - prevLength messageInputField.cursorPosition = d.copyTextStart + messageInputField.length - prevLength
d.internalPaste = false d.internalPaste = false
} else if ((event.key === Qt.Key_V) && (event.modifiers & Qt.ControlModifier)) { } else if (event.matches(StandardKey.Paste)) {
insertInTextInput(d.copyTextStart, QClipboardProxy.text) insertInTextInput(d.copyTextStart, QClipboardProxy.text)
messageInputField.cursorPosition = d.copyTextStart + QClipboardProxy.text.length messageInputField.cursorPosition = d.copyTextStart + QClipboardProxy.text.length
} }