From 983d258a9a20dd97c6005082715d2d2195b92774 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 29 Jul 2020 15:03:10 -0400 Subject: [PATCH] feat: add support for recent emojis (cherry picked from commit 07ccf8f74ee7b37652ccedbddd6dfdaa5908a846) --- .../AppLayouts/Chat/components/EmojiPopup.qml | 43 ++++++++++++++++- .../Chat/components/EmojiSection.qml | 47 +++++-------------- ui/main.qml | 1 + 3 files changed, 54 insertions(+), 37 deletions(-) diff --git a/ui/app/AppLayouts/Chat/components/EmojiPopup.qml b/ui/app/AppLayouts/Chat/components/EmojiPopup.qml index 704dbbfa18..af60f2ed3c 100644 --- a/ui/app/AppLayouts/Chat/components/EmojiPopup.qml +++ b/ui/app/AppLayouts/Chat/components/EmojiPopup.qml @@ -32,6 +32,40 @@ Popup { } } + function addEmoji(emoji) { + const MAX_EMOJI_NUMBER = 36 + const extenstionIndex = emoji.filename.lastIndexOf('.'); + let iconCodePoint = emoji.filename + if (extenstionIndex > -1) { + iconCodePoint = iconCodePoint.substring(0, extenstionIndex) + } + + // Split the filename to get all the parts and then encode them from hex to utf8 + const splitCodePoint = iconCodePoint.split('-') + let codePointParts = [] + splitCodePoint.forEach(function (codePoint) { + codePointParts.push(`0x${codePoint}`) + }) + const encodedIcon = String.fromCodePoint(...codePointParts); + + // Add at the start of the list + appSettings.recentEmojis.unshift(emoji) + // Remove duplicates + appSettings.recentEmojis = appSettings.recentEmojis.filter(function (e, index) { + return !appSettings.recentEmojis.some(function (e2, index2) { + return index2 < index && e2.filename === e.filename + }) + }) + if (appSettings.recentEmojis.length > MAX_EMOJI_NUMBER) { + //remove last one + appSettings.recentEmojis.splice(MAX_EMOJI_NUMBER - 1) + } + emojiSectionsRepeater.itemAt(0).allEmojis = appSettings.recentEmojis + + popup.addToChat(encodedIcon + ' ') // Adding a space because otherwise, some emojis would fuse since it's just an emoji is just a string + popup.close() + } + Component.onCompleted: { var categoryNames = {"recent": 0} var newCategories = [[]] @@ -41,8 +75,14 @@ Popup { categoryNames[emoji.category] = newCategories.length newCategories.push([]) } - newCategories[categoryNames[emoji.category]].push(emoji) + newCategories[categoryNames[emoji.category]].push(Object.assign({}, emoji, {filename: emoji.unicode + '.png'})) }) + + // Add recent + appSettings.recentEmojis.forEach(function (emoji) { + newCategories[categoryNames.recent].push(Object.assign({}, emoji, {category: "recent"})) + }) + if (newCategories[categoryNames.recent].length === 0) { newCategories[categoryNames.recent].push({ category: "recent", @@ -152,6 +192,7 @@ Popup { EmojiSection { searchString: popup.searchString + addEmoji: popup.addEmoji } } } diff --git a/ui/app/AppLayouts/Chat/components/EmojiSection.qml b/ui/app/AppLayouts/Chat/components/EmojiSection.qml index ddfddbf904..b610c1970a 100644 --- a/ui/app/AppLayouts/Chat/components/EmojiSection.qml +++ b/ui/app/AppLayouts/Chat/components/EmojiSection.qml @@ -10,7 +10,8 @@ Item { property int imageWidth: 26 property int imageMargin: 4 property var emojis: [] - property var allEmojis: [] + property var allEmojis: modelData + property var addEmoji: function () {} id: emojiSection visible: emojis.length > 0 || !!(modelData && modelData.length && modelData[0].empty && searchString === "") @@ -31,7 +32,7 @@ Item { StyledText { id: noRecentText - visible: !!(modelData && modelData.length && modelData[0].empty) + visible: !!(allEmojis && allEmojis.length && allEmojis[0].empty) text: qsTr("No recent emojis") color: Style.current.darkGrey font.pixelSize: 10 @@ -41,33 +42,21 @@ Item { onSearchStringLowercaseChanged: { if (emojiSection.searchStringLowercase === "") { - this.emojis = this.allEmojis + this.emojis = modelData return } - this.emojis = this.allEmojis.filter(function (emoji) { + this.emojis = modelData.filter(function (emoji) { return emoji.name.includes(emojiSection.searchStringLowercase) || emoji.shortname.includes(emojiSection.searchStringLowercase) || emoji.aliases.some(a => a.includes(emojiSection.searchStringLowercase)) }) } - Component.onCompleted: { - var myEmojis = [] - modelData.forEach(function (emoji) { - if (emoji.empty) { - return - } - myEmojis.push({ - filename: emoji.unicode + '.png', - name: emoji.name, - shortname: emoji.shortname, - name: emoji.name, - aliases: emoji.aliases - }) - }) - // We use two arrays for filtering purposes - this.emojis = myEmojis - this.allEmojis = myEmojis + onAllEmojisChanged: { + if (this.allEmojis[0].empty) { + return + } + this.emojis = this.allEmojis } GridView { @@ -103,21 +92,7 @@ Item { cursorShape: Qt.PointingHandCursor anchors.fill: parent onClicked: { - const extenstionIndex = modelData.filename.lastIndexOf('.'); - let iconCodePoint = modelData.filename - if (extenstionIndex > -1) { - iconCodePoint = iconCodePoint.substring(0, extenstionIndex) - } - - // Split the filename to get all the parts and then encode them from hex to utf8 - const splitCodePoint = iconCodePoint.split('-') - let codePointParts = [] - splitCodePoint.forEach(function (codePoint) { - codePointParts.push(`0x${codePoint}`) - }) - const encodedIcon = String.fromCodePoint(...codePointParts); - popup.addToChat(encodedIcon + ' ') // Adding a space because otherwise, some emojis would fuse since it's just an emoji is just a string - popup.close() + emojiSection.addEmoji(modelData) } } } diff --git a/ui/main.qml b/ui/main.qml index 2888f3183a..b77bd335d9 100644 --- a/ui/main.qml +++ b/ui/main.qml @@ -68,6 +68,7 @@ ApplicationWindow { property bool displayChatImages: false property bool compactMode property string locale: "en" + property var recentEmojis: [] } SystemTrayIcon {