fix(StatusChatInput): Remove formatting when pasting, pasting on selection fixed

Fixes #7015
Fixes #7097
This commit is contained in:
Michał Cieślak 2022-08-22 11:54:19 +02:00 committed by Michał
parent 037937a04f
commit dfc7a6a951
1 changed files with 51 additions and 31 deletions

View File

@ -39,9 +39,8 @@ Rectangle {
property bool emojiPopupOpened: false
property bool closeGifPopupAfterSelection: true
property bool emojiEvent: false;
property bool paste: false;
property bool isColonPressed: false;
property bool emojiEvent: false
property bool isColonPressed: false
property bool isReply: false
property string replyMessageId: replyArea.messageId
@ -53,7 +52,6 @@ Rectangle {
property var recentStickers
property var stickerPackList
property int extraHeightFactor: calculateExtraHeightFactor()
property int messageLimit: 2000
property int messageLimitVisible: 200
@ -124,9 +122,16 @@ Rectangle {
color: Style.current.transparent
function calculateExtraHeightFactor() {
const factor = (messageInputField.length / 500) + 1;
return (factor > 5) ? 5 : factor;
QtObject {
id: d
//mentions helper properties
property string copiedTextPlain: ""
property string copiedTextFormatted: ""
property int copyTextStart: 0
// set to true when pasted text comes from this component (was copied within this component)
property bool internalPaste: false
}
function insertInTextInput(start, text) {
@ -190,7 +195,7 @@ Rectangle {
// It reset to 0 for some reason, go back to the end
messageInputField.cursorPosition = messageInputField.length
}
mentionsPos.push({"name": aliasName,"leftIndex": lastAtPosition, "rightIndex": (lastAtPosition+aliasName.length+1)});
mentionsPos.push({name: aliasName, leftIndex: lastAtPosition, rightIndex: (lastAtPosition+aliasName.length + 1)});
}
function isUploadFilePressed(event) {
@ -262,18 +267,26 @@ Rectangle {
if ((event.key === Qt.Key_C) && (event.modifiers & Qt.ControlModifier)) {
if (messageInputField.selectedText !== "") {
copiedTextPlain = messageInputField.getText(messageInputField.selectionStart, messageInputField.selectionEnd);
copiedTextFormatted = messageInputField.getFormattedText(messageInputField.selectionStart, messageInputField.selectionEnd);
d.copiedTextPlain = messageInputField.getText(
messageInputField.selectionStart, messageInputField.selectionEnd)
d.copiedTextFormatted = messageInputField.getFormattedText(
messageInputField.selectionStart, messageInputField.selectionEnd)
}
}
if ((event.key === Qt.Key_V) && (event.modifiers & Qt.ControlModifier)) {
if (copiedTextPlain === QClipboardProxy.text) {
copyTextStart = messageInputField.cursorPosition;
paste = true;
messageInputField.remove(messageInputField.selectionStart, messageInputField.selectionEnd)
// cursor position must be stored in a helper property because setting readonly to true causes change
// of the cursor position to the end of the input
d.copyTextStart = messageInputField.cursorPosition
messageInputField.readOnly = true
if (d.copiedTextPlain === QClipboardProxy.text) {
d.internalPaste = true
} else {
copiedTextPlain = "";
copiedTextFormatted = "";
d.copiedTextPlain = ""
d.copiedTextFormatted = ""
}
}
@ -425,11 +438,6 @@ Rectangle {
}
}
//mentions helper properties
property int copyTextStart: 0
property string copiedTextPlain: ""
property string copiedTextFormatted: ""
Instantiator {
id: dummyContactList
model: control.usersStore ? control.usersStore.usersModel : []
@ -446,25 +454,37 @@ Rectangle {
// the text doesn't get registered to the textarea fast enough
// we can only get it in the `released` event
if (paste) {
if (copiedTextPlain.includes("@")) {
copiedTextFormatted = copiedTextFormatted.replace(/underline/g, "none").replace(/span style="/g, "span style=\" text-decoration:none;")
messageInputField.readOnly = false
if (d.internalPaste) {
if (d.copiedTextPlain.includes("@")) {
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 (copiedTextPlain.indexOf(name) > -1) {
if (d.copiedTextPlain.indexOf(name) > -1) {
const subStr = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
const regex = new RegExp(subStr, 'gi')
let result = null
while ((result = regex.exec(copiedTextPlain))) {
mentionsPos.push({"name": name, "leftIndex": (result.index + copyTextStart - 1), "rightIndex": (result.index + copyTextStart + name.length)})
while ((result = regex.exec(d.copiedTextPlain))) {
const mention = {
name: name,
leftIndex: (result.index + d.copyTextStart - 1),
rightIndex: (result.index + d.copyTextStart + name.length)
}
mentionsPos.push(mention)
}
}
}
}
messageInputField.remove(copyTextStart, (copyTextStart + copiedTextPlain.length))
insertInTextInput(copyTextStart, copiedTextFormatted)
paste = false
const prevLength = messageInputField.length
insertInTextInput(d.copyTextStart, d.copiedTextFormatted)
messageInputField.cursorPosition = d.copyTextStart + messageInputField.length - prevLength
d.internalPaste = false
} else if ((event.key === Qt.Key_V) && (event.modifiers & Qt.ControlModifier)) {
insertInTextInput(d.copyTextStart, QClipboardProxy.text)
messageInputField.cursorPosition = d.copyTextStart + QClipboardProxy.text.length
}
if (event.key !== Qt.Key_Escape) {
@ -549,7 +569,7 @@ Rectangle {
if (index2 >= 0 && message.cursor > 0) {
const emojiPart = message.data.substr(index2, message.cursor);
if (emojiPart.length > 2) {
const emojis = EmojiJSON.emoji_json.filter(function (emoji) {
const emojis = StatusQUtils.Emoji.emojiJSON.emoji_json.filter(function (emoji) {
return emoji.name.includes(emojiPart) ||
emoji.shortname.includes(emojiPart) ||
emoji.aliases.some(a => a.includes(emojiPart))
@ -1243,4 +1263,4 @@ Rectangle {
}
}
}
}