From 973830349d10f9f80535b34d401b03c7b42b4b4f Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Wed, 3 Mar 2021 16:13:41 +0100 Subject: [PATCH] uiux(StatusChatInput): toggle markdown from selection Closes #1930 --- ui/shared/status/StatusChatInput.qml | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/ui/shared/status/StatusChatInput.qml b/ui/shared/status/StatusChatInput.qml index 53073798ec..56d9fa7b45 100644 --- a/ui/shared/status/StatusChatInput.qml +++ b/ui/shared/status/StatusChatInput.qml @@ -194,8 +194,29 @@ Rectangle { function wrapSelection(wrapWith) { if (messageInputField.selectionStart - messageInputField.selectionEnd === 0) return - insertInTextInput(messageInputField.selectionStart, wrapWith); - insertInTextInput(messageInputField.selectionEnd, wrapWith); + + let selection = messageInputField.selectedText + let availableAnnotations = ["**", "*", "~~", "`"] + let performWrap = true + + // First we remove all existing annotations of the selections, so that users can easily + // switch from, for example, italic to bold + for (let i = 0; i < availableAnnotations.length; i++) { + let annotation = availableAnnotations[i] + + if (selection.startsWith(annotation) && selection.endsWith(annotation)) { + messageInputField.remove(messageInputField.selectionEnd, messageInputField.selectionEnd - annotation.length); + messageInputField.remove(messageInputField.selectionStart, messageInputField.selectionStart + annotation.length); + performWrap = annotation !== wrapWith; + break; + } + } + + if (performWrap) { + insertInTextInput(messageInputField.selectionStart, wrapWith); + insertInTextInput(messageInputField.selectionEnd, wrapWith); + } + messageInputField.deselect() formatInputMessage() }