gab-social/app/javascript/gabsocial/components/popover/emoji_picker_popover.js

300 lines
8.0 KiB
JavaScript
Raw Normal View History

2020-03-27 15:29:52 +00:00
import { defineMessages, injectIntl } from 'react-intl'
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
import { Map as ImmutableMap } from 'immutable'
import { createSelector } from 'reselect'
import detectPassiveEvents from 'detect-passive-events'
import { changeSetting } from '../../actions/settings'
import { useEmoji } from '../../actions/emojis'
2020-04-16 07:00:43 +01:00
import { closePopover } from '../../actions/popover'
2020-05-02 07:25:55 +01:00
import { insertEmojiCompose } from '../../actions/compose'
2020-03-27 15:29:52 +00:00
import { EmojiPicker as EmojiPickerAsync } from '../../features/ui/util/async_components'
import { buildCustomEmojis } from '../emoji/emoji'
2020-03-27 22:57:03 +00:00
import PopoverLayout from './popover_layout'
2020-04-16 07:00:43 +01:00
import ColumnIndicator from '../column_indicator'
2020-04-15 22:35:57 +01:00
import '!style-loader!css-loader!emoji-mart/css/emoji-mart.css'
2019-07-02 08:10:25 +01:00
const messages = defineMessages({
emoji: { id: 'emoji_button.label', defaultMessage: 'Insert emoji' },
2020-05-01 06:50:27 +01:00
emoji_search: { id: 'emoji_button.search', defaultMessage: 'Search for emoji' },
2019-07-02 08:10:25 +01:00
emoji_not_found: { id: 'emoji_button.not_found', defaultMessage: 'No emojos!! (╯°□°)╯︵ ┻━┻' },
custom: { id: 'emoji_button.custom', defaultMessage: 'Custom' },
recent: { id: 'emoji_button.recent', defaultMessage: 'Frequently used' },
search_results: { id: 'emoji_button.search_results', defaultMessage: 'Search results' },
people: { id: 'emoji_button.people', defaultMessage: 'People' },
nature: { id: 'emoji_button.nature', defaultMessage: 'Nature' },
food: { id: 'emoji_button.food', defaultMessage: 'Food & Drink' },
activity: { id: 'emoji_button.activity', defaultMessage: 'Activity' },
travel: { id: 'emoji_button.travel', defaultMessage: 'Travel & Places' },
objects: { id: 'emoji_button.objects', defaultMessage: 'Objects' },
symbols: { id: 'emoji_button.symbols', defaultMessage: 'Symbols' },
flags: { id: 'emoji_button.flags', defaultMessage: 'Flags' },
2020-03-27 15:29:52 +00:00
})
2019-07-02 08:10:25 +01:00
2020-03-27 15:29:52 +00:00
const assetHost = process.env.CDN_HOST || ''
let EmojiPicker, Emoji // load asynchronously
2019-07-02 08:10:25 +01:00
const backgroundImageFn = () => `${assetHost}/emoji/sheet_32.png`
2020-03-27 15:29:52 +00:00
const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false
2019-07-02 08:10:25 +01:00
2020-04-16 07:00:43 +01:00
const perLine = 8
const lines = 2
const DEFAULTS = [
'+1',
'grinning',
'kissing_heart',
'heart_eyes',
'laughing',
'stuck_out_tongue_winking_eye',
'sweat_smile',
'joy',
'yum',
'disappointed',
'thinking_face',
'weary',
'sob',
'sunglasses',
'heart',
'ok_hand',
]
2019-07-02 08:10:25 +01:00
const categoriesSort = [
'recent',
'custom',
'people',
'nature',
'foods',
'activity',
'places',
'objects',
'symbols',
'flags',
2020-04-16 07:00:43 +01:00
]
const getFrequentlyUsedEmojis = createSelector([
state => state.getIn(['settings', 'frequentlyUsedEmojis'], ImmutableMap()),
], emojiCounters => {
let emojis = emojiCounters
.keySeq()
.sort((a, b) => emojiCounters.get(a) - emojiCounters.get(b))
.reverse()
.slice(0, perLine * lines)
.toArray();
if (emojis.length < DEFAULTS.length) {
let uniqueDefaults = DEFAULTS.filter(emoji => !emojis.includes(emoji));
emojis = emojis.concat(uniqueDefaults.slice(0, DEFAULTS.length - emojis.length));
}
return emojis;
})
const getCustomEmojis = createSelector([
state => state.get('custom_emojis'),
], emojis => emojis.filter(e => e.get('visible_in_picker')).sort((a, b) => {
const aShort = a.get('shortcode').toLowerCase();
const bShort = b.get('shortcode').toLowerCase();
if (aShort < bShort) {
return -1;
} else if (aShort > bShort ) {
return 1;
}
return 0;
}));
2019-07-02 08:10:25 +01:00
@injectIntl
2019-08-13 16:54:29 +01:00
class EmojiPickerMenu extends ImmutablePureComponent {
2019-07-02 08:10:25 +01:00
static propTypes = {
2020-04-16 07:00:43 +01:00
customEmojis: ImmutablePropTypes.list,
2019-07-02 08:10:25 +01:00
frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.string),
loading: PropTypes.bool,
onClose: PropTypes.func.isRequired,
onPick: PropTypes.func.isRequired,
arrowOffsetLeft: PropTypes.string,
arrowOffsetTop: PropTypes.string,
intl: PropTypes.object.isRequired,
skinTone: PropTypes.number.isRequired,
onSkinTone: PropTypes.func.isRequired,
2020-04-16 07:00:43 +01:00
}
2019-07-02 08:10:25 +01:00
static defaultProps = {
loading: true,
frequentlyUsedEmojis: [],
}
getI18n = () => {
2020-04-16 07:00:43 +01:00
const { intl } = this.props
2019-07-02 08:10:25 +01:00
return {
search: intl.formatMessage(messages.emoji_search),
notfound: intl.formatMessage(messages.emoji_not_found),
categories: {
search: intl.formatMessage(messages.search_results),
recent: intl.formatMessage(messages.recent),
people: intl.formatMessage(messages.people),
nature: intl.formatMessage(messages.nature),
foods: intl.formatMessage(messages.food),
activity: intl.formatMessage(messages.activity),
places: intl.formatMessage(messages.travel),
objects: intl.formatMessage(messages.objects),
symbols: intl.formatMessage(messages.symbols),
flags: intl.formatMessage(messages.flags),
custom: intl.formatMessage(messages.custom),
},
2020-04-16 07:00:43 +01:00
}
2019-07-02 08:10:25 +01:00
}
handleClick = emoji => {
if (!emoji.native) {
2020-04-16 07:00:43 +01:00
emoji.native = emoji.colons
2019-07-02 08:10:25 +01:00
}
2020-04-16 07:00:43 +01:00
this.props.onClose()
this.props.onPick(emoji)
2019-07-02 08:10:25 +01:00
}
handleModifierChange = modifier => {
2020-04-16 07:00:43 +01:00
this.props.onSkinTone(modifier)
2019-07-02 08:10:25 +01:00
}
render () {
2020-04-16 07:00:43 +01:00
const {
loading,
intl,
customEmojis,
skinTone,
frequentlyUsedEmojis,
} = this.props
2019-07-02 08:10:25 +01:00
if (loading) {
2020-04-16 07:00:43 +01:00
return (
<div style={{ width: 340, height: 425 }}>
<ColumnIndicator type='loading' />
</div>
)
2019-07-02 08:10:25 +01:00
}
2020-04-16 07:00:43 +01:00
const title = intl.formatMessage(messages.emoji)
2019-07-02 08:10:25 +01:00
return (
2020-04-16 07:00:43 +01:00
<EmojiPicker
backgroundImageFn={backgroundImageFn}
custom={buildCustomEmojis(customEmojis)}
title={title}
i18n={this.getI18n()}
onClick={this.handleClick}
include={categoriesSort}
recent={frequentlyUsedEmojis}
skin={skinTone}
2020-05-15 03:31:24 +01:00
perLine={8}
emojiSize={29}
2020-05-15 03:31:24 +01:00
sheetSize={32}
2020-04-16 07:00:43 +01:00
set='twitter'
color='#30CE7D'
emoji=''
autoFocus
emojiTooltip
onSkinChange={this.handleModifierChange}
/>
)
2019-07-02 08:10:25 +01:00
}
}
2020-04-11 23:29:19 +01:00
const mapStateToProps = (state) => ({
2020-04-16 07:00:43 +01:00
customEmojis: getCustomEmojis(state),
2020-03-27 15:29:52 +00:00
skinTone: state.getIn(['settings', 'skinTone']),
frequentlyUsedEmojis: getFrequentlyUsedEmojis(state),
2020-04-16 07:00:43 +01:00
})
2020-03-27 15:29:52 +00:00
2020-05-02 07:25:55 +01:00
const mapDispatchToProps = (dispatch) => ({
2020-04-16 07:00:43 +01:00
onClosePopover() {
dispatch(closePopover())
},
2020-05-02 07:25:55 +01:00
onSkinTone: (skinTone) => {
2020-04-16 07:00:43 +01:00
dispatch(changeSetting(['skinTone'], skinTone))
2020-03-27 15:29:52 +00:00
},
2020-05-02 07:25:55 +01:00
onPickEmoji: (emoji) => {
2020-04-16 07:00:43 +01:00
dispatch(useEmoji(emoji))
dispatch(insertEmojiCompose(emoji, false))
2020-03-27 15:29:52 +00:00
},
2020-04-16 07:00:43 +01:00
})
2020-03-27 15:29:52 +00:00
2020-02-25 16:04:44 +00:00
export default
2020-03-27 15:29:52 +00:00
@connect(mapStateToProps, mapDispatchToProps)
class EmojiPickerPopover extends ImmutablePureComponent {
2019-07-02 08:10:25 +01:00
static propTypes = {
2020-04-16 07:00:43 +01:00
customEmojis: ImmutablePropTypes.list,
2019-07-02 08:10:25 +01:00
frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.string),
intl: PropTypes.object.isRequired,
onPickEmoji: PropTypes.func.isRequired,
onSkinTone: PropTypes.func.isRequired,
skinTone: PropTypes.number.isRequired,
2020-04-16 07:00:43 +01:00
onClosePopover: PropTypes.func.isRequired,
isXS: PropTypes.bool,
2020-04-16 07:00:43 +01:00
}
2019-07-02 08:10:25 +01:00
state = {
loading: false,
2020-04-16 07:00:43 +01:00
}
2019-07-02 08:10:25 +01:00
2020-03-27 15:29:52 +00:00
componentWillMount = () => {
2019-07-02 08:10:25 +01:00
if (!EmojiPicker) {
2020-04-16 07:00:43 +01:00
this.setState({ loading: true })
2019-07-02 08:10:25 +01:00
2020-04-16 07:00:43 +01:00
EmojiPickerAsync().then((EmojiMart) => {
EmojiPicker = EmojiMart.Picker
Emoji = EmojiMart.Emoji
2019-07-02 08:10:25 +01:00
2020-04-16 07:00:43 +01:00
this.setState({ loading: false })
2019-07-02 08:10:25 +01:00
}).catch(() => {
2020-04-16 07:00:43 +01:00
this.setState({ loading: false })
})
2019-07-02 08:10:25 +01:00
}
}
onHideDropdown = () => {
2020-04-16 07:00:43 +01:00
this.props.onClosePopover()
2019-07-02 08:10:25 +01:00
}
render () {
2020-03-27 15:29:52 +00:00
const {
onPickEmoji,
onSkinTone,
skinTone,
2020-04-16 07:00:43 +01:00
frequentlyUsedEmojis,
customEmojis,
isXS,
2020-03-27 15:29:52 +00:00
} = this.props
2020-04-16 07:00:43 +01:00
const { loading } = this.state
2020-05-02 07:25:55 +01:00
2019-07-02 08:10:25 +01:00
return (
<PopoverLayout
width={340}
isXS={isXS}
onClose={this.onHideDropdown}
>
2020-04-16 07:00:43 +01:00
<EmojiPickerMenu
customEmojis={customEmojis}
loading={loading}
onClose={this.onHideDropdown}
onPick={onPickEmoji}
onSkinTone={onSkinTone}
skinTone={skinTone}
frequentlyUsedEmojis={frequentlyUsedEmojis}
/>
2020-03-27 22:57:03 +00:00
</PopoverLayout>
)
2019-07-02 08:10:25 +01:00
}
}