Return skin emojis in search. Refactor getEmojiDataFromNative

release
Peder Johnsen 2019-03-20 11:25:48 +00:00
parent 22a5ca587f
commit f760c55440
4 changed files with 60 additions and 32 deletions

View File

@ -495,6 +495,7 @@ export default class NimblePicker extends React.PureComponent {
skinEmoji, skinEmoji,
notFound, notFound,
notFoundEmoji, notFoundEmoji,
set,
} = this.props, } = this.props,
{ skin } = this.state, { skin } = this.state,
width = perLine * (emojiSize + 12) + 12 + 2 + measureScrollbar() width = perLine * (emojiSize + 12) + 12 + 2 + measureScrollbar()
@ -522,6 +523,7 @@ export default class NimblePicker extends React.PureComponent {
ref={this.setSearchRef} ref={this.setSearchRef}
onSearch={this.handleSearch} onSearch={this.handleSearch}
data={this.data} data={this.data}
set={set}
i18n={this.i18n} i18n={this.i18n}
emojisToShowFilter={emojisToShowFilter} emojisToShowFilter={emojisToShowFilter}
include={include} include={include}

View File

@ -17,7 +17,7 @@ export default class Search extends React.PureComponent {
} }
this.data = props.data this.data = props.data
this.emojiIndex = new NimbleEmojiIndex(this.data) this.emojiIndex = new NimbleEmojiIndex(this.data, props.set)
this.setRef = this.setRef.bind(this) this.setRef = this.setRef.bind(this)
this.clear = this.clear.bind(this) this.clear = this.clear.bind(this)
this.handleKeyUp = this.handleKeyUp.bind(this) this.handleKeyUp = this.handleKeyUp.bind(this)

View File

@ -1,13 +1,15 @@
import { getData, getSanitizedData, intersect } from '..' import { getData, getSanitizedData, intersect } from '..'
import { uncompress } from '../data' import { uncompress } from '../data'
import store from '../store'
export default class NimbleEmojiIndex { export default class NimbleEmojiIndex {
constructor(data) { constructor(data, set) {
if (data.compressed) { if (data.compressed) {
uncompress(data) uncompress(data)
} }
this.data = data || {} this.data = data || {}
this.set = set || null
this.originalPool = {} this.originalPool = {}
this.index = {} this.index = {}
this.emojis = {} this.emojis = {}
@ -20,7 +22,7 @@ export default class NimbleEmojiIndex {
buildIndex() { buildIndex() {
for (let emoji in this.data.emojis) { for (let emoji in this.data.emojis) {
let emojiData = this.data.emojis[emoji], let emojiData = this.data.emojis[emoji],
{ short_names, emoticons } = emojiData, { short_names, emoticons, skin_variations } = emojiData,
id = short_names[0] id = short_names[0]
if (emoticons) { if (emoticons) {
@ -33,7 +35,16 @@ export default class NimbleEmojiIndex {
}) })
} }
this.emojis[id] = getSanitizedData(id, null, null, this.data) // If skin variations include them
if (skin_variations) {
this.emojis[id] = {}
for (let skinTone = 1; skinTone <= 6; skinTone++) {
this.emojis[id][skinTone] = getSanitizedData({id: id, skin: skinTone}, skinTone, this.set, this.data)
}
} else {
this.emojis[id] = getSanitizedData(id, null, this.set, this.data)
}
this.originalPool[id] = emojiData this.originalPool[id] = emojiData
} }
} }
@ -70,6 +81,8 @@ export default class NimbleEmojiIndex {
if (this.customEmojisList != custom) if (this.customEmojisList != custom)
this.addCustomToPool(custom, this.originalPool) this.addCustomToPool(custom, this.originalPool)
const skinTone = store.get('skin') || 1;
maxResults || (maxResults = 75) maxResults || (maxResults = 75)
include || (include = []) include || (include = [])
exclude || (exclude = []) exclude || (exclude = [])
@ -79,7 +92,7 @@ export default class NimbleEmojiIndex {
if (value.length) { if (value.length) {
if (value == '-' || value == '-1') { if (value == '-' || value == '-1') {
return [this.emojis['-1']] return [this.emojis['-1'][skinTone]]
} }
var values = value.toLowerCase().split(/[\s|,|\-|_]+/), var values = value.toLowerCase().split(/[\s|,|\-|_]+/),
@ -148,7 +161,11 @@ export default class NimbleEmojiIndex {
let score = subIndex + 1 let score = subIndex + 1
if (sub == id) score = 0 if (sub == id) score = 0
if (this.emojis[id] && this.emojis[id][skinTone]) {
aIndex.results.push(this.emojis[id][skinTone])
} else {
aIndex.results.push(this.emojis[id]) aIndex.results.push(this.emojis[id])
}
aIndex.pool[id] = emoji aIndex.pool[id] = emoji
scores[id] = score scores[id] = score

View File

@ -141,20 +141,29 @@ function getEmojiDataFromNative(nativeString, set, data) {
} }
const skinTones = ['', '🏻', '🏼', '🏽', '🏾', '🏿'] const skinTones = ['', '🏻', '🏼', '🏽', '🏾', '🏿']
const skinCodes = ['', '1F3FB', '1F3FC', '1F3FD', '1F3FE', '1F3FF']
let skin let skin
let skinCode
let baseNativeString = nativeString let baseNativeString = nativeString
skinTones.forEach((skinTone) => { skinTones.forEach((skinTone) => {
baseNativeString = baseNativeString.replace(skinTone, '')
if (nativeString.indexOf(skinTone) > 0) { if (nativeString.indexOf(skinTone) > 0) {
skin = skinTones.indexOf(skinTone) + 1 const skinToneIndex = skinTones.indexOf(skinTone)
skin = skinToneIndex + 1
skinCode = skinCodes[skinToneIndex]
} }
}) })
const emojiData = Object.values(data.emojis).find((emoji) => { const emojiData = Object.values(data.emojis).find((emoji) => {
emoji = JSON.parse(_JSON.stringify(emoji))
if (emoji.variations && emoji.variations.length) { if (emoji.variations && emoji.variations.length) {
emoji.unifed = emoji.variations.shift() emoji.unified = emoji.variations.shift()
}
if (skin && emoji.skin_variations && emoji.skin_variations[skinCode]) {
emoji.unified = emoji.skin_variations[skinCode].unified
} }
return unifiedToNative(emoji.unified) === baseNativeString return unifiedToNative(emoji.unified) === baseNativeString