diff --git a/ui/nim-status-client.pro b/ui/nim-status-client.pro index 05c7f57018..ebac89064f 100644 --- a/ui/nim-status-client.pro +++ b/ui/nim-status-client.pro @@ -412,6 +412,7 @@ DISTFILES += \ shared/img/loading.svg \ shared/img/status-logo.png \ shared/qmldir \ + shared/status/StatusChatInputTextFormationAction.qml \ shared/status/StatusEmojiSuggestionPopup.qml \ shared/status/StatusInputListPopup.qml \ shared/status/StatusRadioButtonRow.qml \ diff --git a/ui/shared/status/StatusChatInput.qml b/ui/shared/status/StatusChatInput.qml index 56d9fa7b45..f14eacee76 100644 --- a/ui/shared/status/StatusChatInput.qml +++ b/ui/shared/status/StatusChatInput.qml @@ -220,6 +220,17 @@ Rectangle { messageInputField.deselect() formatInputMessage() } + function unwrapSelection(unwrapWith) { + if (messageInputField.selectionStart - messageInputField.selectionEnd === 0) return + + const text = messageInputField.getText(messageInputField.selectionStart, messageInputField.selectionEnd) + const changedString = text.replace(unwrapWith, '').replace(unwrapWith, '') + messageInputField.remove(messageInputField.selectionStart, messageInputField.selectionEnd) + insertInTextInput(messageInputField.selectionStart, changedString) + + messageInputField.deselect() + formatInputMessage() + } function getPlainText() { const textWithoutMention = messageInputField.text.replace(/(@([a-z\.]+(\ ?[a-z]+\ ?[a-z]+)?))<\/span>/ig, "\[\[mention\]\]$1\[\[mention\]\]") @@ -843,44 +854,39 @@ Rectangle { StatusTextFormatMenu { id: textFormatMenu function surroundedBy(chars) { - return messageInputField.selectedText.trim().startsWith(chars) && - messageInputField.selectedText.trim().endsWith(chars) + const firstIndex = messageInputField.selectedText.trim().indexOf(chars) + if (firstIndex === -1) { + return false + } + + return messageInputField.selectedText.trim().lastIndexOf(chars) > firstIndex } - Action { + StatusChatInputTextFormationAction { + wrapper: "**" icon.name: "format-text-bold" - icon.width: 12 - icon.height: 16 - onTriggered: wrapSelection("**") //% "Bold" text: qsTrId("bold") - checked: textFormatMenu.surroundedBy("**") } - Action { + StatusChatInputTextFormationAction { + wrapper: "*" icon.name: "format-text-italic" - icon.width: 12 - icon.height: 16 - onTriggered: wrapSelection("*") //% "Italic" text: qsTrId("italic") checked: textFormatMenu.surroundedBy("*") && !textFormatMenu.surroundedBy("**") } - Action { + StatusChatInputTextFormationAction { + wrapper: "~~" icon.name: "format-text-strike-through" - icon.width: 20 icon.height: 18 - onTriggered: wrapSelection("~~") //% "Strikethrough" text: qsTrId("strikethrough") - checked: textFormatMenu.surroundedBy("~~") } - Action { + StatusChatInputTextFormationAction { + wrapper: "`" icon.name: "format-text-code" - icon.width: 20 icon.height: 18 - onTriggered: wrapSelection("`") //% "Code" text: qsTrId("code") - checked: textFormatMenu.surroundedBy("`") } } } diff --git a/ui/shared/status/StatusChatInputTextFormationAction.qml b/ui/shared/status/StatusChatInputTextFormationAction.qml new file mode 100644 index 0000000000..654d56f32f --- /dev/null +++ b/ui/shared/status/StatusChatInputTextFormationAction.qml @@ -0,0 +1,10 @@ +import QtQuick 2.13 +import QtQuick.Controls 2.13 + +Action { + property string wrapper + icon.width: 12 + icon.height: 16 + onTriggered: textFormatMenu.surroundedBy(wrapper) ? unwrapSelection(wrapper) : wrapSelection(wrapper) + checked: textFormatMenu.surroundedBy(wrapper) +}