Skip to content

Commit

Permalink
fix: don't trigger emoji replacement prematurely
Browse files Browse the repository at this point in the history
... and support all emoji ASCII aliases

- previously we used to trigger the emoji replacement immediately which
led to various bugs like being unable to get the `:beers:` emoji ;)
- we now trigger the emoji replacement after typing a certain sequence
followed by a space and then check that word for a possible emoji match
- as a result, we now fully support ASCII aliases, not only those having
a length of 2 (e.g. `:D`) but all the longer ones, like `O_O`, `:-#` or
even `-___-`
  • Loading branch information
caybro committed Apr 7, 2023
1 parent 19e7c42 commit 3f66e41
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions ui/imports/shared/status/StatusChatInput.qml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ Rectangle {
QtObject {
id: d

readonly property string emojiReplacementSymbols: ":='xX><0O;*dB8-D#%\\"

//mentions helper properties
property string copiedTextPlain: ""
property string copiedTextFormatted: ""
Expand Down Expand Up @@ -345,6 +347,7 @@ Rectangle {
}

if (messageInputField.length <= messageLimit) {
checkForInlineEmojis(true);
control.sendMessage(event)
control.hideExtendedArea();
event.accepted = true
Expand Down Expand Up @@ -819,9 +822,35 @@ Rectangle {
return length;
}

function replaceWithEmoji(message, shortname, codePoint) {
function checkForInlineEmojis(force = false) {
// trigger inline emoji replacements after space, or always (force==true) when sending the message
if (force || messageInputField.getText(messageInputField.cursorPosition, messageInputField.cursorPosition - 1) === " ") {
// figure out last word (between spaces), max length of 5
var lastWord = ""
const cursorPos = messageInputField.cursorPosition - (force ? 1 : 2) // just before the last non-space character
for (let i = cursorPos; i > cursorPos - 6; i--) { // go back until we found a space or start of line
const lastChar = messageInputField.getText(i, i+1)
if (i < 0 || lastChar === " ") { // reached start of line or a space
break
} else {
lastWord = lastChar + lastWord // collect the last word
}
}

// check if the word contains any of the trigger chars (emojiReplacementSymbols)
if (!!lastWord && Array.prototype.some.call(d.emojiReplacementSymbols, (trigger) => lastWord.includes(trigger))) {
// search the ASCII aliases for a possible match
const emojiFound = StatusQUtils.Emoji.emojiJSON.emoji_json.find(emoji => emoji.aliases_ascii.includes(lastWord))
if (emojiFound) {
replaceWithEmoji("", lastWord, emojiFound.unicode, force ? 0 : 1 /*offset*/);
}
}
}
}

function replaceWithEmoji(message, shortname, codePoint, offset = 0) {
const encodedCodePoint = StatusQUtils.Emoji.getEmojiCodepoint(codePoint)
messageInputField.remove(messageInputField.cursorPosition - shortname.length, messageInputField.cursorPosition);
messageInputField.remove(messageInputField.cursorPosition - shortname.length - offset, messageInputField.cursorPosition);
insertInTextInput(messageInputField.cursorPosition, StatusQUtils.Emoji.parse(encodedCodePoint) + " ");
emojiSuggestions.close()
emojiEvent = false
Expand Down Expand Up @@ -1293,22 +1322,10 @@ Rectangle {

onTextChanged: {
if (length <= control.messageLimit) {
const symbols = ":='xX><0O;*dB8-D#%\\";
const symbolIdx = ((cursorPosition > 2) && (symbols.indexOf(getText((cursorPosition - 2), (cursorPosition - 1)))!==-1))
? (cursorPosition -1) : (symbols.indexOf(getText(0, 1))!==-1) ? 2 : -1;
if ((getText(symbolIdx-2, (symbolIdx-1)) === " ") || (symbolIdx === 2)) {
const textBefore1 = getText((symbolIdx-1), (symbolIdx+1))
const textBefore2 = getText((symbolIdx-2), (symbolIdx+1))
const has2CharsText = getText((symbolIdx-4), (symbolIdx-2))
const emojis = StatusQUtils.Emoji.emojiJSON.emoji_json.filter(function (emoji) {
if (emoji.aliases_ascii.includes(textBefore1) || emoji.aliases_ascii.includes(textBefore2)) {
const has2Chars = emoji.aliases_ascii.includes(has2CharsText)
replaceWithEmoji("", getText((symbolIdx - (has2Chars ? 3 : 2)), symbolIdx), emoji.unicode);
}
})
}
if (length === 0) {
mentionsPos = [];
} else {
checkForInlineEmojis()
}
} else {
const removeFrom = (cursorPosition < messageLimit) ? cursorWhenPressed : messageLimit;
Expand Down

0 comments on commit 3f66e41

Please sign in to comment.