From 7564b40c3ecbbded0458f3befd1783560dd07969 Mon Sep 17 00:00:00 2001 From: miruka Date: Sat, 31 Oct 2020 02:59:56 -0400 Subject: [PATCH] Fix copying multi-line selected rich text For rich text (rendered HTML) fields and areas, the selectedText property for some reason contains "\u2028" or "\u2029" characters that look like newlines in Qt programs but are just invisible anywhere else. Translate them to normal "\n". --- src/gui/Base/HSelectableLabel.qml | 5 +++++ src/gui/Base/HTextArea.qml | 5 +++++ src/gui/Base/HTextContextMenu.qml | 4 ++-- src/gui/Base/HTextField.qml | 5 +++++ src/gui/DebugConsole.qml | 8 ++++---- src/gui/Pages/Chat/Composer/MessageArea.qml | 4 ++-- src/gui/Pages/Chat/Timeline/EventContent.qml | 8 ++++---- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/gui/Base/HSelectableLabel.qml b/src/gui/Base/HSelectableLabel.qml index fb18dbda..ccb4b60d 100644 --- a/src/gui/Base/HSelectableLabel.qml +++ b/src/gui/Base/HSelectableLabel.qml @@ -9,6 +9,11 @@ TextEdit { property bool enableLinkActivation: true + // For rich text, selectedText returns some weird invisible characters + // instead of real newlines + readonly property string selectedPlainText: + selectedText.replace(/[\u2028\u2029]/g, "\n") + function selectWordAt(position) { label.cursorPosition = positionAt(position.x, position.y) label.selectWord() diff --git a/src/gui/Base/HTextArea.qml b/src/gui/Base/HTextArea.qml index 5881335a..54b804cb 100644 --- a/src/gui/Base/HTextArea.qml +++ b/src/gui/Base/HTextArea.qml @@ -30,6 +30,11 @@ TextArea { property string previousDefaultText: "" // private + // For rich text, selectedText returns some weird invisible characters + // instead of real newlines + readonly property string selectedPlainText: + selectedText.replace(/[\u2028\u2029]/g, "\n") + property bool enableCustomImagePaste: false signal customImagePaste() diff --git a/src/gui/Base/HTextContextMenu.qml b/src/gui/Base/HTextContextMenu.qml index f4a5ffde..b3f7ddb3 100644 --- a/src/gui/Base/HTextContextMenu.qml +++ b/src/gui/Base/HTextContextMenu.qml @@ -50,14 +50,14 @@ HMenu { HMenuItem { icon.name: "cut-text" text: qsTr("Cut") - enabled: control.selectedText + enabled: control.selectedPlainText onTriggered: control.cut() } HMenuItem { icon.name: "copy-text" text: qsTr("Copy") - enabled: control.selectedText + enabled: control.selectedPlainText onTriggered: control.copy() } diff --git a/src/gui/Base/HTextField.qml b/src/gui/Base/HTextField.qml index a760d950..459e67a0 100644 --- a/src/gui/Base/HTextField.qml +++ b/src/gui/Base/HTextField.qml @@ -30,6 +30,11 @@ TextField { property string previousDefaultText: "" // private + // For rich text, selectedText returns some weird invisible characters + // instead of real newlines + readonly property string selectedPlainText: + selectedText.replace(/[\u2028\u2029]/g, "\n") + function reset() { clear() text = Qt.binding(() => defaultText || "") diff --git a/src/gui/DebugConsole.qml b/src/gui/DebugConsole.qml index a2be97e1..25b9a59e 100644 --- a/src/gui/DebugConsole.qml +++ b/src/gui/DebugConsole.qml @@ -208,9 +208,9 @@ HDrawer { Layout.fillWidth: true - onSelectedTextChanged: if (selectedText) { + onSelectedTextChanged: if (selectedPlainText) { selectedOutputDelegateIndex = model.index - selectedOutputText = selectedText + selectedOutputText = selectedPlainText } else if (selectedOutputDelegateIndex === model.index) { selectedOutputDelegateIndex = -1 selectedOutputText = "" @@ -249,7 +249,7 @@ HDrawer { icon.name: "copy-text" text: qsTr("Copy") onTriggered: { - if (delegate.selectedText) { + if (delegate.selectedPlainText) { delegate.copy() return } @@ -337,7 +337,7 @@ HDrawer { Keys.onPressed: ev => { if ( ev.matches(StandardKey.Copy) && - ! inputArea.selectedText && + ! inputArea.selectedPlainText && selectedOutputText ) { ev.accepted = true diff --git a/src/gui/Pages/Chat/Composer/MessageArea.qml b/src/gui/Pages/Chat/Composer/MessageArea.qml index 5b4a594c..9d2a932b 100644 --- a/src/gui/Pages/Chat/Composer/MessageArea.qml +++ b/src/gui/Pages/Chat/Composer/MessageArea.qml @@ -225,7 +225,7 @@ HTextArea { if (ev.text && autoCompletionOpen) acceptAutoCompletion() if (ev.matches(StandardKey.Copy) && - ! area.selectedText && + ! area.selectedPlainText && eventList && (eventList.selectedCount || eventList.currentIndex !== -1)) { @@ -237,7 +237,7 @@ HTextArea { // FIXME: buggy // if (ev.modifiers === Qt.NoModifier && // ev.key === Qt.Key_Backspace && - // ! area.selectedText) + // ! area.selectedPlainText) // { // ev.accepted = true // area.remove( diff --git a/src/gui/Pages/Chat/Timeline/EventContent.qml b/src/gui/Pages/Chat/Timeline/EventContent.qml index 8512dfb4..21af980e 100644 --- a/src/gui/Pages/Chat/Timeline/EventContent.qml +++ b/src/gui/Pages/Chat/Timeline/EventContent.qml @@ -82,7 +82,7 @@ HRowLayout { window.settings.maxMessageCharactersPerLine ) - readonly property alias selectedText: contentLabel.selectedText + readonly property alias selectedText: contentLabel.selectedPlainText spacing: theme.chat.message.horizontalSpacing @@ -182,9 +182,9 @@ HRowLayout { Layout.maximumWidth: eventContent.maxMessageWidth Layout.fillWidth: true - onSelectedTextChanged: if (selectedText) { + onSelectedTextChanged: if (selectedPlainText) { eventList.delegateWithSelectedText = model.id - eventList.selectedText = selectedText + eventList.selectedText = selectedPlainText } else if (eventList.delegateWithSelectedText === model.id) { eventList.delegateWithSelectedText = "" eventList.selectedText = "" @@ -274,7 +274,7 @@ HRowLayout { radius: theme.chat.message.radius z: -100 color: eventDelegate.checked && - ! contentLabel.selectedText && + ! contentLabel.selectedPlainText && ! mousePointHandler.active && ! mouseShiftPointHandler.active ? theme.chat.message.checkedBackground :