From dfc7a6a951cc688928076fbf5a9e7c2224618bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Mon, 22 Aug 2022 11:54:19 +0200 Subject: [PATCH] fix(StatusChatInput): Remove formatting when pasting, pasting on selection fixed Fixes #7015 Fixes #7097 --- ui/imports/shared/status/StatusChatInput.qml | 82 ++++++++++++-------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/ui/imports/shared/status/StatusChatInput.qml b/ui/imports/shared/status/StatusChatInput.qml index 344289d45c..2a19dc283c 100644 --- a/ui/imports/shared/status/StatusChatInput.qml +++ b/ui/imports/shared/status/StatusChatInput.qml @@ -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 { } } -} \ No newline at end of file +}