emoji-mart-lazyload/src/components/emoji.js

132 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-05-31 14:36:52 +00:00
import React from 'react'
import data from '../../data'
const SHEET_COLUMNS = 41
2016-05-31 23:36:50 +00:00
const SKINS = [
'1F3FA', '1F3FB', '1F3FC',
'1F3FD', '1F3FE', '1F3FF',
]
2016-05-31 14:36:52 +00:00
export default class Emoji extends React.Component {
2016-05-31 23:36:50 +00:00
constructor(props) {
super(props)
var emojiData = this.getEmojiData()
this.hasSkinVariations = !!emojiData.skin_variations
}
2016-05-31 19:16:48 +00:00
shouldComponentUpdate(nextProps) {
return (
2016-05-31 23:36:50 +00:00
this.hasSkinVariations && nextProps.skin != this.props.skin ||
2016-05-31 19:16:48 +00:00
nextProps.size != this.props.size ||
nextProps.sheetURL != this.props.sheetURL
)
2016-05-31 14:36:52 +00:00
}
getEmojiData() {
2016-05-31 23:36:50 +00:00
var { emoji, skin, sheetURL } = this.props,
emojiData = emoji
if (typeof emoji == 'string') {
emojiData = data.emojis[emoji]
}
2016-05-31 23:36:50 +00:00
if (this.hasSkinVariations && skin > 1) {
emojiData = JSON.parse(JSON.stringify(emojiData))
2016-05-31 23:36:50 +00:00
var skinKey = SKINS[skin - 1],
variationKey = `${emojiData.unified}-${skinKey}`,
variationData = emojiData.skin_variations[variationKey],
kitMatches = sheetURL.match(/(apple|google|twitter|emojione)/),
kit = kitMatches[0]
if (variationData[`has_img_${kit}`]) {
2016-06-01 00:29:37 +00:00
emojiData.skin_tone = skin
2016-05-31 23:36:50 +00:00
for (let k in variationData) {
let v = variationData[k]
emojiData[k] = v
}
}
}
return emojiData
}
getPosition(emojiData) {
var { sheet_x, sheet_y } = emojiData,
multiply = 100 / (SHEET_COLUMNS - 1)
2016-05-31 14:36:52 +00:00
return `${multiply * sheet_x}% ${multiply * sheet_y}%`
2016-05-31 14:36:52 +00:00
}
2016-06-01 00:29:37 +00:00
getNative(emojiData) {
var { unified } = emojiData,
unicodes = unified.split('-'),
codePoints = unicodes.map((u) => `0x${u}`)
return String.fromCodePoint(...codePoints)
}
handleClick(emojiData) {
var { onClick } = this.props,
2016-07-07 21:22:40 +00:00
{ name, short_names, skin_tone, emoticons, unified } = emojiData,
2016-06-01 00:29:37 +00:00
id = short_names[0],
colons = `:${id}:`
if (skin_tone) {
colons += `:skin-tone-${skin_tone}:`
}
onClick({
id,
name,
colons,
2016-07-07 21:22:40 +00:00
emoticons,
2016-06-01 00:29:37 +00:00
skin: skin_tone || 1,
native: this.getNative(emojiData),
})
}
2016-05-31 14:36:52 +00:00
render() {
2016-06-01 00:29:37 +00:00
var { sheetURL, size, onOver, onLeave } = this.props,
emojiData = this.getEmojiData()
2016-05-31 14:36:52 +00:00
return <span
2016-06-01 00:29:37 +00:00
onClick={() => this.handleClick(emojiData)}
onMouseEnter={() => onOver(emojiData)}
onMouseLeave={() => onLeave(emojiData)}
2016-05-31 14:36:52 +00:00
className='emoji-picker-emoji'>
<span style={{
width: size,
height: size,
display: 'inline-block',
backgroundImage: `url(${sheetURL})`,
backgroundSize: `${100 * SHEET_COLUMNS}%`,
backgroundPosition: this.getPosition(emojiData),
2016-05-31 14:36:52 +00:00
}}>
</span>
</span>
}
}
Emoji.propTypes = {
2016-05-31 23:36:50 +00:00
skin: React.PropTypes.number,
2016-05-31 14:36:52 +00:00
onOver: React.PropTypes.func,
onLeave: React.PropTypes.func,
2016-05-31 14:36:52 +00:00
onClick: React.PropTypes.func,
size: React.PropTypes.number.isRequired,
sheetURL: React.PropTypes.string.isRequired,
emoji: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object,
]).isRequired,
2016-05-31 14:36:52 +00:00
}
Emoji.defaultProps = {
2016-05-31 23:36:50 +00:00
skin: 1,
2016-05-31 14:36:52 +00:00
onOver: (() => {}),
onLeave: (() => {}),
2016-05-31 14:36:52 +00:00
onClick: (() => {}),
}