2017-05-03 03:04:16 +03:00
|
|
|
import React from 'react';
|
2017-04-21 21:05:35 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-03-02 01:57:55 +02:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2017-07-08 01:06:02 +03:00
|
|
|
import { EmojiPicker as EmojiPickerAsync } from '../../ui/util/async-components';
|
2017-09-23 02:41:00 +03:00
|
|
|
import { Overlay } from 'react-overlays';
|
|
|
|
import classNames from 'classnames';
|
2017-03-02 01:57:55 +02:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
2017-04-22 04:36:33 +03:00
|
|
|
emoji: { id: 'emoji_button.label', defaultMessage: 'Insert emoji' },
|
2017-04-22 18:28:36 +03:00
|
|
|
emoji_search: { id: 'emoji_button.search', defaultMessage: 'Search...' },
|
2017-09-23 02:41:00 +03: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' },
|
2017-04-22 18:28:36 +03:00
|
|
|
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' },
|
2017-05-20 18:31:47 +03:00
|
|
|
flags: { id: 'emoji_button.flags', defaultMessage: 'Flags' },
|
2017-03-02 01:57:55 +02:00
|
|
|
});
|
|
|
|
|
2017-09-23 02:41:00 +03:00
|
|
|
const assetHost = process.env.CDN_HOST || '';
|
2017-03-02 01:57:55 +02:00
|
|
|
|
2017-09-23 02:41:00 +03:00
|
|
|
let EmojiPicker, Emoji; // load asynchronously
|
|
|
|
|
|
|
|
const backgroundImageFn = () => `${assetHost}/emoji/sheet.png`;
|
|
|
|
|
|
|
|
class ModifierPickerMenu extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
active: PropTypes.bool,
|
|
|
|
onSelect: PropTypes.func.isRequired,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleClick = (e) => {
|
|
|
|
const modifier = [].slice.call(e.currentTarget.parentNode.children).indexOf(e.target) + 1;
|
|
|
|
this.props.onSelect(modifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (nextProps.active) {
|
|
|
|
this.attachListeners();
|
|
|
|
} else {
|
|
|
|
this.removeListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
this.removeListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDocumentClick = e => {
|
|
|
|
if (this.node && !this.node.contains(e.target)) {
|
|
|
|
this.props.onClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
attachListeners () {
|
|
|
|
document.addEventListener('click', this.handleDocumentClick, false);
|
|
|
|
document.addEventListener('touchend', this.handleDocumentClick, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeListeners () {
|
|
|
|
document.removeEventListener('click', this.handleDocumentClick, false);
|
|
|
|
document.removeEventListener('touchend', this.handleDocumentClick, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { active } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='emoji-picker-dropdown__modifiers__menu' style={{ display: active ? 'block' : 'none' }} ref={this.setRef}>
|
|
|
|
<button onClick={this.handleClick}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={1} backgroundImageFn={backgroundImageFn} /></button>
|
|
|
|
<button onClick={this.handleClick}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={2} backgroundImageFn={backgroundImageFn} /></button>
|
|
|
|
<button onClick={this.handleClick}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={3} backgroundImageFn={backgroundImageFn} /></button>
|
|
|
|
<button onClick={this.handleClick}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={4} backgroundImageFn={backgroundImageFn} /></button>
|
|
|
|
<button onClick={this.handleClick}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={5} backgroundImageFn={backgroundImageFn} /></button>
|
|
|
|
<button onClick={this.handleClick}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={6} backgroundImageFn={backgroundImageFn} /></button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ModifierPicker extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
active: PropTypes.bool,
|
|
|
|
modifier: PropTypes.number,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onClose: PropTypes.func,
|
|
|
|
onOpen: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleClick = () => {
|
|
|
|
if (this.props.active) {
|
|
|
|
this.props.onClose();
|
|
|
|
} else {
|
|
|
|
this.props.onOpen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSelect = modifier => {
|
|
|
|
this.props.onChange(modifier);
|
|
|
|
this.props.onClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { active, modifier } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='emoji-picker-dropdown__modifiers'>
|
|
|
|
<Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={modifier} onClick={this.handleClick} backgroundImageFn={backgroundImageFn} />
|
|
|
|
<ModifierPickerMenu active={active} onSelect={this.handleSelect} onClose={this.props.onClose} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@injectIntl
|
|
|
|
class EmojiPickerMenu extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
loading: PropTypes.bool,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
onPick: PropTypes.func.isRequired,
|
|
|
|
style: PropTypes.object,
|
|
|
|
placement: PropTypes.string,
|
|
|
|
arrowOffsetLeft: PropTypes.string,
|
|
|
|
arrowOffsetTop: PropTypes.string,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
style: {},
|
|
|
|
loading: true,
|
|
|
|
placement: 'bottom',
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
modifierOpen: false,
|
|
|
|
modifier: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleDocumentClick = e => {
|
|
|
|
if (this.node && !this.node.contains(e.target)) {
|
|
|
|
this.props.onClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
document.addEventListener('click', this.handleDocumentClick, false);
|
|
|
|
document.addEventListener('touchend', this.handleDocumentClick, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
document.removeEventListener('click', this.handleDocumentClick, false);
|
|
|
|
document.removeEventListener('touchend', this.handleDocumentClick, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
getI18n = () => {
|
|
|
|
const { intl } = this.props;
|
|
|
|
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick = emoji => {
|
|
|
|
this.props.onClose();
|
|
|
|
this.props.onPick(emoji);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleModifierOpen = () => {
|
|
|
|
this.setState({ modifierOpen: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleModifierClose = () => {
|
|
|
|
this.setState({ modifierOpen: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleModifierChange = modifier => {
|
|
|
|
if (modifier !== this.state.modifier) {
|
|
|
|
this.setState({ modifier });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { loading, style, intl } = this.props;
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <div style={{ width: 299 }} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const title = intl.formatMessage(messages.emoji);
|
|
|
|
const { modifierOpen, modifier } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classNames('emoji-picker-dropdown__menu', { selecting: modifierOpen })} style={style} ref={this.setRef}>
|
|
|
|
<EmojiPicker
|
|
|
|
perLine={8}
|
|
|
|
emojiSize={22}
|
|
|
|
sheetSize={32}
|
|
|
|
color=''
|
|
|
|
emoji=''
|
|
|
|
set='twitter'
|
|
|
|
title={title}
|
|
|
|
i18n={this.getI18n()}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
skin={modifier}
|
|
|
|
backgroundImageFn={backgroundImageFn}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<ModifierPicker
|
|
|
|
active={modifierOpen}
|
|
|
|
modifier={modifier}
|
|
|
|
onOpen={this.handleModifierOpen}
|
|
|
|
onClose={this.handleModifierClose}
|
|
|
|
onChange={this.handleModifierChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-05-07 03:42:38 +03:00
|
|
|
|
2017-06-23 20:36:54 +03:00
|
|
|
@injectIntl
|
|
|
|
export default class EmojiPickerDropdown extends React.PureComponent {
|
2017-03-02 01:57:55 +02:00
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
static propTypes = {
|
|
|
|
intl: PropTypes.object.isRequired,
|
2017-05-20 18:31:47 +03:00
|
|
|
onPickEmoji: PropTypes.func.isRequired,
|
2017-05-12 15:44:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
active: false,
|
2017-05-20 18:31:47 +03:00
|
|
|
loading: false,
|
2017-05-12 15:44:10 +03:00
|
|
|
};
|
2017-03-02 01:57:55 +02:00
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
setRef = (c) => {
|
2017-03-02 01:57:55 +02:00
|
|
|
this.dropdown = c;
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2017-03-02 01:57:55 +02:00
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
onShowDropdown = () => {
|
2017-06-01 18:25:10 +03:00
|
|
|
this.setState({ active: true });
|
2017-09-23 02:41:00 +03:00
|
|
|
|
2017-05-07 03:42:38 +03:00
|
|
|
if (!EmojiPicker) {
|
2017-06-01 18:25:10 +03:00
|
|
|
this.setState({ loading: true });
|
2017-09-23 02:41:00 +03:00
|
|
|
|
|
|
|
EmojiPickerAsync().then(EmojiMart => {
|
|
|
|
EmojiPicker = EmojiMart.Picker;
|
|
|
|
Emoji = EmojiMart.Emoji;
|
2017-06-01 18:25:10 +03:00
|
|
|
this.setState({ loading: false });
|
2017-06-23 17:05:04 +03:00
|
|
|
}).catch(() => {
|
2017-06-01 18:25:10 +03:00
|
|
|
this.setState({ loading: false });
|
2017-05-07 03:42:38 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
onHideDropdown = () => {
|
2017-06-01 18:25:10 +03:00
|
|
|
this.setState({ active: false });
|
2017-05-07 03:42:38 +03:00
|
|
|
}
|
|
|
|
|
2017-07-28 05:37:30 +03:00
|
|
|
onToggle = (e) => {
|
|
|
|
if (!this.state.loading && (!e.key || e.key === 'Enter')) {
|
|
|
|
if (this.state.active) {
|
|
|
|
this.onHideDropdown();
|
|
|
|
} else {
|
|
|
|
this.onShowDropdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 02:41:00 +03:00
|
|
|
handleKeyDown = e => {
|
2017-07-28 05:37:30 +03:00
|
|
|
if (e.key === 'Escape') {
|
|
|
|
this.onHideDropdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 02:41:00 +03:00
|
|
|
setTargetRef = c => {
|
|
|
|
this.target = c;
|
|
|
|
}
|
2017-03-02 01:57:55 +02:00
|
|
|
|
2017-09-23 02:41:00 +03:00
|
|
|
findTarget = () => {
|
|
|
|
return this.target;
|
|
|
|
}
|
2017-04-22 18:28:36 +03:00
|
|
|
|
2017-09-23 02:41:00 +03:00
|
|
|
render () {
|
|
|
|
const { intl, onPickEmoji } = this.props;
|
2017-07-28 05:37:30 +03:00
|
|
|
const title = intl.formatMessage(messages.emoji);
|
2017-09-23 02:41:00 +03:00
|
|
|
const { active, loading } = this.state;
|
2017-05-07 03:42:38 +03:00
|
|
|
|
2017-03-02 01:57:55 +02:00
|
|
|
return (
|
2017-09-23 02:41:00 +03:00
|
|
|
<div className='emoji-picker-dropdown' onKeyDown={this.handleKeyDown}>
|
|
|
|
<div ref={this.setTargetRef} className='emoji-button' title={title} aria-label={title} aria-expanded={active} role='button' onClick={this.onToggle} onKeyDown={this.onToggle} tabIndex={0}>
|
2017-06-06 14:20:07 +03:00
|
|
|
<img
|
2017-09-23 02:41:00 +03:00
|
|
|
className={classNames('emojione', { 'pulse-loading': active && loading })}
|
2017-07-14 20:47:53 +03:00
|
|
|
alt='🙂'
|
2017-09-23 02:41:00 +03:00
|
|
|
src={`${assetHost}/emoji/1f602.svg`}
|
2017-06-06 14:20:07 +03:00
|
|
|
/>
|
2017-09-23 02:41:00 +03:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<Overlay show={active} placement='bottom' target={this.findTarget}>
|
|
|
|
<EmojiPickerMenu loading={loading} onClose={this.onHideDropdown} onPick={onPickEmoji} />
|
|
|
|
</Overlay>
|
|
|
|
</div>
|
2017-03-02 01:57:55 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|