diff --git a/qml/js/functions.js b/qml/js/functions.js index b56556c..af88751 100644 --- a/qml/js/functions.js +++ b/qml/js/functions.js @@ -23,24 +23,44 @@ function getUserName(userInformation) { return (firstName + " " + lastName).trim(); } -function getSimpleMessageText(message) { +function getMessageText(message, simple) { if (message.content['@type'] === 'messageText') { - return message.content.text.text; + if (simple) { + return message.content.text.text; + } else { + return enhanceMessageText(message.content.text); + } } if (message.content['@type'] === 'messageSticker') { return qsTr("Sticker: %1").arg(message.content.sticker.emoji); } if (message.content['@type'] === 'messagePhoto') { - return (message.content.caption.text !== "") ? qsTr("Picture: %1").arg(message.content.caption.text) : qsTr("shared a picture"); + if (message.content.caption.text !== "") { + return qsTr("Picture: %1").arg(simple ? message.content.caption.text : enhanceMessageText(message.content.caption)) + } else { + return qsTr("shared a picture"); + } } if (message.content['@type'] === 'messageVideo') { - return (message.content.caption.text !== "") ? qsTr("Video: %1").arg(message.content.caption.text) : qsTr("shared a video"); + if (message.content.caption.text !== "") { + return qsTr("Video: %1").arg(simple ? message.content.caption.text : enhanceMessageText(message.content.caption)) + } else { + return qsTr("shared a video"); + } } if (message.content['@type'] === 'messageAudio') { - return (message.content.caption.text !== "") ? qsTr("Audio: %1").arg(message.content.caption.text) : qsTr("shared an audio"); + if (message.content.caption.text !== "") { + return qsTr("Audio: %1").arg(simple ? message.content.caption.text : enhanceMessageText(message.content.caption)) + } else { + return qsTr("shared an audio"); + } } if (message.content['@type'] === 'messageVoiceNote') { - return (message.content.caption.text !== "") ? qsTr("Voice Note: %1").arg(message.content.caption.text) : qsTr("shared a voice note"); + if (message.content.caption.text !== "") { + return qsTr("Voice Note: %1").arg(simple ? message.content.caption.text : enhanceMessageText(message.content.caption)) + } else { + return qsTr("shared a voice note"); + } } if (message.content['@type'] === 'messageLocation') { return qsTr("shared their location"); @@ -63,3 +83,50 @@ function getSimpleMessageText(message) { function getDateTimeElapsed(timestamp) { return Format.formatDate(new Date(timestamp * 1000), Formatter.DurationElapsed); } + +function MessageInsertion(offset, insertionString) { + this.offset = offset; + this.insertionString = insertionString; +} + +MessageInsertion.prototype.toString = function insertionToString() { + return "Offset: " + this.offset + ", Insertion String: " + this.insertionString; +} + +function enhanceMessageText(formattedText) { + + var messageText = formattedText.text; + var messageInsertions = []; + + for (var i = 0; i < formattedText.entities.length; i++) { + if (formattedText.entities[i]['@type'] !== "textEntity") { + continue; + } + var entityType = formattedText.entities[i].type['@type']; + if (entityType === "textEntityTypeBold") { + messageInsertions.push(new MessageInsertion(formattedText.entities[i].offset, "" )); + messageInsertions.push(new MessageInsertion((formattedText.entities[i].offset + formattedText.entities[i].length), "" )); + } + } + + messageInsertions.sort( function(a, b) { return b.offset - a.offset } ); + + for (var z = 0; z < messageInsertions.length; z++) { + messageText = messageText.substring(0, messageInsertions[z].offset) + messageInsertions[z].insertionString + messageText.substring(messageInsertions[z].offset); + } + + messageText = messageText.replace(new RegExp("\r?\n", "g"), "
"); + + var spaceRegex = /\s{2,}/g; + function spaceReplacer(match, p1, offset, string) { + var replaceString = ""; + for (var i = 0; i < match.length; i++) { + replaceString += " "; + } + return replaceString; + } + messageText = messageText.replace(spaceRegex, spaceReplacer); + + return messageText; + +} diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index c6e13b9..8fe7209 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -332,7 +332,7 @@ Page { id: messageText width: parent.width - text: Emoji.emojify(Functions.getSimpleMessageText(display), font.pixelSize) + text: Emoji.emojify(Functions.getMessageText(display, false), font.pixelSize) font.pixelSize: Theme.fontSizeSmall color: (chatPage.myUserId === display.sender_user_id) ? Theme.highlightColor : Theme.primaryColor wrapMode: Text.Wrap diff --git a/qml/pages/OverviewPage.qml b/qml/pages/OverviewPage.qml index b3e9cd1..d20258e 100644 --- a/qml/pages/OverviewPage.qml +++ b/qml/pages/OverviewPage.qml @@ -219,7 +219,7 @@ Page { chatUnreadMessagesCountBackground.visible = display.unread_count > 0; chatUnreadMessagesCount.text = display.unread_count > 99 ? "99+" : display.unread_count; chatListNameText.text = display.title !== "" ? Emoji.emojify(display.title, Theme.fontSizeMedium) : qsTr("Unknown"); - chatListLastMessageText.text = (typeof display.last_message !== "undefined") ? Emoji.emojify(Functions.getSimpleMessageText(display.last_message), Theme.fontSizeExtraSmall) : qsTr("Unknown"); + chatListLastMessageText.text = (typeof display.last_message !== "undefined") ? Emoji.emojify(Functions.getMessageText(display.last_message, true), Theme.fontSizeExtraSmall) : qsTr("Unknown"); messageContactTimeElapsedText.text = (typeof display.last_message !== "undefined") ? Functions.getDateTimeElapsed(display.last_message.date) : qsTr("Unknown"); } } @@ -325,7 +325,7 @@ Page { } Text { id: chatListLastMessageText - text: (typeof display.last_message !== "undefined") ? Emoji.emojify(Functions.getSimpleMessageText(display.last_message), Theme.fontSizeExtraSmall) : qsTr("Unknown") + text: (typeof display.last_message !== "undefined") ? Emoji.emojify(Functions.getMessageText(display.last_message, true), Theme.fontSizeExtraSmall) : qsTr("Unknown") font.pixelSize: Theme.fontSizeExtraSmall color: Theme.primaryColor width: parent.width - Theme.paddingMedium - chatListLastUserText.width