2020-06-26 01:23:38 +00:00
|
|
|
pragma Singleton
|
|
|
|
|
|
|
|
import QtQuick 2.13
|
2021-09-28 15:04:06 +00:00
|
|
|
import "../assets/twemoji/twemoji.js" as Twemoji
|
|
|
|
import "../../shared/status/emojiList.js" as EmojiJSON
|
2020-06-26 01:23:38 +00:00
|
|
|
|
|
|
|
QtObject {
|
2020-11-17 03:07:01 +00:00
|
|
|
readonly property var size: {
|
|
|
|
"big": "72x72",
|
2021-01-14 08:46:05 +00:00
|
|
|
"middle": "32x32",
|
2021-03-23 10:33:53 +00:00
|
|
|
"small": "18x18"
|
2020-11-17 03:07:01 +00:00
|
|
|
}
|
2021-09-28 15:04:06 +00:00
|
|
|
property string base: Qt.resolvedUrl("../assets/twemoji/")
|
2020-11-17 03:07:01 +00:00
|
|
|
function parse(text, renderSize = size.small) {
|
|
|
|
const renderSizes = renderSize.split("x");
|
|
|
|
if (!renderSize.includes("x") || renderSizes.length !== 2) {
|
|
|
|
throw new Error("Invalid value for 'renderSize' parameter: ", renderSize);
|
|
|
|
}
|
2021-03-03 13:13:53 +00:00
|
|
|
|
2020-06-26 01:23:38 +00:00
|
|
|
Twemoji.twemoji.base = base
|
2021-03-03 13:13:53 +00:00
|
|
|
Twemoji.twemoji.ext = ".svg"
|
|
|
|
Twemoji.twemoji.size = "svg"
|
2020-11-17 03:07:01 +00:00
|
|
|
return Twemoji.twemoji.parse(text, {
|
2021-03-23 10:33:53 +00:00
|
|
|
attributes: function() {
|
|
|
|
return {
|
|
|
|
width: renderSizes[0],
|
|
|
|
height: renderSizes[1],
|
|
|
|
style: "vertical-align: top"
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 03:07:01 +00:00
|
|
|
})
|
2020-06-26 01:23:38 +00:00
|
|
|
}
|
2020-07-02 18:49:02 +00:00
|
|
|
function fromCodePoint(value) {
|
|
|
|
return Twemoji.twemoji.convert.fromCodePoint(value)
|
|
|
|
}
|
2021-03-19 20:13:12 +00:00
|
|
|
function deparse(value) {
|
2021-09-28 15:04:06 +00:00
|
|
|
return value.replace(/<img src=\"qrc:\/imports\/assets\/twemoji\/.+?" alt=\"(.+?)\" width=\"[0-9]*\" height=\"[0-9]*\" style=\"(.+?)\" ?\/>/g, "$1");
|
2020-07-31 21:30:55 +00:00
|
|
|
}
|
2020-09-04 14:06:50 +00:00
|
|
|
function deparseFromParse(value) {
|
2021-09-28 15:04:06 +00:00
|
|
|
return value.replace(/<img class=\"emoji\" draggable=\"false\" alt=\"(.+?)\" src=\"qrc:\/imports\/assets\/twemoji\/.+?" width=\"[0-9]*\" height=\"[0-9]*\" style=\"(.+?)\" ?\/>/g, "$1");
|
2020-09-04 14:06:50 +00:00
|
|
|
}
|
2020-08-27 17:43:06 +00:00
|
|
|
function hasEmoji(value) {
|
2021-09-28 15:04:06 +00:00
|
|
|
let match = value.match(/<img src=\"qrc:\/imports\/assets\/twemoji\/.+?" alt=\"(.+?)\" width=\"[0-9]*\" height=\"[0-9]*\" style=\"(.+?)\" ?\/>/g)
|
2020-08-27 17:43:06 +00:00
|
|
|
return match && match.length > 0
|
|
|
|
}
|
2021-05-06 20:29:22 +00:00
|
|
|
function nbEmojis(value) {
|
2021-09-28 15:04:06 +00:00
|
|
|
let match = value.match(/<img src=\"qrc:\/imports\/assets\/twemoji\/.+?" alt=\"(.+?)\" width=\"[0-9]*\" height=\"[0-9]*\" style=\"(.+?)\" ?\/>/g)
|
2021-05-06 20:29:22 +00:00
|
|
|
return match ? match.length : 0
|
|
|
|
}
|
2020-09-04 14:06:50 +00:00
|
|
|
function getEmojis(value) {
|
2021-09-28 15:04:06 +00:00
|
|
|
return value.match(/<img class=\"emoji\" draggable=\"false\" alt=\"(.+?)\" src=\"qrc:\/imports\/assets\/twemoji\/.+?" width=\"[0-9]*\" height=\"[0-9]*\" style=\"(.+?)\" ?\/>/g, "$1");
|
2020-09-04 14:06:50 +00:00
|
|
|
}
|
|
|
|
function getEmojiUnicode(shortname) {
|
|
|
|
var _emoji;
|
|
|
|
EmojiJSON.emoji_json.forEach(function(emoji) {
|
|
|
|
if (emoji.shortname === shortname)
|
|
|
|
_emoji = emoji;
|
|
|
|
})
|
|
|
|
|
|
|
|
if (_emoji !== undefined)
|
|
|
|
return _emoji.unicode;
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-09-28 15:23:04 +00:00
|
|
|
|
|
|
|
function getEmojiCodepoint(iconCodePoint) {
|
|
|
|
// Split the codepoint 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}`)
|
|
|
|
})
|
|
|
|
return String.fromCodePoint(...codePointParts);
|
|
|
|
}
|
2021-05-10 04:33:28 +00:00
|
|
|
|
|
|
|
function getShortcodeFromId(emojiId) {
|
|
|
|
switch (emojiId) {
|
|
|
|
case 1: return ":heart:"
|
|
|
|
case 2: return ":thumbsup:"
|
|
|
|
case 3: return ":thumbsdown:"
|
|
|
|
case 4: return ":laughing:"
|
|
|
|
case 5: return ":cry:"
|
|
|
|
case 6: return ":angry:"
|
|
|
|
default: return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEmojiFromId(emojiId) {
|
|
|
|
let shortcode = Emoji.getShortcodeFromId(emojiId)
|
|
|
|
let emojiUnicode = Emoji.getEmojiUnicode(shortcode)
|
|
|
|
if (emojiUnicode) {
|
|
|
|
return Emoji.fromCodePoint(emojiUnicode)
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
2020-07-02 18:49:02 +00:00
|
|
|
}
|