refactor: move input formatting code to its own file

This commit is contained in:
Iuri Matias 2021-06-15 14:31:44 -04:00
parent 6bcdb9ca54
commit 45b0e5e756
3 changed files with 73 additions and 52 deletions

View File

@ -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 style=" font-style:italic;">)([^*]+)(?!<\/span>)\*"""
self.formatInputStuff(italicRegex, inputText)
proc formatInputBold(self: ChatsView, inputText: string): string {.slot.} =
let boldRegex = re"""(?<!\>)\*\*(?!<span style=" font-weight:600;">)([^*]+)(?!<\/span>)\*\*"""
self.formatInputStuff(boldRegex, inputText)
proc formatInputStrikeThrough(self: ChatsView, inputText: string): string {.slot.} =
let strikeThroughRegex = re"""(?<!\>)~~(?!<span style=" text-decoration: line-through;">)([^*]+)(?!<\/span>)~~"""
self.formatInputStuff(strikeThroughRegex, inputText)
proc formatInputCode(self: ChatsView, inputText: string): string {.slot.} =
let strikeThroughRegex = re"""(?<!\>)`(?!<span style=" font-family:'monospace';">)([^*]+)(?!<\/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)

View File

@ -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 style=" font-style:italic;">)([^*]+)(?!<\/span>)\*"""
self.formatInputStuff(italicRegex, inputText)
proc formatInputBold(self: FormatInputView, inputText: string): string {.slot.} =
let boldRegex = re"""(?<!\>)\*\*(?!<span style=" font-weight:600;">)([^*]+)(?!<\/span>)\*\*"""
self.formatInputStuff(boldRegex, inputText)
proc formatInputStrikeThrough(self: FormatInputView, inputText: string): string {.slot.} =
let strikeThroughRegex = re"""(?<!\>)~~(?!<span style=" text-decoration: line-through;">)([^*]+)(?!<\/span>)~~"""
self.formatInputStuff(strikeThroughRegex, inputText)
proc formatInputCode(self: FormatInputView, inputText: string): string {.slot.} =
let strikeThroughRegex = re"""(?<!\>)`(?!<span style=" font-family:'monospace';">)([^*]+)(?!<\/span>)`"""
self.formatInputStuff(strikeThroughRegex, inputText)

View File

@ -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, '<span style=" text-decoration: line-through;">', '</span>', '~', 2)
setFormatInInput(chatsModel.formatInputView.formatInputStrikeThrough, '<span style=" text-decoration: line-through;">', '</span>', '~', 2)
// bold
setFormatInInput(chatsModel.formatInputBold, '<b>', '</b>', '*', 2)
setFormatInInput(chatsModel.formatInputView.formatInputBold, '<b>', '</b>', '*', 2)
// code
setFormatInInput(chatsModel.formatInputCode, '<code>', '</code>', '`', 1)
setFormatInInput(chatsModel.formatInputView.formatInputCode, '<code>', '</code>', '`', 1)
// italic
setFormatInInput(chatsModel.formatInputItalic, '<i>', '</i>', '*', 1)
setFormatInInput(chatsModel.formatInputView.formatInputItalic, '<i>', '</i>', '*', 1)
}
function onRelease(event) {