Skip to content

Commit

Permalink
[FIX] Recent emojis not updated when adding via text
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Sep 2, 2017
1 parent ff75c3b commit 5810ed3
Showing 1 changed file with 52 additions and 22 deletions.
74 changes: 52 additions & 22 deletions packages/rocketchat-ui-message/client/popup/messagePopupConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,42 @@ const getUsersFromServerDelayed = _.throttle(getUsersFromServer, 500);

const getRoomsFromServerDelayed = _.throttle(getRoomsFromServer, 500);

const addEmojiToRecents = (emoji) => {
const pickerEl = $('.emoji-picker')[0];
if (pickerEl) {
const view = Blaze.getView(pickerEl);
if (view) {
Template._withTemplateInstanceFunc(view.templateInstance, () => {
RocketChat.EmojiPicker.addRecent(emoji.replace(/:/g, ''));
});
}
}
};

const emojiSort = (recents) => {
return (a, b) => {
let idA = a._id;
let idB = a._id;

if (recents.includes(a._id)) {
idA = recents.indexOf(a._id) + idA;
}
if (recents.includes(b._id)) {
idB = recents.indexOf(b._id) + idB;
}

if (idA < idB) {
return -1;
}

if (idA > idB) {
return 1;
}

return 0;
};
};

Template.messagePopupConfig.helpers({
popupUserConfig() {
const self = this;
Expand Down Expand Up @@ -273,11 +309,12 @@ Template.messagePopupConfig.helpers({
getFilter(collection, filter) {
const key = `:${ filter }`;

if (!RocketChat.emoji.packages.emojione || RocketChat.emoji.packages.emojione.asciiList[key] || filter.length < 2) {
if (!RocketChat.emoji.packages.emojione || RocketChat.emoji.packages.emojione.asciiList[key]) {
return [];
}

const regExp = new RegExp(`^${ RegExp.escape(key) }`, 'i');
const recents = RocketChat.EmojiPicker.getRecent().map(item => `:${ item }:`);
return Object.keys(collection).map(key => {
const value = collection[key];
return {
Expand All @@ -286,16 +323,12 @@ Template.messagePopupConfig.helpers({
};
})
.filter(obj => regExp.test(obj._id))
.slice(0, 10)
.sort(function(a, b) {
if (a._id < b._id) {
return -1;
}
if (a._id > b._id) {
return 1;
}
return 0;
});
.sort(emojiSort(recents))
.slice(0, 10);
},
getValue(_id) {
addEmojiToRecents(_id);
return _id;
}
};
}
Expand All @@ -314,11 +347,12 @@ Template.messagePopupConfig.helpers({
getFilter(collection, filter) {
const key = `${ filter }`;

if (!RocketChat.emoji.packages.emojione || RocketChat.emoji.packages.emojione.asciiList[key] || filter.length < 2) {
if (!RocketChat.emoji.packages.emojione || RocketChat.emoji.packages.emojione.asciiList[key]) {
return [];
}

const regExp = new RegExp(`^${ RegExp.escape(key) }`, 'i');
const recents = RocketChat.EmojiPicker.getRecent().map(item => `:${ item }:`);
return Object.keys(collection).map(key => {
const value = collection[key];
return {
Expand All @@ -327,16 +361,12 @@ Template.messagePopupConfig.helpers({
};
})
.filter(obj => regExp.test(obj._id))
.slice(0, 10)
.sort(function(a, b) {
if (a._id < b._id) {
return -1;
}
if (a._id > b._id) {
return 1;
}
return 0;
});
.sort(emojiSort(recents))
.slice(0, 10);
},
getValue(_id) {
addEmojiToRecents(_id);
return _id;
}
};
}
Expand Down

0 comments on commit 5810ed3

Please sign in to comment.