move emoji category index to helper method

This commit is contained in:
Mike Greiling 2017-06-24 02:25:04 -05:00
parent 73c5c00133
commit 8455e8c481
2 changed files with 32 additions and 27 deletions

View file

@ -3,15 +3,18 @@
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
import { glEmojiTag } from './behaviors/gl_emoji'; import { glEmojiTag } from './behaviors/gl_emoji';
import { emojiMap, filterEmojiNamesByAlias, isEmojiNameValid, normalizeEmojiName } from './emoji'; import {
filterEmojiNamesByAlias,
getEmojiByCategory,
isEmojiNameValid,
normalizeEmojiName,
} from './emoji';
const animationEndEventString = 'animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd'; const animationEndEventString = 'animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd';
const transitionEndEventString = 'transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd'; const transitionEndEventString = 'transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd';
const FROM_SENTENCE_REGEX = /(?:, and | and |, )/; // For separating lists produced by ruby's Array#toSentence const FROM_SENTENCE_REGEX = /(?:, and | and |, )/; // For separating lists produced by ruby's Array#toSentence
let categoryMap = null;
const categoryLabelMap = { const categoryLabelMap = {
activity: 'Activity', activity: 'Activity',
people: 'People', people: 'People',
@ -23,26 +26,6 @@ const categoryLabelMap = {
flags: 'Flags', flags: 'Flags',
}; };
function buildCategoryMap() {
return Object.keys(emojiMap).reduce((currentCategoryMap, emojiNameKey) => {
const emojiInfo = emojiMap[emojiNameKey];
if (currentCategoryMap[emojiInfo.category]) {
currentCategoryMap[emojiInfo.category].push(emojiNameKey);
}
return currentCategoryMap;
}, {
activity: [],
people: [],
nature: [],
food: [],
travel: [],
objects: [],
symbols: [],
flags: [],
});
}
function renderCategory(name, emojiList, opts = {}) { function renderCategory(name, emojiList, opts = {}) {
return ` return `
<h5 class="emoji-menu-title"> <h5 class="emoji-menu-title">
@ -71,8 +54,6 @@ export default class AwardsHandler {
if ($menu.length === 0) { if ($menu.length === 0) {
setTimeout(() => this.createEmojiMenu()); setTimeout(() => this.createEmojiMenu());
} }
// Prebuild the categoryMap
categoryMap = categoryMap || buildCategoryMap();
}); });
this.registerEventListener('on', $(document), 'click', '.js-add-award', (e) => { this.registerEventListener('on', $(document), 'click', '.js-add-award', (e) => {
e.stopPropagation(); e.stopPropagation();
@ -158,7 +139,7 @@ export default class AwardsHandler {
this.isCreatingEmojiMenu = true; this.isCreatingEmojiMenu = true;
// Render the first category // Render the first category
categoryMap = categoryMap || buildCategoryMap(); const categoryMap = getEmojiByCategory();
const categoryNameKey = Object.keys(categoryMap)[0]; const categoryNameKey = Object.keys(categoryMap)[0];
const emojisInCategory = categoryMap[categoryNameKey]; const emojisInCategory = categoryMap[categoryNameKey];
const firstCategory = renderCategory(categoryLabelMap[categoryNameKey], emojisInCategory); const firstCategory = renderCategory(categoryLabelMap[categoryNameKey], emojisInCategory);
@ -198,7 +179,7 @@ export default class AwardsHandler {
} }
this.isAddingRemainingEmojiMenuCategories = true; this.isAddingRemainingEmojiMenuCategories = true;
categoryMap = categoryMap || buildCategoryMap(); const categoryMap = getEmojiByCategory();
// Avoid the jank and render the remaining categories separately // Avoid the jank and render the remaining categories separately
// This will take more time, but makes UI more responsive // This will take more time, but makes UI more responsive

View file

@ -22,12 +22,36 @@ function filterEmojiNamesByAlias(filter) {
return _.uniq(filterEmojiNames(filter).map(name => normalizeEmojiName(name))); return _.uniq(filterEmojiNames(filter).map(name => normalizeEmojiName(name)));
} }
let emojiByCategory;
function getEmojiByCategory(category = null) {
if (!emojiByCategory) {
emojiByCategory = {
activity: [],
people: [],
nature: [],
food: [],
travel: [],
objects: [],
symbols: [],
flags: [],
};
Object.keys(emojiMap).forEach((name) => {
const emoji = emojiMap[name];
if (emojiByCategory[emoji.category]) {
emojiByCategory[emoji.category].push(name);
}
});
}
return category ? emojiByCategory[category] : emojiByCategory;
}
export { export {
emojiMap, emojiMap,
emojiAliases, emojiAliases,
normalizeEmojiName, normalizeEmojiName,
filterEmojiNames, filterEmojiNames,
filterEmojiNamesByAlias, filterEmojiNamesByAlias,
getEmojiByCategory,
getUnicodeSupportMap, getUnicodeSupportMap,
isEmojiNameValid, isEmojiNameValid,
isEmojiUnicodeSupported, isEmojiUnicodeSupported,