From 0c34335d1c2acca8b35090489f86c9974aa46105 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 21 Apr 2021 15:46:58 -0400 Subject: [PATCH] feat: show formation hightlighted even when not selecting the stars --- ui/shared/status/StatusChatInput.qml | 41 ++++++++++++++++--- .../StatusChatInputTextFormationAction.qml | 4 +- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/ui/shared/status/StatusChatInput.qml b/ui/shared/status/StatusChatInput.qml index f14eacee76..0a06719e53 100644 --- a/ui/shared/status/StatusChatInput.qml +++ b/ui/shared/status/StatusChatInput.qml @@ -220,12 +220,17 @@ Rectangle { messageInputField.deselect() formatInputMessage() } - function unwrapSelection(unwrapWith) { + function unwrapSelection(unwrapWith, selectedTextWithFormationChars) { 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) + selectedTextWithFormationChars = selectedTextWithFormationChars.trim() + // Check if the selectedTextWithFormationChars has formation chars and if so, calculate how many so we can adapt the start and end pos + const selectTextDiff = (selectedTextWithFormationChars.length - messageInputField.selectedText.length) / 2 + + const changedString = selectedTextWithFormationChars.replace(unwrapWith, '').replace(unwrapWith, '') + + messageInputField.remove(messageInputField.selectionStart - selectTextDiff, messageInputField.selectionEnd + selectTextDiff) + insertInTextInput(messageInputField.selectionStart, changedString) messageInputField.deselect() @@ -852,14 +857,38 @@ Rectangle { } StatusTextFormatMenu { + readonly property var formationChars: (["*", "`", "~"]) + property string selectedTextWithFormationChars: { + let i = 1 + let text = "" + while(true) { + if (messageInputField.selectionStart - i < 0 && messageInputField.selectionEnd + i > messageInputField.length) { + break + } + + text = messageInputField.getText(messageInputField.selectionStart - i, messageInputField.selectionEnd + i) + + if (!formationChars.includes(text.charAt(0)) || + !formationChars.includes(text.charAt(text.length - 1))) { + break + } + i++ + } + return text + } + id: textFormatMenu function surroundedBy(chars) { - const firstIndex = messageInputField.selectedText.trim().indexOf(chars) + if (selectedTextWithFormationChars === "") { + return false + } + + const firstIndex = selectedTextWithFormationChars.indexOf(chars) if (firstIndex === -1) { return false } - return messageInputField.selectedText.trim().lastIndexOf(chars) > firstIndex + return selectedTextWithFormationChars.lastIndexOf(chars) > firstIndex } StatusChatInputTextFormationAction { wrapper: "**" diff --git a/ui/shared/status/StatusChatInputTextFormationAction.qml b/ui/shared/status/StatusChatInputTextFormationAction.qml index 654d56f32f..de140539aa 100644 --- a/ui/shared/status/StatusChatInputTextFormationAction.qml +++ b/ui/shared/status/StatusChatInputTextFormationAction.qml @@ -5,6 +5,8 @@ Action { property string wrapper icon.width: 12 icon.height: 16 - onTriggered: textFormatMenu.surroundedBy(wrapper) ? unwrapSelection(wrapper) : wrapSelection(wrapper) + onTriggered: textFormatMenu.surroundedBy(wrapper) ? + unwrapSelection(wrapper, textFormatMenu.selectedTextWithFormationChars) : + wrapSelection(wrapper) checked: textFormatMenu.surroundedBy(wrapper) }