Code-split emoji-mart picker and data (#5175)
parent
d841af4e80
commit
b9c612b561
@ -0,0 +1,17 @@ |
||||
// @preval
|
||||
const data = require('emoji-mart/dist/data').default; |
||||
const pick = require('lodash/pick'); |
||||
|
||||
const condensedEmojis = {}; |
||||
Object.keys(data.emojis).forEach(key => { |
||||
condensedEmojis[key] = pick(data.emojis[key], ['short_names', 'unified', 'search']); |
||||
}); |
||||
|
||||
// JSON.parse/stringify is to emulate what @preval is doing and avoid any
|
||||
// inconsistent behavior in dev mode
|
||||
module.exports = JSON.parse(JSON.stringify({ |
||||
emojis: condensedEmojis, |
||||
skins: data.skins, |
||||
categories: data.categories, |
||||
short_names: data.short_names, |
||||
})); |
@ -0,0 +1,154 @@ |
||||
// This code is largely borrowed from:
|
||||
// https://github.com/missive/emoji-mart/blob/bbd4fbe/src/utils/emoji-index.js
|
||||
|
||||
import data from './emoji_data_light'; |
||||
import { getData, getSanitizedData, intersect } from './emoji_utils'; |
||||
|
||||
let index = {}; |
||||
let emojisList = {}; |
||||
let emoticonsList = {}; |
||||
let previousInclude = []; |
||||
let previousExclude = []; |
||||
|
||||
for (let emoji in data.emojis) { |
||||
let emojiData = data.emojis[emoji], |
||||
{ short_names, emoticons } = emojiData, |
||||
id = short_names[0]; |
||||
|
||||
for (let emoticon of (emoticons || [])) { |
||||
if (!emoticonsList[emoticon]) { |
||||
emoticonsList[emoticon] = id; |
||||
} |
||||
} |
||||
|
||||
emojisList[id] = getSanitizedData(id); |
||||
} |
||||
|
||||
function search(value, { emojisToShowFilter, maxResults, include, exclude, custom = [] } = {}) { |
||||
maxResults = maxResults || 75; |
||||
include = include || []; |
||||
exclude = exclude || []; |
||||
|
||||
if (custom.length) { |
||||
for (const emoji of custom) { |
||||
data.emojis[emoji.id] = getData(emoji); |
||||
emojisList[emoji.id] = getSanitizedData(emoji); |
||||
} |
||||
|
||||
data.categories.push({ |
||||
name: 'Custom', |
||||
emojis: custom.map(emoji => emoji.id), |
||||
}); |
||||
} |
||||
|
||||
let results = null; |
||||
let pool = data.emojis; |
||||
|
||||
if (value.length) { |
||||
if (value === '-' || value === '-1') { |
||||
return [emojisList['-1']]; |
||||
} |
||||
|
||||
let values = value.toLowerCase().split(/[\s|,|\-|_]+/); |
||||
|
||||
if (values.length > 2) { |
||||
values = [values[0], values[1]]; |
||||
} |
||||
|
||||
if (include.length || exclude.length) { |
||||
pool = {}; |
||||
|
||||
if (previousInclude !== include.sort().join(',') || previousExclude !== exclude.sort().join(',')) { |
||||
previousInclude = include.sort().join(','); |
||||
previousExclude = exclude.sort().join(','); |
||||
index = {}; |
||||
} |
||||
|
||||
for (let category of data.categories) { |
||||
let isIncluded = include && include.length ? include.indexOf(category.name.toLowerCase()) > -1 : true; |
||||
let isExcluded = exclude && exclude.length ? exclude.indexOf(category.name.toLowerCase()) > -1 : false; |
||||
if (!isIncluded || isExcluded) { |
||||
continue; |
||||
} |
||||
|
||||
for (let emojiId of category.emojis) { |
||||
pool[emojiId] = data.emojis[emojiId]; |
||||
} |
||||
} |
||||
} else if (previousInclude.length || previousExclude.length) { |
||||
index = {}; |
||||
} |
||||
|
||||
let allResults = values.map((value) => { |
||||
let aPool = pool; |
||||
let aIndex = index; |
||||
let length = 0; |
||||
|
||||
for (let char of value.split('')) { |
||||
length++; |
||||
|
||||
aIndex[char] = aIndex[char] || {}; |
||||
aIndex = aIndex[char]; |
||||
|
||||
if (!aIndex.results) { |
||||
let scores = {}; |
||||
|
||||
aIndex.results = []; |
||||
aIndex.pool = {}; |
||||
|
||||
for (let id in aPool) { |
||||
let emoji = aPool[id], |
||||
{ search } = emoji, |
||||
sub = value.substr(0, length), |
||||
subIndex = search.indexOf(sub); |
||||
|
||||
if (subIndex !== -1) { |
||||
let score = subIndex + 1; |
||||
if (sub === id) { |
||||
score = 0; |
||||
} |
||||
|
||||
aIndex.results.push(emojisList[id]); |
||||
aIndex.pool[id] = emoji; |
||||
|
||||
scores[id] = score; |
||||
} |
||||
} |
||||
|
||||
aIndex.results.sort((a, b) => { |
||||
let aScore = scores[a.id], |
||||
bScore = scores[b.id]; |
||||
|
||||
return aScore - bScore; |
||||
}); |
||||
} |
||||
|
||||
aPool = aIndex.pool; |
||||
} |
||||
|
||||
return aIndex.results; |
||||
}).filter(a => a); |
||||
|
||||
if (allResults.length > 1) { |
||||
results = intersect(...allResults); |
||||
} else if (allResults.length) { |
||||
results = allResults[0]; |
||||
} else { |
||||
results = []; |
||||
} |
||||
} |
||||
|
||||
if (results) { |
||||
if (emojisToShowFilter) { |
||||
results = results.filter((result) => emojisToShowFilter(data.emojis[result.id].unified)); |
||||
} |
||||
|
||||
if (results && results.length > maxResults) { |
||||
results = results.slice(0, maxResults); |
||||
} |
||||
} |
||||
|
||||
return results; |
||||
} |
||||
|
||||
export { search }; |
@ -0,0 +1,137 @@ |
||||
// This code is largely borrowed from:
|
||||
// https://github.com/missive/emoji-mart/blob/bbd4fbe/src/utils/index.js
|
||||
|
||||
import data from './emoji_data_light'; |
||||
|
||||
const COLONS_REGEX = /^(?:\:([^\:]+)\:)(?:\:skin-tone-(\d)\:)?$/; |
||||
|
||||
function buildSearch(thisData) { |
||||
const search = []; |
||||
|
||||
let addToSearch = (strings, split) => { |
||||
if (!strings) { |
||||
return; |
||||
} |
||||
|
||||
(Array.isArray(strings) ? strings : [strings]).forEach((string) => { |
||||
(split ? string.split(/[-|_|\s]+/) : [string]).forEach((s) => { |
||||
s = s.toLowerCase(); |
||||
|
||||
if (search.indexOf(s) === -1) { |
||||
search.push(s); |
||||
} |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
addToSearch(thisData.short_names, true); |
||||
addToSearch(thisData.name, true); |
||||
addToSearch(thisData.keywords, false); |
||||
addToSearch(thisData.emoticons, false); |
||||
|
||||
return search; |
||||
} |
||||
|
||||
function unifiedToNative(unified) { |
||||
let unicodes = unified.split('-'), |
||||
codePoints = unicodes.map((u) => `0x${u}`); |
||||
|
||||
return String.fromCodePoint(...codePoints); |
||||
} |
||||
|
||||
function sanitize(emoji) { |
||||
let { name, short_names, skin_tone, skin_variations, emoticons, unified, custom, imageUrl } = emoji, |
||||
id = emoji.id || short_names[0], |
||||
colons = `:${id}:`; |
||||
|
||||
if (custom) { |
||||
return { |
||||
id, |
||||
name, |
||||
colons, |
||||
emoticons, |
||||
custom, |
||||
imageUrl, |
||||
}; |
||||
} |
||||
|
||||
if (skin_tone) { |
||||
colons += `:skin-tone-${skin_tone}:`; |
||||
} |
||||
|
||||
return { |
||||
id, |
||||
name, |
||||
colons, |
||||
emoticons, |
||||
unified: unified.toLowerCase(), |
||||
skin: skin_tone || (skin_variations ? 1 : null), |
||||
native: unifiedToNative(unified), |
||||
}; |
||||
} |
||||
|
||||
function getSanitizedData(emoji) { |
||||
return sanitize(getData(emoji)); |
||||
} |
||||
|
||||
function getData(emoji) { |
||||
let emojiData = {}; |
||||
|
||||
if (typeof emoji === 'string') { |
||||
let matches = emoji.match(COLONS_REGEX); |
||||
|
||||
if (matches) { |
||||
emoji = matches[1]; |
||||
|
||||
} |
||||
|
||||
if (data.short_names.hasOwnProperty(emoji)) { |
||||
emoji = data.short_names[emoji]; |
||||
} |
||||
|
||||
if (data.emojis.hasOwnProperty(emoji)) { |
||||
emojiData = data.emojis[emoji]; |
||||
} |
||||
} else if (emoji.custom) { |
||||
emojiData = emoji; |
||||
|
||||
emojiData.search = buildSearch({ |
||||
short_names: emoji.short_names, |
||||
name: emoji.name, |
||||
keywords: emoji.keywords, |
||||
emoticons: emoji.emoticons, |
||||
}); |
||||
|
||||
emojiData.search = emojiData.search.join(','); |
||||
} else if (emoji.id) { |
||||
if (data.short_names.hasOwnProperty(emoji.id)) { |
||||
emoji.id = data.short_names[emoji.id]; |
||||
} |
||||
|
||||
if (data.emojis.hasOwnProperty(emoji.id)) { |
||||
emojiData = data.emojis[emoji.id]; |
||||
} |
||||
} |
||||
|
||||
emojiData.emoticons = emojiData.emoticons || []; |
||||
emojiData.variations = emojiData.variations || []; |
||||
|
||||
if (emojiData.variations && emojiData.variations.length) { |
||||
emojiData = JSON.parse(JSON.stringify(emojiData)); |
||||
emojiData.unified = emojiData.variations.shift(); |
||||
} |
||||
|
||||
return emojiData; |
||||
} |
||||
|
||||
function intersect(a, b) { |
||||
let aSet = new Set(a); |
||||
let bSet = new Set(b); |
||||
let intersection = new Set( |
||||
[...aSet].filter(x => bSet.has(x)) |
||||
); |
||||
|
||||
return Array.from(intersection); |
||||
} |
||||
|
||||
export { getData, getSanitizedData, intersect }; |
Loading…
Reference in new issue