Skip to content

Commit

Permalink
Amending PR: Refactor emojifier, use better method for retrieving packs
Browse files Browse the repository at this point in the history
  • Loading branch information
Alch-Emi committed Dec 28, 2021
1 parent 4a2b1a5 commit 12124ea
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
4 changes: 1 addition & 3 deletions src/app/organisms/emoji-board/custom-emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ function getUserEmoji(mx) {
// Returns a list of `ImagePack`s. This does not include packs in spaces that contain
// this room.
function getPacksInRoom(room) {
const packs = room.currentState
.events
.get('im.ponies.room_emotes');
const packs = room.currentState.getStateEvents('im.ponies.room_emotes');

if (!packs) {
return [];
Expand Down
100 changes: 50 additions & 50 deletions src/client/state/RoomsInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,54 @@ function bindReplyToContent(roomId, reply, content) {
return newContent;
}

// Apply formatting to a plain text message
//
// This includes inserting any custom emoji that might be relevant, and (only if the
// user has enabled it in their settings) formatting the message using markdown.
function formatAndEmojifyText(room, text) {
const allEmoji = getShortcodeToEmoji(room);

// Start by applying markdown formatting (if relevant)
let formattedText;
if (settings.isMarkdown) {
formattedText = getFormattedBody(text);
} else {
formattedText = text;
}

// Check to see if there are any :shortcode-style-tags: in the message
Array.from(formattedText.matchAll(/\B:([\w-]+):\B/g))
// Then filter to only the ones corresponding to a valid emoji
.filter((match) => allEmoji.has(match[1]))
// Reversing the array ensures that indices are preserved as we start replacing
.reverse()
// Replace each :shortcode: with an <img/> tag
.forEach((shortcodeMatch) => {
const emoji = allEmoji.get(shortcodeMatch[1]);

// Render the tag that will replace the shortcode
let tag;
if (emoji.mxc) {
tag = `<img data-mx-emoticon="" src="${
emoji.mxc
}" alt=":${
emoji.shortcode
}:" title=":${
emoji.shortcode
}:" height="32" />`;
} else {
tag = emoji.unicode;
}

// Splice the tag into the text
formattedText = formattedText.substr(0, shortcodeMatch.index)
+ tag
+ formattedText.substr(shortcodeMatch.index + shortcodeMatch[0].length);
});

return formattedText;
}

class RoomsInput extends EventEmitter {
constructor(mx) {
super();
Expand Down Expand Up @@ -201,54 +249,6 @@ class RoomsInput extends EventEmitter {
return this.roomIdToInput.get(roomId)?.isSending || false;
}

// Apply formatting to a plain text message
//
// This includes inserting any custom emoji that might be relevant, and (only if the
// user has enabled it in their settings) formatting the message using markdown.
static formatAndEmojifyText(room, text) {
const allEmoji = getShortcodeToEmoji(room);

// Start by applying markdown formatting (if relevant)
let formattedText;
if (settings.isMarkdown) {
formattedText = getFormattedBody(text);
} else {
formattedText = text;
}

// Check to see if there are any :shortcode-style-tags: in the message
Array.from(formattedText.matchAll(/\B:([\w-]+):\B/g))
// Then filter to only the ones corresponding to a valid emoji
.filter((match) => allEmoji.has(match[1]))
// Reversing the array ensures that indices are preserved as we start replacing
.reverse()
// Replace each :shortcode: with an <img/> tag
.forEach((shortcodeMatch) => {
const emoji = allEmoji.get(shortcodeMatch[1]);

// Render the tag that will replace the shortcode
let tag;
if (emoji.mxc) {
tag = `<img data-mx-emoticon="" src="${
emoji.mxc
}" alt=":${
emoji.shortcode
}:" title=":${
emoji.shortcode
}:" height="32" />`;
} else {
tag = emoji.unicode;
}

// Splice the tag into the text
formattedText = formattedText.substr(0, shortcodeMatch.index)
+ tag
+ formattedText.substr(shortcodeMatch.index + shortcodeMatch[0].length);
});

return formattedText;
}

async sendInput(roomId) {
const input = this.getInput(roomId);
input.isSending = true;
Expand All @@ -265,7 +265,7 @@ class RoomsInput extends EventEmitter {
};

// Apply formatting if relevant
const formattedBody = RoomsInput.formatAndEmojifyText(
const formattedBody = formatAndEmojifyText(
this.matrixClient.getRoom(roomId),
input.message,
);
Expand Down Expand Up @@ -404,7 +404,7 @@ class RoomsInput extends EventEmitter {
};

// Apply formatting if relevant
const formattedBody = this.formatAndEmojifyText(editedBody);
const formattedBody = formatAndEmojifyText(editedBody);
if (formattedBody !== editedBody) {
content.formatted_body = ` * ${formattedBody}`;
content.format = 'org.matrix.custom.html';
Expand Down

0 comments on commit 12124ea

Please sign in to comment.