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

223 lines
5.0 KiB
JavaScript
Raw Normal View History

2016-05-31 14:36:52 +00:00
import React from 'react'
import PropTypes from 'prop-types'
2016-05-31 14:36:52 +00:00
2018-04-26 15:47:30 +00:00
import { getData, getSanitizedData, unifiedToNative } from '../../utils'
2018-04-30 00:55:13 +00:00
import { uncompress } from '../../utils/data'
import { EmojiPropTypes } from '../../utils/shared-props'
import { EmojiDefaultProps } from '../../utils/shared-default-props'
2016-07-19 16:27:24 +00:00
const _getData = (props) => {
var { emoji, skin, set, data } = props
return getData(emoji, skin, set, data)
}
2018-03-27 18:51:26 +00:00
const _getPosition = (props) => {
2017-04-23 15:46:00 +00:00
var { sheet_x, sheet_y } = _getData(props),
multiplyX = 100 / (props.sheetColumns - 1),
multiplyY = 100 / (props.sheetRows - 1)
2016-05-31 23:36:50 +00:00
return `${multiplyX * sheet_x}% ${multiplyY * sheet_y}%`
2017-04-23 15:46:00 +00:00
}
2016-05-31 23:36:50 +00:00
2018-03-27 18:51:26 +00:00
const _getSanitizedData = (props) => {
var { emoji, skin, set, data } = props
return getSanitizedData(emoji, skin, set, data)
2017-04-23 15:46:00 +00:00
}
2016-05-31 23:36:50 +00:00
2017-04-23 15:46:00 +00:00
const _handleClick = (e, props) => {
if (!props.onClick) {
return
}
2017-04-23 15:46:00 +00:00
var { onClick } = props,
2017-10-07 04:02:02 +00:00
emoji = _getSanitizedData(props)
2016-06-01 00:29:37 +00:00
2017-04-23 15:46:00 +00:00
onClick(emoji, e)
}
2016-05-31 23:36:50 +00:00
2017-04-23 15:46:00 +00:00
const _handleOver = (e, props) => {
if (!props.onOver) {
return
}
2017-04-23 15:46:00 +00:00
var { onOver } = props,
2017-10-07 04:02:02 +00:00
emoji = _getSanitizedData(props)
2017-04-23 15:46:00 +00:00
onOver(emoji, e)
}
2016-05-31 14:36:52 +00:00
2017-04-23 15:46:00 +00:00
const _handleLeave = (e, props) => {
if (!props.onLeave) {
return
}
2017-04-23 15:46:00 +00:00
var { onLeave } = props,
2017-10-07 04:02:02 +00:00
emoji = _getSanitizedData(props)
2016-05-31 14:36:52 +00:00
2017-04-23 15:46:00 +00:00
onLeave(emoji, e)
}
2016-06-01 00:29:37 +00:00
2018-03-27 18:51:26 +00:00
const _isNumeric = (value) => {
return !isNaN(value - parseFloat(value))
}
2018-03-27 18:51:26 +00:00
const _convertStyleToCSS = (style) => {
let div = document.createElement('div')
for (let key in style) {
let value = style[key]
if (_isNumeric(value)) {
value += 'px'
}
div.style[key] = value
}
return div.getAttribute('style')
}
const NimbleEmoji = (props) => {
2018-04-30 00:55:13 +00:00
if (props.data.compressed) {
uncompress(props.data)
}
for (let k in NimbleEmoji.defaultProps) {
if (props[k] == undefined && NimbleEmoji.defaultProps[k] != undefined) {
props[k] = NimbleEmoji.defaultProps[k]
2017-04-23 15:46:00 +00:00
}
2016-06-01 00:29:37 +00:00
}
let data = _getData(props)
2017-12-15 20:31:49 +00:00
if (!data) {
if (props.fallback) {
return props.fallback(null, props)
} else {
return null
}
2017-12-15 20:31:49 +00:00
}
let { unified, custom, short_names, imageUrl } = data,
2017-10-07 04:02:02 +00:00
style = {},
children = props.children,
className = 'emoji-mart-emoji',
nativeEmoji = unified && unifiedToNative(unified),
// combine the emoji itself and all shortcodes into an accessible label
label = [nativeEmoji]
.concat(short_names)
.filter(Boolean)
.join(', '),
2017-10-07 04:02:02 +00:00
title = null
2016-07-19 16:27:24 +00:00
if (!unified && !custom) {
if (props.fallback) {
return props.fallback(data, props)
} else {
return null
}
2016-06-01 00:29:37 +00:00
}
if (props.tooltip) {
title = short_names[0]
}
2017-04-23 15:46:00 +00:00
if (props.native && unified) {
className += ' emoji-mart-emoji-native'
2017-04-23 15:46:00 +00:00
style = { fontSize: props.size }
children = nativeEmoji
2016-10-13 00:26:10 +00:00
2017-04-23 15:46:00 +00:00
if (props.forceSize) {
style.display = 'inline-block'
style.width = props.size
style.height = props.size
style.wordBreak = 'keep-all'
2016-10-13 00:26:10 +00:00
}
} else if (custom) {
className += ' emoji-mart-emoji-custom'
style = {
width: props.size,
height: props.size,
display: 'inline-block',
2018-11-30 14:17:45 +00:00
}
if (data.spriteUrl) {
style = {
...style,
backgroundImage: `url(${data.spriteUrl})`,
backgroundSize: `${100 * props.sheetColumns}% ${100 *
props.sheetRows}%`,
backgroundPosition: _getPosition(props),
}
} else {
style = {
...style,
backgroundImage: `url(${imageUrl})`,
backgroundSize: 'contain',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center',
2018-11-30 14:17:45 +00:00
}
}
2017-04-23 15:46:00 +00:00
} else {
2018-04-30 00:55:13 +00:00
let setHasEmoji =
data[`has_img_${props.set}`] == undefined || data[`has_img_${props.set}`]
if (!setHasEmoji) {
if (props.fallback) {
return props.fallback(data, props)
} else {
return null
}
} else {
style = {
width: props.size,
height: props.size,
display: 'inline-block',
backgroundImage: `url(${props.backgroundImageFn(
props.set,
2018-03-27 18:51:26 +00:00
props.sheetSize,
)})`,
2018-11-30 14:17:45 +00:00
backgroundSize: `${100 * props.sheetColumns}% ${100 *
props.sheetRows}%`,
backgroundPosition: _getPosition(props),
}
2016-11-01 15:54:25 +00:00
}
2016-05-31 14:36:52 +00:00
}
2017-04-23 15:46:00 +00:00
2019-03-28 12:30:18 +00:00
var Tag = {
name: 'span',
props: {},
}
if (props.onClick) {
Tag.name = 'button'
Tag.props = {
2019-03-28 13:14:25 +00:00
type: 'button',
2019-03-28 12:30:18 +00:00
}
}
if (props.html) {
style = _convertStyleToCSS(style)
2019-03-28 12:30:18 +00:00
return `<${Tag.name} style='${style}' aria-label='${label}' ${
2018-03-27 18:51:26 +00:00
title ? `title='${title}'` : ''
2019-03-28 12:30:18 +00:00
} class='${className}'>${children || ''}</${Tag.name}>`
} else {
return (
2019-03-28 12:30:18 +00:00
<Tag.name
2018-03-27 18:51:26 +00:00
onClick={(e) => _handleClick(e, props)}
onMouseEnter={(e) => _handleOver(e, props)}
onMouseLeave={(e) => _handleLeave(e, props)}
aria-label={label}
title={title}
className={className}
2019-03-28 12:30:18 +00:00
{...Tag.props}
>
<span style={style}>{children}</span>
2019-03-28 12:30:18 +00:00
</Tag.name>
)
}
2016-05-31 14:36:52 +00:00
}
NimbleEmoji.propTypes /* remove-proptypes */ = {
...EmojiPropTypes,
data: PropTypes.object.isRequired,
}
NimbleEmoji.defaultProps = EmojiDefaultProps
2017-04-23 15:46:00 +00:00
export default NimbleEmoji