diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index 78c2902a30..7994ff31ed 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -9,7 +9,7 @@ import ../../status/ens as status_ens import ../../status/chat/[chat, message] import ../../status/profile/profile import web3/[conversions, ethtypes] -import views/[channels_list, message_list, chat_item, suggestions_list, reactions, stickers, groups, transactions, communities, community_list, community_item, activity_notification_list] +import views/[channels_list, message_list, chat_item, suggestions_list, reactions, stickers, groups, transactions, communities, community_list, community_item, format_input, activity_notification_list] import ../utils/image_utils import ../../status/tasks/[qt, task_runner_impl] import ../../status/tasks/marathon/mailserver/worker @@ -129,6 +129,7 @@ QtObject: type ChatsView* = ref object of QAbstractListModel status: Status + formatInputView: FormatInputView chats*: ChannelsList currentSuggestions*: SuggestionsList activityNotificationList*: ActivityNotificationList @@ -155,6 +156,7 @@ QtObject: proc delete(self: ChatsView) = self.chats.delete + self.formatInputView.delete self.activeChannel.delete self.contextChannel.delete self.currentSuggestions.delete @@ -176,6 +178,8 @@ QtObject: proc newChatsView*(status: Status): ChatsView = new(result, delete) result.status = status + result.formatInputView = newFormatInputView() + result.connected = false result.chats = newChannelsList(status) result.activeChannel = newChatItemView(status) @@ -196,6 +200,10 @@ QtObject: result.setup() + proc getFormatInput(self: ChatsView): QVariant {.slot.} = newQVariant(self.formatInputView) + QtProperty[QVariant] formatInputView: + read = getFormatInput + proc getMessageListIndexById(self: ChatsView, id: string): int proc getChannel*(self: ChatsView, index: int): Chat = @@ -993,53 +1001,6 @@ QtObject: proc requestAllHistoricMessagesResult(self: ChatsView, resultEncoded: string) {.slot.} = self.setLoadingMessages(true) - proc formatInputStuff(self: ChatsView, regex: Regex, inputText: string): string = - var matches: seq[tuple[first, last: int]] = @[(-1, 0)] - - var resultTuple: tuple[first, last: int] - var start = 0 - var results: seq[tuple[first, last: int]] = @[] - - while true: - resultTuple = inputText.findBounds(regex, matches, start) - if (resultTuple[0] == -1): - break - start = resultTuple[1] + 1 - results.add(matches[0]) - - if (results.len == 0): - return "" - - var jsonString = "[" - var first = true - - for result in results: - if (not first): - jsonString = jsonString & "," - first = false - jsonString = jsonString & "[" & $result[0] & "," & $result[1] & "]" - - jsonString = jsonString & "]" - - return jsonString - - - proc formatInputItalic(self: ChatsView, inputText: string): string {.slot.} = - let italicRegex = re"""(?)(?)([^*]+)(?!<\/span>)\*""" - self.formatInputStuff(italicRegex, inputText) - - proc formatInputBold(self: ChatsView, inputText: string): string {.slot.} = - let boldRegex = re"""(?)\*\*(?!)([^*]+)(?!<\/span>)\*\*""" - self.formatInputStuff(boldRegex, inputText) - - proc formatInputStrikeThrough(self: ChatsView, inputText: string): string {.slot.} = - let strikeThroughRegex = re"""(?)~~(?!)([^*]+)(?!<\/span>)~~""" - self.formatInputStuff(strikeThroughRegex, inputText) - - proc formatInputCode(self: ChatsView, inputText: string): string {.slot.} = - let strikeThroughRegex = re"""(?)`(?!)([^*]+)(?!<\/span>)`""" - self.formatInputStuff(strikeThroughRegex, inputText) - proc createCommunityChannel*(self: ChatsView, communityId: string, name: string, description: string, categoryId: string): string {.slot.} = try: let chat = self.status.chat.createCommunityChannel(communityId, name, description) diff --git a/src/app/chat/views/format_input.nim b/src/app/chat/views/format_input.nim new file mode 100644 index 0000000000..7bd636580e --- /dev/null +++ b/src/app/chat/views/format_input.nim @@ -0,0 +1,60 @@ +import NimQml, Tables, json, sequtils, chronicles, times, re, sugar, strutils, os, strformat, algorithm + +logScope: + topics = "formatinput-view" + +QtObject: + type FormatInputView* = ref object of QObject + + proc setup(self: FormatInputView) = self.QObject.setup + proc delete*(self: FormatInputView) = self.QObject.delete + + proc newFormatInputView*(): FormatInputView = + new(result, delete) + result.setup + + proc formatInputStuff(self: FormatInputView, regex: Regex, inputText: string): string = + var matches: seq[tuple[first, last: int]] = @[(-1, 0)] + + var resultTuple: tuple[first, last: int] + var start = 0 + var results: seq[tuple[first, last: int]] = @[] + + while true: + resultTuple = inputText.findBounds(regex, matches, start) + if (resultTuple[0] == -1): + break + start = resultTuple[1] + 1 + results.add(matches[0]) + + if (results.len == 0): + return "" + + var jsonString = "[" + var first = true + + for result in results: + if (not first): + jsonString = jsonString & "," + first = false + jsonString = jsonString & "[" & $result[0] & "," & $result[1] & "]" + + jsonString = jsonString & "]" + + return jsonString + + proc formatInputItalic(self: FormatInputView, inputText: string): string {.slot.} = + let italicRegex = re"""(?)(?)([^*]+)(?!<\/span>)\*""" + self.formatInputStuff(italicRegex, inputText) + + proc formatInputBold(self: FormatInputView, inputText: string): string {.slot.} = + let boldRegex = re"""(?)\*\*(?!)([^*]+)(?!<\/span>)\*\*""" + self.formatInputStuff(boldRegex, inputText) + + proc formatInputStrikeThrough(self: FormatInputView, inputText: string): string {.slot.} = + let strikeThroughRegex = re"""(?)~~(?!)([^*]+)(?!<\/span>)~~""" + self.formatInputStuff(strikeThroughRegex, inputText) + + proc formatInputCode(self: FormatInputView, inputText: string): string {.slot.} = + let strikeThroughRegex = re"""(?)`(?!)([^*]+)(?!<\/span>)`""" + self.formatInputStuff(strikeThroughRegex, inputText) diff --git a/ui/shared/status/StatusChatInput.qml b/ui/shared/status/StatusChatInput.qml index 40eaae0065..1bbaf3444c 100644 --- a/ui/shared/status/StatusChatInput.qml +++ b/ui/shared/status/StatusChatInput.qml @@ -297,13 +297,13 @@ Rectangle { // TODO fix Those spans are added automatically by QT when you press space after an emoji. They break the code formation process // strikethrough - setFormatInInput(chatsModel.formatInputStrikeThrough, '', '', '~', 2) + setFormatInInput(chatsModel.formatInputView.formatInputStrikeThrough, '', '', '~', 2) // bold - setFormatInInput(chatsModel.formatInputBold, '', '', '*', 2) + setFormatInInput(chatsModel.formatInputView.formatInputBold, '', '', '*', 2) // code - setFormatInInput(chatsModel.formatInputCode, '', '', '`', 1) + setFormatInInput(chatsModel.formatInputView.formatInputCode, '', '', '`', 1) // italic - setFormatInInput(chatsModel.formatInputItalic, '', '', '*', 1) + setFormatInInput(chatsModel.formatInputView.formatInputItalic, '', '', '*', 1) } function onRelease(event) {